-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.js
196 lines (181 loc) · 6.37 KB
/
Game.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
import Background from './Background/Background';
import Player from './Player';
import InputHandler from './InputHandler';
import UI from './UI';
import SoundController from './SoundController';
// effect classes
import Particle from './Effects/Particle'
import Shield from './Effects/Shield';
import FireExplosion from './Effects/FireExplosion';
import SmokeExplosion from './Effects/SmokeExplosion';
// enemy classes
import Angler1 from './Enemies/Angler1';
import Angler2 from './Enemies/Angler2';
import BulbWhale from './Enemies/BulbWhale';
import Drone from './Enemies/Drone';
import HiveWhale from './Enemies/HiveWhale';
import LuckyFish from './Enemies/LuckyFish';
import MoonFish from './Enemies/MoonFish';
import Razorfin from './Enemies/Razorfin';
import Stalker from './Enemies/Stalker';
export default class Game {
constructor(width, height) {
this.width = width;
this.height = height;
this.background = new Background(this);
this.player = new Player(this);
this.input = new InputHandler(this);
this.ui = new UI(this);
this.sound = new SoundController();
this.keys = [];
this.shield = new Shield(this);
this.ammo = 20;
this.maxAmmo = 50;
this.ammoTimer = 0;
this.ammoInterval = 350;
this.particles = [];
this.explosions = [];
this.enemies = [];
this.enemyTimer = 0;
this.enemyInterval = 2000;
this.gameTime = 0;
this.timeLimit = 30000;
this.speed = 1;
this.gameOver = false;
this.score = 0;
this.winningScore = 80;
this.debug = false;
}
update(deltaTime) {
if (!this.gameOver) {
this.gameTime += deltaTime;
}
if (this.gameTime > this.timeLimit) {
this.gameOver = true;
}
this.background.update();
this.background.layer4.update();
this.player.update(deltaTime);
if (this.ammoTimer > this.ammoInterval) {
if (this.ammo < this.maxAmmo) this.ammo++;
this.ammoTimer = 0;
} else {
this.ammoTimer += deltaTime;
}
// handle shield
this.shield.update(deltaTime);
// handle particles
this.particles.forEach(particle => particle.update());
this.particles = this.particles.filter(particle => !particle.markedForDeletion);
// handle explosions
this.explosions.forEach(explosion => explosion.update(deltaTime));
this.explosions = this.explosions.filter(explosion => !explosion.markedForDeletion);
// handle enemy collision
this.enemies.forEach(enemy => {
enemy.update();
if (this.checkCollision(this.player, enemy)) {
enemy.markedForDeletion = true;
this.addExplosion(enemy);
this.sound.play(this.sound.hitSound);
this.shield.play();
for (let i = 0; i < enemy.score; i++) {
this.particles.push(new Particle(this, enemy.x + enemy.width * 0.5,
enemy.y + enemy.height * 0.5));
}
if (enemy.type === 'lucky') {
this.player.enterPowerUp();
} else if (!this.gameOver) {
this.score--;
}
}
// handle projectile collision
this.player.projectiles.forEach(projectile => {
if (this.checkCollision(projectile, enemy)) {
enemy.lives--;
projectile.markedForDeletion = true;
this.particles.push(new Particle(this, enemy.x + enemy.width * 0.5, enemy.y + enemy.height * 0.5));
if (enemy.lives <= 0) {
for (let i = 0; i < enemy.score; i++) {
this.particles.push(new Particle(this, enemy.x + enemy.width * 0.5,
enemy.y + enemy.height * 0.5));
}
enemy.markedForDeletion = true;
this.addExplosion(enemy);
this.sound.play(this.sound.explosionSound);
if (enemy.type === 'moon') {
this.player.enterPowerUp();
}
if (enemy.type === 'hive') {
for (let i = 0; i < 5; i++) {
this.enemies.push(new Drone(this, enemy.x + Math.random() * enemy.width, enemy.y + Math.random() * enemy.height * 0.5));
}
}
if (!this.gameOver) {
this.score += enemy.score;
}
// if (this.score > this.winningScore) {
// this.gameOver = true;
// }
}
}
});
});
this.enemies = this.enemies.filter(enemy => !enemy.markedForDeletion);
if ((this.enemyTimer > this.enemyInterval) && !this.gameOver) {
this.addEnemy();
this.enemyTimer = 0;
} else {
this.enemyTimer += deltaTime;
}
}
draw(context) {
this.background.draw(context);
this.ui.draw(context);
this.player.draw(context);
this.shield.draw(context);
this.particles.forEach(particle => particle.draw(context));
this.enemies.forEach(enemy => {
enemy.draw(context);
});
this.explosions.forEach(explosion => {
explosion.draw(context);
});
this.background.layer4.draw(context);
}
addEnemy() {
const randomize = Math.random();
if (randomize < 0.1) {
this.enemies.push(new Angler1(this));
} else if (randomize < 0.3) {
this.enemies.push(new Stalker(this));
} else if (randomize < 0.5) {
this.enemies.push(new Razorfin(this));
} else if (randomize < 0.6) {
this.enemies.push(new Angler2(this));
} else if (randomize < 0.7) {
this.enemies.push(new HiveWhale(this));
} else if (randomize < 0.8) {
this.enemies.push(new BulbWhale(this));
} else if (randomize < 0.9) {
this.enemies.push(new MoonFish(this));
} else {
this.enemies.push(new LuckyFish(this));
}
}
addExplosion(enemy) {
const randomize = Math.random();
if (randomize < 0.5) {
this.explosions.push(new SmokeExplosion(this, enemy.x + enemy.width * 0.5, enemy.y + enemy.height * 0.5));
} else {
this.explosions.push(new FireExplosion(this, enemy.x + enemy.width * 0.5, enemy.y + enemy.height * 0.5));
}
}
checkCollision(rect1, rect2) {
return (
rect1.x < rect2.x + rect2.width &&
rect1.x + rect1.width > rect2.x &&
rect1.y < rect2.y + rect2.height &&
rect1.height + rect1.y > rect2.y
)
}
}