Skip to content

Commit

Permalink
feat: Add many sounds
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuacurtiss committed Jun 5, 2024
1 parent c3cd922 commit 0f37fb9
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 18 deletions.
8 changes: 3 additions & 5 deletions src/abilities/Salt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const {
add,
anchor,
area,
play,
pos,
sprite,
vec2,
Expand Down Expand Up @@ -44,10 +45,7 @@ export function canSalt(): SaltComp {
},
throwSalt() {
if (this.isFrozen || !this.isAlive) return;
if (!this.salt) {
// TODO: Play empty salt sound
return;
}
if (!this.salt) return;
this.salt-=1;
const saltPos = this.pos.sub(0, this.height/4).add(saltDir.x*this.width*0.75, saltDir.y*this.height),
saltTop = add([
Expand All @@ -66,7 +64,7 @@ export function canSalt(): SaltComp {
z(50),
"salt",
]);
// TODO: Play salt sound
play('pepper');
const anim = saltDir.x ? 'throw' : saltDir.y<0 ? 'throw-up' : 'throw-down';
this.flipX = saltDir.x>0;
this.play(anim);
Expand Down
10 changes: 10 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,21 @@ import StartScene from './scenes/StartScene';

const {
loadRoot,
loadSound,
loadSprite,
go,
scene,
} = k;

loadRoot("sfx/");
loadSound("burger_drop", "burger_drop.ogg");
loadSound("die", "die.ogg");
loadSound("item", "item.ogg");
loadSound("music", "music.ogg");
loadSound("pepper", "pepper.ogg");
loadSound("powerup", "powerup.ogg");
loadSound("start", "start.ogg");
loadSound("win", "win.ogg");
loadRoot("sprites/");
loadSprite("burger", "burger.png", { sliceX: 28 });
loadSprite("enemies", "enemies.png", {
Expand Down
13 changes: 8 additions & 5 deletions src/objects/Peter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const {
add,
anchor,
area,
play,
pos,
Rect,
sprite,
Expand Down Expand Up @@ -150,24 +151,26 @@ export function peter(options: Partial<PeterCompOpt> = {}): PeterComp {
},
async win() {
this.freeze();
this.level+=1;
this.stop();
for (let i=0 ; i<10 ; i+=1) {
this.level+=1;
this.trigger(ON_WIN, this);
play('win');
for (let i=0 ; i<8 ; i+=1) {
this.play(i % 2 ? 'celebrate' : 'idle');
await wait(0.4);
await wait(0.38);
}
this.trigger(ON_WIN, this);
},
async die() {
this.isAlive = false;
this.lives-=1;
this.stop();
this.frame = 14;
this.trigger(ON_DIE, this);
await wait(1);
play('die');
this.play("fall");
await wait(0.55);
this.play("dead");
this.trigger(ON_DIE, this);
},
};
}
3 changes: 3 additions & 0 deletions src/objects/Powerup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const {
area,
color,
lifespan,
play,
pos,
randi,
sprite,
Expand Down Expand Up @@ -69,7 +70,9 @@ export function powerup(options: Partial<PowerupCompOpt> = {}): PowerupComp {
return (this.type+1)*500 + pointsRandomness;
},
add() {
play('item');
this.onCollide('player', player=>{
play('powerup');
player.salt+=1;
player.score+=this.points;
const scoreIndicator = add([
Expand Down
28 changes: 20 additions & 8 deletions src/scenes/GameScene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ const {
go,
isKeyDown,
onKeyPress,
on,
play,
pos,
rect,
sprite,
Expand Down Expand Up @@ -89,6 +91,9 @@ export default function(options: Partial<GameSceneOpt>) {
const opt = Object.assign({}, GameSceneOptDefaults, options);
const player = opt.players[opt.currentPlayer];

// Music Setup
const music = play('music', { paused: true, loop: true, volume: 0.6 });

// UI Setup
const ui = add([fixed(), z(100)]);
ui.add([
Expand Down Expand Up @@ -158,8 +163,8 @@ export default function(options: Partial<GameSceneOpt>) {
if (++currentPlayer>=players.length) currentPlayer=0;
} while (someoneIsAlive && players[currentPlayer].lives<0);
}
if (scene==='gameover') wait(3, ()=>go(scene, deadPlayer, { ...opt, currentPlayer }));
else wait(3, ()=>go(scene, { ...opt, currentPlayer }));
if (scene==='gameover') go(scene, deadPlayer, { ...opt, currentPlayer });
else go(scene, { ...opt, currentPlayer });
}

// Player setup
Expand All @@ -170,10 +175,14 @@ export default function(options: Partial<GameSceneOpt>) {
player.setAnim(vec2(0));
player.isAlive = true;
player.isFrozen = true;
if (!player.isInitialized) {
player.on(ON_DIE, ()=>goNextScene('die'));
player.on(ON_WIN, ()=>goNextScene('win'));
}
on(ON_DIE, 'player', ()=>{
wait(0.5, ()=>music.stop());
wait(5, ()=>goNextScene('die'));
});
on(ON_WIN, 'player', ()=>{
music.stop();
wait(5, ()=>goNextScene('win'));
});

// Controls
onKeyPress(player.controls.keyboard.action, ()=>player.action());
Expand Down Expand Up @@ -204,9 +213,12 @@ export default function(options: Partial<GameSceneOpt>) {
]);
wait(5, ()=>{
dlg.destroy();
wait(player.isInitialized ? 0.5 : 5, ()=>player.isFrozen = false);
wait(player.isInitialized ? 0.25 : 3, ()=>{
player.isFrozen = false;
music.play();
});
if (!player.isInitialized) {
// TODO: Play first-time music
play('start');
player.isInitialized = true;
}
})
Expand Down

0 comments on commit 0f37fb9

Please sign in to comment.