-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebbill.js
763 lines (668 loc) · 28.6 KB
/
webbill.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
/*
WebBill
Copyright (C) 2025 Aldemir Akpinar <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import baseScene from './basescene.js';
import wmaker from './wmaker.js';
export default class webBill extends baseScene
{
// this.curLevel
constructor()
{
super({ key:'webBill'});
this.cables = [];
this.curLevel = 0;
this.maxComputers = 20;
this.computers = ["maccpu","nextcpu","sgicpu","suncpu","palmcpu","os2cpu","bsdcpu"];
this.os = ["wingdows", "apple", "next", "sgi", "sun", "palm", "os2", "bsd", "redhat", "hurd", "linux"];
this.MINPC = 6;
this.OSOFFSETX = -9;
this.OSOFFSETY = -7;
this.MAXBILLS = 100;
this.activeComp = '';
this.activeCompNum = 0;
this.deactiveCompNum = 0;
this.curHorde = '';
this.curBill = [];
this.levelCompArr = [];
this.score = 0;
this.timeString = ':';
this.timeText = '';
this.volImg = '';
// this.offScreen = 0;
this.billTimer = '';
this.efficiency = 1;
this.iteration = 1;
this.aliveBills = 0;
this.scoreText = '';
this.offScreenBillList = [];
}
init(props)
{
const { level = 1 } = props;
this.curLevel = level;
}
preload()
{
super.preload();
// Load sounds
this.load.audio("winding", "sounds/win31.mp3");
// Load start menu related items
this.load.image("xlogo", "images/X11.svg");
this.load.image("shutdown", "images/shutdown.png");
// Load CPU images
this.load.image("maccpu", "images/maccpu.png");
this.load.image("nextcpu", "images/nextcpu.png");
this.load.image("sgicpu", "images/sgicpu.png");
this.load.image("suncpu", "images/suncpu.png");
this.load.image("palmcpu", "images/palmcpu.png");
this.load.image("os2cpu", "images/os2cpu.png");
this.load.image("bsdcpu", "images/bsdcpu.png");
// Load toaster
this.load.image("toaster", "images/toaster.png");
// Load bucket
this.load.image("bucket", "images/bucket.png");
// Load OS Sprites
this.load.image("wingdows", "images/wingdows.png");
this.load.image("apple", "images/apple.png");
this.load.image("next", "images/next.png");
this.load.image("sgi", "images/sgi.png");
this.load.image("sun", "images/sun.png");
this.load.image("palm", "images/palm.png");
this.load.image("bsd", "images/bsd.png");
this.load.image("linux", "images/linux.png");
this.load.image("redhat", "images/redhat.png");
this.load.image("hurd", "images/hurd.png");
this.load.image("os2", "images/os2.png");
// Can't do sprite sheets, this will do.
this.load.image('billD1', 'images/billD_0.png');
this.load.image('billD2', 'images/billD_1.png');
this.load.image('billD3', 'images/billD_2.png');
this.load.image('billD4', 'images/billD_3.png');
this.load.image('billD5', 'images/billD_4.png');
this.load.spritesheet("spark","images/sparksprite.png", {frameWidth: 20, frameHeight: 20});
}
create()
{
super.create();
var div = document.getElementById('gameContainer');
div.style.backgroundColor = "#FFFFFF"; // Window maker Default Style
// Scene specific animations
this.anims.create({
key: 'billDAnim',
frames: [
{ key: 'billD1', frame: 0},
{ key: 'billD2', frame: 1},
{ key: 'billD3', frame: 2},
{ key: 'billD4', frame: 3},
{ key: 'billD5', frame: 4}
],
frameRate: 6,
repeat: 0
});
this.anims.create({
key: 'flame',
frames: this.anims.generateFrameNumbers('spark',{frames: [0, 1]}),
frameRate: 3,
repeat: -1
});
// Bucket setup
var bucket = this.physics.add.sprite(20, 20, "bucket").setInteractive();
this.input.setDraggable(bucket);
this.input.on('drag', function (pointer, gameObject, dragX, dragY) {
gameObject.x = dragX;
gameObject.y = dragY;
});
/*
this.input.on('dragend', function (pointer, gameObject) {
gameObject.clearTint();
});
*/
// Create Computers and Networks
// Computers to draw, formula from xbill-2.1
let curCompNum = Math.min(8 + this.curLevel, this.maxComputers);
this.activeCompNum = curCompNum;
this.deactiveCompNum = 0;
let virtCanvasX = 0;
let virtCanvasY = 0;
let cpuIndex = 0;
// Cables to draw, formula from xbill-2.1
let curCableNum = Math.min(this.curLevel, curCompNum/2);
// TODO make sure computers don't overlap
for(var i = 0; i<curCompNum;i++) {
cpuIndex = Math.floor(Math.random() * this.computers.length);
virtCanvasX = Phaser.Math.Between(this.game.config.width * 0.1, this.game.config.width * 0.9);
virtCanvasY = Phaser.Math.Between(this.game.config.height * 0.1, this.game.config.height * 0.9);
var tmpComp = this.add.image(0, 0, this.computers[cpuIndex]);
var tmpOS = '';
this.levelCompArr[i] = this.add.container(virtCanvasX,virtCanvasY, [ tmpComp ]);
switch (this.computers[cpuIndex]) {
case "maccpu":
tmpOS = this.add.sprite(this.OSOFFSETX, this.OSOFFSETY, "apple")
this.levelCompArr[i].add(tmpOS);
break;
case "bsdcpu":
tmpOS = this.add.sprite(this.OSOFFSETX, this.OSOFFSETY, "bsd");
this.levelCompArr[i].add(tmpOS);
break;
case "nextcpu":
tmpOS = this.add.sprite(this.OSOFFSETX, this.OSOFFSETY, "next");
this.levelCompArr[i].add(tmpOS);
break;
case "palmcpu":
tmpOS = this.add.sprite(this.OSOFFSETX, this.OSOFFSETY, "palm");
this.levelCompArr[i].add(tmpOS);
break;
case "sgicpu":
tmpOS = this.add.sprite(this.OSOFFSETX, this.OSOFFSETY, "sgi");
this.levelCompArr[i].add(tmpOS);
break;
case "suncpu":
tmpOS = this.add.sprite(this.OSOFFSETX, this.OSOFFSETY, "sun");
this.levelCompArr[i].add(tmpOS);
break;
case "os2cpu":
var pcos = Phaser.Math.Between(6, this.os.length)
tmpOS = this.add.sprite(this.OSOFFSETX, this.OSOFFSETY, this.os[pcos]);
this.levelCompArr[i].add(tmpOS);
break;
}
this.levelCompArr[i].setSize(55,45).setDepth(20);
this.physics.world.enable(this.levelCompArr[i]);
}
for (var i = 0; i<curCableNum ; i++) {
let tComp1ID = Phaser.Math.Between(0, curCompNum-1);
let tComp2ID = tComp1ID;
while (tComp1ID == tComp2ID ) {
tComp2ID = Phaser.Math.Between(0, curCompNum-1);
}
this.levelCompArr[tComp1ID].setDepth(10);
this.levelCompArr[tComp2ID].setDepth(10);
// let myLine = new Phaser.Geom.Line(this.levelCompArr[tComp1ID].x,
let myLine = new Phaser.Curves.Line([this.levelCompArr[tComp1ID].x,
this.levelCompArr[tComp1ID].y,
this.levelCompArr[tComp2ID].x,
this.levelCompArr[tComp2ID].y]);
let tempPath = this.add.path();
tempPath.add(myLine);
var netGraphics = this.add.graphics({ lineStyle: { width: 3, color: 0xaa00aa } });
tempPath.draw(netGraphics);
}
// Setup Bill horde
// curMaxBills = Math.min((8 + 3 * curLevel) * Game_scale(2));
// Original game tries to scale number of bills according to the game board dimensions
let curMaxBills = Math.min((8 + 3 * this.curLevel) , this.MAXBILLS);
for(let i = 0 ; i < curMaxBills; i++) {
// Maintain the index of off screen Bills
this.offScreenBillList.push(i);
// 0 add bill just behind the x axis
// 1 add bill just behind the y axis
let xory = Phaser.Math.Between(0,1);
let randComp = Phaser.Math.Between(0, this.computers.length);
if (xory == 0) {
var billStartPointY = Phaser.Math.Between(this.game.config.height * 0.1, this.game.config.height * 0.9);
var billStartPointX = -30;
var billImage = 'billR0';
var billAnim = 'billRAnim';
} else {
var billStartPoint = Phaser.Math.Between(this.game.config.width * 0.1, this.game.config.width * 0.9);
if ( billStartPoint >= this.game.config.width / 2 ) {
var billImage = 'billL0';
var billAnim = 'billLAnim';
} else {
var billImage = 'billR0';
var billAnim = 'billRAnim';
}
var billStartPointX = Phaser.Math.Between(this.game.config.height * 0.1, this.game.config.height * 0.9);
var billStartPointY = -30;
}
var tmpWin = this.add.image(0, -20, 'wingdows');
let tmpBill = this.add.sprite(0, 0, billImage).play(billAnim);
// curBill[i] = this.add.container(billStartPointX, billStartPointY, [tmpBill, tmpWin]);
this.curBill[i] = this.add.container(billStartPointX, billStartPointY);
this.curBill[i].addAt(tmpWin, 0);
this.curBill[i].addAt(tmpBill, 1);
this.curBill[i].setDepth(23)
.setSize(28, 42)
.setInteractive()
.setData("ingame", "false")
.setData("dead", "false");
this.physics.world.enable(this.curBill[i]);
// Kill Bill
this.curBill[i].on('pointerdown', function() {
let deadBillContainer = this.curBill[i];
deadBillContainer.disableInteractive();
var deadBill = deadBillContainer.getAt(1);
if (deadBill == undefined ) {
return;
}
deadBillContainer.body.stop();
// tmpBill.removeInteractive();
// Famous last words: we should only have two object
// in the container, Bill & payload
deadBill.play('billDAnim');
deadBill.once('animationcomplete', ()=>{
deadBill.setActive(false).setVisible(false);
if (deadBillContainer.getAt(0).texture.key == "wingdows") {
deadBillContainer.getAt(0).setActive(false).setVisible(false);
deadBillContainer.removeInteractive();
} else {
// TODO this animate
// I hate js
deadBillContainer.getAt(0).setX(deadBill.x).setY(deadBill.y);
deadBillContainer.setInteractive();
this.input.setDraggable(deadBillContainer);
}
});
this.aliveBills--;
}.bind(this));
} // End for (bill setup)
// this.offScreen = curMaxBills;
this.aliveBills = curMaxBills;
this.billTimer = this.time.addEvent({
delay: 200, // ms
callback: this.timerCallback,
args: [],
callbackScope: this,
loop: true
});
const menuStyle = {fontFamily: "Menlo Regular", fontSize: 12, fill: "#000"};
let bottomBarGraph = this.add.graphics();
let alpha = 0.5 + ((0 / 10) * 0.5);
let bottomBar = this.add.container(0, this.game.config.height);
bottomBarGraph.fillStyle(0x787878, alpha);
// let outerRect = graphics.fillRect(0, this.game.config.height - 40, this.game.config.width, 40);
let outerRect = bottomBarGraph.fillRect(0, -40, this.game.config.width, 40);
bottomBar.add(outerRect);
// Scoreboard
this.scoreText = this.add.text(60,-20, 'Bill:%d/%d System:%d/%d/%d Level:' + this.curLevel.toString() + ' Score:' + this.score, menuStyle);
bottomBar.add(this.scoreText);
let menuText = this.add.image(25, -20, "xlogo").setDisplaySize(25,20).setDepth(1);
bottomBar.add(menuText);
bottomBarGraph.lineStyle(2, 0x000000, 1);
let menuOuterRect = bottomBarGraph.strokeRect(2, -35, 50, 30).setDepth(2);
bottomBar.add(menuOuterRect);
bottomBarGraph.lineStyle(2, 0xffffff, 0.5);
let menuOuterRectShadow = bottomBarGraph.strokeRect(2, -33, 48, 28).setDepth(3);
bottomBar.add(menuOuterRectShadow);
this.timeText = this.add.text(this.game.config.width - 50, -25, this.timeString, menuStyle).setDepth(2);
bottomBar.add(this.timeText);
this.updateTime();
let clockTimer = this.time.addEvent({
delay: 60000,
callback: this.updateTime,
callbackScope: this,
loop: true
});
let myIcon = '';
if (this.sound.mute) {
myIcon = "volume-mute";
} else {
myIcon = "volume-unmute";
}
this.volImg = this.add.image(this.game.config.width-70, -20, myIcon)
.setDisplaySize(24,24)
.setInteractive()
.setDepth(2)
.on('pointerdown', this.muteSound, this);
bottomBar.add(this.volImg);
let menuGraph = this.add.graphics();
menuGraph.fillStyle(0x787878, alpha);
// startContainer = this.add.container(5,this.game.config.height-100);
let startContainer = this.add.container(2,this.game.config.height-202).setVisible(false);
let startMenu = menuGraph.fillRect(0, -40, 100, 200);
startContainer.add(startMenu);
startContainer.add(this.add.text(30, 138, "Shutdown", menuStyle)
.setInteractive()
.on('pointerdown', () => {this.scene.start('gameMenu')}));
startContainer.add(this.add.image(15,145, "shutdown")
.setInteractive()
.on('pointerdown', () => {this.scene.start('gameMenu')})
.setDisplaySize(24,24));
menuText.setInteractive()
.on('pointerdown', () => {startContainer.setVisible(!startContainer.visible); this.scene.pause(startContainer.visible);}, this);
this.monitorInfectedComputers();
}
timerCallback() {
if (this.offScreenBillList.length > 0) {
let launched = this.billLaunch();
console.log("Launched:" + launched);
}
}
// NEW: Restore computer when OS is dragged back
restoreComputer(os, computer) {
if (os.getData('dragging')) return; // Ignore while dragging
// Check if this OS matches the computer's original OS and it's infected
if (this.computerOsMap.get(computer) === os.texture.key && computer.getData('infected')) {
// Find and destroy the Windows icon on this computer
this.windows.getChildren().forEach(win => {
if (Phaser.Math.Distance.Between(win.x, win.y, computer.x, computer.y) < 10) {
win.destroy();
}
});
// Snap OS to computer and disable dragging
os.setPosition(computer.x, computer.y);
os.disableInteractive();
os.setDepth(1);
// Reset computer state
computer.setData('infected', false);
}
}
update()
{
// super.update();
if (this.offScreenBillList.length > 0) {
this.score += (this.curLevel * this.efficiency / this.iteration);
} else {
this.billTimer.remove();
}
// console.log("Alive bills:" + this.aliveBills + " Offscreen: " + this.offScreenBillList.length);
this.scoreText.text = 'Bill:'+ this.aliveBills +'/%d System:%d/'+this.deactiveCompNum+'/'+this.activeCompNum+' Level:' + this.curLevel.toString() + ' Score:' + this.score.toString();
if (this.aliveBills <= 0 && this.deactiveCompNum == 0) {
//console.log('Level done!');
this.showLevelDone();
this.pauseGame();
}
}
billLaunch()
{
// Original xbill 2.1 formula
var minBill = Math.min(2 + this.curLevel / 4, 12);
var n = Phaser.Math.Between(1, Math.min(minBill, this.offScreenBillList.length));
console.log("Should release" + n + "Bills");
let retVal = n;
for (;n>0;n--) {
let myBillIndex = Phaser.Utils.Array.GetRandom(this.offScreenBillList);
const tmpIndex = this.offScreenBillList.indexOf(myBillIndex);
if (tmpIndex > -1) {
this.offScreenBillList.splice(tmpIndex, 1);
} else {
continue;
}
let myBill = this.curBill[myBillIndex];
myBill.setData("ingame", "true");
var myCPU = Phaser.Utils.Array.GetRandom(this.levelCompArr);
this.physics.moveToObject(myBill, myCPU, 20);
this.physics.add.overlap(myBill, myCPU, this.replaceOS, null, this);
}
return retVal;
}
replaceOS(myBill, myCPU)
{
var touching = !myCPU.body.touching.none;
var wasTouching = !myCPU.body.wasTouching.none;
// Do not fire collision second time!
if (touching && !wasTouching) {
console.log("warning bill is replacing the OS!")
myBill.body.stop();
var wingdows = myBill.getAt(0);
var goodOS = myCPU.getAt(1);
if (goodOS == undefined || goodOS.texture.key == "wingdows") {
// Choose a random cpu and work on it.
// XXX turn this code into a function
var myCPU = Phaser.Utils.Array.GetRandom(this.levelCompArr);
this.physics.moveToObject(myBill, myCPU, 20);
this.physics.add.overlap(myBill, myCPU, this.replaceOS, null, this);
return;
//this.physics.moveTo(myBill, 100, 100);
}
myBill.removeAt(0);
myCPU.removeAt(1);
myBill.addAt(goodOS, 0);
myCPU.addAt(wingdows, 1);
goodOS.setPosition(0, -20);
wingdows.setPosition(this.OSOFFSETX, this.OSOFFSETY);
this.sound.play('winding');
let xory = Phaser.Math.Between(0,1);
let x = 0;
let y = 0;
if (xory == 0 ) {
x = Phaser.Math.Between(this.game.config.width * 0.1, this.game.config.width * 0.9);
y = -30;
} else {
x = -30;
y = Phaser.Math.Between(this.game.config.height * 0.1, this.game.config.height * 0.9);
}
this.physics.moveTo(myBill, x, y, 20);
//this.physics.remove.overlap(myBill, myCPU);
}
}
updateTime() {
let time = new Date();
let hours = time.getHours();
let minutes = time.getMinutes();
if (hours < 10) {
hours = "0" + hours;
}
if (minutes < 10) {
minutes = "0" + minutes;
}
this.timeString = hours + ":" + minutes;
this.timeText.text = this.timeString;
}
muteSound()
{
let myIcon = '';
this.game.sound.mute = !this.game.sound.mute;
if (this.game.sound.mute) {
myIcon = "volume-mute";
} else {
myIcon = "volume-unmute";
}
this.volImg.setTexture(myIcon);
}
showLevelDone()
{
let mainWindow = new wmaker(this);
// Get canvas dimensions from the game config
const canvasWidth = this.sys.game.config.width;
const canvasHeight = this.sys.game.config.height;
const windowWidth = 430;
const windowHeight = 375;
// Calculate centered coordinates
const centerX = (canvasWidth / 4) - (windowWidth / 2);
const centerY = (canvasHeight / 4) - (windowHeight / 2);
let levelDoneWindow = mainWindow.createXWindow(this.game.renderer.width/2, this.game.renderer.height/2, windowWidth, windowHeight, 'Level Done!', false, 100);
let textStyle = { fontFamily: "Menlo Regular", fill: "#000", align: "left", fontSize: "14px", backgroundColor: '#fff' };
let donetext = this.add.text(10, 30, 'Level '+ this.curLevel.toString() +' done!', textStyle)
.setOrigin(0)
.setDepth(1);
levelDoneWindow.add(donetext);
donetext = this.add.text(50, 50, 'Next Level', textStyle)
.setInteractive()
.setOrigin(0)
.setDepth(1);
donetext.on('pointerdown', () => {
this.resumeGame();
this.scene.restart({level: this.curLevel+1})
});
levelDoneWindow.add(donetext);
}
pauseGame()
{
this.anims.pauseAll();
this.physics.pause();
//this.tweens.pauseAll();
}
resumeGame()
{
this.anims.resumeAll();
this.physics.resume();
}
createSparkOld()
{
let spark = this.add.sprite(x, y, 'spark')
.play('flame') // Play the flame animation
.setDepth(25); // Ensure it's above other objects
this.time.delayedCall(500, () => {
spark.destroy(); // Remove the spark after 500ms
});
// #define SPARK_SPEED 4
// #define SPARK_DELAY(level) (MAX(20 - (level), 0))
/*
* Remove timer (SPARK_DELAY)
* Set collision with the destionation
* Run animation
* Move it towards the cable
* If not put out kill dest os
*/
// wingdows.play('flame');
}
hordeSetup(t, curLevel)
{
}
createSpark(infectedComputer) {
// Constants for spark behavior
const SPARK_SPEED = 100;
const SPARK_DAMAGE_DELAY = 3000; // 3 seconds before spark starts
// Find connected computers through cables
const connectedComputers = this.findConnectedComputers(infectedComputer);
if (connectedComputers.length === 0) {
return; // No connected computers to spread to
}
// Create the spark sprite
const spark = this.add.sprite(
infectedComputer.x,
infectedComputer.y,
'spark'
).play('flame');
spark.setDepth(30); // Make sure spark appears above cables
this.physics.world.enable(spark);
// For each connected computer, create a spark that travels along the cable
connectedComputers.forEach(targetComputer => {
// Create a timer for the 3 second delay
this.time.addEvent({
delay: SPARK_DAMAGE_DELAY,
callback: () => {
// Calculate the path along the cable
const path = new Phaser.Curves.Line([
infectedComputer.x,
infectedComputer.y,
targetComputer.x,
targetComputer.y
]);
// Clone the spark for this path
const pathSpark = this.add.sprite(
infectedComputer.x,
infectedComputer.y,
'spark'
).play('flame');
pathSpark.setDepth(30);
this.physics.world.enable(pathSpark);
// Calculate the distance and time needed for travel
const distance = Phaser.Math.Distance.Between(
infectedComputer.x,
infectedComputer.y,
targetComputer.x,
targetComputer.y
);
const travelTime = (distance / SPARK_SPEED) * 1000; // Convert to milliseconds
// Create the tween to move the spark along the cable
this.tweens.add({
targets: pathSpark,
x: targetComputer.x,
y: targetComputer.y,
duration: travelTime,
ease: 'Linear',
onComplete: () => {
// When spark reaches target, infect the computer if not protected
this.infectComputer(targetComputer, pathSpark);
}
});
},
callbackScope: this
});
});
}
// Helper method to find computers connected by cables
findConnectedComputers(sourceComputer) {
const connectedComputers = [];
// Get all computers that have cables connecting to the source computer
this.levelCompArr.forEach(computer => {
if (computer !== sourceComputer) {
// Check if there's a cable between these computers
const cable = this.findCable(sourceComputer, computer);
if (cable) {
connectedComputers.push(computer);
}
}
});
return connectedComputers;
}
// Helper method to find a cable between two computers
findCable(comp1, comp2) {
// This would need to be adapted based on how you're storing cable information
// For now, we'll assume cables are stored in a cables array
return this.cables.find(cable =>
(cable.start === comp1 && cable.end === comp2) ||
(cable.start === comp2 && cable.end === comp1)
);
}
// Helper method to infect a computer
infectComputer(computer, spark) {
// Check if the computer isn't already infected and doesn't have protection
const os = computer.getAt(1); // Get the OS sprite from the container
if (os && os.texture.key !== "wingdows") {
// Store the original OS
const originalOS = os.texture.key;
// Replace with Wingdows
computer.removeAt(1);
const wingdows = this.add.sprite(this.OSOFFSETX, this.OSOFFSETY, "wingdows");
computer.addAt(wingdows, 1);
computer.setData('infected', true);
// Play infection sound
this.sound.play('winding');
// Create a visual effect for the infection
const infectEffect = this.add.sprite(computer.x, computer.y, 'spark')
.play('flame')
.setDepth(35);
// Remove the effect after animation
this.time.addEvent({
delay: 1000,
callback: () => {
infectEffect.destroy();
},
callbackScope: this
});
}
// Destroy the spark sprite
spark.destroy();
}
monitorInfectedComputers() {
// Check every second for computers that have been infected
this.time.addEvent({
delay: 1000,
callback: () => {
this.levelCompArr.forEach(computer => {
const os = computer.getAt(1);
if (os && os.texture.key === "wingdows") {
// If computer doesn't have a spark timer, start one
if (!computer.getData('sparkTimer')) {
computer.setData('sparkTimer', this.time.now);
} else if (this.time.now - computer.getData('sparkTimer') >= 3000) {
// If 3 seconds have passed, create spark
this.createSpark(computer);
// Reset timer
computer.setData('sparkTimer', null);
}
} else {
// Reset timer if computer is no longer infected
computer.setData('sparkTimer', null);
}
});
},
callbackScope: this,
loop: true
});
}
}