Skip to content

Commit

Permalink
sound engine as module
Browse files Browse the repository at this point in the history
  • Loading branch information
gtanczyk committed Aug 26, 2024
1 parent 5c5d66c commit 9809d09
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 165 deletions.
8 changes: 4 additions & 4 deletions js13k2024/game/src/game-states/gameplay/bonus-logic.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { soundEngine } from '../../sound/sound-engine';
import { playEffect, SoundEffect } from '../../sound/sound-engine';
import { startGameOverAnimation } from './game-logic';
import { GameState, BonusType, ActiveBonus, LevelConfig, Position, Direction, BlasterShot } from './gameplay-types';
import {
Expand Down Expand Up @@ -120,7 +120,7 @@ export const performTeleportation = (gameState: GameState, teleportPoint: Positi
gameState.player.previousPosition = gameState.player.position;
gameState.player.position = destinationPoint.position;
gameState.player.teleportTimestamp = Date.now();
soundEngine.playTeleport();
playEffect(SoundEffect.Teleport);
}
};

Expand Down Expand Up @@ -205,7 +205,7 @@ export const handleBlasterShot = (gameState: GameState, direction: Direction, le
};
gameState.blasterShots.push(shot);
// Play the blaster sound effect
soundEngine.playBlasterSound();
playEffect(SoundEffect.BlasterSound);

// Check if the shot hits monsters along its path
gameState.monsters = gameState.monsters.filter((monster) => {
Expand Down Expand Up @@ -236,4 +236,4 @@ const isMonsterOnBlasterPath = (
(direction === Direction.Right && monsterPos.x >= start.x && monsterPos.x <= end.x))
);
}
};
};
18 changes: 9 additions & 9 deletions js13k2024/game/src/game-states/gameplay/game-logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
isInExplosionRange,
} from './monster-logic';
import { MOVE_ANIMATION_DURATION, OBSTACLE_DESTRUCTION_DURATION } from './render/animation-utils';
import { soundEngine } from '../../sound/sound-engine';
import { playEffect, SoundEffect } from '../../sound/sound-engine';
import { getDirectionFromKey, getNewPosition, isPositionEqual, isPositionOccupied, isValidMove } from './move-utils';
import {
applyBonus,
Expand Down Expand Up @@ -60,11 +60,11 @@ export const doGameUpdate = (direction: Direction, gameState: GameState, levelCo
? { ...obstacle, isDestroying: true, creationTime: Date.now() }
: obstacle,
);
soundEngine.playElectricalDischarge();
playEffect(SoundEffect.ElectricalDischarge);
}

if (isValidMove(newPosition, newGameState, levelConfig)) {
soundEngine.playStep();
playEffect(SoundEffect.Step);
newGameState.player.position = newPosition;
if (Date.now() - newGameState.player.moveTimestamp > MOVE_ANIMATION_DURATION) {
newGameState.player.previousPosition = oldPosition;
Expand All @@ -89,7 +89,7 @@ export const doGameUpdate = (direction: Direction, gameState: GameState, levelCo
// Check for bonuses
const collectedBonus = newGameState.bonuses.find((bonus) => isPositionEqual(bonus.position, newPosition));
if (collectedBonus) {
soundEngine.playBonusCollected();
playEffect(SoundEffect.BonusCollected);
newGameState.bonuses = newGameState.bonuses.filter((bonus) => !isPositionEqual(bonus.position, newPosition));
applyBonus(newGameState, collectedBonus.type);
}
Expand Down Expand Up @@ -143,7 +143,7 @@ export const doGameUpdate = (direction: Direction, gameState: GameState, levelCo
newGameState.timeBombs = newGameState.timeBombs.map((bomb) => ({ ...bomb, timer: bomb.timer - 1 }));
const explodedBombs = newGameState.timeBombs.filter((bomb) => bomb.timer === 0);
if (explodedBombs.length > 0 || newExplosions.length > 0) {
soundEngine.playExplosion();
playEffect(SoundEffect.Explosion);
}
newGameState.explosions = [
...newGameState.explosions,
Expand Down Expand Up @@ -188,7 +188,7 @@ export const doGameUpdate = (direction: Direction, gameState: GameState, levelCo
)
) {
newGameState.obstacles.push(newObstacle);
soundEngine.playElectricalDischarge();
playEffect(SoundEffect.ElectricalDischarge);
}
}
}
Expand All @@ -206,10 +206,10 @@ export const isGameEnding = (gameState: GameState): boolean => {

export const startGameOverAnimation = (gameState: GameState): void => {
gameState.player.isVanishing = true;
soundEngine.playGameOver();
playEffect(SoundEffect.GameOver);
};

const startLevelCompleteAnimation = (gameState: GameState): void => {
gameState.player.isVictorious = true;
soundEngine.playLevelComplete();
};
playEffect(SoundEffect.LevelComplete);
};
6 changes: 3 additions & 3 deletions js13k2024/game/src/game-states/gameplay/level-updater.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { soundEngine } from '../../sound/sound-engine';
import { playEffect, SoundEffect } from '../../sound/sound-engine';
import { GameState, LevelConfig } from './gameplay-types';
import { spawnMonster } from './monster-spawn';

Expand All @@ -8,7 +8,7 @@ export function simpleLevelUpdater(gameState: GameState, levelConfig: LevelConfi
if (newMonster) {
gameState.monsters.push(newMonster);
gameState.monsterSpawnSteps = 0;
soundEngine.playMonsterSpawn();
playEffect(SoundEffect.MonsterSpawn);
}
}
}
}
6 changes: 3 additions & 3 deletions js13k2024/game/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Gameplay } from './game-states/gameplay/gameplay';
import { GameOver } from './game-states/game-over/game-over';
import { LevelComplete } from './game-states/level-complete/level-complete';
import { LevelStory } from './game-states/level-story/level-story';
import { soundEngine } from './sound/sound-engine';
import { playEffect, SoundEffect } from './sound/sound-engine';
import { clearElement, createDiv } from './utils/dom';
import './global-styles.css';

Expand Down Expand Up @@ -170,7 +170,7 @@ export class MonsterStepsApp {
}

private gameComplete() {
soundEngine.playLevelComplete(); // We can reuse the level complete sound for game completion
playEffect(SoundEffect.LevelComplete); // We can reuse the level complete sound for game completion
this.gameState = GameState.GameComplete;
this.renderCurrentState();
}
Expand Down Expand Up @@ -238,4 +238,4 @@ class GameComplete {

return container;
}
}
}
Loading

0 comments on commit 9809d09

Please sign in to comment.