54 lines
1.7 KiB
C#
54 lines
1.7 KiB
C#
using System.Collections.Generic;
|
|
using Code.Infrastructure.EventBus;
|
|
using UnityEngine;
|
|
|
|
namespace Code.Infrastructure.Audio
|
|
{
|
|
public interface IAudioService
|
|
{
|
|
AudioClip GetClip(AudioNames groupName, int id);
|
|
void PlayTest();
|
|
}
|
|
|
|
public class AudioService : IAudioClipsEventHandle, IAudioService
|
|
{
|
|
private EventBus.EventBus _eventBus;
|
|
private IAudioFactory _audioFactory;
|
|
private IAudioSourcePool _pool;
|
|
private Dictionary<AudioNames, Dictionary<int, AudioClip>> _audioClips = new();
|
|
|
|
public AudioService(EventBus.EventBus eventBus, IAudioFactory audioFactory, IAudioSourcePool pool)
|
|
{
|
|
_eventBus = eventBus;
|
|
_audioFactory = audioFactory;
|
|
_pool = pool;
|
|
|
|
_eventBus.Subscribe(this);
|
|
|
|
for (int i = 0; i < typeof(AudioNames).GetEnumValues().Length; i++)
|
|
_audioClips.Add((AudioNames)i, new Dictionary<int, AudioClip>());
|
|
}
|
|
|
|
public void PlayTest()
|
|
{
|
|
_pool.GetSource(AudioSourceType.NoBlend).PlayOneShot(GetClip(AudioNames.UISounds, 0));
|
|
}
|
|
public AudioClip GetClip(AudioNames groupName, int id) => _audioClips[groupName][id];
|
|
|
|
public void AudioClipsLoaded(AudioNames groupname, Dictionary<int, AudioClip> clips)
|
|
{
|
|
_audioClips[groupname] = clips;
|
|
_eventBus.RaiseEvent<IReadyToPlayHandle>(x=> x.OnReadyToPlay(groupname));
|
|
}
|
|
|
|
public void AudioClipsUnLoaded(AudioNames groupName)
|
|
{
|
|
_audioClips[groupName] = null;
|
|
}
|
|
}
|
|
|
|
public interface IReadyToPlayHandle : IGlobalSubscriber
|
|
{
|
|
void OnReadyToPlay(AudioNames groupName);
|
|
}
|
|
} |