-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoss.js
131 lines (108 loc) · 3.93 KB
/
Boss.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
'use strict';
/* The final boss */
class Boss extends Entity {
constructor(x, y) {
super(x, y, tilesize * 2);
this.size = tilesize * 2;
/* Final boss should not try to attack until activated */
this.active = false;
this.totalHealth = 50;
this.health = this.totalHealth;
this.moveTimer = new Cooldown(5000);
this.invincibleTimer = new Cooldown(300);
this.attackTimer = new Cooldown(3000);
this.currentProjectile = 0;
this.projectiles = (() => {
let arr = [];
for (let i = 0; i < 5; i++) {
arr.push(new Projectile(0, 0));
}
return arr;
})();
this.flyingFrames = new Keyframes([
utils.makeRect( 0, 0),
utils.makeRect( this.size, 0),
utils.makeRect(this.size * 2, 0),
utils.makeRect( this.size, 0),
], 50);
this.flyingFrames.start();
this.dir = 1;
}
activate() {
this.active = true;
assets.stopSound("main_music");
assets.loopSound("boss_music");
}
/* Take more health depending on progression */
takeHealth(tilemap) {
if (this.invincibleTimer.expired) {
this.invincibleTimer.start();
let damage = map(tilemap.player.collectablesFound, 0, tilemap.totalCollectables, 1, 5);
this.health = max(0, this.health - damage);
}
}
shoot(from, to) {
let p = this.projectiles[this.currentProjectile++];
p.position = from.copy();
p.fired = true;
let shootForce = p5.Vector.sub(to, from);
shootForce.normalize();
shootForce.mult(2.2);
p.applyForce(shootForce);
if (this.currentProjectile >= this.projectiles.length) {
this.currentProjectile = 0;
}
}
update(game) {
if (this.active) {
let player = game.tilemaps[game.currentLevel].player;
if (this.attackTimer.expired) {
this.attackTimer.start();
this.shoot(this.getCenter(), player.getCenter());
}
if (this.moveTimer.expired) {
this.moveTimer.start();
/* Set another attraction point */
this.attractPointAngle = p5.Vector.fromAngle(random(190, 350));
this.attractPointAngle.setMag(180);
} else {
let attractPoint = p5.Vector.add(this.attractPointAngle, player.position);
let attractForce = p5.Vector.sub(attractPoint, this.position);
attractForce.mult(0.01);
this.velocity.mult(0.95);
this.applyForce(attractForce);
}
let toPlayer = p5.Vector.sub(player.position, this.position);
this.dir = toPlayer.x;
super.update(game);
this.attackTimer.update();
this.moveTimer.update();
this.invincibleTimer.update();
this.flyingFrames.update();
this.projectiles.forEach((projectile) => {
if (projectile.fired && utils.checkBoxCollision(player.getCollisionBox(), projectile.getCollisionBox())) {
projectile.fired = false;
player.takeHealth(1);
}
projectile.update(game);
});
}
}
draw(camera) {
if (this.active) {
let dx = this.position.x;
let dy = this.position.y;
let cf = this.flyingFrames.getCurrent();
push();
if (this.dir < 0) {
scale(-1, 1);
dx = (dx * -1) - tilesize;
}
image(assets.getImage("queen_bee"), floor(dx), dy, this.size, this.size, cf.x, cf.y, this.size, this.size);
pop();
this.projectiles.forEach((projectile) => {
projectile.draw(camera);
});
}
}
}