@@ -13,36 +13,37 @@ import {
13
13
} from "./assets" ;
14
14
15
15
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 ,
26
16
ASCII_CHARS ,
27
- DEF_FONT ,
28
- VERT_TEMPLATE ,
29
- FRAG_TEMPLATE ,
30
17
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 ,
36
19
COMP_EVENTS ,
37
- DEF_TEXT_SIZE ,
38
- DEF_HASH_GRID_SIZE ,
39
20
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 ,
43
26
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 ,
44
39
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" ;
46
47
47
48
import {
48
49
chance ,
@@ -221,6 +222,7 @@ import beanSpriteSrc from "./assets/bean.png";
221
222
import boomSpriteSrc from "./assets/boom.png" ;
222
223
import burpSoundSrc from "./assets/burp.mp3" ;
223
224
import kaSpriteSrc from "./assets/ka.png" ;
225
+ import { health } from "./components/health" ;
224
226
225
227
interface SpriteCurAnim {
226
228
name : string ;
@@ -288,7 +290,8 @@ export default (gopt: KaboomOpt = {}): KaboomCtx => {
288
290
}
289
291
290
292
// 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" ) ) ;
292
295
293
296
// global pixel scale
294
297
const gscale = gopt . scale ?? 1 ;
@@ -3694,7 +3697,11 @@ export default (gopt: KaboomOpt = {}): KaboomCtx => {
3694
3697
return col && col . hasOverlap ( ) ;
3695
3698
} ,
3696
3699
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 {
3698
3705
const e = app . onMousePress ( btn , ( ) => {
3699
3706
if ( this . isHovering ( ) ) {
3700
3707
f ( ) ;
@@ -4704,70 +4711,25 @@ export default (gopt: KaboomOpt = {}): KaboomCtx => {
4704
4711
} ;
4705
4712
}
4706
4713
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
-
4758
4714
function lifespan ( time : number , opt : LifespanCompOpt = { } ) : EmptyComp {
4759
4715
if ( time == null ) {
4760
4716
throw new Error ( "lifespan() requires time" ) ;
4761
4717
}
4762
4718
const fade = opt . fade ?? 0 ;
4763
4719
return {
4764
4720
id : "lifespan" ,
4765
- require : [ "opacity" ] ,
4721
+ require : [ "opacity" ] ,
4766
4722
async add ( this : GameObj < OpacityComp > ) {
4767
4723
await wait ( time ) ;
4768
- this . opacity = this . opacity ?? 1
4724
+ this . opacity = this . opacity ?? 1 ;
4769
4725
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
+ ) ;
4771
4733
}
4772
4734
this . destroy ( ) ;
4773
4735
} ,
@@ -6668,7 +6630,7 @@ export default (gopt: KaboomOpt = {}): KaboomCtx => {
6668
6630
timer,
6669
6631
fixed,
6670
6632
stay,
6671
- health,
6633
+ health : health ,
6672
6634
lifespan,
6673
6635
z,
6674
6636
move,
0 commit comments