Hi all,
So I've been thinking about some ways to extend the functionality of game objects by adding some properties using the composition approach with writing classes.
This is my concrete idea and problem:
Let's say we have several properties of a game object: Renderable, Moveable, etc..
And we have some concrete objects: Background (which has the Renderable property), Character (which has both the Renderable and the Moveable property) and Zone (which has only the Moveable property).
So we have the following classes (written in C++) :
class Renderable
{
void render(Screen*)
{
// render at X,Y
}
};
class Moveable
{
void move_to(int x, int y)
{
// change X and Y
}
};
class Character
{
Renderable render;
Moveable move;
};
class Background
{
Renderable render;
};
The problem is where to store the X and Y variables and how to access them?
As you can see X and Y are both used by the Renderable and the Moveable properties for calculation.
If we instantiate them in Renderable, they are not visible by the Moveable property and vice-versa.
If we instantiate them in both Renderable and Moveable we need to maintain two copies of each variable and this calls for trouble.
If we instantiate them in the parent, Character, for example, we need to have a reference or a pointer in the child objects pointing to the parent, which is also not ideal because we're restricting the child to certain parent class only and it leads to circular dependency.
Can you suggest another approach that could make use of these property classes but avoids the complications above?
P.S. I also thought of another possible solution: Instead of using composition, we could use inheritance, but in the example above the Character class would then lead to multiple inheritance, which has its own problems (like the diamond problem) and is not supported by most OOP languages (C++ is an exception).
So what would you suggest?
Thanks.