-
Notifications
You must be signed in to change notification settings - Fork 0
/
Actors.js
652 lines (544 loc) · 23.2 KB
/
Actors.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
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
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
// Guardian Actors
// Copyright (c) 2023 CROQUET CORPORATION
// The Guardian game is basically a 2D game. Virtually all computations in the model are 2D.
// The flat world is placed on a Perlin noise generated surface in the view, but all interactions including
// driving and collisions are computed in 2D.
import { ModelRoot, Actor, mix, AM_Spatial, AM_Behavioral, v3_add, v3_sub, UserManager, User, AM_Avatar, q_axisAngle, v3_normalize, v3_rotate, AM_Grid, AM_OnGrid } from "@croquet/worldcore-kernel"; // eslint-disable-line import/no-extraneous-dependencies
const v_dist2Sqr = function (a,b) {
const dx = a[0] - b[0];
const dy = a[2] - b[2];
return dx*dx+dy*dy;
};
const v_mag2Sqr = function (a) {
return a[0]*a[0]+a[2]*a[2];
};
//------------------------------------------------------------------------------------------
//-- BaseActor -----------------------------------------------------------------------------
// This is the ground plane.
//------------------------------------------------------------------------------------------
class BaseActor extends mix(Actor).with(AM_Spatial, AM_Grid) {
get pawn() {return "BasePawn"}
get gamePawnType() { return "" } // don't build a connected pawn for Unity
// init(options) {
// super.init(options);
// }
}
BaseActor.register('BaseActor');
//------------------------------------------------------------------------------------------
// HealthCoinActor ---------------------------------------------------------------------------
// Displays the current state of health of the tower in a spinning coin
//------------------------------------------------------------------------------------------
class HealthCoinActor extends mix(Actor).with(AM_Spatial) {
get pawn() { return "HealthCoinPawn" }
get gamePawnType() { return "healthcoin" }
init(...args) {
super.init(...args);
this.angle = 0;
this.deltaAngle = 0.1; // radians per 100ms step
this.spin();
}
spin() {
this.angle+=this.deltaAngle;
this.set({rotation: q_axisAngle([0,1,0], this.angle)});
this.future(100).spin();
}
}
HealthCoinActor.register('HealthCoinActor');
//------------------------------------------------------------------------------------------
// FireballActor ---------------------------------------------------------------------------
// Bot explosions - small one when you shoot them, big one when they suicide at the tower
//------------------------------------------------------------------------------------------
class FireballActor extends mix(Actor).with(AM_Spatial) {
get pawn() { return "FireballPawn" }
get gamePawnType() { return "fireball" }
get onTarget() { return this._onTarget }
init(...args) {
super.init(...args);
this.timeScale = 0.00025 + Math.random()*0.00002;
this.future(2000).destroy();
}
}
FireballActor.register('FireballActor');
//------------------------------------------------------------------------------------------
// BotActor --------------------------------------------------------------------------------
// The bad guys - they try to get to the tower to blow it up
//------------------------------------------------------------------------------------------
class BotActor extends mix(Actor).with(AM_Spatial, AM_OnGrid, AM_Behavioral) {
get pawn() { return "BotPawn" }
get gamePawnType() { return "bot" }
get index() {return this._index || 0}
init(options) {
super.init(options);
this.radius = 5;
this.radiusSqr = this.radius*this.radius;
this.doFlee();
this.go([0,0,0]);
this.future(Math.random()*500).beep();
}
beep() { // beep twice a second
this.future(500).beep();
this.say("beep");
}
go(target) {
if (this.ggg) {
this.ggg.destroy();
this.ggg = null;
}
const speed = (16 + 4 * Math.random());
this.ggg = this.behavior.start( {name: "GotoBehavior", target, speed, noise:2, radius:1} );
}
killMe(s=0.3, onTarget) {
let t = this.translation;
FireballActor.create({translation:[t[0],t[1], t[2]], scale:[s,s,s], onTarget});
this.publish("bots", "destroyedBot", onTarget);
this.destroy();
}
resetGame() {
if (this.ggg) {
this.ggg.destroy();
this.ggg = null;
}
this.destroy();
}
doFlee() {
let distSqr = v_mag2Sqr(this.translation);
// stop avoiding collisions when we get close to the tower
if ( distSqr < 1000 ) {
// if we are close to the tower, blow up
if ( distSqr < 20 ) {
this.killMe(1, true);
}
if ( !this.doomed ) this.future(100).doFlee();
} else { // otherwise, check if we need to move around an object
if ( !this.doomed ) this.future(100).doFlee();
const blockers = this.pingAll("block");
if (blockers.length===0 || blockers.length>4) return;
blockers.forEach(blocker => this.flee(blocker));
}
}
flee(blocker) {
const from = v3_sub(this.translation, blocker.translation);
const mag2 = v_mag2Sqr(from);
let r, r2;
if (blocker.isAvatar) {
r2 = this.radiusSqr*2;
r = this.radius*2;
} else {
r2 = this.radiusSqr;
r = this.radius;
}
if (mag2 > r2) return;
// move the bot to the radius of the blocker
if (mag2<0.00001) {
const a = Math.random() * 2 * Math.PI;
from[0] = r * Math.cos(a);
from[1] = 0;
from[2] = r * Math.sin(a);
} else {
let mag = Math.sqrt(mag2);
from[0] = r * from[0] / mag;
from[1] = 0;
from[2] = r * from[2] / mag;
}
const translation = v3_add(blocker.translation, from);
this.set({translation});
}
}
BotActor.register("BotActor");
//------------------------------------------------------------------------------------------
//--BollardActor, TowerActor ---------------------------------------------------------------
// Actors that place themselves on the grid so other actors can avoid them
//------------------------------------------------------------------------------------------
class BollardActor extends mix(Actor).with(AM_Spatial, AM_OnGrid) {
get pawn() { return "BollardPawn" }
get gamePawnType() { return "bollard" }
get radius() { return this._radius }
}
BollardActor.register('BollardActor');
class TowerActor extends mix(Actor).with(AM_Spatial, AM_OnGrid) {
get pawn() { return "TowerPawn"}
get gamePawnType() { return this._index >= 0 ? `tower${this._index}` : "" } // tower "-1" has no pawn; actor collisions only
get radius() { return this._radius || 0 } // central tower isn't even assigned a radius
}
TowerActor.register('TowerActor');
//------------------------------------------------------------------------------------------
//--MissileActor ---------------------------------------------------------------------------
// Fired by the tank - they destroy the bots but bounce off of everything else
//------------------------------------------------------------------------------------------
const missileSpeed = 75;
class MissileActor extends mix(Actor).with(AM_Spatial, AM_Behavioral) {
get pawn() { return "MissilePawn" }
get gamePawnType() { return "missile" }
init(options) {
super.init(options);
this.future(8000).destroy(); // destroy after some time
this.lastTranslation = [0,0,0];
this.lastBounce = null; // the thing we last bounced off
this.tick();
}
resetGame() {
this.destroy();
}
get colorIndex() { return this._colorIndex }
tick() {
this.test();
if (!this.doomed) this.future(10).tick();
}
test() {
const bot = this.parent.pingAny("bot", this.translation, 4, this);
if (bot) {
const d2 = v_dist2Sqr(this.translation, bot.translation);
if (d2 < 4) { // bot radius is 2
bot.killMe(0.3, false);
this._avatar.addKill();
console.log('Bot killed by: ', this._avatar.id, this._avatar.kills);
// console.log(`bot ${bot.id} hit at distance ${Math.sqrt(d2).toFixed(2)}`);
this.destroy();
return;
}
}
// the blockers (tagged with "block") include all avatars
const blocker = this.parent.pingAny("block", this.translation, 4, this);
if (blocker) {
if (!this.lastBounce && blocker.tags.has("avatar") && blocker.colorIndex === this.colorIndex) {
// ignore own avatar when it's the first object we've encountered
} else if (blocker !== this.lastBounce) {
const d2 = v_dist2Sqr(this.translation, blocker.translation);
if (d2 < 2.5) {
// console.log("bounce", blocker);
this.lastBounce = blocker;
let aim = v3_sub(this.translation, blocker.translation);
aim[1]=0;
aim = v3_normalize(aim);
if (this.go) this.go.destroy();
this.go = this.behavior.start({name: "GoBehavior", aim, speed: missileSpeed, tickRate: 20});
this.ballisticVelocity = aim.map(val => val * missileSpeed);
this.say("bounce");
}
}
}
this.lastTranslation = this.translation;
}
}
MissileActor.register('MissileActor');
//------------------------------------------------------------------------------------------
//-- AvatarActor ---------------------------------------------------------------------------
// This is you. Most of the control code for the avatar is in the pawn in Avatar.js.
//------------------------------------------------------------------------------------------
class AvatarActor extends mix(Actor).with(AM_Spatial, AM_Avatar, AM_OnGrid) {
get pawn() { return "AvatarPawn" }
get gamePawnType() { return "tank" }
init(options) {
super.init(options);
this.isAvatar = true;
this._kills = 0;
this.listen("shoot", this.doShoot);
this.subscribe("all", "godMode", this.doGodMode);
}
get colorIndex() { return this._colorIndex }
doGodMode(gm) {
this.publish("all", "godModeChanged", gm);
}
addKill() {
this.publish(this.id, "killset");
this._kills++;
}
get kills() { return this._kills; }
set kills(k) { this._kills = k; }
doShoot(argFloats) {
// view is now expected to set the launch location, given that the launcher
// can compensate for its own velocity
const [ x, y, z, yaw ] = argFloats;
const aim = v3_rotate([0,0,1], q_axisAngle([0,1,0], yaw));
const translation = [x, y, z]; // v3_add([x, y, z], v3_scale(aim, 5));
const missile = MissileActor.create({parent: this.parent, translation, colorIndex: this.colorIndex, avatar: this});
missile.go = missile.behavior.start({name: "GoBehavior", aim, speed: missileSpeed, tickRate: 20});
missile.ballisticVelocity = aim.map(val => val * missileSpeed);
this.say("didShoot");
}
resetGame() { // don't go home at end of game
this.publish(this.id, "killtotal", this.kills);
this.kills = 0;
// this.say("goHome");
}
}
AvatarActor.register('AvatarActor');
//------------------------------------------------------------------------------------------
//-- Users ---------------------------------------------------------------------------------
// Create a new avatar when a new user joins.
//------------------------------------------------------------------------------------------
class MyUserManager extends UserManager {
init() {
super.init();
this.props = new Map();
this.propsTimeout = 60*60*1000; // 1 hour
}
get defaultUser() {return MyUser}
createUser(options) {
const { userId } = options;
// restore saved props
const saved = this.props.get(userId);
if (saved) {
options = {...options, savedProps: saved.props};
this.props.delete(userId);
}
// delete old saved props
const expired = this.now() - this.propsTimeout;
for (const [uid, {lastSeen}] of this.props) {
if (lastSeen < expired) {
this.props.delete(uid);
}
}
return super.createUser(options);
}
destroyUser(user) {
const props = user.saveProps();
if (props) {
this.props.set(user.userId, {props, lastSeen: this.now()});
}
super.destroyUser(user);
}
}
MyUserManager.register('MyUserManager');
class MyUser extends User {
init(options) {
super.init(options);
const base = this.wellKnownModel("ModelRoot").base;
const placementAngle = Math.random() * Math.PI * 2;
const placementDist = 15 + Math.random() * 30; // 15 to 45 (closest bollard is around 50 from centre)
// choose an orientation that isn't out along the placement spoke, in case
// we're near the tower and the camera behind us gets blocked
const yaw = placementAngle + Math.PI + (1 - Math.random() * 2) * Math.PI/2;
const props = options.savedProps || {
colorIndex: options.userNumber%24,
translation: [placementDist * Math.sin(placementAngle), 0, placementDist * Math.cos(placementAngle)],
rotation: q_axisAngle([0,1,0], yaw),
};
this.avatar = AvatarActor.create({
parent: base,
driver: this.userId,
tags: ["avatar", "block"],
...props
});
}
saveProps() {
const { color, colorIndex, translation, rotation } = this.avatar;
return { color, colorIndex, translation, rotation };
}
destroy() {
super.destroy();
if (this.avatar) this.avatar.destroy();
}
}
MyUser.register('MyUser');
//------------------------------------------------------------------------------------------
//-- GameStateActor ------------------------------------------------------------------------
// Manage global game state.
//------------------------------------------------------------------------------------------
class GameStateActor extends Actor {
// get pawn() { return "GameStatePawn" } // if needed
get gamePawnType() { return "gamestate" }
init(options) {
super.init(options);
this.demoMode = false;
this.subscribe("game", "gameStarted", this.gameStarted); // from ModelRoot.startGame
this.subscribe("game", "undying", this.undying); // from user input
this.subscribe("bots", "madeWave", this.madeBotWave); // from ModelRoot.makeWave
this.subscribe("bots", "destroyedBot", this.destroyedBot); // from BotActor.killMe
this.subscribe("stats", "update", this.updateStats); // from BotHUD (forcing stats to be published, as an alternative to just reading them)
}
gameStarted() {
this.runKey = Math.random();
this.wave = 0;
this.totalBots = 0;
this.health = 100;
this.gameEnded = false;
this.updateStats();
}
undying() {
this.demoMode = !this.demoMode;
console.log("demo mode is:", this.demoMode?"on":"off");
}
madeBotWave({ wave, addedBots }) {
this.wave = wave;
this.totalBots += addedBots;
this.updateStats();
}
destroyedBot(onTarget) {
this.totalBots--;
if (onTarget && !this.demoMode) {
this.health--;
this.publish("stats", "health", this.health);
if (this.health === 0) {
console.log("publish the endGame");
this.gameEnded = true;
this.publish("game", "endGame");
}
}
this.publish("stats", "bots", this.totalBots);
}
updateStats() {
// legacy code for THREE version
this.publish("stats", "wave", this.wave);
this.publish("stats", "bots", this.totalBots);
this.publish("stats", "health", this.health);
if (this.gameEnded) this.publish("user", "endGame");
}
}
GameStateActor.register('GameStateActor');
//------------------------------------------------------------------------------------------
//-- LobbyRelayActor -----------------------------------------------------------------------
//------------------------------------------------------------------------------------------
// Todo: make Elected into a mixin
class Elected extends Actor {
init() {
super.init();
this.viewIds = new Set();
this.electedViewId = "";
this.subscribe(this.sessionId, "view-join", this.viewJoined);
this.subscribe(this.sessionId, "view-exit", this.viewExited);
}
viewJoined(viewId) {
this.viewIds.add(viewId);
this.viewsChanged();
}
viewExited(viewId) {
this.viewIds.delete(viewId);
this.viewsChanged();
}
viewsChanged() {
if (!this.viewIds.has(this.electedViewId)) {
this.electedViewId = this.viewIds.values().next().value;
this.viewElected(this.electedViewId);
// console.log(this.now(), "elected", this.electedViewId);
}
}
viewElected(viewId) {
this.publish(this.sessionId, "elected-view", viewId);
}
}
Elected.register("Elected");
class LobbyRelayActor extends Elected {
get pawn() { return "LobbyRelayPawn" }
get gamePawnType() { return "" } // will create a vanilla Pawn
init() {
super.init();
this.beWellKnownAs("lobbyRelayActor");
this.changeId = 0;
this.toRelay = null;
}
viewsChanged() {
super.viewsChanged();
if (this.viewIds.size === 0) {
this.toRelay = null;
} else {
this.toRelay = { changeId: ++this.changeId, views: [...this.viewIds] };
this.say("relay-views", this.toRelay);
}
// console.log("relay", this.now(), "relay-views", this.toRelay);
}
viewElected(viewId) {
// console.log("relay", this.now(), "relay-changed", this.electedViewId);
this.say("relay-changed", viewId);
}
}
LobbyRelayActor.register("LobbyRelayActor");
//------------------------------------------------------------------------------------------
//-- MyModelRoot ---------------------------------------------------------------------------
//------------------------------------------------------------------------------------------
export class MyModelRoot extends ModelRoot {
static modelServices() {
return [MyUserManager];
}
init(options) {
super.init(options);
this.gameState = GameStateActor.create({});
this.subscribe("game", "endGame", this.endGame); // from GameState.destroyedBot
this.subscribe("game", "startGame", this.startGame); // from BotHUD button
this.subscribe("game", "bots", this.demoBots); // from user input
const bollardScale = 3; // size of the bollard
const bollardDistance = bollardScale*3; // distance between bollards
this.base = BaseActor.create({gridScale: bollardScale});
this.maxBots = 1000;
this.spawnRadius = 400;
let v = [-10,0,0];
// place the fins for collisions
for (let i=0; i<3; i++) {
const p3 = Math.PI*2/3;
this.makeSkyscraper(v[0], 0, v[2],i*p3-Math.PI/2, -1, 1.5);
v = v3_rotate( v, q_axisAngle([0,1,0], p3) );
}
let corner = 12;
[[-corner,-corner, Math.PI/2-Math.PI/4], [-corner, corner, Math.PI/2+Math.PI/4], [corner, corner, Math.PI/2+Math.PI-Math.PI/4], [corner,-corner, Math.PI/2+Math.PI+Math.PI/4]].forEach( xy => {
this.makeSkyscraper(bollardDistance*xy[0]+1.5, 0, bollardDistance*xy[1]+1.5,xy[2], 5, 1.5);
});
//place the bollards
corner--;
for (let x=-corner; x<=corner; x++) for (let y=-corner; y<=corner; y++) {
if ((y<=-corner+2 || y>=corner-2) || (x<=-corner+2 || x>=corner-2) || (y<=-corner+7 && x<=-corner+7)) {
this.makeBollard(bollardDistance*x, bollardDistance*y);
}
}
const d = 290;
// the main tower
const tower0 = this.makeSkyscraper( 0, -1.2, 0, -0.533, 0); // no radius on central tower
this.makeSkyscraper( 0, -1, d, Math.PI/2, 1, 0);
this.makeSkyscraper( 0, -1, -d, 0, 2, 0);
this.makeSkyscraper( d, -1, 0, 0, 3, 0);
this.makeSkyscraper(-d-10, -3, -8, Math.PI+2.5, 4, 0);
HealthCoinActor.create({ parent: tower0, translation: [0, 15, 0] });
LobbyRelayActor.create();
this.startGame();
}
startGame() {
console.log("Start Game");
this.publish("game", "gameStarted"); // alert the users to remove the start button
this.makeWave(1, 10);
}
endGame() {
console.log("End Game");
this.service('ActorManager').actors.forEach( value => {if (value.resetGame) value.future(0).resetGame();});
}
demoBots( numBots ) {
this.makeWave(0, numBots);
}
makeWave(wave, numBots, key = this.gameState.runKey) {
// filter out scheduled waves from games that already finished
if (this.gameState.gameEnded || key !== this.gameState.runKey) return;
const { totalBots } = this.gameState;
let actualBots = Math.min(this.maxBots, numBots);
if ( totalBots + actualBots > this.maxBots) actualBots = this.maxBots-totalBots;
const r = this.spawnRadius; // radius of spawn
const a = Math.PI*2*Math.random(); // come from random direction
let xx = 0, yy = 0;
for (let n = 0; n<actualBots-1; n++) {
const aa = a + (0.5-Math.random())*Math.PI/4; // angle +/- Math.PI/4 around r
const rr = r+100*Math.random();
const x = Math.sin(aa)*rr;
const y = Math.cos(aa)*rr;
xx += x;
yy += y;
const index = Math.floor(20*Math.random());
// stagger when the bots get created
this.future(Math.floor(Math.random()*200)).makeBot(x, y, index, false);
}
// create the last bot in the center of the group. This is the one that plays the group sound
xx /= (actualBots-1);
yy /= (actualBots-1);
this.future(Math.floor(Math.random()*200)).makeBot(xx, yy, Math.floor(20*Math.random()), true);
if (wave>0) this.future(30000).makeWave(wave+1, Math.floor(numBots*1.2), key);
this.publish("bots", "madeWave", { wave, addedBots: actualBots });
}
makeBollard(x, z) {
BollardActor.create( { tags: ["block"], parent: this.base, obstacle: true, radius:1.5, translation: [x, 0, z]} );
}
makeSkyscraper(x, y, z, r, index, radius) {
const tower = TowerActor.create( { tags: radius ? ["block"] : [], parent: this.base, index, obstacle: true, radius, translation:[x, y, z], height:y, rotation: q_axisAngle([0,1,0],r)} );
return tower;
}
makeBot(x, z, index, centerBot = false) {
const bot = BotActor.create({parent: this.base, tags: ["block", "bot"], index, radius: 2, translation: [x, 0.5, z], centerBot});
return bot;
}
}
MyModelRoot.register("MyModelRoot");