diff --git a/public/sprites/star.png b/public/sprites/star.png index 1e3ac58..d0b8ea9 100644 Binary files a/public/sprites/star.png and b/public/sprites/star.png differ diff --git a/src/gameobjects/index.ts b/src/gameobjects/index.ts index b2f78f8..2f141be 100644 --- a/src/gameobjects/index.ts +++ b/src/gameobjects/index.ts @@ -2,4 +2,5 @@ export * from './buttons'; export * from './keys'; export * from './pause'; export * from './score'; +export * from './star'; export * from './timer'; diff --git a/src/gameobjects/keys.ts b/src/gameobjects/keys.ts index 4179cce..605cc83 100644 --- a/src/gameobjects/keys.ts +++ b/src/gameobjects/keys.ts @@ -1,15 +1,9 @@ import type { AudioPlay } from 'kaboom'; -import { - DirectionKey, - directions, - Key, - Sound, - Sprite, - Tag, -} from '../constants'; +import { DirectionKey, directions, Key, Sound, Tag } from '../constants'; import { getPosition } from '../helpers'; import { addScore } from './score'; +import { addStar } from './star'; // const LONG_PRESS_MILLISECONDS = 500; @@ -58,14 +52,7 @@ export function addKeys() { (directions as DirectionKey[]).forEach((key) => { onCollideUpdate(key, Tag.direction, () => { if (isKeyPressed(key)) { - add([ - sprite(Sprite.star), - pos(center()), - anchor('center'), - scale(0.4), - lifespan(0.03, { fade: 0.5 }), - opacity(1), - ]); + addStar(); incrementScore(); } }); diff --git a/src/gameobjects/star.ts b/src/gameobjects/star.ts new file mode 100644 index 0000000..400aa21 --- /dev/null +++ b/src/gameobjects/star.ts @@ -0,0 +1,39 @@ +import type { GameObj, ScaleComp } from 'kaboom'; + +import { Sprite } from '../constants'; + +export function addStar(position = center()) { + const kaboom = add([pos(position), stay()]); + + const speed = 1 * 5; + const size = 1; + + const ka = kaboom.add([ + sprite(Sprite.star), + scale(0), + anchor('center'), + timer(), + opacity(0.5), + ]); + + ka.wait(0.4 / speed, () => ka.use(boom(speed, size))); + ka.onDestroy(() => kaboom.destroy()); + + return kaboom; +} + +function boom(speed: number = 2, size: number = 1) { + let time = 0; + + return { + require: ['scale'], + update(this: GameObj) { + const s = Math.sin(time * speed) * size; + if (s < 0) { + this.destroy(); + } + this.scale = vec2(s); + time += dt(); + }, + }; +}