How to avoid circular dependencies between Player and World?

Posted by futlib on Game Development See other posts from Game Development or by futlib
Published on 2012-11-13T10:35:26Z Indexed on 2012/11/13 11:23 UTC
Read the original article Hit count: 239

I'm working on a 2D game where you can move up, down, left and right. I have essentially two game logic objects:

  • Player: Has a position relative to the world
  • World: Draws the map and the player

So far, World depends on Player (i.e. has a reference to it), needing its position to figure out where to draw the player character, and which portion of the map to draw.

Now I want to add collision detection to make it impossible for the player to move through walls.

The simplest way I can think of is to have the Player ask the World if the intended movement is possible. But that would introduce a circular dependency between Player and World (i.e. each holds a reference to the other), which seems worth avoiding. The only way I came up with is to have the World move the Player, but I find that somewhat unintuitive.

What is my best option? Or is avoiding a circular dependency not worth it?

© Game Development or respective owner

Related posts about collision-detection

Related posts about objects