diff --git a/src/kaboom.ts b/src/kaboom.ts index 9d9621e1..1e095da8 100644 --- a/src/kaboom.ts +++ b/src/kaboom.ts @@ -13,36 +13,37 @@ import { } from "./assets"; import { - MAX_TEXT_CACHE_SIZE, - DEF_VERT, - DEF_FRAG, - VERTEX_FORMAT, - MAX_BATCHED_VERTS, - MAX_BATCHED_INDICES, - SPRITE_ATLAS_WIDTH, - SPRITE_ATLAS_HEIGHT, - DEF_FONT_FILTER, - DEF_TEXT_CACHE_SIZE, ASCII_CHARS, - DEF_FONT, - VERT_TEMPLATE, - FRAG_TEMPLATE, BG_GRID_SIZE, - DEF_ANCHOR, - UV_PAD, - FONT_ATLAS_WIDTH, - FONT_ATLAS_HEIGHT, - LOG_MAX, COMP_DESC, + COMP_DESC, COMP_EVENTS, - DEF_TEXT_SIZE, - DEF_HASH_GRID_SIZE, DBG_FONT, - LOG_TIME, - TEXT_STYLE_RE, - DEF_OFFSCREEN_DIS, + DEF_ANCHOR, + DEF_FONT, + DEF_FONT_FILTER, + DEF_FRAG, + DEF_HASH_GRID_SIZE, DEF_JUMP_FORCE, + DEF_OFFSCREEN_DIS, + DEF_TEXT_CACHE_SIZE, + DEF_TEXT_SIZE, + DEF_VERT, + FONT_ATLAS_HEIGHT, + FONT_ATLAS_WIDTH, + FRAG_TEMPLATE, + LOG_MAX, + LOG_TIME, + MAX_BATCHED_INDICES, + MAX_BATCHED_VERTS, + MAX_TEXT_CACHE_SIZE, MAX_VEL, -} from "./constants" + SPRITE_ATLAS_HEIGHT, + SPRITE_ATLAS_WIDTH, + TEXT_STYLE_RE, + UV_PAD, + VERT_TEMPLATE, + VERTEX_FORMAT, +} from "./constants"; import { chance, @@ -288,7 +289,8 @@ export default (gopt: KaboomOpt = {}): KaboomCtx => { } // create a if user didn't provide one - const canvas = gopt.canvas ?? root.appendChild(document.createElement("canvas")) + const canvas = gopt.canvas + ?? root.appendChild(document.createElement("canvas")); // global pixel scale const gscale = gopt.scale ?? 1; @@ -3694,7 +3696,11 @@ export default (gopt: KaboomOpt = {}): KaboomCtx => { return col && col.hasOverlap(); }, - onClick(this: GameObj, f: () => void, btn: MouseButton = "left"): EventController { + onClick( + this: GameObj, + f: () => void, + btn: MouseButton = "left", + ): EventController { const e = app.onMousePress(btn, () => { if (this.isHovering()) { f(); @@ -4346,15 +4352,26 @@ export default (gopt: KaboomOpt = {}): KaboomCtx => { this: GameObj, time: number, action?: () => void, - ): TimerController { - const actions = []; - if (action) actions.push(action); - let t = 0; + ): EventController { + return this.loop(time, action, 1); + }, + loop( + this: GameObj, + time: number, + action: () => void, + count: number = -1, + ): EventController { + let t: number = 0; const ev = this.onUpdate(() => { t += dt(); if (t >= time) { - actions.forEach((f) => f()); - ev.cancel(); + if (action){ + action(); + } + t -= time; + if (count != -1 && --count === 0) { + ev.cancel(); + } } }); return { @@ -4365,31 +4382,6 @@ export default (gopt: KaboomOpt = {}): KaboomCtx => { ev.paused = p; }, cancel: ev.cancel, - onEnd(action) { - actions.push(action); - }, - then(action) { - this.onEnd(action); - return this; - }, - }; - }, - loop(t: number, action: () => void): EventController { - let curTimer: null | TimerController = null; - const newAction = () => { - // TODO: should f be execute right away as loop() is called? - curTimer = this.wait(t, newAction); - action(); - }; - curTimer = this.wait(0, newAction); - return { - get paused() { - return curTimer.paused; - }, - set paused(p) { - curTimer.paused = p; - }, - cancel: () => curTimer.cancel(), }; }, tween( @@ -4762,12 +4754,18 @@ export default (gopt: KaboomOpt = {}): KaboomCtx => { const fade = opt.fade ?? 0; return { id: "lifespan", - require: [ "opacity" ], + require: ["opacity"], async add(this: GameObj) { await wait(time); - this.opacity = this.opacity ?? 1 + this.opacity = this.opacity ?? 1; if (fade > 0) { - await tween(this.opacity, 0, fade, (a) => this.opacity = a, easings.linear); + await tween( + this.opacity, + 0, + fade, + (a) => this.opacity = a, + easings.linear, + ); } this.destroy(); }, diff --git a/src/types.ts b/src/types.ts index 5066c9c2..51d933da 100644 --- a/src/types.ts +++ b/src/types.ts @@ -5335,13 +5335,22 @@ export interface TimerComp extends Comp { /** * Run the callback after n seconds. */ - wait(time: number, action?: () => void): TimerController; + wait( + this: GameObj, + time: number, + action?: () => void, + ): EventController; /** * Run the callback every n seconds. * * @since v3000.0 */ - loop(time: number, action: () => void): EventController; + loop( + this: GameObj, + time: number, + action: () => void, + count: number, + ): EventController; /** * Tweeeeen! Note that this doesn't specifically mean tweening on this object's property, this just registers the timer on this object, so the tween will cancel with the object gets destroyed, or paused when obj.paused is true. *