Skip to content

Thoughts on Game State and object representations

dylanbeattie edited this page Jun 4, 2011 · 1 revision

object model hacks

Game coordinates

(0.0) is the top left. Two sets of coordinates are in use:

TILE coordinates refer to positions of tiles in the game grid PIXEL coordinates refer to the top-left absolute coordinates of the sprite tile, etc. offset from the top-left corner of the RENDERED game arena.

So assuming a 48x48 tile size, the tile at (0,0) has pixel coordinates (0,0) and the tile at (1,1) has pixel coordinates (48,48)

Coordinates are X,Y coordinates , NOT CSS top,left offsets. So you'll need to map them into CSS positions to render your sprites.

Game {
 players[] /* A fixed-size array of 8 (nullable) players, corresponding to spawn points 1:8 */
 bombs[] /* needs XY coordinates, and time of dropping */
 level: { 
     /* the level we're playing on */
     for clients who join a game in progress, the game state needs to include:
         the CURRENT STATE of the level including walls destroyed, etc.
 }
 
  game clock (?) - need a game epoch for each game so things like bomb countdowns can remain in sync.     
  
 /* various event handlers */ 

}

Player {
 position (in pixels - NOT tile coordinates)
 velocity - ([-1,0,1], [-1,0,1]) where 0,0 is the TOP LEFT CORNER and (1,1) is running SOUTHEAST
}

Bomb {
    position: /* pixel coordinates */
    dropTime: in ticks
}

Game Clocks

Games are ALWAYS IN PROGRESS - you don't wait for a game to start.

When a client receives a "welcome" message from the server on joining a game, they should capture new Date().valueOf() - local ticks since the epoch - and remember this. This is the client's LOCAL T0

The welcome message will also include the SERVER's dT - number of ticks elapsed on the server since the game was started.

When a client gets a message (e.g. BOMB DROPPED), the message will include the bomb's drop time, which will be the LOCAL dT from the CLIENT WHO DROPPED THE BOMB

I join a game at 14:27:08 that's been running for 15 seconds and there is a bomb about to go off.

I get from the server: t_elapsed = 15000 I calculate now.valueOf() => /* big number */ - call that t0_client I calculate t0_server by subtracting server's t_elapsed from t0_client

If I have a bomb that was dropped at 16000 and will explode at 21000, those times are GAME ELAPSED TIME.

In my timing loop, I must check for any bomb that explodes in the next game cycle, and handle locally:

  1. Explosion animation
  2. Scenery deformation
  3. Whether I am dead
Clone this wiki locally