forked from Code-Bullet/NEAT-Template-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnemy.js
More file actions
248 lines (208 loc) · 7.61 KB
/
Enemy.js
File metadata and controls
248 lines (208 loc) · 7.61 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
class Enemy {
// Full, unchanging original list
static topLeftSpawn = [
//top left
{ spawn: {x: -10, y: 100 }, patrol: [{ x: 60, y: 100 }, { x: 500, y: 100 }] },
{ spawn: {x: 500, y: -10 }, patrol: [{ x: 500, y: 70 }, { x: 60, y: 70 }] },
{ spawn: { x: -10, y: 235 }, patrol: [{ x: 60, y: 235 }, { x: 500, y: 235 }] },
{ spawn: { x: -10, y: 490 }, patrol: [{ x: 100, y: 490 }, { x: 500, y: 490 }] },
{ spawn: { x: -10, y: 440 }, patrol: [{ x: 100, y: 440 }, { x: 500, y: 440 }] },
{ spawn: { x: -10, y: 380 }, patrol: [{ x: 100, y: 380 }, { x: 500, y: 380 }] },
{ spawn: { x: 500, y: -10}, patrol: [{ x: 500, y: 100 }, { x: 500, y: 500 }] },
{ spawn: {x: 60, y: -10}, patrol: [{ x: 60, y: 100 }, { x: 60, y: 400 }] },
];
static topRightSpawn = [
//top right
{ spawn: {x: 1090, y: 100 }, patrol: [{ x: 1020, y: 100 }, { x: 560, y: 100 }] },
{ spawn: {x: 560, y: -10 }, patrol: [{ x: 560, y: 70 }, { x: 1020, y: 70 }] },
{ spawn: { x: 1090, y: 235 }, patrol: [{ x: 1020, y: 235 }, { x: 560, y: 235 }] },
{ spawn: { x: 1090, y: 490 }, patrol: [{ x: 1020, y: 490 }, { x: 560, y: 490 }] },
{ spawn: { x: 1090, y: 440 }, patrol: [{ x: 1020, y: 440 }, { x: 560, y: 440 }] },
{ spawn: { x: 1090, y: 380 }, patrol: [{ x: 1020, y: 380 }, { x: 560, y: 380 }] },
{ spawn: { x: 550, y: -10}, patrol: [{ x: 550, y: 100 }, { x: 550, y: 500 }] },
{ spawn: { x: 1020, y: -10}, patrol: [{ x: 1020, y: 100 }, { x: 1020, y: 440 }] },
{ spawn: { x: 1000, y: -10}, patrol: [{ x: 1000, y: 100 }, { x: 1000, y: 440 }] },
];
static bottomLeftSpawn = [
//bottom left
{ spawn: { x: 90, y: 910}, patrol: [{ x: 90, y: 830 }, { x: 90, y: 500 }] },
{ spawn: { x: 220, y: 910}, patrol: [{ x: 220, y: 830 }, { x: 220, y: 500 }] },
{ spawn: {x: 350, y: 910}, patrol: [{ x: 350, y: 830 }, { x: 350, y: 500 }] },
{ spawn: {x: -10, y: 600}, patrol: [{ x: 90, y: 600 }, { x: 500, y: 600 }] },
{ spawn: {x: -10, y: 830}, patrol: [{ x: 90, y: 830 }, { x: 350, y: 830 }] },
{ spawn: {x: 350, y: 910}, patrol: [{ x: 350, y: 770 }, { x: 720, y: 770 }] },
];
static bottomRightSpawn = [
//bottom right
{ spawn: { x: 1020, y: 910}, patrol: [{ x: 1020, y: 830 }, { x: 1020, y: 500 }] },
{ spawn: {x: 1090, y: 600}, patrol: [{ x: 1020, y: 600 }, { x: 560, y: 600 }] },
{ spawn: {x: 730, y: 910}, patrol: [{ x: 730, y: 830 }, { x: 730, y: 500 }] },
{ spawn: {x: 855, y: 910}, patrol: [{ x: 855, y: 830 }, { x: 855, y: 500 }] },
{ spawn: {x: 980, y: 910}, patrol: [{ x: 980, y: 830 }, { x: 980, y: 500 }] },
];
static topLeftSpawnOptions = [...Enemy.topLeftSpawn];
static topRightSpawnOptions = [...Enemy.topRightSpawn];
static bottomLeftSpawnOptions = [...Enemy.bottomLeftSpawn];
static bottomRightSpawnOptions = [...Enemy.bottomRightSpawn];
static resetSpawns() {
Enemy.topLeftSpawnOptions = [...Enemy.topLeftSpawn];
Enemy.topRightSpawnOptions = [...Enemy.topRightSpawn];
Enemy.bottomLeftSpawnOptions = [...Enemy.bottomLeftSpawn];
Enemy.bottomRightSpawnOptions = [...Enemy.bottomRightSpawn];
}
static enemyCount = 0; // static counter shared by all enemies
constructor() {
let randomIndex;
Enemy.enemyCount++; // increment count on each new enemy
this.i = (Enemy.enemyCount % 4) + 1;
this.spawnIndex = 0;
switch (this.i) {
case 1:
randomIndex = floor(random(Enemy.topLeftSpawnOptions.length));
this.spawn = Enemy.topLeftSpawnOptions.splice(randomIndex, 1)[0];
break;
case 2:
randomIndex = floor(random(Enemy.topRightSpawnOptions.length));
this.spawn = Enemy.topRightSpawnOptions.splice(randomIndex, 1)[0];
break;
case 3:
randomIndex = floor(random(Enemy.bottomLeftSpawnOptions.length));
this.spawn = Enemy.bottomLeftSpawnOptions.splice(randomIndex, 1)[0];
break;
case 4:
randomIndex = floor(random(Enemy.bottomRightSpawnOptions.length));
this.spawn = Enemy.bottomRightSpawnOptions.splice(randomIndex, 1)[0];
break;
}
this.patrolPoints = this.spawn.patrol;
//random spawn position along patrol line
//let t = random(0, 1);
//let p0 = this.patrolPoints[0];
//let p1 = this.patrolPoints[1];
this.x = this.spawn.spawn.x;
this.y = this.spawn.spawn.y;
this.h = 18;
this.speed = 3;
this.w = 36;
this.playInvin = false;
this.currentPatrolIndex = 0;
this.dropCooldown = 0;
this.facing = "d";
this.isActive = true;
this.sprite = new Sprite(squirrel, this.w, this.h, 3);
this.spriteDown = new Sprite(squirrelDown, this.h, this.w, 4);
this.spawnTime = millis();
//this.collisionDelay = 1500; //if enemy spawns on player, no insta-kill
}
moveTo(targetX, targetY) {
let angle = atan2(targetY - this.y, targetX - this.x);
let newX = this.x + this.speed * cos(angle);
let newY = this.y + this.speed * sin(angle);
this.x = newX;
this.y = newY;
if (abs(targetX - this.x) > abs(targetY - this.y)) {
this.facing = targetX < this.x ? "a" : "d";
} else {
this.facing = targetY < this.y ? "w" : "s";
}
}
move() {
// .01% chance per frame to disappear
if (random(1) < 0.001) {
if (this.spawn && this.i == 1) {
Enemy.topLeftSpawnOptions.push(this.spawn);
}
else if (this.spawn && this.i == 2) {
Enemy.topRightSpawnOptions.push(this.spawn);
}
else if (this.spawn && this.i == 3) {
Enemy.bottomLeftSpawnOptions.push(this.spawn);
}
else if (this.spawn && this.i == 4) {
Enemy.bottomRightSpawnOptions.push(this.spawn);
}
this.isActive = false;
return;
}
this.patrol();
if (this.dropCooldown > 0) {
this.dropCooldown--;
} else {
if (random(1) < 0.0001) {
this.dropAnti();
this.dropCooldown = 60000;
}
}
}
dropAnti() {
anti.push(new Anti(this.x, this.y));
}
patrol() {
let target = this.patrolPoints[this.currentPatrolIndex];
let d = dist(this.x, this.y, target.x, target.y);
if (d < 5) {
this.x = target.x;
this.y = target.y;
this.currentPatrolIndex = (this.currentPatrolIndex + 1) % this.patrolPoints.length;
} else {
this.moveTo(target.x, target.y);
}
}
show() {
push();
const cx = this.x + this.w / 2;
const cy = this.y + this.h / 2;
translate(cx, cy);
if (this.facing === "a") {
scale(-1, 1);
}
imageMode(CENTER);
//different sprites for different directions
if (this.facing === "w") {
scale(-1, -1);
this.spriteDown.draw();
this.w = 18;
this.h = 36;
} else if (this.facing === "s") {
this.spriteDown.draw();
this.w = 18;
this.h = 36;
} else {
this.sprite.draw();
this.w = 36;
this.h = 18;
}
imageMode(CORNER);
pop();
//noFill();
//stroke(255, 0, 0);
//rect(this.x, this.y, this.w, this.h);
}
checkCollision(player) {
let thisLeft = this.x;
let thisRight = this.x + this.w;
let thisTop = this.y;
let thisBottom = this.y + this.h;
let playerLeft = player.x;
let playerRight = player.x + player.w;
let playerTop = player.y;
let playerBottom = player.y + player.h;
let overlapX = thisRight > playerLeft && thisLeft < playerRight;
let overlapY = thisBottom > playerTop && thisTop < playerBottom;
return overlapX && overlapY;
}
/*collidesWithBlocks(x, y) {
let tempEnemy = {
x: x - this.size / 2,
y: y - this.size / 2,
w: this.size,
h: this.size
};
for (let block of blocks) {
if (block.intersects(tempEnemy)) {
return true;
}
}
return false;
}*/
}