-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathminigame.js
252 lines (211 loc) · 8.5 KB
/
minigame.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
var ws = require('ws');
var server = new ws.Server({ port: 8001 }, function() {
console.log('Websockets server up on port 8001');
});
var PLAYER_RADIUS = 15;
var BULLET_RADIUS = 5;
var ARENA_WIDTH = 800;
var ARENA_HEIGHT = 600;
var MAX_HEALTH = 50;
var TIMESTEP_MS = 20;
var players = [];
var bullets = [];
var totalBulletCount = 0;
var powerups = [];
var lastPowerupSpawnTime = 0;
var currentCooldownTime = 0;
var POWERUP_RADIUS = 5;
var POWERUP_COOLDOWN_MIN = 8 * 1000;
var POWERUP_COOLDOWN_MAX = 15 * 1000;
var POWERUP_SHOOT_FASTER = 1;
var POWERUP_SHOOT_FASTER_TIME = 5 * 1000;
function generatePlayer(id) {
players[id].x = (ARENA_WIDTH - 20) * Math.random() + 10;
players[id].y = (ARENA_HEIGHT - 20) * Math.random() + 10;
players[id].health = MAX_HEALTH;
}
server.on('connection', function(conn) {
var id = players.length;
console.log('Player ' + id + ' has connected.');
conn.on('message', function(msg) {
try {
var data = JSON.parse(msg);
if(players[id] && data.player) {
if(Math.abs(players[id].x - data.player.x) > 3) {
data.player.x = players[id].x;
players[id].sendPos = true;
}
if(Math.abs(players[id].y - data.player.y) > 3) {
data.player.y = players[id].y;
players[id].sendPos = true;
}
var offset = 25, angleOff = Math.PI / 4;
var angle = calcAngle(data.player, data.aiming);
var aimX = data.player.x + offset * Math.cos(angle + angleOff);
var aimY = data.player.y + offset * Math.sin(angle + angleOff);
players[id] = Object.assign(players[id], {
timestamp: Date.now(),
x: data.player.x,
y: data.player.y,
xdiff: data.player.x - players[id].x,
ydiff: data.player.y - players[id].y,
pingMsg: data.pingMsg,
ping: data.ping,
angle: calcAngle({x: aimX, y: aimY}, data.aiming),
aiming: data.aiming,
shooting: data.shooting,
});
} else if(!players[id] && data.name) {
players[id] = {
name: data.name,
sendPos: true,
conn: conn,
bulletsShot: [],
score: 0
};
generatePlayer(id);
console.log('Player ' + id + ' has named themselves ' + data.name);
}
} catch(e) {
console.error(e + ' ' + msg);
}
});
conn.on('close', function() {
players[id] = null;
console.log('Player ' + id + ' has left.');
});
});
setInterval(tick, TIMESTEP_MS);
function intersects(center1, radius1, center2, radius2) {
var xdiff = center2.x - center1.x;
var ydiff = center2.y - center1.y;
var distSqr = xdiff * xdiff + ydiff * ydiff;
var lenSqr = radius1 * radius1 + radius2 * radius2;
return distSqr <= lenSqr;
}
function calcAngle(from, to) {
return Math.atan2(to.y - from.y, to.x - from.x)
}
function chooseRandom(low, high) {
return Math.floor(Math.random() * (high - low) + low);
}
function tick() {
var now = Date.now();
for(var i = 0; i < bullets.length; i++) {
if(bullets[i].x < BULLET_RADIUS || bullets[i].x > ARENA_WIDTH - BULLET_RADIUS || bullets[i].y < BULLET_RADIUS || bullets[i].y > ARENA_HEIGHT - BULLET_RADIUS) {
bullets.splice(i--, 1);
continue;
}
var speed = 12;
bullets[i].x += speed * Math.cos(bullets[i].angle);
bullets[i].y += speed * Math.sin(bullets[i].angle);
}
for(var id = 0; id < players.length; id++) {
if(!players[id])
continue;
players[id].respawned = undefined;
players[id].changes = {};
if(players[id].sendPos) {
players[id].changes.x = players[id].x;
players[id].changes.y = players[id].y;
players[id].sendPos = false;
}
if(players[id].x < PLAYER_RADIUS) {
players[id].changes.x = players[id].x = PLAYER_RADIUS;
}
if(players[id].x > ARENA_WIDTH - PLAYER_RADIUS) {
players[id].changes.x = players[id].x = ARENA_WIDTH - PLAYER_RADIUS;
}
if(players[id].y < PLAYER_RADIUS) {
players[id].changes.y = players[id].y = PLAYER_RADIUS;
}
if(players[id].y > ARENA_HEIGHT - PLAYER_RADIUS) {
players[id].changes.y = players[id].y = ARENA_HEIGHT - PLAYER_RADIUS;
}
for(var i = 0; i < bullets.length; i++) {
if(players[id].bulletsShot.indexOf(bullets[i].id) == -1 && intersects(players[id], PLAYER_RADIUS, bullets[i], BULLET_RADIUS)) {
players[id].health -= 10;
if(players[id].health <= 0) {
generatePlayer(id);
players[id].changes.x = players[id].x;
players[id].changes.y = players[id].y;
players[id].respawned = true;
players[bullets[i].playerId].score++;
}
if(players[bullets[i].playerId]) {
var idx = players[bullets[i].playerId].bulletsShot.indexOf(bullets[i].id);
if(idx == -1) {
console.log('could not find bullet?');
} else {
players[bullets[i].playerId].bulletsShot.splice(idx, 1);
}
}
bullets.splice(i, 1);
i--;
}
}
if(players[id].powerup && (Date.now() - players[id].powerup.timeEquipped >= players[id].powerup.activeTime)) {
players[id].changes.powerup = players[id].powerup = null;
}
for(var i = 0; i < powerups.length; i++) {
if(intersects(players[id], PLAYER_RADIUS, powerups[i], POWERUP_RADIUS)) {
players[id].changes.powerup = players[id].powerup = powerups.splice(i--, 1)[0];
players[id].powerup.timeEquipped = Date.now();
}
}
players[id].changes.score = players[id].score;
players[id].changes.health = players[id].health;
var bulletCooldown = players[id].powerup && players[id].powerup.type == POWERUP_SHOOT_FASTER ? 100 : 300;
if(players[id].shooting && (!players[id].lastShotTime || now - players[id].lastShotTime > bulletCooldown)) {
var offset = 25, angleOff = Math.PI / 4;
var angle = calcAngle(players[id], players[id].aiming);
var bulletX = players[id].x + offset * Math.cos(angle + angleOff);
var bulletY = players[id].y + offset * Math.sin(angle + angleOff);
bullets.push({
id: totalBulletCount++,
playerId: id,
x: bulletX,
y: bulletY,
angle: players[id].angle,
});
players[id].bulletsShot.push(totalBulletCount - 1);
players[id].lastShotTime = now;
}
}
if(powerups.length < 5 && now - lastPowerupSpawnTime >= currentCooldownTime) {
powerups.push({
x: chooseRandom(POWERUP_RADIUS, ARENA_WIDTH - POWERUP_RADIUS),
y: chooseRandom(POWERUP_RADIUS, ARENA_HEIGHT - POWERUP_RADIUS),
type: POWERUP_SHOOT_FASTER,
activeTime: POWERUP_SHOOT_FASTER_TIME
});
lastPowerupSpawnTime = now;
currentCooldownTime = chooseRandom(POWERUP_COOLDOWN_MIN, POWERUP_COOLDOWN_MAX);
}
for(var id = 0; id < players.length; id++) {
if(!players[id])
continue;
var others = [];
players.forEach(function(curr, idx) {
if(idx != id && curr) {
others.push({
id: idx,
name: curr.name,
timestamp: curr.timestamp,
x: curr.x,
y: curr.y,
angle: curr.angle,
health: curr.health,
score: curr.score,
respawned: curr.respawned,
powerup: curr.powerup
});
}
});
try {
players[id].conn.send(JSON.stringify({ pingMsg: players[id].pingMsg, self: players[id].changes, others: others, bullets: bullets, powerups: powerups }));
} catch(e) {
console.error('error: ' + e.message);
}
}
}