Game Development Reference
In-Depth Information
smooth. This simple idea, simulating the game locally while the real game is being played on the server, is
sometimes called “dead reckoning.”
Ideally, we can share the code for the game logic between the client side (for responsiveness) and the server
side (for actually running the game). That way, we don't need to have two separate copies of complicated
code that perform very similar tasks. It's not always true that this can be done. In fact, JavaScript has been,
until recently, totally confined to users' web browsers. Only now, with JavaScript interpreters like Google's V8
( http://code.google.com/p/v8/ ) and Mozilla's Rhino ( www.mozilla.org/rhino/ ), ca n JavaScript code be run
outside the context of any browser. In this chapter, we will write our game logic once , in JavaScript, and then
use it for both the game client and for the game server, which will run on node.js, built atop V8.
Designing the bumper cars game
Now let's make a game! The game will be simple in concept: each player controls a bumper car. He can
move his car around and bounce off of other players' cars and off of the rigid walls. For simplicity of
physics, we'll take the cars to be circular. We'll split up the program into a few parts with well-defined
purposes.
game.js : This file will contain the game logic, i.e. the collision detection. This file will be used by
both the client (the player in his web browser) and the server (in node.js) to run their game loops.
game.js will have a routine RunGameFrame(Cars) that will be called over and over again to
progress the game's status. The argument, Cars , will be a collection of data about all of the cars'
positions, velocities, and so forth, so that they may be processed. game.js will also have a set of
game options controlling things like the size of the game environment, strength of friction, etc.
client.js : This file will run a game loop and draw the game on an HTML canvas . We will first
create a “local” game client (client-local.js) that will operate entirely within the browser, and then
upgrade it to the real game client (client-multiplayer.js) that will connect to the WebSocket server
and communicate with it (sending user input and receiving game state).
server.js : This file, to be executed under node.js, will run the “real” game loop, keeping a list of
the players and their car data. server.js will establish a WebSocket server and communicate with
the clients (receiving user input and sending game state).
bumper.htm : This file will be a bare-bones HTML page serving as the game client. It will refer to
game.js, client.js, and have an HTML canvas .
The game logic
The logic in our game is all related to detecting and handling collisions. Because the focus of this chapter
is netcode, the explanation of the physics involved will be cursory. Conceptually, it is simple: the bumper
cars will collide elastically (conserving both momentum and energy) with each other and with the walls. We
will take the cars to be circular (of fixed radius) to maximize symmetry. The collision detection presented
here is very good, but not perfect. For our purposes, it more than suffices.
 
Search WWH ::




Custom Search