movement refactor, airstepping, landing sound
This commit is contained in:
@@ -7,6 +7,15 @@ using Object = FlaxEngine.Object;
|
||||
|
||||
namespace Game
|
||||
{
|
||||
[Flags]
|
||||
public enum AudioFlags
|
||||
{
|
||||
None = 0,
|
||||
|
||||
/// Avoid replacing the existing playing audio source in this channel.
|
||||
ContinuePlayingExistingSource = 1,
|
||||
}
|
||||
|
||||
public static class AudioManager
|
||||
{
|
||||
private class AudioInfo
|
||||
@@ -15,21 +24,74 @@ namespace Game
|
||||
public int lastAudioPlayed;
|
||||
}
|
||||
|
||||
private class ActorAudioChannels
|
||||
{
|
||||
public Dictionary<int, AudioSource> channelSources;
|
||||
|
||||
public ActorAudioChannels()
|
||||
{
|
||||
channelSources = new Dictionary<int, AudioSource>();
|
||||
}
|
||||
}
|
||||
|
||||
private static Random random = new Random();
|
||||
|
||||
private static Dictionary<string, AudioInfo> cachedAudioInfos = new Dictionary<string, AudioInfo>();
|
||||
|
||||
private static Dictionary<Actor, ActorAudioChannels> actorAudioChannels =
|
||||
new Dictionary<Actor, ActorAudioChannels>();
|
||||
|
||||
public static void PlaySound(string soundName, Actor actor, Vector3 position, float volume = 1f)
|
||||
{
|
||||
PlaySound(soundName, actor, position, volume, Vector2.One);
|
||||
PlaySound(soundName, actor, 0, AudioFlags.None, position, volume, Vector2.One);
|
||||
}
|
||||
|
||||
public static void PlaySound(string soundName, Actor actor, Vector3 position, float volume, Vector2 pitchRange)
|
||||
{
|
||||
PlaySound(soundName, actor, 0, AudioFlags.None, position, volume, pitchRange);
|
||||
}
|
||||
|
||||
public static void PlaySound(string soundName, Actor actor, int channel, Vector3 position, float volume = 1f)
|
||||
{
|
||||
PlaySound(soundName, actor, channel, AudioFlags.None, position, volume, Vector2.One);
|
||||
}
|
||||
|
||||
public static void PlaySound(string soundName, Actor actor, int channel, AudioFlags flags, Vector3 position, float volume = 1f)
|
||||
{
|
||||
PlaySound(soundName, actor, channel, flags, position, volume, Vector2.One);
|
||||
}
|
||||
|
||||
public static void PlaySound(string soundName, Actor actor, int channel, AudioFlags flags, Vector3 position, float volume, Vector2 pitchRange)
|
||||
{
|
||||
AudioInfo audio = GetSound(soundName);
|
||||
if (audio.AudioClips.Length == 0)
|
||||
return;
|
||||
|
||||
channel = Math.Max(channel, 0);
|
||||
ActorAudioChannels actorChannels = null;
|
||||
if (channel > 0)
|
||||
{
|
||||
if (!actorAudioChannels.TryGetValue(actor, out actorChannels))
|
||||
{
|
||||
actorChannels = new ActorAudioChannels();
|
||||
actorAudioChannels.Add(actor, actorChannels);
|
||||
}
|
||||
|
||||
if (!actorChannels.channelSources.TryGetValue(channel, out AudioSource existingAudioSource))
|
||||
actorChannels.channelSources.Add(channel, null);
|
||||
|
||||
if (existingAudioSource && existingAudioSource != null &&
|
||||
existingAudioSource.State == AudioSource.States.Playing)
|
||||
{
|
||||
if (flags.HasFlag(AudioFlags.ContinuePlayingExistingSource))
|
||||
return;
|
||||
|
||||
existingAudioSource.Stop();
|
||||
Object.Destroy(existingAudioSource);
|
||||
actorChannels.channelSources[channel] = null;
|
||||
}
|
||||
}
|
||||
|
||||
AudioClip audioClip;
|
||||
if (audio.AudioClips.Length > 1)
|
||||
{
|
||||
@@ -64,6 +126,51 @@ namespace Game
|
||||
|
||||
audioSource.Play();
|
||||
Object.Destroy(audioSource, audioClip.Length);
|
||||
|
||||
if (channel > 0)
|
||||
{
|
||||
actorChannels.channelSources[channel] = audioSource;
|
||||
}
|
||||
}
|
||||
|
||||
public static void StopSound(Actor actor, int channel)
|
||||
{
|
||||
if (channel <= 0)
|
||||
return;
|
||||
|
||||
if (!actorAudioChannels.TryGetValue(actor, out ActorAudioChannels actorChannels))
|
||||
return;
|
||||
|
||||
if (!actorChannels.channelSources.TryGetValue(channel, out AudioSource existingAudioSource))
|
||||
return;
|
||||
|
||||
if (existingAudioSource && existingAudioSource != null &&
|
||||
existingAudioSource.State == AudioSource.States.Playing)
|
||||
{
|
||||
existingAudioSource.Stop();
|
||||
Object.Destroy(existingAudioSource);
|
||||
actorChannels.channelSources[channel] = null;
|
||||
}
|
||||
}
|
||||
|
||||
public static bool IsSoundPlaying(Actor actor, int channel)
|
||||
{
|
||||
if (channel <= 0)
|
||||
return false;
|
||||
|
||||
if (!actorAudioChannels.TryGetValue(actor, out ActorAudioChannels actorChannels))
|
||||
return false;
|
||||
|
||||
if (!actorChannels.channelSources.TryGetValue(channel, out AudioSource existingAudioSource))
|
||||
return false;
|
||||
|
||||
if (existingAudioSource && existingAudioSource != null &&
|
||||
existingAudioSource.State == AudioSource.States.Playing)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private static AudioInfo GetSound(string soundName)
|
||||
|
||||
Reference in New Issue
Block a user