-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.ts
More file actions
580 lines (527 loc) · 14.9 KB
/
app.ts
File metadata and controls
580 lines (527 loc) · 14.9 KB
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
import * as Paper from 'paper';
import { TYPE, Curve as Messages_Curve, WorkerMessage, curveCommand, WorkerMessageType, AppMessageType, AppMessageData, CurveUpdate } from './messages';
import { config } from './config';
import { CurvePainter } from './shared';
declare const paper:typeof Paper;
const DEBUG = config.debug;
const VISUALS = config.visuals;
export interface GameConfig {
game: {
players: {
name: string,
file: string
}[]
},
visuals: {
explosions: boolean
}
debug: {
step: boolean,
devMode: boolean
}
}
const colors = [
new paper.Color({hue:355, saturation: 0.65, lightness: 0.65}), // red
new paper.Color({hue:29, saturation: 0.67, lightness: 0.69}), // orange
new paper.Color({hue:187, saturation: 0.47, lightness: 0.55}), // cyan
new paper.Color({hue:207, saturation: 0.82, lightness: 0.66}), // blue
new paper.Color({hue:286, saturation: 0.60, lightness: 0.67}), // purple
new paper.Color({hue:95, saturation: 0.38, lightness: 0.62}), // green
];
class Game {
players:Player[];
round:Round;
sidebar:Sidebar;
static params = {
speed: 1,
radius: 30
}
constructor(opts:{
players:Player[]
}) {
const paperCanvas = <HTMLCanvasElement>document.getElementById('paper');
paperCanvas.width = 400;
paperCanvas.height = 400;
paper.setup(paperCanvas);
this.players = opts.players;
for (const player of opts.players) {
player.id = opts.players.indexOf(player);
player.color = colors[player.id];
player.name = player.name || 'Player ' + player.id;
player.score = 0;
}
this.newRound();
this.sidebar = new Sidebar(this);
this.sidebar.render();
}
newRound() {
this.round = new Round({
players: this.players,
game: this
});
}
}
class Sidebar {
container:HTMLElement;
game:Game;
constructor(game) {
this.game = game;
const element = document.getElementById('sidebar');
if (element !== null) {
this.container = element;
}
}
render() {
const players = this.game.players;
this.container.innerHTML = `
<div class="players">
${players.map((player) => {
return `
<div class="player ${player.winner ? 'winner': ''}">
<div class="color" style="background-color:${player.color.toCSS(true)}"></div>
${player.name}
<div class="score">${player.score}</div>
</div>
`;
}).join('')}
</div>
`;
}
}
class Round {
curvesGroup: Paper.Group;
curves:Curve[];
game:Game;
players:Player[];
paused:boolean;
ended:boolean;
constructor(opts:{ players:Player[], game:Game }) {
this.curves = [];
this.game = opts.game;
this.players = opts.players;
this.curvesGroup = new paper.Group();
let readyCurveCount = 0;
for (const player of opts.players) {
const curve = new Curve({
pos: this.randomPosition(),
player: player,
round: this
});
curve.onReady(() => {
readyCurveCount++;
if (readyCurveCount === this.players.length) {
// Start round
this.loop();
}
});
this.curves.push(curve);
}
if (DEBUG.step) {
document.addEventListener('keydown', (event) => {
const keyName = event.key;
if (keyName === 'ArrowUp') {
this.loop();
}
});
}
}
end() {
this.paused = true;
if (this.ended) {
// Round already ended
return;
}
this.ended = true;
const startNextRound = () => {
// Clear all paths
paper.project.clear();
// start new round
const pointsToWin = game.players.length * 10;
const players = this.players;
// Kill all bots
for (const curve of this.curves) {
curve.kill();
}
if (players[0].score >= pointsToWin && players[0].score >= players[1].score + 2) {
// Game end
players[0].winner = true;
this.game.sidebar.render();
} else {
this.game.newRound();
}
};
setTimeout(startNextRound, 3000);
}
getDeadPlayerCount() {
let deadPlayerCount = 0;
for (const curve of this.curves) {
if (!curve.alive) {
deadPlayerCount++;
}
}
return deadPlayerCount;
}
score (player:Player) {
let deadPlayerCount = this.getDeadPlayerCount();
player.score = player.score + deadPlayerCount;
// Sort players by score
const players = this.game.players.sort((a: Player, b: Player) => {
return b.score - a.score;
});
this.game.sidebar.render();
}
checkRoundEnd() {
let deadPlayerCount = this.getDeadPlayerCount();
if (deadPlayerCount >= this.curves.length - 1) {
// Score the last player alive
for (const curve of this.curves) {
if (curve.alive) {
this.score(curve.player);
}
}
this.end();
}
}
randomPosition() {
// Random position within the center 70% of the map
return new paper.Point({
x: (Math.random() * 0.70 + 0.15) * paper.view.bounds.width,
y: (Math.random() * 0.70 + 0.15) * paper.view.bounds.height
});
}
loop() {
let curvecommandcount = 0;
for (const curve of this.curves) {
// Ask player to give command
curve.getCommand((command) => {
curvecommandcount++;
// Callback
curve.update(command);
curve.render();
// If all curves have given their command
if (curvecommandcount === this.curves.length) {
paper.view.draw();
if (!this.paused && !DEBUG.step) {
window.requestAnimationFrame(() => this.loop());
}
}
});
}
}
}
class Player {
winner:boolean;
id:number;
score:number;
name:string;
color:Paper.Color;
botFileName: string;
constructor(opts:{
file:string;
name?: string;
}) {
this.winner = false;
if (opts.name) {
this.name = opts.name;
}
this.botFileName = opts.file;
}
}
class Direction {
x:number;
y:number;
rad:number;
deg:number;
constructor(deg:number) {
this.x = Math.cos(Direction.degToRad(deg));
this.y = Math.sin(Direction.degToRad(deg));
this.rad = Math.atan2(this.y, this.x);
this.deg = Direction.radToDeg(this.rad);
}
static radToDeg(rad) {
return rad * (180 / Math.PI);
}
static degToRad(deg) {
return deg * (Math.PI / 180);
}
}
class Curve {
curvePainter: CurvePainter;
debugOverlay: Paper.Group;
pos:Paper.Point;
color:Paper.Color;
direction:Direction;
speed:number;
player:Player;
path:Paper.CompoundPath;
dot:Paper.Path.Circle;
draw:boolean;
holeSpacing:number;
round:Round;
collide:boolean;
alive:boolean;
ready: boolean;
private worker: Worker;
private onReadyCallbacks: Function[];
private commandCallbacks: {[id:number]: Function};
private commandId: number;
private lastCommand: curveCommand | null;
constructor(opts:{pos:{x:number, y:number}, player:Player, round:Round}) {
this.speed = Game.params.speed;
this.round = opts.round;
this.direction = new Direction(Math.random() * 360);
this.pos = new paper.Point(opts.pos);
this.player = opts.player;
this.lastCommand = null;
this.alive = true;
this.draw = true;
this.collide = true;
this.holeSpacing = this.getRandomHoleSpacing();
this.commandId = 0;
this.worker = new Worker(this.player.botFileName);
this.ready = false;
this.onReadyCallbacks = [];
this.commandCallbacks = {};
;
this.curvePainter = new CurvePainter(paper, this.player.id, this.player.color);
this.path = this.curvePainter.path;
this.round.curvesGroup.addChild(this.path);
this.dot = new paper.Path.Circle({
center: this.pos,
radius: 4,
strokeColor: this.player.color,
strokeWidth: 1
});
this.curvePainter.startRecording();
this.startDrawing();
// Listen for events from worker
this.worker.addEventListener('message', (e: WorkerMessage) => {
switch (e.data.type) {
case WorkerMessageType.READY:
this.ready = true;
for (const callback of this.onReadyCallbacks) {
callback();
}
break;
case WorkerMessageType.UPDATE:
this.commandCallbacks[e.data.id](e.data.command);
delete this.commandCallbacks[e.data.id];
break;
case WorkerMessageType.PAINT:
if (DEBUG.devMode) {
if (!this.debugOverlay) {
this.debugOverlay = new paper.Group();
} else {
this.debugOverlay.removeChildren();
}
this.debugOverlay.importJSON(e.data.paperState);
}
break;
}
}, false);
this.postMessage({
type: AppMessageType.INIT,
playerId: this.player.id,
width: 400,
height: 400
});
}
postMessage(message: AppMessageData) {
this.worker.postMessage(message);
}
onReady(callback: Function) {
if (this.ready) {
callback();
} else {
this.onReadyCallbacks.push(callback);
}
}
getCommand(callback:(command:curveCommand) => void) {
const id = this.commandId++;
const curves:Messages_Curve[] = [];
const updates:CurveUpdate[] = [];
for (const curve of this.round.curves) {
curves.push({
pos: curve.pos,
direction: curve.direction,
id: curve.player.id,
updates: curve.curvePainter.recordedData
});
}
this.commandCallbacks[id] = callback;
const paperState = this.round.curvesGroup;
this.postMessage({
type: AppMessageType.UPDATE,
pos: this.pos,
direction: this.direction,
curves: curves,
id: id
});
}
kill() {
this.worker.terminate();
}
getRandomHoleSpacing() {
// Return a length between 10 and 310
return Math.random() * 300 + 10;
}
explosion() {
const expandingCircle = (radius: number) => {
let circle = new paper.Path.Circle({
center: this.pos, radius: radius,
strokeColor: this.player.color,
strokeWidth: 2,
opacity: 1
});
circle.onFrame = (event) => {
circle.strokeWidth = Math.max(0, 2 - Math.pow(event.time * 2, 1.5));
circle.opacity = Math.max(0, 1 - event.time * 2);
circle.scale(1.0 + Math.pow(event.time, 0.1) * 0.1);
};
return circle;
}
let circles = new paper.Group();
const initialRadius = 5;
circles.addChild(expandingCircle(initialRadius));
setTimeout(() => circles.addChild(expandingCircle(initialRadius)), 150);
setTimeout(() => circles.addChild(expandingCircle(initialRadius)), 300);
// Cleanup after a bit, so shit doesn't slow down
setTimeout(() => circles.remove(), 1000);
const rand = (min, max) => Math.random() * (max - min) + min;
const randomBit = (angle: number, size: number, speed: number, rotationSpeed: number) => {
let rect = new paper.Path.Rectangle({
from: this.pos.add([-size / 2, -size / 2]),
to: this.pos.add([size / 2, size / 2]),
fillColor: this.player.color,
opacity: 1
});
rect.onFrame = function (event) {
rect.opacity = Math.max(0, 1 - event.time / 2);
rect.rotate(rotationSpeed * event.time);
rect.translate(new Paper.Point(
Math.cos(angle) * speed / Math.exp(event.time / 5),
Math.sin(angle) * speed / Math.exp(event.time / 5)
));
};
return rect;
}
let bits = new paper.Group();
for (let i = 0; i < rand(10, 20); i++) {
// Send bits in a cone shape, 50% in forwards and 50% in backwards direction
const angle = (i % 2) * Math.PI + this.direction.rad + rand(-Math.PI / 3, Math.PI / 3);
let r = randomBit(angle, rand(0.5, 3), rand(1, 2), rand(1, 5));
bits.addChild(r);
}
// Cleanup after a bit, so shit doesn't slow down
setTimeout(() => bits.remove(), 2000);
}
collision() {
this.round.score(this.player);
if (VISUALS.explosions) {
this.explosion();
}
this.alive = false;
this.round.checkRoundEnd();
}
render() {
this.dot.position = this.pos;
}
checkCollision(point:Paper.Point):boolean {
if (!this.collide) {
return false;
}
// Bounds
if (!point.isInside(paper.view.bounds.expand(-this.path.strokeWidth))) {
return true;
}
// Grab our last path
const lastPath = <Paper.Path>this.path.lastChild;
// Check collision with all curves
const hitResult = this.round.curvesGroup.hitTest(this.pos, {
stroke: true,
tolerance: this.path.strokeWidth / 2,
match: (hit) => {
if (hit.item.parent.data.type === TYPE.curve) {
if (hit.location.path === lastPath) {
// If it's our own curve, we ignore collisions with the tip.
const offset = hit.location.offset;
const curOffset = lastPath.length;
const diff = curOffset - offset;
if (diff <= 0.001) {
// Ignore last part of own curve
return false;
}
}
return true;
} else {
return false;
}
}
});
if (hitResult) {
return true;
}
return false;
}
startDrawing() {
this.draw = true;
this.collide = true;
this.curvePainter.startSegment(this.pos);
}
stopDrawing() {
this.draw = false;
this.collide = false;
}
update(command:curveCommand) {
this.curvePainter.startRecording();
if (!this.alive) {
return;
}
let lastPath = <Paper.Path>this.path.lastChild;
// Draw holes
// When the length of the last path reaches a set number
if (this.draw && lastPath.length > this.holeSpacing) {
this.stopDrawing();
}
// When your distance to the last segment is over X, start drawing again
if (!this.draw && this.pos.getDistance(lastPath.lastSegment.point) > this.path.strokeWidth * 4) {
this.startDrawing();
this.holeSpacing = this.getRandomHoleSpacing();
}
// Calculate new position
const r = Game.params.radius;
const s = this.speed;
const deltaAngle = Direction.radToDeg(Math.acos(1 - (s * s) / ( 2 * r * r))) * command;
this.direction = new Direction(this.direction.deg + deltaAngle);
this.pos = new paper.Point({
x: this.pos.x + (this.direction.x * this.speed),
y: this.pos.y + (this.direction.y * this.speed)
});
if (this.checkCollision(this.pos)) {
this.collision();
return;
}
// Build path
if (!this.draw) {
return;
}
this.curvePainter.addToSegment(this.pos, command);
}
}
class Keyboard {
keys: {[key:string]:{pressed:boolean}} = {};
constructor() {
document.addEventListener('keydown', (event) => {
const keyName = event.key;
this.keys[keyName] = {pressed: true};
});
document.addEventListener('keyup', (event) => {
const keyName = event.key;
this.keys[keyName] = {pressed: false};
});
}
}
const keyboard = new Keyboard();
const game = new Game({
players: config.game.players.map((player) => new Player(player))
});