forked from dannz510/Tank_Game_V2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.js
More file actions
304 lines (259 loc) · 8.2 KB
/
game.js
File metadata and controls
304 lines (259 loc) · 8.2 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
import {
LiveBonus,
ShieldBonus,
UpgradeWeaponBonus,
RocketBonus,
} from './src/bonus.js'
import { checkLocalStorage, updateLocalStorage } from './src/utils.js'
import Enemy from './src/enemy.js'
import InputHandler from './src/input.js'
import Collision from './src/collision.js'
import Player from './src/player.js'
import Snow from './src/terrains/type/snow.js'
import GrassLight from './src/terrains/type/grassLight.js'
import GrassLight2 from './src/terrains/type/grassLight2.js'
import GrassDark from './src/terrains/type/grassDark.js'
import GrassDark2 from './src/terrains/type/grassDark2.js'
import UI from './src/UI.js'
import './src/prototype.js'
export default class Game {
constructor(width, height) {
this.width = width
this.height = height
this.scale = 1
this.initialize()
}
initialize() {
this.player = new Player(this)
this.input = new InputHandler()
this.UI = new UI(this)
this.terrainsType = [
new Snow(this),
new GrassLight(this),
new GrassLight2(this),
new GrassDark(this),
new GrassDark2(this),
]
this.terrains = this.getRandomTerrain()
this.enemies = []
this.maxEnemies = 1 // max enemies for first wave
this.collisions = []
this.score = 0
this.bestScore = this.getBestScore()
this.checkBestScore()
this.wave = 0
this.gameOver = false
this.bonuses = []
this.bonusTimer = 0
this.bonusExpiredTimer = 0
this.bonusInterval = 15000 // in ms
this.bonusExpiredInterval = 10000
this.stroke = false
}
update() {
this.handleDebugMode()
this.handleRestartGame()
this.handleLoseCondition()
this.updateBestScore()
this.summonEnemies()
this.cleanupObjects()
}
render(deltaTime, ctx) {
this.update()
this.updateBonuses(deltaTime)
this.updateAndDraw(this.terrains, deltaTime, ctx)
this.updateAndDraw(this.bonuses, deltaTime, ctx)
this.updateAndDraw(this.enemies, deltaTime, ctx)
this.updateAndDraw(this.collisions, deltaTime, ctx)
this.updateAndDraw(this.player, deltaTime, ctx)
this.updateAndDraw(this.UI, deltaTime, ctx)
}
restart() {
this.gameOver = false
this.maxEnemies = 3
this.enemies = []
this.bonuses = []
this.score = 0
this.wave = 0
this.bonusTimer = 0
this.bonusExpiredTimer = 0
this.terrains = this.terrainsType.getRandomValue()
this.player.restart()
}
getRandomTerrain() {
return this.terrainsType.getRandomValue()
}
getBestScore() {
return localStorage.getItem('tankBestScore') || '0'
}
handleDebugMode() {
if (this.input.keys.includes('p')) {
this.stroke = !this.stroke
}
}
handleRestartGame() {
if (
(this.input.keys.includes('ArrowDown') ||
this.input.keys.includes('r')) &&
this.gameOver
) {
this.restart()
}
}
handleLoseCondition() {
if (this.player.lives < 1) {
this.gameOver = true
}
}
updateBestScore() {
updateLocalStorage('tankBestScore', this.score)
this.bestScore = this.getBestScore()
}
checkBestScore() {
checkLocalStorage('tankBestScore', '0')
}
summonEnemies() {
if (this.enemies.length < 1 && !this.gameOver) {
for (let i = 0; i < this.maxEnemies; i++) {
this.addEnemy()
}
this.wave += 1
if (this.wave > 1) {
this.terrains = this.getRandomTerrain()
}
this.maxEnemies += 1
}
}
addEnemy() {
this.enemies.push(new Enemy(this))
}
updateBonuses(deltaTime) {
if (this.bonuses.length < 1 && !this.gameOver) {
this.bonusExpiredTimer = 0
this.handleBonusInterval(deltaTime)
}
if (this.bonuses.length >= 1) {
this.bonusTimer = 0
this.handleBonusExpiredTimer(deltaTime)
}
}
handleBonusInterval(deltaTime) {
if (this.bonusTimer > this.bonusInterval) {
this.createBonus()
this.bonusTimer = 0
} else {
this.bonusTimer += deltaTime
}
}
createBonus() {
const bonusType = [
new LiveBonus(this),
new ShieldBonus(this),
new UpgradeWeaponBonus(this),
new RocketBonus(this),
]
const randomBonus = bonusType.getRandomValue()
this.bonuses.push(randomBonus)
}
handleBonusExpiredTimer(deltaTime) {
if (this.bonusExpiredTimer > this.bonusExpiredInterval) {
this.bonuses = []
this.bonusExpiredTimer = 0
} else {
this.bonusExpiredTimer += deltaTime
}
}
cleanupObjects() {
this.enemies = this.enemies.filter((enemy) => !enemy.markedForDeletion)
this.bonuses = this.bonuses.filter((bonus) => !bonus.markedForDeletion)
this.collisions = this.collisions.filter(
(collision) => !collision.markedForDeletion,
)
}
// Define an updated updateAndDraw method for arrays of game elements and single elements
updateAndDraw(target, deltaTime, ctx) {
if (Array.isArray(target)) {
target.forEach((element) => {
if (typeof element.update === 'function') {
element.update(deltaTime)
}
if (typeof element.draw === 'function') {
element.draw(ctx)
}
})
} else if (typeof target === 'object') {
if (typeof target.update === 'function') {
target.update(deltaTime)
}
if (typeof target.draw === 'function') {
target.draw(ctx)
}
}
}
// Create explosion animation
createExplosion(x, y, size) {
this.collisions.push(new Collision(x, y, size))
}
// collision detection between two circle
checkCircleCollision(a, b) {
// Calculate the center coordinates of the a
const aX = a.x + a.width * 0.5
const aY = a.y + a.height * 0.5
// Calculate the center coordinates of the b
const bX = b.x + b.width * 0.5
const bY = b.y + b.width * 0.5
// Calculate the distance between the centers of the a and b
const dx = aX - bX
const dy = aY - bY
const distance = Math.sqrt(dx * dx + dy * dy)
return distance < a.width * 0.33 + b.width * 0.33
}
// collision detection between two rectangle
checkCollision(a, b) {
return (
a.x < b.x + b.width &&
a.x + a.width > b.x &&
a.y < b.y + b.height &&
a.y + a.height > b.y
)
}
// resolve collision betwen two moving objects
resolveCollision(obj1, obj2) {
const dx = obj1.x + obj1.width / 2 - (obj2.x + obj2.width / 2)
const dy = obj1.y + obj1.height / 2 - (obj2.y + obj2.height / 2)
const distance = Math.sqrt(dx * dx + dy * dy)
const angle = Math.atan2(dy, dx)
const totalSpeed = Math.sqrt(
obj1.speedX * obj1.speedX + obj1.speedY * obj1.speedY,
)
const newSpeedX1 = totalSpeed * Math.cos(angle)
const newSpeedY1 = totalSpeed * Math.sin(angle)
obj1.speedX = newSpeedX1
obj1.speedY = newSpeedY1
}
// Bounce object when collision
bounceObject(value) {
const object = value
const pX = object.x
const pY = object.y
switch (object.direction) {
case 'up':
object.x = pX
object.y = pY + 5
break
case 'down':
object.x = pX
object.y = pY - 5
break
case 'left':
object.x = pX + 5
object.y = pY
break
case 'right':
object.x = pX - 5
object.y = pY
break
}
return object
}
}