Managing game objects/components
- by Xeon06
Good day everyone,
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. Let's have an example.
Say I have a class Player, a class PlayerInput and a class Map. The player class contains information as to the location of the player, whereas the player input class handles changing that location, but by first making sure it's within a walkable area from the map class. How to structure this?
My usual approach is to pass those 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);
So, how do you guys usually manage this?
Thanks.