diff --git a/public/sprites/powerups.png b/public/sprites/powerups.png new file mode 100644 index 0000000..5e9df4d Binary files /dev/null and b/public/sprites/powerups.png differ diff --git a/src/com/Powerup.ts b/src/com/Powerup.ts new file mode 100644 index 0000000..2060abc --- /dev/null +++ b/src/com/Powerup.ts @@ -0,0 +1,37 @@ +import { k } from '../kaboom'; +import { Comp } from 'kaboom'; + +const { + anchor, + area, + destroy, + lifespan, + rand, + sprite, +} = k; + +export interface PowerupComp extends Comp { + timeout: number, + type: 0 | 1 | 2; + points: ()=>number; +} + +export function powerup(): PowerupComp { + return { + id: "powerup", + require: ["pos"], + timeout: 20, + type: Math.floor(rand() * 3) as 0 | 1 | 2, // Random int between 0-2 + points: ()=>this.type*100 + Math.floor(rand()*99), + add() { + this.use(sprite('powerups', { frame: this.type })); + this.use(anchor('center')); + this.use(area({ scale: 0.6 })); + this.use(lifespan(this.timeout)); + this.onCollide('player', player=>{ + player.saltQty+=1; + destroy(this); + }); + }, + }; +} \ No newline at end of file diff --git a/src/main.ts b/src/main.ts index 24d5489..30d59bd 100644 --- a/src/main.ts +++ b/src/main.ts @@ -53,6 +53,7 @@ loadSprite("floor-stair-green", "floor-stair-green.png", { sliceX: 2 }); loadSprite("head-h", "head-h.png"); loadSprite("head-p", "head-p.png"); loadSprite("plate", "plate.png", { sliceX: 2 }); +loadSprite("powerups", "powerups.png", { sliceX: 3 }); loadSprite("salt", "salt.png", { sliceX: 4 }); loadSprite("stair-blue", "stair-blue.png", { sliceX: 2 }); loadSprite("stair-green", "stair-green.png", { sliceX: 2 }); diff --git a/src/scenes/GameScene.ts b/src/scenes/GameScene.ts index 4a23af9..c499bf2 100644 --- a/src/scenes/GameScene.ts +++ b/src/scenes/GameScene.ts @@ -5,6 +5,7 @@ import { SpriteComp, } from 'kaboom'; import { k } from '../kaboom'; +import { powerup } from '../com/Powerup'; import { peter, PeterComp } from '../com/Peter'; import { canSalt, SaltComp } from '../com/Salt'; import { canWalk, WalkComp, WalkableObj } from '../com/Walk'; @@ -16,9 +17,11 @@ const { anchor, area, fixed, + rand, Rect, sprite, vec2, + wait, z, } = k; @@ -73,8 +76,13 @@ const levelConf: LevelOpt = { peter(), canSalt(), canWalk(), + z(10), "player", ], + '$': () => [ + powerup(), + z(5), + ], }, }; @@ -114,4 +122,13 @@ export default function(levelNumber = 0) { // Player setup const player: GameObj = level.spawn("p", 16, 21.625); player.setObjects({ floors, stairs, stairtops }); + + // Powerups + function waitSpawnPowerup() { + // Wait 20-60 seconds + wait(rand()*40+20, ()=>{ + level.spawn('$', 16, 13.5).onDestroy(waitSpawnPowerup) + }); + } + waitSpawnPowerup(); }