-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenemyhandler.js
170 lines (164 loc) · 10.6 KB
/
enemyhandler.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
import {Enemy} from './enemy.js';
export class EnemyHandler {
constructor(game){
this.game = game;
this.basicEnemies = [];
this.healthyEnemies = [];
this.dangerousEnemies = [];
this.enemySpawnTimer = 2000;
this.color = "green";
setInterval((e) => {
this.switch = true;
this.enemyGenerator();
},this.enemySpawnTimer); //spawn new set of enemies
}
//spawn new set of enemies every 2000(value of this.enemySpawnTimer) miliseconds
enemyGenerator(){
this.speed = 2;
this.health = 1;
if (this.game.score<1500) {
this.basicEnemy00 = new Enemy(10,20,10,5,this.speed,this.health,this.color,this.game.player);
this.basicEnemy01 = new Enemy(10,20,60,-10,this.speed,this.health,this.color,this.game.player);
this.basicEnemy02 = new Enemy(10,20,110,5,this.speed,this.health,this.color,this.game.player);
this.basicEnemy03 = new Enemy(10,20,260,-10,this.speed,this.health,this.color,this.game.player);
this.basicEnemy04 = new Enemy(10,20,310,5,this.speed,this.health,this.color,this.game.player);
this.basicEnemy05 = new Enemy(10,20,360,-10,this.speed,this.health,this.color,this.game.player);
this.basicEnemies.push(this.basicEnemy00);
this.basicEnemies.push(this.basicEnemy01);
this.basicEnemies.push(this.basicEnemy02);
this.basicEnemies.push(this.basicEnemy03);
this.basicEnemies.push(this.basicEnemy04);
this.basicEnemies.push(this.basicEnemy05);
if (this.basicEnemies.length > 30) this.basicEnemies.splice(0,6); //seemed easier than removing based on position in canvas. speed of enemies set such that 30(five rows of 6) will never been on screen at same time anyway
}
else if (this.game.score < 2500) {
this.basicEnemy00 = new Enemy(10,20,10,5,this.speed,this.health,this.color,this.game.player);
this.basicEnemy01 = new Enemy(10,20,60,-10,this.speed,this.health,this.color,this.game.player);
this.basicEnemy02 = new Enemy(10,20,110,5,this.speed,this.health,this.color,this.game.player);
this.basicEnemy03 = new Enemy(10,20,260,-10,this.speed,this.health,this.color,this.game.player);
this.basicEnemy04 = new Enemy(10,20,310,5,this.speed,this.health,this.color,this.game.player);
this.basicEnemy05 = new Enemy(10,20,360,-10,this.speed,this.health,this.color,this.game.player);
this.basicEnemies.push(this.basicEnemy00);
this.basicEnemies.push(this.basicEnemy01);
this.basicEnemies.push(this.basicEnemy02);
this.basicEnemies.push(this.basicEnemy03);
this.basicEnemies.push(this.basicEnemy04);
this.basicEnemies.push(this.basicEnemy05);
if (this.basicEnemies.length > 30) this.basicEnemies.splice(0,6);
this.healthyEnemies0 = new Enemy(30,30,180,-25,this.speed,3*this.health,this.color,this.game.player)
this.healthyEnemies.push(this.healthyEnemies0);
if (this.healthyEnemies.length > 5) this.healthyEnemies.splice(0,1)
}
else if(this.game.score<5000) {
this.basicEnemy00 = new Enemy(10,20,10,5,this.speed,this.health,this.color,this.game.player);
this.basicEnemy01 = new Enemy(10,20,60,-10,this.speed,this.health,this.color,this.game.player);
this.basicEnemy02 = new Enemy(10,20,110,5,this.speed,this.health,this.color,this.game.player);
this.basicEnemy03 = new Enemy(10,20,260,5,this.speed,this.health,this.color,this.game.player);
this.basicEnemy04 = new Enemy(10,20,310,-10,this.speed,this.health,this.color,this.game.player);
this.basicEnemy05 = new Enemy(10,20,360,5,this.speed,this.health,this.color,this.game.player);
this.basicEnemy10 = new Enemy(10,20,10,-30,this.speed,this.health,this.color,this.game.player);
this.basicEnemy11 = new Enemy(10,20,60,-45,this.speed,this.health,this.color,this.game.player);
this.basicEnemy12 = new Enemy(10,20,110,-30,this.speed,this.health,this.color,this.game.player);
this.basicEnemy13 = new Enemy(10,20,260,-30,this.speed,this.health,this.color,this.game.player);
this.basicEnemy14 = new Enemy(10,20,310,-45,this.speed,this.health,this.color,this.game.player);
this.basicEnemy15 = new Enemy(10,20,360,-30,this.speed,this.health,this.color,this.game.player);
this.basicEnemies.push(this.basicEnemy00);
this.basicEnemies.push(this.basicEnemy01);
this.basicEnemies.push(this.basicEnemy02);
this.basicEnemies.push(this.basicEnemy03);
this.basicEnemies.push(this.basicEnemy04);
this.basicEnemies.push(this.basicEnemy05);
this.basicEnemies.push(this.basicEnemy10);
this.basicEnemies.push(this.basicEnemy11);
this.basicEnemies.push(this.basicEnemy12);
this.basicEnemies.push(this.basicEnemy13);
this.basicEnemies.push(this.basicEnemy14);
this.basicEnemies.push(this.basicEnemy15);
if (this.basicEnemies.length > 60) this.basicEnemies.splice(0,12);
this.healthyEnemies0 = new Enemy(30,30,150,-40,this.speed,3*this.health,this.color,this.game.player)
this.healthyEnemies1 = new Enemy(30,30,200,-40,this.speed,3*this.health,this.color,this.game.player)
this.healthyEnemies.push(this.healthyEnemies0);
this.healthyEnemies.push(this.healthyEnemies1);
if (this.healthyEnemies.length > 10) this.healthyEnemies.splice(0,2)
this.dangerousEnemies0 = new Enemy(20,10,10,-60,2*this.speed,this.health,"red",this.game.player)
this.dangerousEnemies.push(this.dangerousEnemies0);
if (this.dangerousEnemies.length > 5) this.dangerousEnemies.splice(0,1)
}
else /* if(this.game.score<7000) */ {
this.basicEnemy00 = new Enemy(10,20,10,5,this.speed,this.health,this.color,this.game.player);
this.basicEnemy01 = new Enemy(10,20,60,-10,this.speed,this.health,this.color,this.game.player);
this.basicEnemy02 = new Enemy(10,20,110,5,this.speed,this.health,this.color,this.game.player);
this.basicEnemy03 = new Enemy(10,20,260,5,this.speed,this.health,this.color,this.game.player);
this.basicEnemy04 = new Enemy(10,20,310,-10,this.speed,this.health,this.color,this.game.player);
this.basicEnemy05 = new Enemy(10,20,360,5,this.speed,this.health,this.color,this.game.player);
this.basicEnemy10 = new Enemy(10,20,10,-30,this.speed,this.health,this.color,this.game.player);
this.basicEnemy11 = new Enemy(10,20,60,-45,this.speed,this.health,this.color,this.game.player);
this.basicEnemy12 = new Enemy(10,20,110,-30,this.speed,this.health,this.color,this.game.player);
this.basicEnemy13 = new Enemy(10,20,260,-30,this.speed,this.health,this.color,this.game.player);
this.basicEnemy14 = new Enemy(10,20,310,-45,this.speed,this.health,this.color,this.game.player);
this.basicEnemy15 = new Enemy(10,20,360,-30,this.speed,this.health,this.color,this.game.player);
this.basicEnemies.push(this.basicEnemy00);
this.basicEnemies.push(this.basicEnemy01);
this.basicEnemies.push(this.basicEnemy02);
this.basicEnemies.push(this.basicEnemy03);
this.basicEnemies.push(this.basicEnemy04);
this.basicEnemies.push(this.basicEnemy05);
this.basicEnemies.push(this.basicEnemy10);
this.basicEnemies.push(this.basicEnemy11);
this.basicEnemies.push(this.basicEnemy12);
this.basicEnemies.push(this.basicEnemy13);
this.basicEnemies.push(this.basicEnemy14);
this.basicEnemies.push(this.basicEnemy15);
if (this.basicEnemies.length > 60) this.basicEnemies.splice(0,12);
this.healthyEnemies1 = new Enemy(30,30,200,-40,this.speed,3*this.health,this.color,this.game.player)
this.healthyEnemies0 = new Enemy(30,30,150,-40,this.speed,3*this.health,this.color,this.game.player)
this.healthyEnemies.push(this.healthyEnemies0);
this.healthyEnemies.push(this.healthyEnemies1);
if (this.healthyEnemies.length > 10) this.healthyEnemies.splice(0,2)
this.dangerousEnemies0 = new Enemy(20,10,10,-40,2*this.speed,this.health,"red",this.game.player)
this.dangerousEnemies1 = new Enemy(20,10,390 ,-10,2*this.speed,this.health,"red",this.game.player)
this.dangerousEnemies.push(this.dangerousEnemies0);
this.dangerousEnemies.push(this.dangerousEnemies1);
if (this.dangerousEnemies.length > 15) this.dangerousEnemies.splice(0,2)
}
}
draw(context){
this.basicEnemies.forEach((enemy) => { //for each enemy within basic enemies check collision,draw,update location
this.game.player.collisionRough(enemy)
if(this.game.projectileHandler.collision(enemy)) {
// console.log("enemy collision");
if (enemy.health <= 0) {
this.basicEnemies.splice(this.basicEnemies.indexOf(enemy), 1);
this.game.score += 50;
}
}
else {enemy.draw(context);
enemy.update();}
});
this.healthyEnemies.forEach((enemy) => { //for each enemy within healthy enemies check collision,draw,update location
if(this.game.projectileHandler.collision(enemy)) {
// console.log("enemy collision");
if (enemy.health <= 0) {
this.healthyEnemies.splice(this.healthyEnemies.indexOf(enemy), 1);
this.game.score += 100;
}
}
else {enemy.draw(context);
this.game.player.collisionRough(enemy)
enemy.update();}
});
this.dangerousEnemies.forEach((enemy) => { //for each enemy within dangerous enemies check collision,draw,update location
if(this.game.projectileHandler.collision(enemy)) {
// console.log("enemy collision");
if (enemy.health <= 0) {
this.dangerousEnemies.splice(this.dangerousEnemies.indexOf(enemy), 1);
this.game.score += 100;
}
}
else {enemy.draw(context);
this.game.player.collisionRough(enemy)
enemy.update();}
});
// console.log("basic enemies length ",this.basicEnemies.length);
}
}