-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
188 lines (153 loc) · 4.25 KB
/
app.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
let express = require('express');
let app = express();
let serv = require('http').Server(app);
let Matrix = require( __dirname + '/server/matrix.js');
let TileCollider = require( __dirname + '/server/tilecollision.js');
let PlayerCollider = require( __dirname + '/server/playercollision.js');
let PlayerClass = require( __dirname + '/server/player.js');
let port = process.env.PORT || 2000;
app.get('/', (req, res) => {
res.sendFile(__dirname + '/client/index.html');
});
app.use('/client',express.static(__dirname + '/client'));
serv.listen(port);
console.log('Server Started');
const deltaTime = 1/60;
const gravity = 0.5;
// Create Tiles
let levelTiles = new Matrix();
let tileSetter = function(levelTiles, tileType, xStart, xEnd, yStart, yEnd) {
for (let x = xStart; x < xEnd; ++x) {
for (let y = yStart; y < yEnd; ++y) {
levelTiles.set(x, y, {
name:tileType
});
}
}
};
tileSetter(levelTiles, 'sky', 0, 25, 0, 14);
tileSetter(levelTiles, 'ground', 2, 29, 16, 18);
tileSetter(levelTiles, 'ground', 6, 10, 13, 14);
tileSetter(levelTiles, 'ground', 13, 17, 13, 14);
tileSetter(levelTiles, 'ground', 20, 24, 13, 14);
tileSetter(levelTiles, 'ground', 9, 13, 10, 11);
tileSetter(levelTiles, 'ground', 17, 21, 10, 11);
let tileCollider = new TileCollider(levelTiles);
// List to store multiple players
let SOCKET_LIST = {};
let PLAYER_LIST = {};
let DEAD_PLAYER_LIST = {};
let numPlayers = 0;
let io = require('socket.io')(serv, {});
io.sockets.on('connection', socket => {
console.log('socket connection');
numPlayers++;
socket.id = Math.random();
SOCKET_LIST[socket.id] = socket;
let player = new PlayerClass(socket.id, numPlayers);
PLAYER_LIST[socket.id] = player;
socket.on('disconnect', () => {
delete SOCKET_LIST[socket.id];
if (PLAYER_LIST[socket.id]) {
delete PLAYER_LIST[socket.id];
} else {
delete DEAD_PLAYER_LIST[socket.id];
}
});
socket.on('keyPress', data => {
if (player.state) {
if (data.inputID === 'jump' && player.alreadyJumped === false) {
player.jump();
} else if (data.inputID === 'shoot' && player.shotRecently === false) {
player.shoot();
}
if (data.inputID === 'left') {
player.moveLeft();
} else if (data.inputID === 'right') {
player.moveRight();
}
}
});
socket.on('keyRelease', data => {
if (player.state) {
if ((data.inputID === 'left') || (data.inputID === 'right')) {
player.cancelHorizontal();
} else if (data.inputID === 'shoot') {
player.shotRecently = false;
}
}
});
socket.on('revive', () => {
PLAYER_LIST[socket.id] = DEAD_PLAYER_LIST[socket.id]
delete DEAD_PLAYER_LIST[socket.id];
player.resetState();
});
});
setInterval(() => {
let positions = [];
let playerCollider = new PlayerCollider();
for (let i in PLAYER_LIST) {
let player = PLAYER_LIST[i];
if (player.hp <= 0) {
DEAD_PLAYER_LIST[i] = player;
delete PLAYER_LIST[i];
}
player.vel.y += gravity;
player.updateX();
tileCollider.checkXPlayer(player);
player.updateY();
tileCollider.checkYPlayer(player);
playerCollider.add({
id: player.id,
player: player,
left: player.pos.x + 2,
right: player.pos.x + player.size.x - 2,
top: player.pos.y + 2,
bottom: player.pos.y + player.size.y - 2,
});
}
for (let i in PLAYER_LIST) {
let player = PLAYER_LIST[i];
(player.fireballs).forEach((ball) => {
ball.vel.y += gravity;
ball.updateX();
tileCollider.checkXBall(ball);
ball.updateY();
tileCollider.checkYBall(ball);
playerCollider.checkCollision(ball);
});
// checking if player has fallen out of bounds
if (player.pos.x < -10 || player.pos.x > 510 || player.pos.y > 450) {
player.hp = 0;
}
player.filter();
(player.fireballs).forEach((ball) => {
if (ball.collided) {
ball.explode();
}
});
positions.push({
id: player.id,
x: player.pos.x,
y: player.pos.y,
number: player.number,
velX: player.vel.x,
velY: player.vel.y,
direction: player.direction,
distance: player.distance,
fireballs: player.fireballs,
hp: player.hp,
state: player.state,
number: player.num,
alreadyJumped: player.alreadyJumped
});
}
for (let i in SOCKET_LIST) {
let socket = SOCKET_LIST[i];
let pack = {
playerID: socket.id,
positions: positions
}
socket.emit('newPosition', pack);
}
}, 1000/48);