0
\$\begingroup\$

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).

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Why is map updated when player move? Do you have multiple players? Do you have multiple maps active at a time? \$\endgroup\$
    – wondra
    Commented Jul 27, 2014 at 18:02
  • \$\begingroup\$ Yes there are multiple players and multiple maps. Map keeps track of all the objects in the game, so (currently) to change Player.position I have need Map to check that no other object has that same position, so Player has to know about Map (has to get the "ok"), and Map has to know about Player (check its and others' positions). \$\endgroup\$
    – o_o_o--
    Commented Jul 27, 2014 at 18:08

3 Answers 3

4
\$\begingroup\$

Your question is a bit wider than you exposed I think. Player and Map are two objects and should be thought as it. They are not different, from an engine point of view, of a ping-pong table or a flying toaster.

That said the question is : how to make NORMAL objects findable and communicate?

For a map / player relation I usually put responsability... on a third object. Something called PlayerController who'd be in charge of moving the Player according to his current position and the possibilities the map offers. The PlayerController needs to have access to Player and also to Map, so how to do so?

Imagine that EVERY object in the game registers itself (at construction) in a static/global (whatever the langage you use) object, for it to act like a big collection containing all your game objects.

With such object you'd retrieve any object, from anywhere, like this:

game_map = GameObjects[typeof(Map)];
player = GameObjects[typeof(Player)];

(in this example I use typeof, C# style, but of course you can identify your game objects with anything you want, integer id, string name... whatever adapted to the langage you use.)

However I'd suggest you to take care of "global/static/Singleton" objects, and not use them too much in your game code (this topic is largely discussed everywhere on the Internet, but you can learn more here).

I hope it helps.

EDIT: This solution is obviously a naive approach. In case you have to manage several players or maps you will have to identify your objects in a better way (at least more precise way).

\$\endgroup\$
1
\$\begingroup\$

I would personally discourage you from any kind of direct map-player communication there is no need for it. It is game logic responsibility to handle such interaction. Often, there is some kind of GameObject manager or component system.
As for multiple players/maps, server should be resonsible for instantiating maps and moving players between them.

\$\endgroup\$
0
\$\begingroup\$

There is no reason for the Map and Player objects to communicate at all that I can see. So far as what you've told us we can assume that Player object only holds data about their position, but then what does the map hold? (rhetorical Q). It sounds like the objects are doing the same job. I.e. the map already knows the Player object's position.

If your map has some kind of internal data so that you can ask it to check who/what is at some position (See example below), then you are duplicating effort. The Player object may not need to know it's position at all if you leave that up to the user (i.e. we can see where our Player is on the screen) and the map knows where all the objects are. That is, the user tries to do something (based on the output shown them) and whether they are allowed to do that or not is dictated by game logic which checks the player and map.

-- Map Data
 0,0
[---][---][---][P2 ]
[---][---][---][---]
[---][P1 ][---][---]
[---][---][---][---]
________________3,3

-- Player Data stamina = 2 (can move twice)

-- Game Object Player2: I want to move from 0,3 to 1,3 (>Game) check: player2 stamina > 0 check: position 1,3 not occupied move: player2 to 1,3 Game: Player2 moves to 1,3 (>Player2)

Player <-> Game Object <-> Map

So, the Player and Map only ever communicate with the Game object respectively. The client might have a local static map object that simply stores what the server says the state of the map is at the moment. The player indicates to the game object what they want to do and if it is possible the change is made and any consequences processed. Then the player is told that it's done and given the updated map data to display.

\$\endgroup\$
1
  • \$\begingroup\$ since the answer won't update with changes for some reason, when I mean the real person that's the 'user' and the when it's the in-game object that's the 'player'. \$\endgroup\$ Commented Sep 25, 2014 at 21:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .