-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
138 lines (123 loc) · 4.09 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
'use strict';
function Game(canvas) {
this.canvas = canvas;
this.ctx = this.canvas.getContext('2d');
this.spaceship = new Spaceship(canvas);
this.enemies = [];
this.enemiesBullets = [];
this.bulletSpawn = 0.98;
this.gameOver = false;
this.shootSound = new Audio('./src/space-shoot-final.wav')
this.explosionSound = new Audio('./src/explosion.wav')
}
Game.prototype.startLoop = function () {
this.createEnemies()
const loop = () => {
this.checkEnemies();
this.clearCanvas();
this.updateCanvas();
this.drawCanvas();
this.checkCollision();
if (this.gameOver === false) {
window.requestAnimationFrame(loop)
} else {
this.onGameOver()
}
}
window.requestAnimationFrame(loop)
}
Game.prototype.clearCanvas = function () {
this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height)
}
Game.prototype.updateCanvas = function () {
document.querySelector('#actual-score').innerHTML = this.spaceship.score
document.querySelector('#levels').innerHTML = `Level: ${this.spaceship.currentLevel}`
this.spaceship.update()
this.enemies.forEach((element) => {
element.update()
})
this.spaceship.bullets.forEach((element, index) => {
if (element.y < 0) {
this.spaceship.bullets.splice(index, 1)
}
element.update()
})
this.enemiesBullets.forEach((element) => {
element.update()
})
}
Game.prototype.drawCanvas = function () {
this.spaceship.draw()
this.enemies.forEach((element) => {
element.draw()
})
this.spaceship.bullets.forEach((element) => {
element.draw()
})
if (Math.random() > this.bulletSpawn) {
let randomPos = Math.floor(Math.random()*this.enemies.length)
this.enemiesBullets.push(new Bullet(this.canvas, this.enemies[randomPos].x + this.enemies[randomPos].size / 2, this.enemies[randomPos].y + this.enemies[randomPos].size / 2, -1, '#FF4C20'))
}
this.enemiesBullets.forEach((element) => {
element.draw()
})
}
Game.prototype.checkCollision = function () {
this.spaceship.bullets.forEach((bullet, indexBullet) => {
this.enemies.forEach((enemy) => {
if (bullet.y < enemy.y + enemy.size && bullet.y > enemy.y && bullet.x > enemy.x && bullet.x + bullet.width < enemy.x + enemy.size) {
enemy.image.src = './img/explosion.png'
this.explosionSound.currentTime = 0
this.explosionSound.volume = 0.2
this.explosionSound.play()
setTimeout(() => {
let currentIndex = this.enemies.indexOf(enemy)
this.enemies.splice(currentIndex, 1)
}, 50)
this.spaceship.bullets.splice(indexBullet, 1)
this.spaceship.updateScore()
}
})
})
this.enemies.forEach((element) => {
if (element.y + element.size/2 > this.spaceship.y) {
this.gameOver = true
}
})
this.enemiesBullets.forEach((bullet, index) => {
if (bullet.y + bullet.height > this.canvas.height) {
this.enemiesBullets.splice(index, 1)
} else if (bullet.y + bullet.height > this.spaceship.y) {
if (bullet.x > this.spaceship.x && bullet.x + bullet.width < this.spaceship.x + this.spaceship.width) {
this.enemiesBullets.splice(index, 1)
this.gameOver = this.spaceship.removeLife()
}
}
})
}
Game.prototype.setGameOver = function (callaback) {
this.onGameOver = callaback;
}
Game.prototype.createEnemies = function () {
//could have done better
let enemiesNumber = Math.floor((this.canvas.width - 200) / 60)
if (enemiesNumber % 2 !== 0) {
enemiesNumber++
}
let distanceX = 60
let distanceY = 40
let speedMultiplier = 1.5
let images = ['./img/enemy-1-purple.png', './img/enemy-2-blue.png', './img/enemy-2-blue.png', './img/enemy-3-green.png', './img/enemy-3-green.png']
for (var i=0; i<images.length; i++) {
for (var j=0; j<enemiesNumber; j++) {
this.enemies.push(new Enemy(this.canvas, (j*distanceX)+distanceX, i*distanceY, images[i], (this.spaceship.currentLevel*speedMultiplier)+speedMultiplier))
}
}
}
Game.prototype.checkEnemies = function () {
if (this.enemies.length === 0) {
this.createEnemies()
this.bulletSpawn -= 0.02
this.spaceship.levelUp()
}
}