133 lines
3.4 KiB
C#
133 lines
3.4 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
namespace DarkTonic.MasterAudio
|
|
{
|
|
[AddComponentMenu("Dark Tonic/Master Audio/Footstep Sounds")]
|
|
public class FootstepSounds : MonoBehaviour
|
|
{
|
|
public enum FootstepTriggerMode
|
|
{
|
|
None = 0,
|
|
OnCollision = 1,
|
|
OnTriggerEnter = 2,
|
|
OnCollision2D = 3,
|
|
OnTriggerEnter2D = 4
|
|
}
|
|
|
|
public MasterAudio.SoundSpawnLocationMode soundSpawnMode = MasterAudio.SoundSpawnLocationMode.AttachToCaller;
|
|
|
|
public FootstepTriggerMode footstepEvent;
|
|
|
|
public List<FootstepGroup> footstepGroups = new List<FootstepGroup>();
|
|
|
|
public EventSounds.RetriggerLimMode retriggerLimitMode;
|
|
|
|
public int limitPerXFrm;
|
|
|
|
public float limitPerXSec;
|
|
|
|
public int triggeredLastFrame = -100;
|
|
|
|
public float triggeredLastTime = -100f;
|
|
|
|
private Transform _trans;
|
|
|
|
private Transform Trans
|
|
{
|
|
get
|
|
{
|
|
if (_trans != null)
|
|
{
|
|
return _trans;
|
|
}
|
|
_trans = base.transform;
|
|
return _trans;
|
|
}
|
|
}
|
|
|
|
private void OnTriggerEnter(Collider other)
|
|
{
|
|
if (footstepEvent == FootstepTriggerMode.OnTriggerEnter)
|
|
{
|
|
PlaySoundsIfMatch(other.gameObject);
|
|
}
|
|
}
|
|
|
|
private void OnCollisionEnter(Collision collision)
|
|
{
|
|
if (footstepEvent == FootstepTriggerMode.OnCollision)
|
|
{
|
|
PlaySoundsIfMatch(collision.gameObject);
|
|
}
|
|
}
|
|
|
|
private bool CheckForRetriggerLimit()
|
|
{
|
|
switch (retriggerLimitMode)
|
|
{
|
|
case EventSounds.RetriggerLimMode.FrameBased:
|
|
if (triggeredLastFrame > 0 && AudioUtil.FrameCount - triggeredLastFrame < limitPerXFrm)
|
|
{
|
|
return false;
|
|
}
|
|
break;
|
|
case EventSounds.RetriggerLimMode.TimeBased:
|
|
if (triggeredLastTime > 0f && AudioUtil.Time - triggeredLastTime < limitPerXSec)
|
|
{
|
|
return false;
|
|
}
|
|
break;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
private void PlaySoundsIfMatch(GameObject go)
|
|
{
|
|
if (!CheckForRetriggerLimit())
|
|
{
|
|
return;
|
|
}
|
|
switch (retriggerLimitMode)
|
|
{
|
|
case EventSounds.RetriggerLimMode.FrameBased:
|
|
triggeredLastFrame = AudioUtil.FrameCount;
|
|
break;
|
|
case EventSounds.RetriggerLimMode.TimeBased:
|
|
triggeredLastTime = AudioUtil.Time;
|
|
break;
|
|
}
|
|
for (int i = 0; i < footstepGroups.Count; i++)
|
|
{
|
|
FootstepGroup footstepGroup = footstepGroups[i];
|
|
if ((!footstepGroup.useLayerFilter || footstepGroup.matchingLayers.Contains(go.layer)) && (!footstepGroup.useTagFilter || footstepGroup.matchingTags.Contains(go.tag)))
|
|
{
|
|
float volume = footstepGroup.volume;
|
|
float? pitch = footstepGroup.pitch;
|
|
if (!footstepGroup.useFixedPitch)
|
|
{
|
|
pitch = null;
|
|
}
|
|
string variationName = null;
|
|
if (footstepGroup.variationType == EventSounds.VariationType.PlaySpecific)
|
|
{
|
|
variationName = footstepGroup.variationName;
|
|
}
|
|
switch (soundSpawnMode)
|
|
{
|
|
case MasterAudio.SoundSpawnLocationMode.CallerLocation:
|
|
MasterAudio.PlaySound3DAtTransformAndForget(footstepGroup.soundType, Trans, volume, pitch, footstepGroup.delaySound, variationName);
|
|
break;
|
|
case MasterAudio.SoundSpawnLocationMode.AttachToCaller:
|
|
MasterAudio.PlaySound3DFollowTransformAndForget(footstepGroup.soundType, Trans, volume, pitch, footstepGroup.delaySound, variationName);
|
|
break;
|
|
case MasterAudio.SoundSpawnLocationMode.MasterAudioLocation:
|
|
MasterAudio.PlaySoundAndForget(footstepGroup.soundType, volume, pitch, footstepGroup.delaySound, variationName);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|