Level and Player objects - which should contain which?

Posted by Thane Brimhall on Game Development See other posts from Game Development or by Thane Brimhall
Published on 2012-10-11T18:03:00Z Indexed on 2012/10/11 21:49 UTC
Read the original article Hit count: 292

Filed under:
|
|

I've been working on a several simple games, and I've always come to a decision point where I have to choose whether to have the Level object as an attribute of the Player class or the Player as an attribute of the Level class.

I can see arguments for both:

The Level should contain the player because it also contains every other entity. In fact it just makes sense this way: "John is in the room." It makes it a bit more difficult to move the player to a new level, however, because then each level has to pass its player object to an upcoming level.

On the other hand, it makes programming sense to me to leave the player as the top-level object that is persistent between levels, and the environment changes because the player decides to change his level and location. It becomes very easy to change levels, because all I have to do is replace the level variable on the player.

What's the most common practice here? Or better yet, is there a "right" way to architecture this relationship?

© Game Development or respective owner

Related posts about levels

Related posts about objects