C++ Classes, Hierarchy, Friends Chart

Started by
6 comments, last by Sky Warden 8 years, 4 months ago

I have been creating a game in C++ (using GDI+)

I have read tutorials and examples but cannot figure out where to even start, and or, how this works. Currently, everything has redundant code.

Attached is a chart I came up with, I am wondering how it looks, and how I can at least get this connected. From there on, I can add the code.

Thanks.

Advertisement
"Move" and "Type" seems weird to me, classes are normally objects, things that live in the game. Those two probably don't really exist (or if they do it's yet to be seen whether you really need an object for it, I think).

In general, if you don't know how to go, your thing is too complex. Cut stuff away from it to simplify. You can always add new things later, or move things around.


I would suggest to start much simpler, eg just a map and an player that moves on it.

Sorry. I don't quite understand the tree. Do all of the classes inherit from Map? And is there a class Type that inherits from Item? I think what you mean is that class Type has a script as an attribute, Item has a type as an attribute, and that a Map has a group of moving objects, again as an attribute.

The general idea of class inheritance is that classes that inherit from another class have similar functionalities and attributes with the class they inherit from. Class inheritance is an IS-A relationship, like an Enemy is a Character, and a Character is a MovingObject. An attribute is a HAS-A relationship, like an Item has a Type, and a Type has a Script.

Like Alberth suggested, I think you need to start with something simpler first. Make the map first, make sure it renders the tiles well. Then give a player that moves in it. You'll gain more understanding as you go, and you can develop it further once you get the hang of things.

Thanks for replies.

That is actually how far I got.

TL;DR

Maps perfect - as well as info

Player can be moved

Text Styles and everything done

Full spritesheet

I am wanting to half rewrite the code now that I want to add items and enemies.

I probably misunderstood inheritance or got it backwards.

But keeping it simple is what I am going to do. There is just one thing now.

Here

I have 3 types of "objs" Player, Enemy, Items

Each contains a vector.

I want one loop to iterate a single vector. How can I relate all 3 in a way I can add to a single vector?

Look in terms of processes and systems. Identify what things stick around, and how those things communicate with each other.

Break down all the responsibilities. Keep breaking them down, and make a long list of each individual responsibility. When you can no longer break down the responsibilities, that is a good list to make classes from. Every class should have only a single responsibility. Communications patterns between them should be figured out to help you identify and remove dependencies.

What you describe in your graph is about general relationships. It is a start, but it has nothing to do with inheritance as it exists in computer code. Inheritance is an implementation detail on how to implement a bunch of items that are perfectly interchangeable but still different.

Also, this is not the time to figure out what lives in various data structures. That comes several steps later, after you have decomposed the things into a single responsibility and worked out communications patterns.

Here

I have 3 types of "objs" Player, Enemy, Items

Each contains a vector.

I want one loop to iterate a single vector. How can I relate all 3 in a way I can add to a single vector?

You can contain objects of those classes in a single vector of the base class of those classes, like std::vector<Move>.


You can contain objects of those classes in a single vector of the base class of those classes, like std::vector<Move>.

No you can't! That will cause slicing.

If you need polymorphism, you need to use pointers. So,

std::vector<Move*>, or preferably std::vector<std::unique_ptr<Move>>.

No you can't! That will cause slicing.

If you need polymorphism, you need to use pointers. So,

std::vector<Move*>, or preferably std::vector<std::unique_ptr<Move>>.

Oopsies. My bad. I wrote that example without thinking. mellow.png

This topic is closed to new replies.

Advertisement