feat: implement Music and SoundEffects systems for enhanced audio management, including background music and sound effects playback
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
143072f0a0
commit
2213f64e60
16 changed files with 739 additions and 14 deletions
59
src/systems/MusicSystem.ts
Normal file
59
src/systems/MusicSystem.ts
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
import { System } from '../core/System.ts';
|
||||
import { SystemName, ComponentType } 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';
|
||||
|
||||
/**
|
||||
* System responsible for managing background music playback.
|
||||
*/
|
||||
export class MusicSystem extends System {
|
||||
private audioContext: AudioContext | null;
|
||||
|
||||
constructor() {
|
||||
super(SystemName.MUSIC);
|
||||
this.requiredComponents = [ComponentType.MUSIC];
|
||||
this.priority = 5;
|
||||
this.audioContext = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the audio context when system is added to engine.
|
||||
*/
|
||||
init(engine: Engine): void {
|
||||
super.init(engine);
|
||||
}
|
||||
|
||||
/**
|
||||
* Process music entities - currently just ensures audio context exists.
|
||||
*/
|
||||
process(_deltaTime: number, entities: Entity[]): void {
|
||||
entities.forEach((entity) => {
|
||||
const music = entity.getComponent<Music>(ComponentType.MUSIC);
|
||||
if (!music) return;
|
||||
|
||||
if (!this.audioContext) {
|
||||
this.audioContext = new AudioContext();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Get or create the shared audio context.
|
||||
*/
|
||||
getAudioContext(): AudioContext {
|
||||
if (!this.audioContext) {
|
||||
this.audioContext = new AudioContext();
|
||||
}
|
||||
return this.audioContext;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resume audio context (required after user interaction).
|
||||
*/
|
||||
resumeAudioContext(): void {
|
||||
if (this.audioContext && this.audioContext.state === 'suspended') {
|
||||
this.audioContext.resume();
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue