feat: refactor audio management by introducing setup functions for music and sound effects, enhancing modularity and maintainability
All checks were successful
Build and Publish Docker Image / Build and Validate (pull_request) Successful in 10s
All checks were successful
Build and Publish Docker Image / Build and Validate (pull_request) Successful in 10s
This commit is contained in:
parent
2213f64e60
commit
5a24d6a2af
6 changed files with 375 additions and 80 deletions
|
|
@ -1,20 +1,23 @@
|
|||
import { System } from '../core/System.ts';
|
||||
import { SystemName, ComponentType } from '../core/Constants.ts';
|
||||
import { SystemName, ComponentType, GameState } from '../core/Constants.ts';
|
||||
import type { Entity } from '../core/Entity.ts';
|
||||
import type { Engine } from '../core/Engine.ts';
|
||||
import type { Music } from '../components/Music.ts';
|
||||
import type { MenuSystem } from './MenuSystem.ts';
|
||||
|
||||
/**
|
||||
* System responsible for managing background music playback.
|
||||
*/
|
||||
export class MusicSystem extends System {
|
||||
private audioContext: AudioContext | null;
|
||||
private wasPaused: boolean;
|
||||
|
||||
constructor() {
|
||||
super(SystemName.MUSIC);
|
||||
this.requiredComponents = [ComponentType.MUSIC];
|
||||
this.priority = 5;
|
||||
this.audioContext = null;
|
||||
this.wasPaused = false;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -25,9 +28,15 @@ export class MusicSystem extends System {
|
|||
}
|
||||
|
||||
/**
|
||||
* Process music entities - currently just ensures audio context exists.
|
||||
* Process music entities - ensures audio context exists and handles pause/resume.
|
||||
*/
|
||||
process(_deltaTime: number, entities: Entity[]): void {
|
||||
const menuSystem = this.engine.systems.find((s) => s.name === SystemName.MENU) as
|
||||
| MenuSystem
|
||||
| undefined;
|
||||
const gameState = menuSystem ? menuSystem.getGameState() : GameState.PLAYING;
|
||||
const isPaused = gameState === GameState.PAUSED;
|
||||
|
||||
entities.forEach((entity) => {
|
||||
const music = entity.getComponent<Music>(ComponentType.MUSIC);
|
||||
if (!music) return;
|
||||
|
|
@ -35,6 +44,14 @@ export class MusicSystem extends System {
|
|||
if (!this.audioContext) {
|
||||
this.audioContext = new AudioContext();
|
||||
}
|
||||
|
||||
if (isPaused && !this.wasPaused) {
|
||||
music.pause();
|
||||
this.wasPaused = true;
|
||||
} else if (!isPaused && this.wasPaused) {
|
||||
music.resume();
|
||||
this.wasPaused = false;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue