-
Notifications
You must be signed in to change notification settings - Fork 0
/
netmons.js
818 lines (780 loc) · 24.2 KB
/
netmons.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
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
"use strict";
/*
Copyright (c) 2022 Oliver "oz" Z.
This software is free-as-in-freedom and licensed under GNU AGPLv3.
See LICENSE.txt for details!
*/
const DEBUG = false;
const BASE_SIZE = 240;
const GRID_SIZE = BASE_SIZE / 8;
const HALF_GRID_SIZE = GRID_SIZE / 2;
const SPRITE_SIZE = 32;
const HALF_SPRITE_SIZE = 32 / 2;
const ITEM_SIZE = HALF_SPRITE_SIZE;
const WIDTH = BASE_SIZE;
const HEIGHT = BASE_SIZE;
/*
* Each Mon has a (typed) Special Attack
* Each Mon has a (untyped) Neutral Attack
* Range can be avoided, melee attacks only work in melee range
* Utility abilities?
*/
const TB = 1.5; // Bonus
const TN = 1.0; // Neutral
const TM = 0.5; // Malus
const DB = { // Stats: HP, Atk, Def, Spd
types: ["Neutral", "Gaia", "Fire", "Water"],
typeMatrix: [ // BASE: Gaia > Water > Fire > Gaia | EXP: Neutral > Void > Light > Neutral
[TN, TN, TN, TN],
[TN, TN, TM, TB],
[TN, TB, TN, TM],
[TN, TM, TB, TN],
],
items: [
{
name: "Candy"
},
{
name: "Salad"
},
{
name: "Steak"
},
{
name: "Water"
},
{
name: "Phone"
},
],
attacks: [
{
name: "Nope",
type: 0,
dmg: 3
},
{
name: "Hit",
type: 0,
dmg: 5
},
{
name: "Axe Throw",
type: 0,
dmg: 5
},
{
name: "Rootgrip",
type: 1,
dmg: 5
},
{
name: "Fireball",
type: 2,
dmg: 5
},
{
name: "Slash",
type: 0,
dmg: 5
},
{
name: "Tide",
type: 3,
dmg: 5
},
{
name: "Choke",
type: 0,
dmg: 5
},
],
mons: [
{
name: "Glitchee",
type: 0,
stats: [10, 3, 3, 3],
melee: 0,
range: null,
evo: {
"1234": "Gooh"
}
},
{
name: "Gooh",
type: 0,
stats: [15, 5, 5, 5],
melee: 1,
range: null,
evo: {
"1113": "Trolmon",
"1222": "Drakano",
"2333": "Nessya",
"4444": "Glomon",
"1234": "Lalima"
}
},
{
name: "Trolmon",
type: 1,
stats: [30, 10, 10, 10],
melee: 3,
range: 2,
evo: {
"2222": "Gooh"
}
},
{
name: "Drakano",
type: 2,
stats: [30, 10, 10, 10],
melee: 5,
range: 4,
evo: {
"3333": "Gooh"
}
},
{
name: "Nessya",
type: 3,
stats: [30, 10, 10, 10],
melee: 7,
range: 6,
evo: {
"1111": "Gooh"
}
},
{
name: "Glomon",
type: 2, // Neutral (0) type would be more accurate, but it should glow at night for now (like fire types do)!
stats: [30, 10, 10, 10],
melee: 0,
range: null,
evo: {
"1234": "Gooh"
}
},
{
name: "Lalima",
type: 1,
stats: [30, 10, 10, 10],
melee: 0,
range: null,
evo: {
"1111": "Gooh",
"2222": "Gooh",
"3333": "Gooh",
"4444": "Gooh"
}
}
],
phonebook: {
"ghostbusters": {url: "https://www.youtube.com/watch?v=Fe93CLbHjxQ", embeddable: false},
"god": {url: "https://www.youtube.com/watch?v=79fzeNUqQbQ", embeddable: false},
"rick astley": {url: "https://www.youtube.com/watch?v=dQw4w9WgXcQ", embeddable: false},
"adventures": {url: youtubeAutoplayURL("MkNeIUgNPQ8"), embeddable: true}, // A Himitsu
"stop": {url: "", embeddable: true},
"mute": {url: "", embeddable: true},
"off": {url: "", embeddable: true},
"sound off": {url: "", embeddable: true},
},
decor: {
grasses: ["grass0", "grass1", "grass2", "grass3"],
flowers: ["flower1", "flower2"]
}
}
function sanitizeKind(kind) {
if (DB.mons.map(m => m.name).some(n => n === kind)) {
return kind;
}
return 'Glitchee';
}
function dbEntryForKind(kind) {
kind = sanitizeKind(kind);
return DB.mons.filter(m => m.name === kind)[0];
}
let config = {
type: Phaser.AUTO,
scale: {
parent: "game",
fullscreenTarget: "game",
mode: Phaser.Scale.FIT,
width: WIDTH,
height: HEIGHT
},
render: {
pixelArt: true
},
scene: {
preload: preload,
create: create,
update: update
}
};
let game = new Phaser.Game(config);
function loadAsset(scene, name) {
scene.load.image(name.toLowerCase(), `a/su${name.toLowerCase()}.png`);
}
function preload() {
loadAsset(this, "sun");
loadAsset(this, "moon");
loadAsset(this, "sky");
loadAsset(this, "ground");
loadAsset(this, "evolution");
this.load.spritesheet('flowers', "a/suflowers.png", {frameWidth: 16, frameHeight: 16});
for (let item of DB.items) {
loadAsset(this, item.name);
}
for (let mon of DB.mons) {
loadAsset(this, mon.name);
}
for (let grass of DB.decor.grasses) {
loadAsset(this, grass);
}
}
let GRAPHICS;
let events = [];
let _gameState = {
scene: null,
mon: null,
stomach: [],
maxStomachSize: 4,
idleThreshold: 5000,
itemWaitTime: 0,
itemWaitThreshold: 8000,
itemSpawnLocations: [ // 0 top left, 1 top right, 2 bottom left, 3 bottom right
[HALF_GRID_SIZE, GRID_SIZE * 2 + HALF_GRID_SIZE],
[GRID_SIZE * 7 + HALF_GRID_SIZE, GRID_SIZE * 2 + HALF_GRID_SIZE],
[HALF_GRID_SIZE, GRID_SIZE * 7 + HALF_GRID_SIZE],
[GRID_SIZE * 7 + HALF_GRID_SIZE, GRID_SIZE * 7 + HALF_GRID_SIZE],
],
items: [
null,
null,
null,
null
],
sun: null,
moon: null,
tint: 0xFFFFFF
};
// Events
class NMEvent {
constructor() { }
update(t, dt) { return null; } // Return this if you want to keep event, return null if done processing and drop it
}
class EventMonMoveTo extends NMEvent {
constructor(mon, x, y, item=null) {
super();
this.mon = mon;
this.x = x;
this.y = y;
this.item = item;
}
update(t, dt) {
if (this.mon !== null) {
this.mon.idleTime = 0;
if (this.item !== null) {
this.mon.moveToItem(this.item);
} else {
this.mon.moveTo(this.x, this.y);
}
}
return null;
}
}
class EventTap extends EventMonMoveTo {
constructor(x, y, item=null) {
super(_gameState.mon, x, y, item);
}
}
class EventMonSpawn extends NMEvent {
constructor(x, y, kind) {
super();
this.mon = newMon(_gameState.scene, x, y, kind);
this.x = x;
this.y = y;
}
update(t, dt) {
// Out of bounds? Move to center, used for friends
if (this.x < 0 || this.x >= BASE_SIZE || this.y < 0 || this.y >= BASE_SIZE)
this.mon.moveTo(BASE_SIZE / 2 + random(-40, 40), BASE_SIZE / 2 + random(-40, 40));
return null;
}
}
class EventPlayerSpawn extends EventMonSpawn {
constructor(x, y, kind) {
super(x, y, kind);
_gameState.mon = this.mon;
setFavicon(`a/su${_gameState.mon.kind.toLowerCase()}.png`);
}
}
const IDLE_RANGE = 32;
class EventIdle extends EventMonMoveTo {
constructor(mon) {
let pos = mon.getPos();
super(mon, pos.x, pos.y);
this.idleThreshold = random(3500, 7000);
}
update(t, dt) {
if (this.mon.idleCount !== undefined) {
if (this.mon.idleTime >= this.idleThreshold) {
if (this.mon.idleCount > 0) {
events.push(new EventIdle(this.mon));
} else {
events.push(new EventFriendLeave(this.mon));
return null;
}
this.mon.idleCount -= 1;
} else {
this.mon.idleTime += dt;
return this;
}
}
let pos = this.mon.getPos();
this.x = pos.x + random(-IDLE_RANGE, IDLE_RANGE);
this.y = pos.y + random(-IDLE_RANGE, IDLE_RANGE);
return super.update(t, dt);
}
}
class EventItemSpawn extends NMEvent {
constructor() {
super();
let locationIdx = this._determineLocation();
let locationCoords = [..._gameState.itemSpawnLocations[locationIdx]];
locationCoords[0] += random(-2, 2);
locationCoords[1] += random(-2, 2);
let itemName = DB.items[random(0, DB.items.length - 1)].name;
this._destroyLocationIfNeeded(locationIdx);
_gameState.items[locationIdx] = _gameState.scene.add.sprite(locationCoords[0], locationCoords[1], itemName.toLowerCase()).setInteractive();
_gameState.items[locationIdx].setDepth(locationCoords[1] - ITEM_SIZE / 2);
_gameState.items[locationIdx].setTint(_gameState.tint);
_gameState.items[locationIdx].itemName = itemName;
_gameState.items[locationIdx].on('pointerdown', (pointer) => {
events.push(new EventTap(Math.floor(pointer.x), Math.floor(pointer.y), locationIdx));
});
}
_determineLocation() {
let freeLocationIndices = [0, 1, 2, 3].filter(idx => _gameState.items[idx] === null);
if (freeLocationIndices.length > 0) {
return freeLocationIndices[random(0, freeLocationIndices.length - 1)];
}
return random(0, 3);
}
_destroyLocationIfNeeded(locationIdx) {
if (_gameState.items[locationIdx] !== null) {
_gameState.items[locationIdx].destroy();
}
}
}
class EventItemConsume extends NMEvent {
constructor(mon, itemIdx) {
super();
if (_gameState.items[itemIdx] !== null) {
let itemId = DB.items.map(i => i.name).indexOf(_gameState.items[itemIdx].itemName);
if (itemId === 4) { // 4 is Phone
let friendURL = prompt("Whom to call?") || "";
if (friendURL === "") {
_gameState.stomach.push(itemId);
} else {
events.push(new EventCallFriend(friendURL));
}
} else {
_gameState.stomach.push(itemId);
}
_gameState.items[itemIdx].destroy();
_gameState.items[itemIdx] = null;
while (_gameState.stomach.length > _gameState.maxStomachSize || (itemId == 0 && _gameState.stomach.length > 0)) {
_gameState.stomach.shift();
}
writeStateToURL();
if (_gameState.stomach.length === 4) {
let stomachString = [..._gameState.stomach].sort().reduce((a, b) => String(a) + String(b));
let monKind = _gameState.mon.kind;
let maybeEvolution = DB.mons.filter(mon => mon.name === monKind).map(mon => mon.evo[stomachString]).shift();
if (maybeEvolution !== undefined) events.push(new EventEvolution(maybeEvolution));
}
}
}
}
class EventEvolution extends NMEvent {
constructor(toMon) {
super();
if (_gameState.mon !== null) {
_gameState.mon.evolve(toMon);
_gameState.stomach = [];
setFavicon(`a/su${toMon.toLowerCase()}.png`);
writeStateToURL();
}
}
}
class EventCallFriend extends NMEvent {
constructor(friendURL) {
super();
let phonebookEntry = DB.phonebook[friendURL.toLowerCase()];
if (phonebookEntry !== undefined) {
events.push(new EventJukebox(phonebookEntry.url, phonebookEntry.embeddable));
return;
}
this._inviteFriend(friendURL);
}
_inviteFriend(friendURL) {
let friendState = readStateFromURL(friendURL);
let x = (random(0, 1) === 0) ? -32 : BASE_SIZE + 32;
let y = 150 + random(-BASE_SIZE / 4, BASE_SIZE / 4);
let event = new EventMonSpawn(x, y, friendState.kind);
events.push(event);
event.mon.idleCount = random(5, 8); // Friends leave after idling a few times
events.push(new EventIdle(event.mon));
}
}
class EventFriendLeave extends NMEvent {
constructor(mon) {
super();
this.mon = mon;
this.timer = 0;
}
update(t, dt) {
if (this.timer >= 1000) {
this.mon.leave();
return null;
}
this.timer += dt;
return this;
}
}
class EventJukebox extends NMEvent {
constructor(url, embeddable=false) {
super();
this.url = url;
this.embeddable = embeddable;
}
update(t, dt) {
if (this.embeddable) {
document.getElementById("jukebox").src = this.url;
} else {
window.location.href = this.url;
}
return null;
}
}
// Mon
function newMon(scene, x, y, kind) {
let dbEntry = dbEntryForKind(kind);
kind = dbEntry.name;
let sprite = scene.add.follower(null, x, y, kind.toLowerCase());
sprite.setDepth(y);
if (dbEntry.type == 2) {
sprite.toggleData("burns");
} else {
sprite.setTint(_gameState.tint);
}
function getPos() {
return {x: this.sprite.x, y: this.sprite.y};
}
function onMoveUpdate(self) {
return () => self.sprite.setDepth(self.getPos().y);
}
function onMoveToItemUpdate(self, item, itemIdx) {
return () => {
onMoveUpdate.apply(self);
let pos = self.getPos();
let distanceToItem = distance(pos.x, pos.y, item.x, item.y);
if (distanceToItem <= 12) {
events.push(new EventItemConsume(_gameState.mon, itemIdx));
}
}
}
function moveTo(x, y, onUpdate=onMoveUpdate(this)) {
let pos = this.getPos();
this.sprite.setFlipX(x > pos.x);
// HACK: hardcoded game area, consider onclick on sprites/game area later
if (x < HALF_SPRITE_SIZE) x = HALF_SPRITE_SIZE;
if (x >= BASE_SIZE - HALF_SPRITE_SIZE) x = BASE_SIZE - HALF_SPRITE_SIZE;
if (y < 56) y = 56;
if (y >= BASE_SIZE - HALF_SPRITE_SIZE) y = BASE_SIZE - HALF_SPRITE_SIZE;
this.sprite.setPath(new Phaser.Curves.Path(pos.x, pos.y).lineTo(x, y));
this.sprite.startFollow({
positionOnPath: true,
duration: monRunTimeForDistance(distance(pos.x, pos.y, x, y)),
repeat: 0,
rotateToPath: false,
onComplete: () => { },
onUpdate: onUpdate
});
}
function moveToItem(positionIdx) {
let item = _gameState.items[positionIdx];
if (item !== null) {
this.moveTo(item.x, item.y, onMoveToItemUpdate(this, item, positionIdx));
}
}
function evolve(toKind) {
let dbEntry = dbEntryForKind(toKind);
let pos = this.getPos();
let flip = this.sprite.flipX;
this.sprite.destroy();
this.sprite = this.scene.add.follower(null, pos.x, pos.y, toKind.toLowerCase())
this.sprite.setFlipX(flip);
this.sprite.setDepth(pos.y);
this.kind = toKind;
this.type = dbEntry.type;
if (this.type == 2) {
this.sprite.toggleData("burns");
} else {
this.sprite.setTint(_gameState.tint);
}
_evoFX(this.scene, pos);
}
function _evoFX(scene, pos) {
let evoFX = scene.add.follower(null, pos.x, pos.y, "evolution");
evoFX.setPath(new Phaser.Curves.Path(pos.x, pos.y).lineTo(pos.x, pos.y - 40));
evoFX.setDepth(pos.y + 1);
evoFX.toggleData("burns");
evoFX.startFollow({
positionOnPath: true,
duration: 500,
repeat: 0,
rotateToPath: false,
onComplete: () => { evoFX.destroy(); },
});
}
function leave() {
let x = (random(0, 1) === 0) ? -32 : BASE_SIZE + 32;
let y = 150 + random(-BASE_SIZE / 4, BASE_SIZE / 4);
let pos = this.getPos();
this.sprite.setFlipX(x > pos.x);
this.sprite.setPath(new Phaser.Curves.Path(pos.x, pos.y).lineTo(x, y));
this.sprite.startFollow({
positionOnPath: true,
duration: monRunTimeForDistance(distance(pos.x, pos.y, x, y)),
repeat: 0,
rotateToPath: false,
onComplete: () => { this.sprite.destroy() }
});
}
return {
kind: kind,
type: dbEntry.type,
sprite: sprite,
scene: scene,
idleTime: 0,
getPos: getPos,
moveTo: moveTo,
moveToItem: moveToItem,
evolve: evolve,
leave: leave
}
}
function onTap(pointer) {
events.push(new EventTap(Math.floor(pointer.x), Math.floor(pointer.y)));
}
function create() {
_gameState.scene = this;
// Background
let sky = this.add.image(WIDTH / 2, 60, 'sky');
_gameState.sun = this.add.image(2 * BASE_SIZE, 0, 'sun');
_gameState.moon = this.add.image(2 * BASE_SIZE, 0, 'moon');
let ground = this.add.sprite(WIDTH / 2, HEIGHT / 2, 'ground').setInteractive();
this.anims.create({
key: "flower1",
frames: this.anims.generateFrameNumbers("flowers", {frames: [0, 1]}),
frameRate: 1,
repeat: -1
});
this.anims.create({
key: "flower2",
frames: this.anims.generateFrameNumbers("flowers", {frames: [2, 3]}),
frameRate: 1,
repeat: -1,
repeatDelay: 500
});
for (let i = 0; i < 32; i++) {
this.add.image(0 + random(0, BASE_SIZE), 60 + random(0, BASE_SIZE - 60), 'grass' + random(0, 3));
}
for (let i = 0; i < 6; i++) {
let flower = this.add.sprite(0 + random(0, BASE_SIZE), 60 + random(0, BASE_SIZE - 60));
flower.setDepth(flower.y - ITEM_SIZE / 2);
flower.play("flower" + random(1, 2));
}
//this.input.on('pointerdown', onTap, this);
ground.on('pointerdown', onTap);
let initialState = readStateFromURL(window.location.href);
events.push(new EventPlayerSpawn(WIDTH / 2, HEIGHT / 2, initialState.kind));
_gameState.stomach = initialState.stomach;
if (DEBUG) {
GRAPHICS = this.add.graphics();
GRAPHICS.fillStyle(0xffff00, 1.0); // yellow, full alpha
for (let itemSpawnLocation of _gameState.itemSpawnLocations) {
GRAPHICS.fillRect(itemSpawnLocation[0], itemSpawnLocation[1], 1, 1);
}
}
}
function update(t, dt) {
gameMaster(t, dt);
let event = undefined;
let newEvent = null;
let leftovers = [];
while(typeof(event = events.shift()) !== 'undefined') {
newEvent = event.update(t, dt);
if (newEvent !== null) leftovers.push(newEvent);
}
Array.prototype.push.apply(events, leftovers);
}
// Game Master populates the event queue based on game state
let celestialUpdate = 60001;
function gameMaster(t, dt) {
// Idling
if (_gameState.mon !== null) {
if (_gameState.mon.idleTime > _gameState.idleThreshold) {
events.push(new EventIdle(_gameState.mon));
_gameState.idleThreshold = random(3500, 7000);
} else {
_gameState.mon.idleTime += dt;
}
}
// Item spawn
if (_gameState.itemWaitTime > _gameState.itemWaitThreshold) {
events.push(new EventItemSpawn());
_gameState.itemWaitTime = 0;
} else {
_gameState.itemWaitTime += dt;
}
// Sun / moon
if (celestialUpdate >= 60000) {
updateCelestials();
celestialUpdate = 0;
} else {
celestialUpdate += dt;
}
}
/*
Sun:
1440 --> -BASE_SIZE / 2 (-120)
1080 ---> 0
720 ---> BASE_SIZE / 2 (120)
360.0 --> BASE_SIZE (240)
0 --> (360)
Moon:
0 --> BASE_SIZE / 2 (120)
360 --> 0 0
720 --> -120 / 360
1080 --> BASE_SIZE (240)
1440 --> (120)
*/
function updateCelestials() {
let minutes = minutesOfDay();
let dayProgressInBaseSizes = ((1440.0 - minutes) / 1440.0) * (2 * BASE_SIZE); // 12h ~= 1 BASE_SIZE
let sunX = Math.round(dayProgressInBaseSizes - (BASE_SIZE / 2));
_gameState.sun.x = sunX;
_gameState.sun.y = celestialHeight(_gameState.sun.x);
_gameState.moon.x = (sunX < 120) ? sunX + BASE_SIZE : sunX - BASE_SIZE;
_gameState.moon.y = celestialHeight(_gameState.moon.x);
tintGame(minutes);
if (DEBUG) console.log("Minutes", minutes, "sunX", _gameState.sun.x, "sunY", _gameState.sun.y, "moonX", _gameState.moon.x, "moonY", _gameState.moon.y);
}
function minutesOfDay() {
let d = new Date();
let hours = d.getHours();
let minutes = d.getMinutes();
return hours * 60 + minutes;
}
function celestialHeight(x) {
return Math.round(50 - 60 * Math.sin((x / BASE_SIZE) * Math.PI));
}
const DARKEST_COLOR = 0x33; // Formerly: 0x444499; // 68, 68, 153
const NIGHT_BLUE = 0x88;
const TO_LIGHTEST = 0xFF - DARKEST_COLOR;
function tintGame(minutes) {
let tint = 0xFFFFFF;
if (minutes < 360 || minutes >= 1080) {
let darkMinutes = (minutes < 360) ? minutes : (1440 - minutes); // 6h (360min) before and after Midnight. The closer to Midnight, the darker
let darkeningRate = darkMinutes / 300;
let rg = Math.min(Math.ceil(DARKEST_COLOR + darkeningRate * TO_LIGHTEST), 255);
let b = Math.min(Math.ceil(NIGHT_BLUE + darkeningRate * TO_LIGHTEST), 255);
tint = rgbToInt(rg, rg, b);
}
updateGameTint(tint);
}
function updateGameTint(tint) {
_gameState.tint = tint;
if (DEBUG) console.log("Scene children", _gameState.scene.children.length);
for (let gameObj of _gameState.scene.children.getChildren()) {
if (gameObj != _gameState.moon
&& gameObj != _gameState.sun
&& !gameObj.getData("burns")) { // Fire types don't lack light!
gameObj.setTint(tint);
}
}
}
// Library
function distance(x1, y1, x2, y2) {
return Phaser.Math.Distance.Between(x1, y1, x2, y2);
}
const MON_SPEED_IN_PIXEL_PER_S = 90; // TODO: make dependent on mon, 100 fastest? 80 slowest?
function monRunTimeForDistance(distance) {
return Math.floor((distance / MON_SPEED_IN_PIXEL_PER_S) * 1000);
}
function random(min, max) {
return Phaser.Math.RND.between(min, max);
}
function setFavicon(url) {
let link = document.querySelector("link[rel~='icon']");
link.href = url;
}
function readStateFromURL(urlStr) {
let url;
let kind = 'Gooh';
let stomach = [];
try {
url = new URL(urlStr);
let parts = String(url.searchParams.get('s') || '').trim().split('-');
for (let part of parts) {
switch(part[0]) {
case 'k':
kind = part.substring(1);
break;
case 's':
stomach = Array.from(part.substring(1)).map(s => Number(s));
break;
}
}
} catch (_) { }
return {
kind: sanitizeKind(kind),
stomach: stomach
}
}
function writeStateToURL() {
let kind = _gameState.mon.kind;
let stomach = "";
if (_gameState.stomach.length > 0) {
stomach = _gameState.stomach.reduce((a, b) => String(a) + String(b));
}
let stateStr = String(`k${kind}-s${stomach}`);
var queryParams = new URLSearchParams(window.location.search);
queryParams.set("s", stateStr);
history.replaceState(null, null, "?"+queryParams.toString());
}
function youtubeAutoplayURL(videoID) {
return `https://www.youtube-nocookie.com/embed/${videoID}?autoplay=1&rel=0&loop=1&controls=0`;
}
function rgbToInt(red, green, blue) {
return Phaser.Display.Color.GetColor(red, green, blue);
}
// Fullscreen
game.scale.on(Phaser.Scale.Events.LEAVE_FULLSCREEN, () => {
game.scale.scaleMode = Phaser.Scale.NONE;
game.scale.resize(WIDTH, HEIGHT);
game.scale.setZoom(1);
});
game.scale.on(Phaser.Scale.Events.ENTER_FULLSCREEN, () => {
game.scale.scaleMode = Phaser.Scale.FIT;
game.scale.setGameSize(WIDTH, HEIGHT);
});
function goFullscreen() {
if (!game.scale.isFullscreen) {
game.scale.startFullscreen();
}
}
window.netmonsFullscreen = goFullscreen;