Skip to content

Commit 5d1ec5e

Browse files
committed
refactor: separate health from kaboom.ts
1 parent b1d6f0f commit 5d1ec5e

File tree

2 files changed

+95
-81
lines changed

2 files changed

+95
-81
lines changed

src/components/health.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { EventController, GameObj, HealthComp } from "../types";
2+
3+
export const health = (hp: number, maxHP?: number): HealthComp => {
4+
if (hp == null) {
5+
throw new Error("health() requires the initial amount of hp");
6+
}
7+
return {
8+
id: "health",
9+
hurt(this: GameObj, n: number = 1) {
10+
this.setHP(hp - n);
11+
this.trigger("hurt", n);
12+
},
13+
heal(this: GameObj, n: number = 1) {
14+
const origHP = hp;
15+
this.setHP(hp + n);
16+
this.trigger("heal", hp - origHP);
17+
},
18+
hp(): number {
19+
return hp;
20+
},
21+
maxHP(): number | null {
22+
return maxHP ?? null;
23+
},
24+
setMaxHP(n: number): void {
25+
maxHP = n;
26+
},
27+
setHP(this: GameObj, n: number) {
28+
hp = maxHP ? Math.min(maxHP, n) : n;
29+
if (hp <= 0) {
30+
this.trigger("death");
31+
}
32+
},
33+
onHurt(
34+
this: GameObj,
35+
action: (amount?: number) => void,
36+
): EventController {
37+
return this.on("hurt", action);
38+
},
39+
onHeal(
40+
this: GameObj,
41+
action: (amount?: number) => void,
42+
): EventController {
43+
return this.on("heal", action);
44+
},
45+
onDeath(this: GameObj, action: () => void): EventController {
46+
return this.on("death", action);
47+
},
48+
inspect() {
49+
return `${hp}`;
50+
},
51+
};
52+
};

src/kaboom.ts

Lines changed: 43 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -13,36 +13,37 @@ import {
1313
} from "./assets";
1414

1515
import {
16-
MAX_TEXT_CACHE_SIZE,
17-
DEF_VERT,
18-
DEF_FRAG,
19-
VERTEX_FORMAT,
20-
MAX_BATCHED_VERTS,
21-
MAX_BATCHED_INDICES,
22-
SPRITE_ATLAS_WIDTH,
23-
SPRITE_ATLAS_HEIGHT,
24-
DEF_FONT_FILTER,
25-
DEF_TEXT_CACHE_SIZE,
2616
ASCII_CHARS,
27-
DEF_FONT,
28-
VERT_TEMPLATE,
29-
FRAG_TEMPLATE,
3017
BG_GRID_SIZE,
31-
DEF_ANCHOR,
32-
UV_PAD,
33-
FONT_ATLAS_WIDTH,
34-
FONT_ATLAS_HEIGHT,
35-
LOG_MAX, COMP_DESC,
18+
COMP_DESC,
3619
COMP_EVENTS,
37-
DEF_TEXT_SIZE,
38-
DEF_HASH_GRID_SIZE,
3920
DBG_FONT,
40-
LOG_TIME,
41-
TEXT_STYLE_RE,
42-
DEF_OFFSCREEN_DIS,
21+
DEF_ANCHOR,
22+
DEF_FONT,
23+
DEF_FONT_FILTER,
24+
DEF_FRAG,
25+
DEF_HASH_GRID_SIZE,
4326
DEF_JUMP_FORCE,
27+
DEF_OFFSCREEN_DIS,
28+
DEF_TEXT_CACHE_SIZE,
29+
DEF_TEXT_SIZE,
30+
DEF_VERT,
31+
FONT_ATLAS_HEIGHT,
32+
FONT_ATLAS_WIDTH,
33+
FRAG_TEMPLATE,
34+
LOG_MAX,
35+
LOG_TIME,
36+
MAX_BATCHED_INDICES,
37+
MAX_BATCHED_VERTS,
38+
MAX_TEXT_CACHE_SIZE,
4439
MAX_VEL,
45-
} from "./constants"
40+
SPRITE_ATLAS_HEIGHT,
41+
SPRITE_ATLAS_WIDTH,
42+
TEXT_STYLE_RE,
43+
UV_PAD,
44+
VERT_TEMPLATE,
45+
VERTEX_FORMAT,
46+
} from "./constants";
4647

4748
import {
4849
chance,
@@ -221,6 +222,7 @@ import beanSpriteSrc from "./assets/bean.png";
221222
import boomSpriteSrc from "./assets/boom.png";
222223
import burpSoundSrc from "./assets/burp.mp3";
223224
import kaSpriteSrc from "./assets/ka.png";
225+
import { health } from "./components/health";
224226

225227
interface SpriteCurAnim {
226228
name: string;
@@ -288,7 +290,8 @@ export default (gopt: KaboomOpt = {}): KaboomCtx => {
288290
}
289291

290292
// create a <canvas> if user didn't provide one
291-
const canvas = gopt.canvas ?? root.appendChild(document.createElement("canvas"))
293+
const canvas = gopt.canvas
294+
?? root.appendChild(document.createElement("canvas"));
292295

293296
// global pixel scale
294297
const gscale = gopt.scale ?? 1;
@@ -3694,7 +3697,11 @@ export default (gopt: KaboomOpt = {}): KaboomCtx => {
36943697
return col && col.hasOverlap();
36953698
},
36963699

3697-
onClick(this: GameObj<AreaComp>, f: () => void, btn: MouseButton = "left"): EventController {
3700+
onClick(
3701+
this: GameObj<AreaComp>,
3702+
f: () => void,
3703+
btn: MouseButton = "left",
3704+
): EventController {
36983705
const e = app.onMousePress(btn, () => {
36993706
if (this.isHovering()) {
37003707
f();
@@ -4704,70 +4711,25 @@ export default (gopt: KaboomOpt = {}): KaboomCtx => {
47044711
};
47054712
}
47064713

4707-
function health(hp: number, maxHP?: number): HealthComp {
4708-
if (hp == null) {
4709-
throw new Error("health() requires the initial amount of hp");
4710-
}
4711-
return {
4712-
id: "health",
4713-
hurt(this: GameObj, n: number = 1) {
4714-
this.setHP(hp - n);
4715-
this.trigger("hurt", n);
4716-
},
4717-
heal(this: GameObj, n: number = 1) {
4718-
const origHP = hp;
4719-
this.setHP(hp + n);
4720-
this.trigger("heal", hp - origHP);
4721-
},
4722-
hp(): number {
4723-
return hp;
4724-
},
4725-
maxHP(): number | null {
4726-
return maxHP ?? null;
4727-
},
4728-
setMaxHP(n: number): void {
4729-
maxHP = n;
4730-
},
4731-
setHP(this: GameObj, n: number) {
4732-
hp = maxHP ? Math.min(maxHP, n) : n;
4733-
if (hp <= 0) {
4734-
this.trigger("death");
4735-
}
4736-
},
4737-
onHurt(
4738-
this: GameObj,
4739-
action: (amount?: number) => void,
4740-
): EventController {
4741-
return this.on("hurt", action);
4742-
},
4743-
onHeal(
4744-
this: GameObj,
4745-
action: (amount?: number) => void,
4746-
): EventController {
4747-
return this.on("heal", action);
4748-
},
4749-
onDeath(this: GameObj, action: () => void): EventController {
4750-
return this.on("death", action);
4751-
},
4752-
inspect() {
4753-
return `${hp}`;
4754-
},
4755-
};
4756-
}
4757-
47584714
function lifespan(time: number, opt: LifespanCompOpt = {}): EmptyComp {
47594715
if (time == null) {
47604716
throw new Error("lifespan() requires time");
47614717
}
47624718
const fade = opt.fade ?? 0;
47634719
return {
47644720
id: "lifespan",
4765-
require: [ "opacity" ],
4721+
require: ["opacity"],
47664722
async add(this: GameObj<OpacityComp>) {
47674723
await wait(time);
4768-
this.opacity = this.opacity ?? 1
4724+
this.opacity = this.opacity ?? 1;
47694725
if (fade > 0) {
4770-
await tween(this.opacity, 0, fade, (a) => this.opacity = a, easings.linear);
4726+
await tween(
4727+
this.opacity,
4728+
0,
4729+
fade,
4730+
(a) => this.opacity = a,
4731+
easings.linear,
4732+
);
47714733
}
47724734
this.destroy();
47734735
},
@@ -6668,7 +6630,7 @@ export default (gopt: KaboomOpt = {}): KaboomCtx => {
66686630
timer,
66696631
fixed,
66706632
stay,
6671-
health,
6633+
health: health,
66726634
lifespan,
66736635
z,
66746636
move,

0 commit comments

Comments
 (0)