-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
259 lines (228 loc) · 7.42 KB
/
index.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
SPACE.IO v0.1
Gent Semaj
12/10/2019
Server-side node.js file
*/
const ARGS = process.argv.slice(2);
const PORT = ARGS[0] ? ARGS[0] : 3000;
const USE_SSL = ARGS[1] !== undefined && ARGS[2] !== undefined;
const { readFileSync } = require('fs');
const express = require('express');
const app = express();
const http = require('http');
const https = require('https');
const server = USE_SSL ? https.createServer({
key: readFileSync(ARGS[1]),
cert: readFileSync(ARGS[2])
}, app) : http.createServer(app);
const { Server } = require("socket.io");
const io = new Server(server);
// Static files
app.use(express.static('public'));
// Client file to send
app.get('/', function (req, res) {
res.sendFile(__dirname + '/client.html');
});
// HTTP listener
server.listen(PORT, function () {
console.log('listening on *:' + PORT);
});
// Top-level container
var universe = {
ships: {}, // dict of ships
lasers: [] // array of lasers
}
// Periodic tick function
function tick() {
// tick lasers
universe.lasers.forEach(laser => {
// translate
laser.yPos += Math.cos(toStandard(laser.theta) * Math.PI / 180.0) * laser.vel;
laser.xPos += -Math.sin(toStandard(laser.theta) * Math.PI / 180.0) * laser.vel;
// hitscan
Object.entries(universe.ships).forEach(_ship => {
var ship = _ship[1];
if(ship.id === laser.id || ship.iFrames > 0) return;
var coords = getRealCoords(ship);
var deltas = [];
for(var i = 0; i < 3; i++) {
var base = coords[i];
var next = coords[i + 1 < 3 ? i + 1 : 0];
var slopeDelta = (laser.yPos - base[1]) * (next[0] - base[0])
- (laser.xPos - base[0]) * (next[1] - base[1]);
deltas.push(slopeDelta);
}
//console.log(deltas);
if(
(deltas[0] < 0
&& deltas[1] < 0
&& deltas[2] < 0) ||
(deltas[0] > 0
&& deltas[1] > 0
&& deltas[2] > 0)) {
console.log(`${ship.id} hit by ${laser.id}`);
universe.ships[laser.id].kills++;
universe.ships[ship.id].deaths++;
laser.timer = 1;
ship.iFrames = 35; // 1 second
io.emit('hit', {
hitee: ship.id,
hitter: laser.id,
killCount: universe.ships[laser.id].kills,
deathCount: universe.ships[ship.id].deaths
});
}
});
laser.timer--;
});
universe.lasers = universe.lasers.filter(laser => laser.timer > 0);
// tick ships
Object.entries(universe.ships).forEach(_ship => {
var ship = _ship[1];
if(ship.iFrames > 0) ship.iFrames--;
if(ship.laserTimer > 0) ship.laserTimer--;
});
io.emit('universe', universe); // update the universe for all clients
}
/** Removes a ship from the universe ship array
params:
id: client id #
*/
function removeShip(id) {
if(universe.ships[id] == undefined)
return false;
delete universe.ships[id];
return true;
}
function toStandard(bearing) {
return (bearing - 90) * -1;
}
function multMatrix(...mats) {
if(mats.length == 1) return mats[0];
if(mats[0][0].length != mats[1].length) {
console.log("dimension mismatch");
return null;
}
var result = [];
for(var x = 0; x < mats[0].length; x++) {
result[x] = [];
for(var y = 0; y < mats[1][0].length; y++) {
var cell = 0;
for(var i = 0; i < mats[0][0].length; i++)
cell += mats[0][x][i] * mats[1][i][y];
result[x][y] = cell;
}
}
return multMatrix(result, ...mats.slice(2));
}
function getRealCoords(ship) {
var theta = toStandard(ship.theta) * Math.PI / 180.0;
var mat1 = [
[1, 0, ship.xPos],
[0, 1, ship.yPos],
[0, 0, 1]
];
var mat2 = [
[Math.cos(theta), -Math.sin(theta), 0],
[Math.sin(theta), Math.cos(theta), 0],
[0, 0, 1]
];
var mat3 = [
[1, 0, -ship.xPos],
[0, 1, -ship.yPos],
[0, 0, 1]
];
var M = multMatrix(mat1, mat2, mat3);
var coords = [
[
[ship.xPos],
[ship.yPos - 36],
[1]
],
[
[ship.xPos + 30],
[ship.yPos + 30],
[1]
],
[
[ship.xPos - 30],
[ship.yPos + 30],
[1]
]
];
return coords.map(coord => multMatrix(M, coord));
}
/** Inserts or updates a ship in the universe
params:
id: client id #
newship: ship object to insert
*/
function updateShip(id, newship) {
// new
if(!universe.ships[id]) universe.ships[id] = newship;
// patch
universe.ships[id] = {
...universe.ships[id],
xPos: newship.xPos,
yPos: newship.yPos,
theta: newship.theta,
linVel: newship.linVel,
rotVel: newship.rotVel,
name: newship.name.substring(0, 8)
};
}
setInterval(tick, 35); // set interval in ms for tick function
/** New connection handler
params:
socket: client socket object
*/
io.on('connection', function (socket) {
console.log('user connected'); // log the connection
var clientShip = {
id: socket.id, // client id is the socket id
name: "", // display name
color: "#" + (0x1000000 + (Math.random()) * 0xffffff).toString(16).substr(1, 6), // random hexadecimal color
xPos: 0, // x coordinate
yPos: 0, // y coordinate
theta: Math.random() * 360, // random angle in degrees
linVel: 0, // linear velocity in units/tick
rotVel: 0, // rotational velocity in degrees/tick
laserTimer: 0, // cooldown to shoot laser
iFrames: 0, // invincibility frames
kills: 0,
deaths: 0
};
updateShip(socket.id, clientShip); // add new ship
socket.emit('universe', universe); // send the universe object to the client
/** Update request handler
params:
rec: received client ship data
*/
socket.on('update', function (rec) {
// TODO validate input
updateShip(socket.id, rec); // replace the server's current copy of the client's ship with the new data
});
socket.on('fire', function() {
if(universe.ships[socket.id].linVel > 40) return; // no pew pew during zoom zoom
if(universe.ships[socket.id].laserTimer > 0) return; // no pew pew during cooldown
var laser = {
id: socket.id,
color: universe.ships[socket.id].color,
xPos: universe.ships[socket.id].xPos,
yPos: universe.ships[socket.id].yPos,
theta: universe.ships[socket.id].theta,
vel: universe.ships[socket.id].linVel + 25,
timer: 29
};
universe.ships[socket.id].laserTimer = 45;
laser.yPos += Math.cos(toStandard(laser.theta) * Math.PI / 180.0) * laser.vel;
laser.xPos += -Math.sin(toStandard(laser.theta) * Math.PI / 180.0) * laser.vel;
universe.lasers.push(laser);
});
/** Disconnect handler */
socket.on('disconnect', function () {
console.log('user disconnected'); // log the disconnect
console.log(removeShip(socket.id) ? "clean" : "dirty"); // log whether the removal of the ship executed correctly
});
});