By far, the biggest problem that has always dawned on my when programming games is how to structure my code. It just becomes an incredible mess, after a while. The reason for that is because I have no idea how different classes should interact with each other.
For example, say I have a
Player class, a
PlayerInput class and a
Map class. The
Player class contains information as to the location of the player, whereas the
PlayerInput class handles changing that location, but by first making sure it is within a walkable area, using the
Map class. How should I structure this?
My usual approach is to pass these components, as parameters, in the constructors of the parameters that need them, like so:
var map = new Map(); var player = new Player(); var input = new PlayerInput(player, map);
The problem with that is that it quickly gets messy; when you add new components, you have to go through your constructors and update them, and it doesn't work well if you have mirroring references:
var physics = new Physics(input); //Oops, doesn't work var input = new Input(physics);
How should I manage my game objects and components?