Game Development Reference
In-Depth Information
try { Message = JSON.parse(Message); }
catch (Err) { return; }
if (!("Type" in Message && "Data" in Message)) return;
// Handle the different types of messages we expect.
var C = Connections[ID];
switch (Message.Type)
{
// Handshake.
case "HI":
// If this player already has a car, abort.
if (C.Car) break;
// Create the player's car with random initial position.
C.Car =
{
X: Game.GP.CarRadius + Math.random() * (Game.GP.GameWidth
- 2 * Game.GP.CarRadius),
Y: Game.GP.CarRadius + Math.random() * (Game.GP.GameHeight
- 2 * Game.GP.CarRadius),
VX: 0,
VY: 0,
OR: 0,
// Put a reasonable length restriction on usernames.
// Usernames will be displayed to all players.
Name: Message.Data.toString().substring(0, 10)
};
// Initialize the input bitfield.
C.KeysPressed = 0;
System.log(C.Car.Name + " spawned a car!");
SendGameState();
break;
// Key up.
case "U":
if (typeof C.KeysPressed === "undefined") break;
if (Message.Data == 37) C.KeysPressed &= ~2; // Left
else if (Message.Data == 39) C.KeysPressed &= ~4; // Right
else if (Message.Data == 38) C.KeysPressed &= ~1; // Up
break;
// Key down.
case "D":
if (typeof C.KeysPressed === "undefined") break;
if (Message.Data == 37) C.KeysPressed |= 2; // Left
else if (Message.Data == 39) C.KeysPressed |= 4; // Right
else if (Message.Data == 38) C.KeysPressed |= 1; // Up
break;
}
}
 
Search WWH ::




Custom Search