forked from robhawkes/mozilla-festival
-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
139 lines (107 loc) · 3.5 KB
/
game.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/**************************************************
** NODE.JS REQUIREMENTS
**************************************************/
var util = require("util"), // Utility resources (logging, object inspection, etc)
io = require("socket.io"), // Socket.IO
Player = require("./Player").Player; // Player class
/**************************************************
** GAME VARIABLES
**************************************************/
var socket, // Socket controller
players; // Array of connected players
/**************************************************
** GAME INITIALISATION
**************************************************/
function init() {
// Create an empty array to store players
players = [];
// Set up Socket.IO to listen on port 8000
socket = io.listen(8000);
// Configure Socket.IO
/*socket.configure(function() {
// Only use WebSockets
socket.set("transports", ["websocket"]);
// Restrict log output
socket.set("log level", 2);
});*/
// Start listening for events
setEventHandlers();
};
/**************************************************
** GAME EVENT HANDLERS
**************************************************/
var setEventHandlers = function() {
// Socket.IO
socket.sockets.on("connection", onSocketConnection);
};
// New socket connection
function onSocketConnection(client) {
util.log("New player has connected: "+client.id);
// Listen for client disconnected
client.on("disconnect", onClientDisconnect);
// Listen for new player message
client.on("new player", onNewPlayer);
// Listen for move player message
client.on("move player", onMovePlayer);
};
// Socket client has disconnected
function onClientDisconnect() {
util.log("Player has disconnected: "+this.id);
var removePlayer = playerById(this.id);
// Player not found
if (!removePlayer) {
util.log("Player not found: "+this.id);
return;
};
// Remove player from players array
players.splice(players.indexOf(removePlayer), 1);
// Broadcast removed player to connected socket clients
this.broadcast.emit("remove player", {id: this.id});
};
// New player has joined
function onNewPlayer(data) {
// Create a new player
var newPlayer = new Player(data.x, data.y);
newPlayer.id = this.id;
// Broadcast new player to connected socket clients
this.broadcast.emit("new player", {id: newPlayer.id, x: newPlayer.getX(), y: newPlayer.getY()});
// Send existing players to the new player
var i, existingPlayer;
for (i = 0; i < players.length; i++) {
existingPlayer = players[i];
this.emit("new player", {id: existingPlayer.id, x: existingPlayer.getX(), y: existingPlayer.getY()});
};
// Add new player to the players array
players.push(newPlayer);
};
// Player has moved
function onMovePlayer(data) {
// Find player in array
var movePlayer = playerById(this.id);
// Player not found
if (!movePlayer) {
util.log("Player not found: "+this.id);
return;
};
// Update player position
movePlayer.setX(data.x);
movePlayer.setY(data.y);
// Broadcast updated position to connected socket clients
this.broadcast.emit("move player", {id: movePlayer.id, x: movePlayer.getX(), y: movePlayer.getY()});
};
/**************************************************
** GAME HELPER FUNCTIONS
**************************************************/
// Find player by ID
function playerById(id) {
var i;
for (i = 0; i < players.length; i++) {
if (players[i].id == id)
return players[i];
};
return false;
};
/**************************************************
** RUN THE GAME
**************************************************/
init();