-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
158 lines (120 loc) · 4.23 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/**************************************************
** NODE.JS REQUIREMENTS
**************************************************/
var express = require('express')
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io')(server);
var Player = require("./Player").Player; // Player class
app.set('port', (process.env.PORT || 8000))
app.use(express.static(__dirname + '/src/'));
app.get('/', function(request, response) {
response.render('index.html')
})
/**************************************************
** GAME VARIABLES
**************************************************/
var socket, // Socket controller
players; // Array of connected players
/**************************************************
** GAME INITIALISATION
**************************************************/
function init() {
// Create an empty array to store players
players = [];
server.listen(app.get('port'), function() {
console.log("Node app is running at localhost:" + app.get('port'))
})
io.on('connection',onSocketConnection);
};
/**************************************************
** GAME EVENT HANDLERS
**************************************************/
// New socket connection
function onSocketConnection(client) {
console.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);
// Listen for player rejoin message
client.on("rejoin", onRejoin);
};
// Socket client has disconnected
function onClientDisconnect() {
console.log("Player has disconnected: "+this.id);
var removePlayer = playerById(this.id);
// Player not found
if (!removePlayer) {
console.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) {
var shiptype = Math.floor(Math.random() * 9) + 0 ;
// Create a new player
var newPlayer = new Player(data.nick, data.x, data.y, data.type);
newPlayer.id = this.id;
// Broadcast new player to connected socket clients
this.broadcast.emit("new player", {nick: data.nick, id: newPlayer.id, x: newPlayer.getX(), y: newPlayer.getY(), angle: newPlayer.getAngle(), type: newPlayer.getType()});
// Send existing players to the new player
var i, existingPlayer;
for (i = 0; i < players.length; i++) {
existingPlayer = players[i];
this.emit("new player", {nick: existingPlayer.getNick(), id: existingPlayer.id, x: existingPlayer.getX(), y: existingPlayer.getY(), angle: existingPlayer.getAngle(), type: existingPlayer.getType()});
};
// 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) {
console.log("Player not found: "+this.id);
return;
};
// Update player position
movePlayer.setX(data.x);
movePlayer.setY(data.y);
movePlayer.setAngle(data.angle);
movePlayer.setType(data.type);
// Broadcast updated position to connected socket clients
this.broadcast.emit("move player", {id: movePlayer.id, x: movePlayer.getX(), y: movePlayer.getY(), angle: movePlayer.getAngle(), type: movePlayer.getType(), isFiring: data.isFiring});
};
// Player has rejoined
function onRejoin(data) {
// Find player in array
var rejoinedPlayer = playerById(this.id);
// Player not found
if (!rejoinedPlayer) {
console.log("Player not found: "+this.id);
return;
};
// Broadcast updated position to connected socket clients
this.broadcast.emit("rejoin", {id: rejoinedPlayer.id});
};
/**************************************************
** 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();