-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
90 lines (80 loc) · 1.89 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
import {default as express } from "express";
import * as http from 'http';
import { Server } from "socket.io";
import { SocketConstants as sc } from "./htdocs/js/utils/Constants.js";
import { DebriGame } from './htdocs/js/debri.js';
var app = express();
var httpServer = http.Server(app);
var io = new Server(httpServer);
var game = new DebriGame(io);
game.start(undefined);
var pid = 0;
var sockets = {};
httpServer.listen(process.env.PORT || 918, () => {
console.log("Game listening at 918");
})
for(var i = 0; i < 200; ++i) {
game.spawnAsteroid();
}
setInterval(() => {
io.to("ig").volatile.emit(sc.server.update, game.toData());
}, 20);
io.on("connection", (socket) => {
socket.pid = pid;
sockets[pid] = socket;
socket.lastFire = Date.now();
++pid;
socket.on(sc.client.enter, (name) => {
if(socket.playing) return;
socket.playing = true;
var ship = game.addPlayer({x: 100, y: 100, name}, socket.pid);
socket.ship = ship;
io.to("ig").emit(sc.server.newplayer, {
pid: socket.pid,
ship
})
socket.join("ig");
socket.emit(sc.server.enter, {
pid: socket.pid,
players: game.players,
asteroids: game.asteroids,
})
})
socket.on(sc.client.thrust, t => {
if(!socket.ship) return;
if(t) socket.ship.thruston();
else socket.ship.thrustoff();
})
socket.on(sc.client.turn, t => {
if(!socket.ship) return;
switch(t) {
case 0:
case 1:
case -1:
socket.ship.turn(t);
break;
}
})
socket.on(sc.client.fire, t => {
if(!socket.ship) return;
if(t) {
var a = () => {
var now = Date.now();
if(now - socket.lastFire >= 333) {
socket.lastFire = now;
game.spawnBullet(socket.ship);
}
};
a();
socket.fireInterval = setInterval(a, 333)
}
else {
clearInterval(socket.fireInterval);
}
})
socket.on("disconnect", () => {
delete sockets[socket.pid];
game.removePlayer(socket.pid)
})
})
app.use('/', express.static('htdocs'))