File size: 670 Bytes
5c0862e
29a88f8
 
5c0862e
 
29a88f8
5c0862e
 
 
 
 
29a88f8
5c0862e
29a88f8
 
 
 
 
 
5c0862e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**
 * Lecture des sons d'unités (mouvement, mort, tir) avec max 3 pistes simultanées.
 * Si la limite est atteinte, le son est ignoré (pas de file d'attente).
 */

export const MAX_CONCURRENT_UNIT_SOUNDS = 5;

let activeCount = 0;

export function playUnitSound(baseUrl: string, unit: string, kind: 'move_ack' | 'death' | 'fire'): void {
  if (typeof window === 'undefined') return;
  if (activeCount >= MAX_CONCURRENT_UNIT_SOUNDS) return;

  const url = `${baseUrl}/sounds/units/${unit}/${kind}.mp3`;
  activeCount += 1;
  const audio = new Audio(url);
  audio.onended = () => { activeCount -= 1; };
  audio.onerror = () => { activeCount -= 1; };
  audio.play();
}