-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.js
More file actions
1595 lines (1430 loc) · 45.3 KB
/
game.js
File metadata and controls
1595 lines (1430 loc) · 45.3 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
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
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
document.addEventListener("DOMContentLoaded", function () {
// Create background halves
const topHalf = document.createElement("div");
topHalf.id = "top-half";
topHalf.classList.add("background-half");
topHalf.style.backgroundImage =
"url('media/graphics/sprites/ui/horizon.png')";
const bottomHalf = document.createElement("div");
bottomHalf.id = "bottom-half";
bottomHalf.classList.add("background-half");
bottomHalf.style.backgroundImage =
"url('media/graphics/sprites/ui/foreground.png')";
// Create logo container
const logoContainer = document.createElement("div");
logoContainer.id = "logo-container";
const logoImage = document.createElement("img");
logoImage.src = "media/graphics/sprites/ui/title.png";
logoImage.alt = "Game Logo";
logoContainer.appendChild(logoImage);
// Append elements to the body
document.body.appendChild(topHalf);
document.body.appendChild(bottomHalf);
document.body.appendChild(logoContainer);
// Create Play button
const playButton = document.createElement("button");
playButton.id = "play-button";
playButton.textContent = "PLAY";
const logoContainerBottomPosition = 10; // Example value; adjust based on actual layout
playButton.style.bottom = `${logoContainerBottomPosition}%`;
// Append Play button to the body
document.body.appendChild(playButton);
// Event listener for the Play button
playButton.addEventListener("click", function () {
// Logic to transition to the main game scene goes here
console.log("Play button clicked. Transition to the main game scene.");
document.getElementById("top-half").style.display = "none";
document.getElementById("bottom-half").style.display = "none";
document.getElementById("logo-container").style.display = "none";
playButton.style.display = "none";
document.getElementById("background").style.display = "block";
document.getElementById("character").style.display = "block";
document.getElementById("status").style.display = "block";
init();
});
});
var game = new Game();
function init() {
if (game.init()) game.start();
}
var imageRepository = new (function () {
var numImages = 5;
var numLoaded = 0;
this.control = new Image();
this.island = new Image();
this.tree = new Image();
this.background = {
"tile-water-1": new Image(),
"tile-water-2": new Image(),
};
this.character = {
"walk-up": new Image(),
"walk-down": new Image(),
"walk-left": new Image(),
"walk-right": new Image(),
"walk-up-left": new Image(),
"walk-down-left": new Image(),
"walk-up-right": new Image(),
"walk-down-right": new Image(),
"work-up": new Image(),
"work-down": new Image(),
};
this.bridge = {
horizontal: new Image(),
vertical: new Image(),
};
this.particles = {
ruby: new Image(),
emerald: new Image(),
amethyst: new Image(),
money: new Image(),
};
this.status = {
ruby: new Image(),
emerald: new Image(),
amethyst: new Image(),
money: new Image(),
};
this.jewel = {
"amethyst-mine": new Image(),
"emerald-mine": new Image(),
"ruby-mine": new Image(),
};
this.factory = {
amethyst: new Image(),
emerald: new Image(),
ruby: new Image(),
barn: new Image(),
};
function imageLoaded() {
numLoaded++;
if (numLoaded === numImages) {
window.init();
}
}
this.tree = "media/graphics/sprites/ingame/tree.png";
this.island.src = "media/graphics/sprites/ingame/island.png";
this.control.src = "media/graphics/sprites/ui/control.png";
this.island.onload = function () {
imageLoaded();
};
this.control.onload = function () {
imageLoaded();
};
this.tree.onload = function () {
imageLoaded();
};
Object.keys(this.character).forEach((key) => {
this.character[key].src = `media/graphics/sprites/character/${key}.png`;
this.character[key].onload = function () {
imageLoaded();
};
});
Object.keys(this.background).forEach((key) => {
this.background[key].src = `media/graphics/sprites/ingame/${key}.png`;
this.background[key].onload = function () {
imageLoaded();
};
});
Object.keys(this.jewel).forEach((key) => {
this.jewel[key].src = `media/graphics/sprites/ingame/${key}.png`;
this.jewel[key].onload = function () {
imageLoaded();
};
});
Object.keys(this.bridge).forEach((key) => {
this.bridge[key].src = `media/graphics/sprites/ingame/bridge-${key}.png`;
this.bridge[key].onload = function () {
imageLoaded();
};
});
Object.keys(this.factory).forEach((key) => {
this.factory[key].src = `media/graphics/sprites/ingame/factory-${key}.png`;
this.factory[key].onload = function () {
imageLoaded();
};
});
Object.keys(this.particles).forEach((key) => {
this.particles[
key
].src = `media/graphics/sprites/ingame/${key}-gem-particle.png`;
this.particles[key].onload = function () {
imageLoaded();
};
});
Object.keys(this.status).forEach((key) => {
this.status[key].src = `media/graphics/sprites/ui/${key}-gem.png`;
this.status[key].onload = function () {
imageLoaded();
};
});
})();
function Drawable() {
this.init = function (x, y, width, height) {
// Defualt variables
this.x = x;
this.y = y;
this.width = width;
this.height = height;
};
this.speed = 0;
this.canvasWidth = 0;
this.canvasHeight = 0;
this.type = "";
// Define abstract function to be implemented in child objects
this.draw = function () {};
this.move = function () {};
}
function Background() {
this.speed = 0; // Redefine speed of the background for panning
this.acc = 0;
this.tileSizeX = 720;
this.tileSizeY = 720;
this.drawTile = function (tileName, direction = 1) {
let tileCountX =
parseInt(game.viewSizeX / imageRepository.background[tileName].width) + 1;
let tileCountY =
parseInt(game.viewSizeY / imageRepository.background[tileName].height) +
1;
for (let offsetX = -10 * tileCountX; offsetX < 10 * tileCountX; offsetX++)
for (
let offsetY = -10 * tileCountY;
offsetY < 10 * tileCountY;
offsetY++
) {
this.context.drawImage(
imageRepository.background[tileName],
0,
0,
imageRepository.background[tileName].width,
imageRepository.background[tileName].height,
direction * offsetX * this.tileSizeX +
direction * this.x -
game.characterX,
direction * offsetY * this.tileSizeY +
direction * this.y -
game.characterY,
imageRepository.background[tileName].width,
imageRepository.background[tileName].height
);
}
};
this.draw = function () {
this.x += this.speed;
this.y += this.speed;
this.acc += 0.001;
this.context.clearRect(0, 0, game.viewSizeX, game.viewSizeY);
this.drawTile("tile-water-2", -1);
this.drawTile("tile-water-1", 1);
this.speed = 0.5 * Math.sin(this.acc);
};
}
Background.prototype = new Drawable();
function Character() {
this.speed = 4;
this.type = "character";
this.shadowOffsetY = 0;
this.shadowOffsetX = 0;
this.moveImage = imageRepository.character["walk-up"];
this.workImage = imageRepository.character["work-up"];
this.curMoveIdx = 0;
this.curWorkIdx = 0;
this.maxMoveIdx = 12;
this.maxWorkIdx = 6;
this.init = function (x, y, width, height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
};
this.moveSx = function () {
return parseInt(this.curMoveIdx % 6);
};
this.moveSy = function () {
return parseInt(this.curMoveIdx / 6);
};
this.workSx = function () {
return parseInt(this.curWorkIdx % 6);
};
this.workSy = function () {
return parseInt(this.curWorkIdx / 6);
};
this.moveIdx = function () {
this.curMoveIdx += 0.1;
if (this.curMoveIdx >= this.maxMoveIdx) this.curMoveIdx = 0;
};
this.workIdx = function () {
this.curWorkIdx += 0.1;
if (this.curWorkIdx >= this.maxWorkIdx) this.curWorkIdx = 0;
};
this.draw = function () {
this.context.clearRect(0, 0, game.viewSizeX, game.viewSizeY);
this.context.fillStyle = "rgba(33, 33, 33, 0.5)";
this.context.beginPath();
this.context.ellipse(
this.shadowOffsetX +
game.characterX -
game.viewPosX +
game.characterWidth / 2,
this.shadowOffsetY +
game.characterY -
game.viewPosY +
game.characterHeight,
game.characterWidth / 2 + 30,
50,
0,
0,
Math.PI * 2
);
this.context.fill();
//draw character
if (game.mining) {
this.workImage = this.moveImage.src.includes("down")
? imageRepository.character["work-down"]
: imageRepository.character["work-up"];
this.workIdx();
this.work();
} else {
this.move();
}
//draw control
if (game.control) {
this.context.fillStyle = "rgba(255, 0, 0, 0.5)";
this.context.beginPath();
this.context.arc(
game.controlX + game.controlOffsetX,
game.controlY + game.controlOffsetY,
30,
0,
Math.PI * 2
);
this.context.drawImage(
imageRepository.control,
game.controlX - imageRepository.control.width / 2,
game.controlY - imageRepository.control.height / 2
);
this.context.fill();
}
};
this.move = function () {
if (
KEY_STATUS.left ||
KEY_STATUS.right ||
KEY_STATUS.down ||
KEY_STATUS.up ||
game.control
) {
this.moveIdx();
if ((KEY_STATUS.left && KEY_STATUS.down) || game.direction == "DL") {
this.moveImage = imageRepository.character["walk-down-left"];
this.shadowOffsetX =
this.shadowOffsetX < 30 ? this.shadowOffsetX + 1 : 30;
if (game.movableLeft && game.movableDown) {
game.characterX -= this.speed;
game.characterY += this.speed;
}
} else if (
(KEY_STATUS.right && KEY_STATUS.down) ||
game.direction == "DR"
) {
this.moveImage = imageRepository.character["walk-down-right"];
this.shadowOffsetX =
this.shadowOffsetX > -30 ? this.shadowOffsetX - 1 : -30;
if (game.movableRight && game.movableDown) {
game.characterX += this.speed;
game.characterY += this.speed;
}
} else if ((KEY_STATUS.left && KEY_STATUS.up) || game.direction == "UL") {
this.moveImage = imageRepository.character["walk-up-left"];
if (game.movableLeft && game.movableUp) {
game.characterX -= this.speed;
game.characterY -= this.speed;
}
this.shadowOffsetX =
this.shadowOffsetX < 30 ? this.shadowOffsetX + 1 : 30;
} else if (
(KEY_STATUS.right && KEY_STATUS.up) ||
game.direction == "UR"
) {
this.moveImage = imageRepository.character["walk-up-right"];
this.shadowOffsetX =
this.shadowOffsetX > -30 ? this.shadowOffsetX - 1 : -30;
if (game.movableRight && game.movableUp) {
game.characterX += this.speed;
game.characterY -= this.speed;
}
} else if (KEY_STATUS.left || game.direction == "L") {
this.moveImage = imageRepository.character["walk-left"];
this.shadowOffsetX =
this.shadowOffsetX < 30 ? this.shadowOffsetX + 1 : 30;
if (game.movableLeft) {
game.characterX -= this.speed;
}
} else if (KEY_STATUS.right || game.direction == "R") {
this.moveImage = imageRepository.character["walk-right"];
this.shadowOffsetX =
this.shadowOffsetX > -30 ? this.shadowOffsetX - 1 : -30;
if (game.movableRight) {
game.characterX += this.speed;
}
} else if (KEY_STATUS.up || game.direction == "U") {
this.moveImage = imageRepository.character["walk-up"];
if (game.movableUp) {
game.characterY -= this.speed;
}
} else if (KEY_STATUS.down || game.direction == "D") {
this.moveImage = imageRepository.character["walk-down"];
if (game.movableDown) {
game.characterY += this.speed;
}
} else this.shadowOffsetX = 0;
}
let dx = game.characterX - game.viewSizeX / 2 - game.viewPosX;
let dy = game.characterY - game.viewSizeY / 2 - game.viewPosY;
game.viewPosX += dx / 5;
game.viewPosY += dy / 5;
this.context.drawImage(
this.moveImage,
this.moveSx() * 150,
this.moveSy() * 160,
150,
160,
game.characterX - game.viewPosX - 75,
game.characterY - game.viewPosY - 80,
game.characterWidth + 150,
game.characterHeight + 160
);
};
this.work = function () {
this.context.drawImage(
this.workImage,
this.workSx() * 187,
0,
187,
188,
game.characterX - game.viewPosX - 187 / 2,
game.characterY - game.viewPosY - 188 / 2,
game.characterWidth + 187,
game.characterHeight + 188
);
};
}
Character.prototype = new Drawable();
function Islands() {
const respawnDelay = 8000; // Delay for jewel respawn (in milliseconds)
this.jewelMoveSpeed = 0;
this.touchedJewelId = null;
// Fade-out properties
this.fadeOutDuration = 250; // Duration for fade-out in milliseconds
this.fadeOutStartTime = null; // Tracks when fade-out starts
this.drawParticles = function () {
if (game.curMintJewel !== null) {
if (game.curMintJewel.pieces.length == 0) {
game.curMintJewel = null;
return;
} else {
}
game.curMintJewel.pieces.forEach((piece, id) => {
piece.speed += 0.05;
let dx = 0;
let dy = 0;
if (
Math.abs(piece.x - piece.motion[piece.curMotionId].targetX) > 5 &&
Math.abs(piece.y - piece.motion[piece.curMotionId].targetY) > 5
) {
dx =
((piece.motion[piece.curMotionId].targetX - piece.x) / 50) *
Math.log(piece.speed);
dy =
((piece.motion[piece.curMotionId].targetY - piece.y) / 50) *
Math.log(piece.speed);
} else {
if (piece.curMotionId === piece.motion.length - 1) {
game.curMintJewel.pieces.splice(id, 1);
game.ownedJewel[game.curMintJewel.type] += 1;
} else {
piece.curMotionId++;
}
}
piece.x += dx;
piece.y += dy;
this.context.drawImage(
imageRepository.particles[game.curMintJewel.type],
0,
0,
imageRepository.particles[game.curMintJewel.type].width,
imageRepository.particles[game.curMintJewel.type].height,
piece.x,
piece.y,
piece.width,
piece.height
);
if (game.curMintJewel.pieces.length) {
this.context.font = "bold 60px Arial";
this.context.fillStyle = "black";
this.context.strokeStyle = "white";
this.context.lineWidth = 5;
this.context.strokeText(
"+" + game.curMintJewel.pieces.length,
game.curMintJewel.pieces[game.curMintJewel.pieces.length - 1].x,
game.curMintJewel.pieces[game.curMintJewel.pieces.length - 1].y
);
this.context.fillText(
"+" + game.curMintJewel.pieces.length,
game.curMintJewel.pieces[game.curMintJewel.pieces.length - 1].x,
game.curMintJewel.pieces[game.curMintJewel.pieces.length - 1].y
);
}
});
}
};
this.drawJewel = function (island, x, y, jewel) {
this.context.drawImage(
imageRepository.jewel[island.jewelType],
0,
0,
imageRepository.jewel[island.jewelType].width,
imageRepository.jewel[island.jewelType].height,
x,
y,
jewel.width,
jewel.height
);
};
this.drawIsland = function (island) {
this.context.drawImage(
imageRepository.island,
0,
0,
imageRepository.island.width,
imageRepository.island.height,
island.x - game.viewPosX,
island.y - game.viewPosY,
imageRepository.island.width,
imageRepository.island.height
);
};
this.drawBridge = function (island) {
if (island.bridges) {
island.bridges.forEach((bridge) => {
let type =
bridge.direction == "left" || bridge.direction == "right"
? "horizontal"
: "vertical";
bridge.width = imageRepository.bridge[type].width;
bridge.height = imageRepository.bridge[type].height;
switch (bridge.direction) {
case "up":
bridge.x =
island.x - game.viewPosX + island.width / 2 - bridge.width / 2;
bridge.y = island.y - game.viewPosY - bridge.height;
break;
case "down":
bridge.x =
island.x - game.viewPosX + island.width / 2 - bridge.width / 2;
bridge.y = island.y - game.viewPosY + island.height;
break;
case "left":
bridge.x = island.x - game.viewPosX - bridge.width;
bridge.y =
island.y - game.viewPosY + island.height / 2 - bridge.height / 2;
break;
case "right":
bridge.x = island.x - game.viewPosX + island.width;
bridge.y =
island.y - game.viewPosY + island.height / 2 - bridge.height / 2;
break;
default:
bridge.x = 0;
bridge.y = 0;
break;
}
this.context.drawImage(
imageRepository.bridge[type],
0,
0,
imageRepository.bridge[type].width,
imageRepository.bridge[type].height,
bridge.x,
bridge.y,
bridge.width,
bridge.height
);
});
}
};
this.drawTree = function (tree) {
this.context.drawImage(
imageRepository.tree,
0,
0,
imageRepository.tree.width,
imageRepository.tree.height,
tree.x,
tree.y,
tree.width,
tree.height
);
};
this.draw = function () {
this.data.forEach((island, index) => {
// Initialize movement flags
let movementFlags = {
movableUp: true,
movableDown: true,
movableLeft: true,
movableRight: true,
};
let collisionJewel;
let insideIsland;
// Helper function to update movement flags
const updateMovementFlags = (conditions) => {
conditions.forEach((condition) => {
movementFlags.movableRight =
movementFlags.movableRight && condition.right;
movementFlags.movableDown =
movementFlags.movableDown && condition.down;
movementFlags.movableLeft =
movementFlags.movableLeft && condition.left;
movementFlags.movableUp = movementFlags.movableUp && condition.up;
});
};
insideIsland = checkInside(
island.x,
island.y,
game.characterX,
game.characterY,
island.width,
game.characterWidth,
island.height,
game.characterHeight,
{
size: imageRepository.bridge["vertical"].width,
length: imageRepository.bridge["vertical"].height,
direction: { up: true, left: true, right: true, down: true },
}
);
// if (insideIsland.out.right || insideIsland.out.left||insideIsland.out.up||insideIsland.out.down)
game.onBridge = insideIsland;
console.log("game.onBridge", index, game.onBridge);
// Draw the island
this.drawIsland(island);
this.drawBridge(island);
// Helper function to calculate the distance between two points
const calculateDistance = (x1, y1, x2, y2) => {
return Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
};
// Modified function to respawn a jewel after removal, now with position condition
const respawnJewel = (x, y, lifecycle) => {
const checkAndRespawn = () => {
// Calculate the distance between the character and the jewel's respawn point
const distance = calculateDistance(
game.characterX,
game.characterY,
x,
y
);
// Only respawn if the character is farther than 500 units away
if (distance > 500) {
island.jewels.push(
game.initJewel(
x,
y,
0.5 * game.jewelWidth,
0.5 * game.jewelHeight,
lifecycle
)
);
} else {
// If the character is too close, check again after a short delay
setTimeout(checkAndRespawn, 1000); // Re-check every second
}
};
// Start the respawn process after the initial respawn delay
setTimeout(checkAndRespawn, respawnDelay);
};
island.trees&& island.trees.forEach(tree => {
this.drawTree(tree);
})
// Process jewels and check collisions
island.jewels.forEach((jewel, i) => {
const jewelX = island.x + jewel.x;
const jewelY = island.y + jewel.y;
// Check collision with character
collisionJewel = checkCollision(
game.characterX,
game.characterY,
jewelX,
jewelY,
game.characterWidth,
game.jewelWidth,
game.characterHeight,
game.jewelHeight
);
// Update movement flags based on collisions
updateMovementFlags([collisionJewel, game.onBridge]);
// Handle jewel touching logic
if (
(!collisionJewel.up ||
!collisionJewel.down ||
!collisionJewel.left ||
!collisionJewel.right) &&
!jewel.isFadingOut
) {
game.mining = true;
this.touchedJewelId = i;
this.jewelMoveSpeed += 0.04;
if (!jewel.touched) {
jewel.touched = true;
jewel.touchStartTime = Date.now(); // Start tracking touch time
jewel.totalTouchedTime = jewel.totalTouchedTime || 0; // Initialize total touch time
} else {
// Accumulate the touch time
let currentTouchTime = Date.now() - jewel.touchStartTime;
jewel.totalTouchedTime += currentTouchTime;
jewel.touchStartTime = Date.now(); // Reset start time for the next check
// Remove the jewel if it has been touched for longer than its lifecycle
if (jewel.totalTouchedTime >= jewel.lifecycle) {
jewel.fadeOutAlpha = 1; // Start fade-out with full opacity
jewel.isFadingOut = true; // Set fading out state
jewel.touched = false;
this.touchedJewelId = null;
this.jewelMoveSpeed = 0;
game.mining = false;
// Start fade out process
this.fadeOutStartTime = Date.now();
return; // Skip the rest of the loop for this jewel since it's marked for fade out
}
}
} else {
// If the jewel was touched but is no longer being touched, stop tracking
if (jewel.touched) {
jewel.touched = false;
let currentTouchTime = Date.now() - jewel.touchStartTime;
jewel.totalTouchedTime += currentTouchTime; // Accumulate total touch time
this.jewelMoveSpeed = 0;
this.touchedJewelId = null;
game.mining = false;
}
}
// Draw the jewel
if (jewel.isFadingOut) {
const elapsedTime = Date.now() - this.fadeOutStartTime;
const fadeOutProgress = elapsedTime / this.fadeOutDuration;
jewel.fadeOutAlpha = Math.max(0, 1 - fadeOutProgress); // Calculate current alpha
// Remove the jewel after it fades out
if (jewel.fadeOutAlpha === 0) {
const removedJewelPosition = {
x: jewel.x,
y: jewel.y,
lifecycle: jewel.lifecycle,
}; // Store position
island.jewels.splice(i, 1); // Remove the jewel
this.touchedJewelId = null;
this.jewelMoveSpeed = 0;
game.mining = false;
// Respawn the jewel after a delay
respawnJewel(
removedJewelPosition.x,
removedJewelPosition.y,
removedJewelPosition.lifecycle
);
game.curMintJewel = {
type: island.jewelType.split("-")[0],
index: 0,
pieces: Array(parseInt(Math.random() * 4 + 8))
.fill()
.map((_, i) => {
let size = parseInt(Math.random() * 50 + 50);
return {
x:
island.x -
game.viewPosX +
jewel.x +
game.jewelWidth -
jewel.width / 2,
y:
island.y -
game.viewPosY +
jewel.y +
game.jewelHeight -
jewel.height / 2,
speed: 1,
width: size,
height: size,
amount: 1,
curMotionId: 0,
motion: [
{
targetX:
parseInt(
(Math.random() * 100 + 200) *
(Math.random() * 2 - 1)
) +
island.x -
game.viewPosX +
jewel.x +
game.jewelWidth -
jewel.width / 2,
targetY:
parseInt(
(Math.random() * 100 + 200) *
(Math.random() * 2 - 1)
) +
island.y -
game.viewPosY +
jewel.y +
game.jewelHeight -
jewel.height / 2,
},
{
targetX: game.viewSizeX / 2,
targetY: game.viewSizeY / 2,
},
{
type: "linear",
targetX: 0,
targetY:
Object.keys(game.ownedJewel).indexOf(
island.jewelType.split("-")[0]
) * 100,
delay: 3000,
},
],
};
}),
};
return; // Skip the rest of the loop for this jewel since it's removed
}
}
jewel.width =
jewel.width < game.jewelWidth
? (jewel.width += game.jewelWidth * 0.005)
: game.jewelWidth;
jewel.height =
jewel.height < game.jewelHeight
? (jewel.height += game.jewelHeight * 0.005)
: game.jewelHeight;
let x =
i === this.touchedJewelId
? island.x -
game.viewPosX +
jewel.x +
15 * Math.sin(this.jewelMoveSpeed)
: island.x -
game.viewPosX +
jewel.x +
game.jewelWidth / 2 -
jewel.width / 2;
let y =
island.y -
game.viewPosY +
jewel.y +
game.jewelHeight / 2 -
jewel.height / 2;
// Set the global alpha value for the context before drawing
this.context.globalAlpha = jewel.isFadingOut ? jewel.fadeOutAlpha : 1;
this.drawJewel(island, x, y, jewel);
// Reset the alpha for subsequent drawings
this.context.globalAlpha = 1;
});
// Update game's movement flags based on the results of collisionJewels
this.drawParticles();
Object.assign(game, movementFlags);
});
// Allow character to move even while touching a jewel or during fade-out
game.character.move();
};
}
Islands.prototype = new Drawable();
function Status() {
this.showScoreAlpha = 1;
this.showScoreY = 0;
this.init = function (x, y, width, height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
};
this.mintJewelMotion = [
{
type: "linear",
targetX: game.viewSizeX / 2,
targetY: game.viewSizeY / 2,
delay: 3000,
},
];
this.curMotionId = 0;
this.oneComplete = false;
this.drawJewelStatus = function (jewel, x, y) {
this.context.globalAlpha = 1;
this.context.drawImage(
imageRepository.status[jewel],
0,
0,
imageRepository.status[jewel].width,
imageRepository.status[jewel].height,
x,
y,
100,
100
);
this.context.font = "bold 60px Arial";
this.context.fillStyle = "black";
this.context.strokeStyle = "white";
this.context.lineWidth = 5;
this.context.strokeText(game.ownedJewel[jewel], x + 100, y + 70);
this.context.fillText(game.ownedJewel[jewel], x + 100, y + 70);
// console.log("fill", game.ownedJewel[jewel]);
};
this.drawMint = function () {
if (game.curMintJewel) {
this.showScoreAlpha -= 0.01;
this.showScoreY += 1;
this.context.globalAlpha = this.showScoreAlpha;
this.context.drawImage(
imageRepository.status[game.curMintJewel.type],
0,
0,
imageRepository.status[game.curMintJewel.type].width,
imageRepository.status[game.curMintJewel.type].height,
game.characterX - game.viewPosX + game.characterWidth / 4,
game.characterY -
game.viewPosY -
game.characterHeight -
this.showScoreY,
100,
100