-
Notifications
You must be signed in to change notification settings - Fork 0
/
Enemies.js
140 lines (134 loc) · 4.37 KB
/
Enemies.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
import Enemy from "./Enemy.js"
import PowerUps from "./PowerUps.js"
export default class Enemies {
width = 100
height = 90
middleSlotX = 360
middleSlotY = 390
enemies = []
score = 0
canvas = null
powerup = null
imgSrc = './enemy.png'
constructor(canvas, cartridge, player) {
this.scoreTxt = document.getElementById('score')
this.cartridge = cartridge
this.player = player
this.canvas = canvas
}
push(rand) {
switch(rand) {
case 0:
this.enemies.push(new Enemy({
position: {
x: -this.width/2,
y: Math.random()* (720 - this.height - 360) + 360
},
size: {
width: this.width,
height: this.height
},
imgSrc: this.imgSrc,
speed: {
x: 1,
y: 0
}, canvas: this.canvas, player: this.player
}))
break;
case 1:
this.enemies.push(new Enemy({
position: {
x: Math.random()* (785 - this.width - 395) + 395,
y: -this.height/2
}, size: {
width: this.width,
height: this.height
},
imgSrc: this.imgSrc,
speed: {
x: 0,
y: 1
}, canvas: this.canvas, player: this.player
}))
break;
case 2:
this.enemies.push(new Enemy({
position: {
x: this.canvas.width + this.width/2,
y: Math.random()* (720 - this.height - 360) + 360
}, size: {
width: this.width,
height: this.height
},
imgSrc: this.imgSrc,
speed: {
x: -1,
y: 0
}, canvas: this.canvas, player: this.player
}))
break;
case 3:
this.enemies.push(new Enemy({
position: {
x: Math.random()* (785 - this.width - 395) + 395,
y: this.canvas.height + this.height/2
}, size: {
width: this.width,
height: this.height
},
imgSrc: this.imgSrc,
speed: {
x: 0,
y: -1
}, canvas: this.canvas, player: this.player
}))
break;
}
}
update() {
if (this.powerup != null) {
this.powerup.draw()
if (this.player.checkCollision(this.powerup)) {
this.cartridge.powerup = 1
this.powerup = null
}
}
this.enemies.forEach((enemy) => {
enemy.update()
})
}
checkCollision(player) {
this.enemies.forEach((enemy) => {
if (this.cartridge.checkCollision(enemy)) {
this.enemies.splice(this.enemies.indexOf(enemy), 1);
this.score += 10
this.scoreTxt.innerHTML = this.score
let rand = Math.floor(Math.random()*5)
console.log(rand)
if (rand == 0) {
this.powerup = new PowerUps({
canvas: this.canvas,
position: {
x: enemy.position.x + enemy.width/2,
y: enemy.position.y + enemy.height/2
},
cartridge: this.cartridge,
player: player
})
this.powerup.draw()
}
}
});
return this.enemies.some((enemy) => {
if (player.checkCollision(enemy)) {
return true;
}
})
}
getScore() {
return this.score
}
clear(){
this.enemies.splice(0, this.enemies.length)
}
}