Game Development Reference
In-Depth Information
Cars[i].VX *= GP.MaxSpeed / Speed;
Cars[i].VY *= GP.MaxSpeed / Speed;
}
// Friction will act on the cars at all times, eventually bringing them to rest.
Cars[i].VX *= GP.FrictionMultiplier;
Cars[i].VY *= GP.FrictionMultiplier;
}
That's all for the RunGameFrame method. In fact, that is all of the game logic, and there wasn't very much!
We complete game.js by adding the lines in Listing 9-6.
Listing 9-6. Code Required for node.js Compatibility
if (typeof exports !== "undefined")
{
exports.GP = GP;
exports.RunGameFrame = RunGameFrame;
}
These lines will allow us to use game.js within node.js when we write our server in a later section.
The game client, Part 1
The game client—what shows up in the user's browser—is slightly more complicated than game.js. We
need to
open a WebSocket connection
transmit user input to the server
receive game state from the server
draw the game
Before diving into the multiplayer stuff, let's just get the game client working on its own. First, make a
simple HTML file, bumper.htm, to house the game, as shown in Listing 9-7.
Listing 9-7. bumper.htm
<!DOCTYPE html>
<html>
<head>
<title>Bumper Cars</title>
<script language="JavaScript" src="game.js"></script>
<script language="JavaScript" src="client-local.js"></script>
</head>
<body style="text-align: center">
<canvas id="BumperCanvas" style="border: 1px solid black">
Your browser does not support HTML canvas!
</canvas>
 
Search WWH ::




Custom Search