mercredi 5 août 2015

Need help in avoiding a circular dependency [duplicate]

This question already has an answer here:

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.

Aucun commentaire:

Enregistrer un commentaire