-
Notifications
You must be signed in to change notification settings - Fork 35
/
behaviours.ts
230 lines (195 loc) Β· 5.58 KB
/
behaviours.ts
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
import * as fx from "./fx";
import * as sprites from "./sprites.json";
import { Damage } from "./actions";
import { tween } from "./engine";
import { Behaviour, GameObject, MAX_STREAK } from "./game";
import { screenshake } from "./renderer";
import type { Damage as Dmg } from "./game";
import { distance, vectorToAngle, angleBetweenPoints, vectorFromAngle, clamp, randomInt } from "./helpers";
import { LightningSpell } from "./objects";
export class Attack extends Behaviour {
onCollision(target: GameObject): void {
let dealDamage = this.object.hp;
let takeDamage = target.hp;
Damage(target, dealDamage, this.object);
Damage(this.object, takeDamage, target);
}
}
export class DespawnTimer extends Behaviour {
elapsed = 0;
constructor(object: GameObject, readonly duration: number) {
super(object);
}
onFrame(dt: number): void {
if ((this.elapsed += dt) >= this.duration) {
game.despawn(this.object);
}
}
}
export class March extends Behaviour {
step: number;
constructor(object: GameObject, step: number) {
super(object);
this.step = step;
}
onUpdate(): void {
// Can't march if you're flying
if (this.object.y > 0) return;
// Animate the march with a little hop
tween(this.object.x, this.object.x + this.step, 200, (x, t) => {
this.object.x = x;
this.object.hop = Math.sin(t * Math.PI) * 2;
if (t === 1 && this.object.mass >= 100) screenshake(50);
});
// Despawn units that march offscreen
if (
(this.step < 0 && this.object.x < 0) ||
(this.step > 0 && this.object.x > game.stage.width)
) {
game.despawn(this.object);
}
}
}
export class Damaging extends Behaviour {
amount = 1;
onCollision(target: GameObject): void {
Damage(target, this.amount, this.object);
}
}
export class Bleeding extends Behaviour {
override sprite = sprites.status_bleeding;
override turns = 3;
amount = 1;
emitter = fx.cloud({ x: 0, y: 0, w: 0, h: 0 }, [
[sprites.health_orb, sprites.health_pip],
[sprites.health_pip]
]).extend({
mass: [10, 30],
velocity: [10, 30],
frequency: 0,
});
onUpdate(): boolean | void {
this.emitter.extend(this.object.center());
this.emitter?.burst(1);
Damage(this.object, 1, this.object);
}
}
export class Enraged extends Behaviour {
override sprite = sprites.status_enraged;
emitter = fx.cloud({ x: 0, y: 0, w: 0, h: 0 }, [
[sprites.health_orb, sprites.health_pip],
[sprites.health_pip]
]).extend({
mass: [10, 30],
velocity: [10, 30],
frequency: 0,
});
constructor(object: GameObject, public mask: number) {
super(object);
}
onDamage(damage: Dmg): void {
if (damage.dealer && damage.dealer.is(this.mask)) {
Damage(this.object, -damage.amount, this.object);
damage.amount = 0;
this.emitter.extend(this.object.bounds()).burst(4);
}
}
}
export class Seeking extends Behaviour {
onFrame(): void {
let projectile = this.object;
let target: GameObject | undefined;
let minDist = 100;
for (let object of game.objects) {
if (object.is(this.object.collisionMask)) {
let dist = distance(projectile, object);
if (dist < minDist) {
target = object;
minDist = dist;
}
}
}
if (target) {
let currentAngle = vectorToAngle(projectile.vx, projectile.vy);
let desiredAngle = angleBetweenPoints(projectile, target.center());
let angle = currentAngle + (desiredAngle - currentAngle) / 20;
let magnitude = Math.hypot(projectile.vx, projectile.vy);
let [vx, vy] = vectorFromAngle(angle);
projectile.vx = vx * magnitude;
projectile.vy = vy * magnitude;
}
}
}
export class Summon extends Behaviour {
private summonTimer = 0;
public summonCounter = 0;
constructor(
object: GameObject,
private create: () => GameObject,
private summonSpeed: number,
) {
super(object);
}
onSummon(object: GameObject) {}
onFrame(dt: number): void {
if ((this.summonTimer += dt) > this.summonSpeed) {
this.summonTimer = 0;
this.summonCounter++;
let object = this.create();
game.spawn(object, this.object.x, this.object.y);
this.onSummon(object);
}
}
}
interface SpellCounter {
total: number;
hits: number;
}
export class HitStreak extends Behaviour {
static counters: Record<number, SpellCounter> = {};
hit = false;
counter: SpellCounter = undefined!;
onCollision = () => this.hit = true;
onAdded = () => {
this.counter = HitStreak.counters[this.object.groupId] ||= { total: 0, hits: 0 };
this.counter.total++;
}
onRemoved() {
if (this.hit) this.counter.hits++;
if (--this.counter.total) return;
if (this.counter.hits) {
game.streak = clamp(game.streak + 1, 0, MAX_STREAK);
} else {
game.streak = 0;
}
}
}
export class Invulnerable extends Behaviour {
sprite = sprites.status_shielded;
onDamage(damage: Dmg): void {
if (damage.amount > 0) damage.amount = 0;
}
}
export class Frozen extends Behaviour {
freezeTimer = 10;
onUpdate() {
if (this.freezeTimer-- <= 0) {
this.object.removeBehaviour(this);
}
return true;
}
}
export class LightningStrike extends Behaviour {
onCollision(target: GameObject): void {
let bolts = 3;
for (let i = 0; i < bolts; i++) {
let bolt = LightningSpell();
//bolt.addBehaviour(new Seeking(bolt));
bolt.vy = -200;
bolt.vx = randomInt(20) - 10;
bolt.y = clamp(50 + randomInt(100), 0, game.stage.ceiling - 10);
bolt.x = target.x + randomInt(50) - 25;
game.spawn(bolt);
}
}
}