Skip to content

Commit

Permalink
feat: Add powerup component
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuacurtiss committed May 31, 2024
1 parent 9be18ac commit a70fdb2
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 0 deletions.
Binary file added public/sprites/powerups.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
37 changes: 37 additions & 0 deletions src/com/Powerup.ts
Original file line number Diff line number Diff line change
@@ -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);
});
},
};
}
1 change: 1 addition & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down
17 changes: 17 additions & 0 deletions src/scenes/GameScene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -16,9 +17,11 @@ const {
anchor,
area,
fixed,
rand,
Rect,
sprite,
vec2,
wait,
z,
} = k;

Expand Down Expand Up @@ -73,8 +76,13 @@ const levelConf: LevelOpt = {
peter(),
canSalt(),
canWalk(),
z(10),
"player",
],
'$': () => [
powerup(),
z(5),
],
},
};

Expand Down Expand Up @@ -114,4 +122,13 @@ export default function(levelNumber = 0) {
// Player setup
const player: GameObj<PeterComp & PosComp & SpriteComp & SaltComp & WalkComp> = 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();
}

0 comments on commit a70fdb2

Please sign in to comment.