-
Notifications
You must be signed in to change notification settings - Fork 0
/
bundle.js
1649 lines (1356 loc) · 41.8 KB
/
bundle.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
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
/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId])
/******/ return installedModules[moduleId].exports;
/******/
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ exports: {},
/******/ id: moduleId,
/******/ loaded: false
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.loaded = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/******************************* Inicio de botones y sus comportaamientos *********************************************************************************************************************/
/***/ function(module, exports, __webpack_require__) {
const GameView = __webpack_require__(1);
document.addEventListener('DOMContentLoaded', () => {
const canvas = document.getElementById('game-canvas');
canvas.height = 600;
canvas.width = 900;
const canvasSize = [canvas.width, canvas.height];
const ctx = canvas.getContext('2d');
const gameView = new GameView(ctx, canvasSize);
gameView.welcome(); // pantalla de bienvenida
// Botones del juego
const mainLogo = document.getElementById('main-logo');
const playGameButton = document.getElementById('play-game');
const gameOverImage = document.getElementById('game-over');
const menuButton = document.getElementById('menu-button');
const menuContainer = document.getElementById('menu-container');
const aboutButton = document.getElementById('about-button');
const about = document.getElementById('about');
const instructionsButton = document.getElementById('instructions-button');
const instructions = document.getElementById('instructions');
const resumeButton = document.getElementById('resume-button');
const restartButton = document.getElementById('restart-button');
const closeAbout = document.getElementById('close-about');
const closeInstructions = document.getElementById('close-instructions');
const grunt = document.getElementById('grunt-1');
const soldier = document.getElementById('soldier-1');
const invader = document.getElementById('invader-1');
const ufo = document.getElementById('ufo');
const invaderInfo = document.getElementById('invader-info');
const audio = document.getElementById('audio');
const mute = document.getElementById('mute');
const splashInstruction = document.getElementById('splash-instruction');
audio.addEventListener('click', () => { // funcion para el audio
if (audio.className === 'hide') {
audio.className = 'show';
mute.className = 'hide';
} else {
audio.className = 'hide';
mute.className = 'show';
}
gameView.toggleAudio();
});
mute.addEventListener('click', () => { // funcion para el muteo del juego
if (audio.className === 'hide') {
audio.className = 'show';
mute.className = 'hide';
} else {
audio.className = 'hide';
mute.className = 'show';
}
gameView.toggleAudio();
});
playGameButton.addEventListener("click", () => { // funcion para el boton de play y lo que pasa cuando se lo presiona
menuButton.className = '';
playGameButton.className = 'hide';
mainLogo.className = 'hide';
gameOverImage.className = 'hide';
grunt.className = 'hide';
soldier.className = 'hide';
invader.className = 'hide';
ufo.className = 'hide';
invaderInfo.className = 'hide';
splashInstruction.className = 'hide';
gameView.start();
});
menuButton.addEventListener("click", () => { // funcion para el boton de menu y lo que pasa cuando se lo presiona
gameView.pause();
menuContainer.className = 'show';
aboutButton.className = '';
instructionsButton.className = '';
resumeButton.className = '';
restartButton.className = '';
});
closeAbout.addEventListener('click', () => { // funcion para el boton de Cerrar en la sección del descripción del juego y lo que pasa cuando se lo presiona
gameView.pause();
about.className = 'hide';
instructions.className = 'hide';
closeAbout.className = 'hide';
menuContainer.className = '';
aboutButton.className = '';
instructionsButton.className = '';
resumeButton.className = '';
restartButton.className = '';
});
aboutButton.addEventListener('click', () => { // funcion para el boton de About (acerca de) y lo que pasa cuando se lo presiona
gameView.pause();
menuContainer.className = 'hide';
aboutButton.className = 'hide';
instructionsButton.className = 'hide';
resumeButton.className = 'hide';
restartButton.className = 'hide';
about.className = 'show';
closeAbout.className = 'show';
});
closeInstructions.addEventListener('click', () => { // funcion para el boton de Cerrar en la sección de instrucciones y lo que pasa cuando se lo presiona
gameView.pause();
about.className = 'hide';
instructions.className = 'hide';
closeAbout.className = 'hide';
menuContainer.className = 'show';
aboutButton.className = '';
instructionsButton.className = '';
resumeButton.className = '';
restartButton.className = '';
});
instructionsButton.addEventListener('click', () => { // funcion para el boton de Instrucciones y lo que pasa cuando se lo presiona
gameView.pause();
menuContainer.className = 'hide';
aboutButton.className = 'hide';
instructionsButton.className = 'hide';
resumeButton.className = 'hide';
restartButton.className = 'hide';
instructions.className = '';
closeInstructions.className = '';
});
resumeButton.addEventListener('click', () => {
menuContainer.className = 'hide';
aboutButton.className = 'hide';
instructionsButton.className = 'hide';
resumeButton.className = 'hide';
restartButton.className = 'hide';
gameView.resume();
});
restartButton.addEventListener('click', () => {
menuContainer.className = 'hide';
aboutButton.className = 'hide';
instructionsButton.className = 'hide';
resumeButton.className = 'hide';
restartButton.className = 'hide';
gameView.restart();
});
});
/************************************************************FIn de botones y sus comportamientos *********************************************************************************************************************/
/***/ },
/* 1 */
/***/ function(module, exports, __webpack_require__) { //
const Game = __webpack_require__(2);
const GameView = function(ctx, canvasSize) {
this.ctx = ctx;
this.canvasSize = canvasSize;
this.game = new Game({
canvasSize: this.canvasSize,
ctx: this.ctx,
gameView: this
});
this.defender = this.game.defender;
this.isPaused = false;
this.rightPressed = false;
this.leftPressed = false;
this.spacePressed = false;
this.isMuted = false;
this.addKeyListeners();
};
GameView.prototype.toggleAudio = function() {
this.isMuted = this.isMuted ? false : true;
};
GameView.prototype.start = function() {
this.interval = setInterval(() => {
if (!this.isPaused) {
this.game.draw(this.ctx);
this.addLivesText(this.ctx);
this.addScoreText(this.ctx);
this.addLevelText(this.ctx);
this.moveDefender();
this.game.moveInvaders();
this.game.addUfo();
this.game.step();
}
}, 10);
// Animate enemy sprites
this.toggle = setInterval(() => {
if (!this.isPaused) this.game.toggleInvaders();
}, 500);
};
GameView.prototype.stop = function() {
clearInterval(this.interval);
clearInterval(this.toggle);
this.interval = null;
this.toggle = null;
this.rightPressed = false;
this.leftPressed = false;
this.spacePressed = false;
this.isPaused = false;
this.defender = this.game.defender;
this.game = new Game({
canvasSize: this.canvasSize,
gameView: this,
ctx: this.ctx
});
};
GameView.prototype.restart = function() {
this.stop();
this.start();
};
GameView.prototype.welcome = function() {
this.ctx.fillStyle = '#000';
this.ctx.fillRect(0, 0, this.game.DIM_X, this.game.DIM_Y);
};
GameView.prototype.pause = function() {
this.isPaused = true;
};
GameView.prototype.resume = function() {
this.isPaused = false;
};
GameView.prototype.gameOver = function() {
this.stop();
document.getElementById('menu-container').className='hide';
setTimeout(() => {
this.ctx.clearRect(0, 0, this.DIM_X, this.DIM_Y);
this.ctx.fillStyle = '#000';
this.ctx.fillRect(0, 0, this.game.DIM_X, this.game.DIM_Y);
let gameOverImage = document.getElementById('game-over'),
playGameButton = document.getElementById('play-game');
playGameButton.className = '';
gameOverImage.className = '';
}, 600);
};
GameView.KEY_BINDS = {
'left': [-2, 0],
'right': [2, 0]
};
GameView.prototype.addLivesText = function(ctx) {
let x = this.game.DIM_X * .87, y = this.game.DIM_Y * .05;
ctx.font = "23px Bungee Inline";
ctx.fillText(`VIDAS: ${this.game.defenderLives}`, x, y);
};
GameView.prototype.addMenu = function(ctx) {
let x = this.game.DIM_X * .5, y = this.game.DIM_Y * .1;
};
GameView.prototype.addScoreText = function(ctx) {
let x = this.game.DIM_X * .01, y = this.game.DIM_Y * .05;
// ctx.find = "20px Georgia";
ctx.fillText(`PUNTOS: ${this.game.score}`, x, y);
};
GameView.prototype.addLevelText = function(ctx) {
let x = this.game.DIM_X * .01, y = this.game.DIM_Y * .95;
ctx.fillText(`NIVEL: ${this.game.level}`, x, y);
}
GameView.prototype.bindKeyHandlers = function() {
const defender = this.defender;
Object.keys(GameView.KEY_BINDS).forEach(k => {
let offset = GameView.KEY_BINDS[k];
key(k, function() { defender.power(offset); });
});
key('space', function() { defender.fireBullet(); });
};
GameView.prototype.addKeyListeners = function() {
document.addEventListener('keydown', this.handleKeyDown.bind(this), false);
document.addEventListener('keyup', this.handleKeyUp.bind(this), false);
};
GameView.prototype.handleKeyDown = function(e) {
if (e.keyCode === 37) {
this.leftPressed = true;
} else if (e.keyCode === 39) {
this.rightPressed = true;
}
if (e.keyCode === 32) {
this.spacePressed = true;
}
};
GameView.prototype.handleKeyUp = function(e) {
if (e.keyCode === 37) {
this.leftPressed = false;
} else if (e.keyCode === 39) {
this.rightPressed = false;
}
if (e.keyCode === 32) {
this.spacePressed = false;
}
};
GameView.prototype.moveDefender = function() {
/*
if (this.leftPressed) {
this.defender.power([-9,0]);
} else if (this.rightPressed) {
this.defender.power([6,0]);
}
*/
// if (this.spacePressed) {
// this.defender.fireBullet();
// }
};
module.exports = GameView;
/***/ },
/* 2 */
/***/ function(module, exports, __webpack_require__) {
const Ship = __webpack_require__(3);
const Bullet = __webpack_require__(6);
const Shield = __webpack_require__(10);
const ShieldPiece = __webpack_require__(7);
const Star = __webpack_require__(11);
const PowerUp = __webpack_require__(9);
const Util = __webpack_require__(5);
const Game = function(options) {
this.canvasSize = options.canvasSize;
this.ctx = options.ctx;
this.stars = [];
this.defender = null;
this.defenderLives = 3;
this.score = 0;
this.level = 1;
this.invaderShips = [];
this.ufo = null;
this.bullets = [];
this.bulletId = 0;
this.shields = [];
this.shieldPieces = [];
this.powerUps = [];
this.gameView = options.gameView;
this.DIM_X = this.canvasSize[0];
this.DIM_Y = this.canvasSize[1];
this.addStars();
this.addDefenderShip();
this.addInvaderShips();
this.addShields();
this.gameView.addKeyListeners();
};
Game.BG_COLOR = "#000000";
Game.NUM_STARS = 50;
Game.prototype.randomPosition = function() {
return [
this.DIM_X * Math.random(),
this.DIM_Y * Math.random()
];
};
Game.prototype.addStars = function() { // agrega las estrellas al juego
for (let i = 0; i < Game.NUM_STARS; i++) {
this.stars.push(new Star({
id: i,
color: "#ffffff",
pos: this.randomPosition(),
vel: Util.randomVec(.1),
game: this
}));
}
};
Game.prototype.addUfo = function(ctx) { // EXTRA 1: agrega el ufo al juego
if (this.ufo) { return; }
let spawnUfoChance = Math.random() * 700;
let spawnPosition = Math.round(Math.random() * 10);
let vel, spawnIdx;
if (spawnPosition > 5) {
spawnIdx = 0;
vel = [2, 0];
} else {
spawnIdx = 1;
vel = [-2, 0];
}
let spawnPositions = [-30, 930];
if (spawnUfoChance < 1) {
let ufoImage = document.getElementById('ufo');
let ufoShip = new Ship ({
name: 'ufo',
game: this,
canvasSize: this.canvasSize,
img: ufoImage,
radius: 27,
pos: [spawnPositions[spawnIdx], 45],
vel: vel,
side: 'invader'
});
this.ufo = ufoShip;
}
};
Game.prototype.addInvaderShips = function(level = 1) { // agrega las naves enemigas al juego
let invaderShipName, invaderShipImage;
let y = 100;
let invaderIdx = 0;
let vel = [0.27, 0];
vel[0] += 0.05 * level;
for (let row = 0; row < 5; row++) {
if (row < 1) {
invaderShipName = 'invader';
invaderShipImage = document.getElementById('invader-1');
} else if (row < 3) {
invaderShipName = 'soldier';
invaderShipImage = document.getElementById('soldier-1');
} else if (row > 2) {
invaderShipName = 'grunt';
invaderShipImage = document.getElementById('grunt-1');
}
for (let x = 1; x < 14; x++, invaderIdx++) {
let invaderShip = new Ship ({
id: invaderIdx,
name: invaderShipName,
game: this,
canvasSize: this.canvasSize,
img: invaderShipImage,
radius: 12,
pos: [
x * 35,
y
],
vel: vel,
side: 'invader'
});
this.invaderShips.push(invaderShip);
}
y += 40;
}
};
Game.prototype.addShields = function() { // agrega los escudos al juego
for (let i = 0, x = .05; i < 5; i++, x += 0.2) {
let shieldPosX = this.canvasSize[0] * x + 14;
let shieldPosY = this.canvasSize[1] * .8;
let shield = new Shield ({
id: i,
pos: [shieldPosX, shieldPosY],
radius: 7,
color: "#a0a09b",
game: this
});
shield.draw(this.ctx);
}
};
Game.prototype.refreshShields = function() { // refresca los escudos al juego
this.shieldPieces = [];
this.shields = [];
this.addShields();
};
Game.prototype.addDefenderShip = function() { // agrega la nave del jugador al juego
const defender = new Ship ({
name: 'defender',
game: this,
canvasSize: this.canvasSize,
img: document.getElementById('defender'),
radius: 16,
pos: [
(this.canvasSize[0] - 30) * .52,
this.canvasSize[1] - 70
],
vel: [0, 0],
side: 'defender'
});
this.defender = defender;
};
Game.prototype.getAllObjects = function() { // obtiene todos los objetos del juego
return [].concat(
this.ufo,
this.shieldPieces,
this.bullets,
this.stars,
this.powerUps
);
};
Game.prototype.moveObjects = function() { // mueve los objetos del juego
this.getAllObjects().forEach(object => {
if (object == null) { return; }
object.move();
});
};
Game.prototype.moveInvaders = function() { // mueve las naves enemigas del juego
this.invaderShips.forEach(ship => {
ship.move();
});
};
Game.prototype.toggleInvaders = function() { // cambia la imagen de las naves enemigas del juego
this.invaderShips.forEach(ship => {
ship.toggleImage();
});
};
Game.prototype.reverseAllInvaders = function() { // invierte el movimiento de las naves enemigas del juego
this.invaderShips.forEach(invader => {
invader.reverse();
});
};
Game.prototype.wrap = function(pos) { // envuelve los objetos del juego
let x = pos[0], y = pos[1];
let maxX = this.DIM_X, maxY = this.DIM_Y;
let wrappedX = Util.wrap(x, maxX);
let wrappedY = Util.wrap(y, maxY);
return [wrappedX, wrappedY];
};
Game.prototype.draw = function(ctx) { // dibuja los objetos del juego
ctx.clearRect(0, 0, this.DIM_X, this.DIM_Y);
ctx.fillStyle = Game.BG_COLOR;
ctx.fillRect(0, 0, this.DIM_X, this.DIM_Y);
this.defender.draw(ctx);
this.getAllObjects().forEach(object => {
if (object == null) { return; }
object.draw(ctx);
});
};
Game.prototype.lose = function() { // pierde el juego
this.gameView.pause();
this.gameView.addLivesText(this.ctx);
this.gameView.gameOver();
};
Game.prototype.winRound = function() { // gana la ronda del juego
if (this.invaderShips.length === 0) {
setTimeout(() => {
if (this.invaderShips.length === 0) {
this.refreshShields();
this.level++;
this.defenderLives++;
this.addInvaderShips(this.level);
}
}, 1000);
}
};
Game.prototype.isOutOfBounds = function (pos) { // verifica si los objetos del juego estan fuera de los limites
return (pos[0] < -50) || (pos[1] < 0) ||
(pos[0] > this.DIM_X + 50) || (pos[1] > this.DIM_Y);
};
Game.prototype.collisionObjects = function() { // verifica si los objetos del juego colisionan
return [].concat(
this.bullets,
this.invaderShips,
this.defender,
this.shieldPieces,
this.ufo,
this.powerUps
);
};
// EXTRA 2: Este metodo hace que los enemigos disparen balas
Game.prototype.enemyFire = function() {
let fireChance, invaderCount = this.invaderShips.length;
if (invaderCount < 10) {
fireChance = 500;
} else if (invaderCount < 25) {
fireChance = 1500;
} else if (invaderCount < 40) {
fireChance = 3000;
} else if (invaderCount < 50) {
fireChance = 4000;
} else {
fireChance = 5000;
}
this.invaderShips.forEach(invader => {
let fire = Math.random() * fireChance;
if (fire < 1) {
invader.fireBullet();
invader.currentBullet = false;
}
});
//retorno temprano si el ufo no esta en el juego
if (this.ufo == null) { return; }
let ufoFire = Math.random() * 1000;
if (ufoFire < 15) {
this.ufo.fireBullet();
}
};
// Este metodo hace que las naves enemigas se muevan mas rapido con cada una que muere
Game.prototype.increaseInvadersSpeed = function() {
this.invaderShips.forEach(invader => {
invader.increaseSpeed();
});
};
Game.prototype.checkCollisions = function() { // verifica las colisiones de los objetos del juego
let collisionObjects = this.collisionObjects();
for (var i = 0; i < collisionObjects.length; i++) {
for (var j = 0; j < collisionObjects.length; j++) {
let object1 = collisionObjects[i];
let object2 = collisionObjects[j];
let options = {
ship: Ship,
bullet: Bullet,
shieldPiece: ShieldPiece,
powerUp: PowerUp,
object1: object1,
object2: object2
};
if (Util.validCollision(options)) {
if (object1.isCollidedWith(object2)) {
// collideWith maneja la logica para eliminar objetos fuera del canvas
object1.collideWith(object2);
}
}
}
}
};
Game.prototype.currentDefenderBullet = function() { // verifica si la bala actual es del jugador
this.bullets.forEach(bullet => {
if (bullet.ship.name === 'defender') return true;
});
return false;
};
Game.prototype.remove = function(object) { // remueve los objetos del juego
if (object instanceof Bullet) {
object.removeBullet(object);
} else if (object instanceof Ship) {
if (object.name === 'ufo') {
this.ufo = null;
return;
}
object.removeShip(object);
} else if (object instanceof ShieldPiece) {
this.shieldPieces.splice(this.shieldPieces.indexOf(object), 1);
} else if (object instanceof PowerUp) {
this.powerUps.splice(this.powerUps.indexOf(object), 1);
}
};
Game.prototype.step = function() { // hace que el juego se actualice
this.moveObjects();
this.checkCollisions();
this.enemyFire();
this.winRound();
};
module.exports = Game;
/***/ },
/* 3 */
/***/ function(module, exports, __webpack_require__) {
const MovingObject = __webpack_require__(4);
const Util = __webpack_require__(5);
const Bullet = __webpack_require__(6);
const Note = __webpack_require__(8);
const PowerUp = __webpack_require__(9);
const Ship = function(options = { radius: 13 }) { // constructor de la nave
this.id = options.id;
this.name = options.name;
this.game = options.game;
this.canvasSize = options.canvasSize;
this.img = options.img;
this.pos = options.pos;
this.vel = options.vel;
this.radius = options.radius;
this.side = options.side;
this.currentBullet = false;
this.isDead = false;
this.hasThreeGuns = false;
this.hasFiveGuns = false;
this.speedUp = false;
this.speedUp2 = false;
this.bulletsInPlay = [];
MovingObject.call(this, options);
};
Util.inherits(Ship, MovingObject);
Ship.prototype.removeShip = function(shipToRemove) { // remueve la nave del juego
this.game.invaderShips.forEach(ship => {
if (ship.id === shipToRemove.id) {
this.game.invaderShips = this.game.invaderShips.filter(s => (
s.id !== shipToRemove.id
));
}
});
};
Ship.prototype.draw = function(ctx) { // dibuja la nave
if (this.name === 'ufo') {
let x = this.pos[0] - 26;
let y = this.pos[1] - 3;
ctx.drawImage(this.img, x, y, 53, 30);
return;
}
//sustraer de pos para alinear la imagen con el radio
let x = this.pos[0] - 12;
let y = this.pos[1] - 12;
if (this.name === 'defender') {
x -= 4;
y -= 4;
ctx.drawImage(this.img, x, y, 33, 33);
} else {
ctx.drawImage(this.img, x, y, 25, 25);
}
};
Ship.prototype.respawn = function() { // hace que la nave vuelva a aparecer en el juego
this.deathImage();
this.game.defenderLives = this.game.defenderLives- 3;
this.hasThreeGuns = false;
this.hasFiveGuns = false;
this.speedUp = false;
this.speedUp2 = false;
if (this.game.defenderLives < 0) this.game.lose();
this.game.gameView.pause();
setTimeout(() => {
this.pos = [
(this.canvasSize[0] - 30) * .52,
this.canvasSize[1] - 70
];
this.vel = [0,0];
this.img = document.getElementById('defender');
this.game.gameView.resume();
}, 600);
};
Ship.prototype.death = function() { // hace que la nave muera
let deathSound;
if (this.name === 'defender') {
if (!this.game.gameView.isMuted) deathSound = './sounds/defender_death.mp3';
this.respawn();
} else {
this.game.score += this.killScore();
this.game.increaseInvadersSpeed();
this.currentBullet = false;
this.isDead = true;
this.deathImage();
if (this.name === 'ufo') {
this.dropPowerUp(this.pos);
this.vel = [0,0];
}
setTimeout(() => {
this.game.remove(this);
}, 200);
if (!this.game.gameView.isMuted) {
if (this.name === 'ufo') {
deathSound = './sounds/ufo_death.wav';
} else {
deathSound = './sounds/grunt_death.wav';
}
}
}
var sound = new Howl({
src: [deathSound],
volume: 0.5,
});
sound.play();
};
Ship.prototype.deathImage = function() { // imagen de la muerte de la nave
if (this.name === 'defender') {
this.img = document.getElementById('defender-death');
} else {
this.img = document.getElementById('ship-death');
}
this.draw(this.game.ctx);
};
Ship.prototype.dropPowerUp = function(pos) { // hace que la nave suelte un power up
const powerUp = new PowerUp({
vel: [0, 4],
pos: pos,
radius: 5,
color: '#ff00ff',
game: this.game,
ship: this,
ctx: this.game.ctx
});
this.game.powerUps.push(powerUp);
};
Ship.prototype.toggleImage = function() { // cambia la imagen de la nave
if (this.isDead) { return; }
if (this.name === 'grunt') {
let grunt1 = document.getElementById('grunt-1');
let grunt2 = document.getElementById('grunt-2');
this.img.id === 'grunt-1' ? this.img = grunt2 : this.img = grunt1;
} else if (this.name === 'soldier') {
let soldier1 = document.getElementById('soldier-1');
let soldier2 = document.getElementById('soldier-2');
this.img.id === 'soldier-1' ? this.img = soldier2 : this.img = soldier1;
} else if (this.name === 'invader') {
let invader1 = document.getElementById('invader-1');
let invader2 = document.getElementById('invader-2');
this.img.id === 'invader-1' ? this.img = invader2 : this.img = invader1;
}
};
Ship.prototype.killScore = function() { // puntaje por matar a la nave
if (this.name === 'grunt') {
return 0;
} else if (this.name === 'soldier') {
return 0;
} else if (this.name === 'invader') {
return 0;
} else if (this.name === 'ufo') {
let ufoPoints = [50, 100, 200, 300, 500];
let idx = Math.random() * 4;
idx = Math.round(idx);
return ufoPoints[idx];
}
};
Ship.prototype.collideWith = function(object) { // maneja la colision de la nave con otros objetos
if (this.side === object.shipSide) return;
this.bulletsInPlay.shift();
if (this.bulletsInPlay.length === 0) this.currentBullet = false;
if (object.type === 'powerUp') {
if (this.name === 'defender') {
if (object.power === 'life') {
this.game.defenderLives++;
} else if (object.power === 'gun') {
if (this.hasThreeGuns) {
this.hasFiveGuns = true;
} else {
this.hasThreeGuns = true;
}
} else if (object.power === 'speed') {
if (this.speedUp) {
this.speedUp2 = true;
} else {
this.speedUp = true;
}
}
}
}
};
Ship.prototype.fireBullet = function() { // hace que la nave dispare balas
// retorno temprano evita que el jugador dispare balas, limitando
// al jugador a una bala a la vez
if (this.currentBullet) { return; }
let bulletPosX = this.pos[0] - 2;
let bulletPosY = this.pos[1] - 25;
let bulletColor;
let bulletVel;
if (this.name === 'defender') {
bulletVel = [0, -4];
bulletColor = "#FF00FF";
} else {
bulletVel = [0, 4];
bulletPosY += 30;
}