I'm writing my first game (in Javascript / Node over WebSockets) and am running into a bit of chicken and egg problem, and am afraid how I'm doing it right now won't scale once the game gets more complex.
Here is how the game is architected. It's a tiled strategy game. When a user connects, a new Game object is created:
// width and height of the board / map
function Game(width, height) {
this.map = new Map(width, height);
this.player = new Player();
// ...
}
game = new Game(100,100);
What's reasonable way for 2-way communication between a player object (or any object), and the map?
For example, if a player wants to move left, first I must check that there is no obstacle in his way, then, once this condition is true, his position must be updated, and he loses a stamina point, so both the map and the player objects are updated depending on conditions that most objects both meet (in the simplest case, where there is no interaction with other objects).
One way I can think, is to have a global GAME object, so that the map can access GAME.player and the player can access GAME.map, but this doesn't seem elegant (and will cause me a headache once I have multiple rooms with separate games running on the same node instance).