Skip to content

Commit

Permalink
feat: Add salt component
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuacurtiss committed May 30, 2024
1 parent ae82044 commit 9be18ac
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 11 deletions.
28 changes: 18 additions & 10 deletions src/com/Peter.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { k } from '../kaboom';
import { Comp } from 'kaboom';
import { Comp, Vec2 } from 'kaboom';

const {
isKeyDown,
lifespan,
onKeyPress,
vec2,
} = k;

Expand All @@ -12,24 +13,31 @@ export interface PeterComp extends Comp {
isAlive: boolean;
freeze: Function;
die: Function;
setAnim: (dir: Vec2) => void;
}

export function peter(): PeterComp {
return {
id: "peter",
require: ["area", "sprite", "can-walk"],
require: ["area", "sprite", "can-salt", "can-walk"],
isFrozen: false,
isAlive: true,
add() {
this.onDirChange(newdir=>{
let anim = 'idle';
let flipX = newdir.x>0;
if (newdir.y<0) anim = 'up';
else if (newdir.y>0) anim = 'down';
else if (newdir.x) anim = 'walk';
if (this.curAnim() !== anim) this.play(anim);
this.flipX = flipX;
onKeyPress(key=>{
if (key==='space') {
this.throwSalt();
}
});
this.onDirChange(this.setAnim);
},
setAnim(newdir) {
let anim = 'idle';
let flipX = newdir.x>0;
if (newdir.y<0) anim = 'up';
else if (newdir.y>0) anim = 'down';
else if (newdir.x) anim = 'walk';
if (this.curAnim() !== anim) this.play(anim);
this.flipX = flipX;
},
update() {
if (this.isFrozen) {
Expand Down
64 changes: 64 additions & 0 deletions src/com/Salt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { k } from '../kaboom';
import { Comp, Vec2 } from 'kaboom';

const {
add,
anchor,
area,
pos,
sprite,
vec2,
wait,
} = k;

export interface SaltComp extends Comp {
saltDelay: number;
saltDir: Vec2;
saltQty: number;
throwSalt: ()=>void;
}

export function canSalt(): SaltComp {
return {
id: "can-salt",
require: ["peter", "can-walk"],
saltDelay: 0.6,
saltDir: vec2(1, 0),
saltQty: 5,
add() {
this.onDirChange(newdir=>{
// Set last MOVING direction, never the idle state.
if (!newdir.isZero()) this.saltDir = newdir;
});
},
throwSalt() {
if (!this.saltQty) {
// TODO: Play empty salt sound
return;
}
this.saltQty-=1;
const saltPos = this.pos.sub(0, this.height/4).add(this.saltDir.x*this.width*0.75, this.saltDir.y*this.height),
saltTop = add([
sprite('salt', { frame: 1, flipX: this.saltDir.x>0 }),
anchor('center'),
pos(saltPos),
area({collisionIgnore: ['player']}),
]),
saltBottom = add([
sprite('salt', { frame: 2, flipX: this.saltDir.x>0 }),
anchor('center'),
pos(saltPos.add(0, saltTop.height)),
area({collisionIgnore: ['player']}),
]);
// TODO: Play salt sound
const anim = this.saltDir.x ? 'throw' : this.saltDir.y<0 ? 'throw-up' : 'throw-down';
this.flipX = this.saltDir.x>0;
this.play(anim);
wait(this.saltDelay, ()=>{
this.setAnim(this.dir);
saltTop.destroy();
saltBottom.destroy();
});
},
};
};
4 changes: 3 additions & 1 deletion src/scenes/GameScene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
} from 'kaboom';
import { k } from '../kaboom';
import { peter, PeterComp } from '../com/Peter';
import { canSalt, SaltComp } from '../com/Salt';
import { canWalk, WalkComp, WalkableObj } from '../com/Walk';
import LEVELS from '../levels.json';

Expand Down Expand Up @@ -70,6 +71,7 @@ const levelConf: LevelOpt = {
area({ shape: new Rect(vec2(0), 8, 15) }),
anchor('center'),
peter(),
canSalt(),
canWalk(),
"player",
],
Expand Down Expand Up @@ -110,6 +112,6 @@ export default function(levelNumber = 0) {
});

// Player setup
const player: GameObj<PeterComp & PosComp & SpriteComp & WalkComp> = level.spawn("p", 16, 21.625);
const player: GameObj<PeterComp & PosComp & SpriteComp & SaltComp & WalkComp> = level.spawn("p", 16, 21.625);
player.setObjects({ floors, stairs, stairtops });
}

0 comments on commit 9be18ac

Please sign in to comment.