-
Notifications
You must be signed in to change notification settings - Fork 6
/
game.js
367 lines (307 loc) · 9.52 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
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
const SERVER_URL = "wss://stevensheffey.me/ws";
const PROTOCOL = "heroesoftheswarm";
const FPS = 60;
const PING_RATE = 30;
const SEND_VIEWPORT_P = true;
const MAP_SIZE = new vec2(1600, 900);
const XP_LEVEL = 300;
// ESR forgive me, for I have sinned
var GLOBAL_STATE_NO_TOUCH;
var GLOBAL_CONFIG_NO_TOUCH;
var BACKGROUND;
// ESR forgive me, for I have sinned
var GLOBAL_STATE_NO_TOUCH;
var GLOBAL_CONFIG_NO_TOUCH;
var BACKGROUND;
function init() {
var foo = 0;
var ws = initializeWebSocket(SERVER_URL, PROTOCOL, function(event) {
data = JSON.parse(JSON.parse(JSON.stringify(event.data)));
type = data.mt;
switch(type) {
case 'i':
GLOBAL_CONFIG_NO_TOUCH = data.message.config; break;
case 'w':
GLOBAL_STATE_NO_TOUCH = data.message.world; break;
case 'c':
updateEditor(data.message.compile);
break;
}
});
var ctx = initializeCanvas();
BACKGROUND = initializeBackground();
setTimeout(pingServer, 1000, ws, ctx);
setTimeout(_loop, 1500, ctx, 1000 / FPS, 0);
$("#upload-button").on('click', function(event) {
ws.send(JSON.stringify({program: $("#code").val()}));
});
$(document).ready(function(){
$('[data-toggle="popover"]').popover();
});
}
function frame(ctx, dt, frameN) {
screenSize = new vec2(ctx.canvas.width, ctx.canvas.height);
state = getState();
viewport = getViewport(ctx);
ctx.clearRect(0, 0, screenSize.x, screenSize.y);
if (checkDeath()) {
drawDeathText(ctx);
$("#respawn").removeClass("invisible").addClass("visible");
}
$.each(state.swarms, function(id, swarm) {
color = ints2HexColor(swarm.color);
swarm_pos = new vec2(swarm.x, swarm.y);
$.each(swarm.members, function(i, particle) {
pos = swarm_pos.add(new vec2(particle.x, particle.y));
drawParticle(ctx, pos.sub(viewport[0]), 7, color, healthColor(particle.health), 5, particle.direction);
})
});
$.each(state.bullets, function(i, bullet) {
owner = state.swarms[bullet.owner];
if (owner !== undefined) {
color = ints2HexColor(state.swarms[bullet.owner].color);
} else {
color = 'white';
}
pos = new vec2(bullet.x, bullet.y);
drawBullet(ctx, pos.sub(viewport[0]), 10, color, 'white', bullet.direction);
})
x = state.swarms[getID()].x
y = state.swarms[getID()].y
ctx.drawImage(
BACKGROUND,
viewport[0].x, viewport[0].y,
screenSize.x, screenSize.y,
0, 0,
screenSize.x, screenSize.y
);
xp = state.swarms[getID()].experience
$("#xp").html(xp.toString() + ' XP');
$("#lvl").html("(<strong>" + (XP_LEVEL - (xp % 300)) + "</strong> to next particle)");
updateLeaderboard();
}
function drawParticle(ctx, pos, radius, fillColor, borderColor, borderWidth, dir) {
dir = d2r(dir);
ctx.beginPath();
ctx.fillStyle = fillColor;
ctx.strokeStyle = borderColor;
ctx.lineWidth = borderWidth;
ctx.arc(pos.x, pos.y, radius, 0, 2*Math.PI, false);
ctx.fill();
ctx.stroke();
ctx.closePath()
ctx.beginPath();
var pos2 = pos.add(new vec2(Math.cos(dir), -Math.sin(dir)).times(radius));
ctx.arc(pos2.x, pos2.y, radius*.5, 0, 2*Math.PI, false);
ctx.fillStyle = fillColor;
ctx.strokeStyle = borderColor;
ctx.fill();
ctx.lineWidth = borderWidth;
ctx.strokeStyle = borderColor;
ctx.stroke();
ctx.closePath();
}
function drawBullet(ctx, pos, length, playerColor, borderColor, dir) {
dir = d2r(dir);
var pos2 = pos.add(new vec2(Math.cos(dir), -Math.sin(dir)).times(length));
ctx.lineWidth = 1;
ctx.beginPath();
ctx.strokeStyle = borderColor;
ctx.moveTo(pos.x, pos.y-1);
ctx.lineTo(pos2.x, pos2.y-1);
ctx.stroke();
ctx.closePath();
ctx.beginPath();
ctx.strokeStyle = playerColor;
ctx.moveTo(pos.x, pos.y);
ctx.lineTo(pos2.x, pos2.y);
ctx.stroke();
ctx.closePath();
}
function drawGrid(ctx, screenSize, spacing, color) {
var x; var y;
w = screenSize.x; h = screenSize.y;
ctx.lineWidth = 2;
ctx.strokeStyle = color;
ctx.beginPath();
for (x = 0; x <= w; x += spacing) {
for (y = 0; y <= h; y += spacing) {
ctx.moveTo(x, 0);
ctx.lineTo(x, h);
ctx.stroke();
ctx.moveTo(0, y);
ctx.lineTo(w, y);
ctx.stroke();
}
}
ctx.closePath();
}
function drawBullet(ctx, pos, length, playerColor, borderColor, dir) {
dir = d2r(dir);
var pos2 = pos.add(new vec2(Math.cos(dir), -Math.sin(dir)).times(length));
ctx.beginPath();
ctx.strokeStyle = borderColor;
ctx.moveTo(pos.x, pos.y-1);
ctx.lineTo(pos2.x, pos2.y-1);
ctx.stroke();
ctx.beginPath();
ctx.strokeStyle = playerColor;
ctx.moveTo(pos.x, pos.y);
ctx.lineTo(pos2.x, pos2.y);
ctx.stroke();
}
function drawDeathText(ctx) {
ctx.fillStyle = 'white';
ctx.font = '50px monospace';
ctx.fillText("YOU DIED", screenSize.x / 2 - 115, screenSize.y / 2);
}
function initializeWebSocket(url, protocol, onmessage) {
// Initialize the websocket
var ws = new WebSocket(url, protocol);
// This is run when the server sends us something. It should handle all the different types of messages
ws.onmessage = onmessage;
// This is run when something fscks up
ws.onerror = function (event) {
console.log(event.data);
}
return ws;
}
function initializeCanvas() {
// Get the canvas
var ctx = document.getElementById("game_canvas").getContext("2d");
// Set up the context
ctx.globalCompositeOperation = 'destination-over';
return ctx;
}
function initializeBackground() {
var img = new Image();
img.src = "background2.png";
return img;
}
// Hey look modern web design
function updateEditor(message) {
btn = $("#compile-report");
if (message.success) {
console.log("success");
btn.removeClass("btn-danger")
.addClass("btn-success")
.html("<strong>Compilation was successful</strong>")
.attr('data-content', "");
} else {
console.log("failure");
btn.removeClass("btn-success")
.addClass("btn-danger")
.html("<strong>Compilation failed (hover)</strong>")
.attr('data-content', message.error);
}
}
function updateLeaderboard() {
leaderboard = getState().leaderboard;
for (var i = 0; i < 3; i++) {
ctx = document.getElementById("leaderboardC"+i.toString()).getContext('2d');
ctx.globalCompositeOperation = 'destination-over';
ctx.fillStyle = ints2HexColor(leaderboard[i][0])
ctx.fillRect(0, 0, 25, 25);
$("#leaderboardS"+i.toString()).html(leaderboard[i][1].toString());
}
}
function pingServer(ws, ctx) {
if (SEND_VIEWPORT_P) {
viewport = getViewport(ctx);
obj = [viewport[0].coords(), viewport[1].coords()];
ws.send(JSON.stringify(obj));
} else {
ws.send('U');
}
setTimeout(pingServer, 1000 / PING_RATE, ws, ctx);
}
function getState() {
return GLOBAL_STATE_NO_TOUCH;
}
function getID() {
return GLOBAL_CONFIG_NO_TOUCH.player_id;
}
function getMaxHealth() {
// return GLOBAL_CONFIG_NO_TOUCH.max_health;
return 5; // P
}
function getViewport(ctx) {
state = getState();
screenSize = new vec2(ctx.canvas.width, ctx.canvas.height);
if (state === undefined) {
return [new vec2(0,0), screenSize];
}
id = getID();
swarm = state.swarms[id];
center = new vec2(swarm.x, swarm.y);
topLeft = center.sub(screenSize.times(.5));
if (topLeft.x < 0) {
topLeft.x = 0;
}
if (topLeft.y < 0) {
topLeft.y = 0;
}
bottomRight = topLeft.add(screenSize);
if (bottomRight.x > MAP_SIZE.x) {
bottomRight.x = MAP_SIZE.x;
topLeft.x = MAP_SIZE.x - screenSize.x;
}
if (bottomRight.y > screenSize.y) {
bottomRight.y = screenSize.y;
topLeft.y = MAP_SIZE.y - screenSize.y;
}
return [topLeft, bottomRight];
}
function _loop(ctx, dt, frameN) {
frameN++;
var before = performance.now();
frame(ctx, dt, frameN);
var frameT = performance.now() - before;
dt = (1000 / FPS) - frameT - 20;
setTimeout(_loop, dt, ctx, dt, frameN);
}
function randint(min, max) {
return Math.floor(Math.random() * (max - min) + min);
}
function d2r(deg) {
return deg * Math.PI / 180;
}
function checkDeath() {
return getState().swarms[getID()].members.length == 0
}
// Takes an array of 3 ints: [red, green, blue] (each 0-255)
function ints2HexColor(arr) {
return '#' + ((arr[0] << 16) | (arr[1] << 8) | arr[2]).toString(16).padStart(6, '0');
}
function healthColor(health) {
g = health / getMaxHealth() * 255;
r = 255 - g;
b = 0;
return ints2HexColor([r, g, b]);
}
function _loop(ctx, dt, frameN) {
frameN++;
var before = performance.now();
frame(ctx, dt, frameN);
var frameT = performance.now() - before;
dt = (1000 / FPS) - frameT;
setTimeout(_loop, dt, ctx, dt, frameN);
}
function randint(min, max) {
return Math.floor(Math.random() * (max - min) + min);
}
function d2r(deg) {
return deg * Math.PI / 180;
}
$(function() {
var current_progress = 0;
var interval = setInterval(function() {
current_progress = state.swarms[getID()].experience;
console.log(current_progress);
$("#dynamic")
.css("width", current_progress + "px")
.attr("aria-valuenow", "% Complete");
if (current_progress >= 300)
curretn_progress = 0;
}, 100);
});