I'm making a game. I have a Level class, which has a collection of Drawing objects and keeps track of where the player is in the level.
The main character (who takes the form of a Drawing object) needs to be able to to stop level movement when the main character collides with another object.
class Level {
...
Position offset;
vector<Drawing*> drawings;
...
}
class MainCharacter : public Drawing {
...
void onCollide(Drawing* other, Level& level) {
level.stopMovement(); // prevent level from moving any further in that direction
}
}
As you can see, my intended implementation causes a circular dependency: Level has a collection of Drawings, which require a reference to the Level in order to stop it.
Is there a design pattern that can help me with this? I've looked into the Observer and Model View Controller patterns already, but I'm not sure how much good those can do here.
I'm currently just passing the position by reference to the onCollide function.
EDIT: I realized that I have an object factory which is compounding the circular dependency because it includes the Drawing .h so that it can create Drawing objects.
So, Level includes both Factory and Drawing, Factory includes Drawing. Drawing would like to forward declare Level, but it needs access to its members, so it can't forward declare.