Skip to content

Commit

Permalink
refactor(gameobjects): improve star
Browse files Browse the repository at this point in the history
  • Loading branch information
remarkablemark committed Feb 18, 2024
1 parent a238c51 commit c32fa65
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 16 deletions.
Binary file modified public/sprites/star.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/gameobjects/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ export * from './buttons';
export * from './keys';
export * from './pause';
export * from './score';
export * from './star';
export * from './timer';
19 changes: 3 additions & 16 deletions src/gameobjects/keys.ts
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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();
}
});
Expand Down
39 changes: 39 additions & 0 deletions src/gameobjects/star.ts
Original file line number Diff line number Diff line change
@@ -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<ScaleComp>) {
const s = Math.sin(time * speed) * size;
if (s < 0) {
this.destroy();
}
this.scale = vec2(s);
time += dt();
},
};
}

0 comments on commit c32fa65

Please sign in to comment.