FiE-Game/Assets/Scripts/Fie/Footstep/FieFootstepMaterial.cs

58 lines
1.6 KiB
C#
Raw Normal View History

2018-11-20 20:10:49 +01:00
using Fie.Utility;
using System.Collections.Generic;
using UnityEngine;
2018-11-21 21:16:20 +01:00
namespace Fie.Footstep {
/// <summary>
/// Plays the footstep animations created as players or enemies move around.
/// </summary>
public class FieFootstepMaterial : MonoBehaviour {
2018-11-20 20:10:49 +01:00
[SerializeField]
private float _audioVolume = 0.5f;
[SerializeField]
private List<AudioClip> _audioClips = new List<AudioClip>();
2018-11-21 21:16:20 +01:00
/// <summary>
/// The particle material to spawn under an entity's hooves as they walk.
/// </summary>
2018-11-20 20:10:49 +01:00
[SerializeField]
private Material _footStepParticleMaterial;
2018-11-21 21:16:20 +01:00
/// <summary>
/// A lotto of audio clips to play. Provides a random one when queried.
/// </summary>
2018-11-20 20:10:49 +01:00
private Lottery<AudioClip> _lottery = new Lottery<AudioClip>();
2018-11-21 21:16:20 +01:00
/// <summary>
/// The pool of sounds that can be played by this material.
/// </summary>
2018-11-20 20:10:49 +01:00
public List<AudioClip> audioClips => _audioClips;
2018-11-21 21:16:20 +01:00
private void Awake() {
foreach (AudioClip audioClip in _audioClips) {
_lottery.AddItem(audioClip); // add each audio clip to the lottery
2018-11-20 20:10:49 +01:00
}
}
2018-11-21 21:16:20 +01:00
/// <summary>
/// Attempts to play an audio clip for the given footstep player.
/// Accepts nulls.
/// </summary>
public void playFootstepAudio(FieFootstepPlayer player) {
if (!(player == null) && _lottery.IsExecutable()) {
2018-11-20 20:10:49 +01:00
AudioClip audioClip = _lottery.Lot();
2018-11-21 21:16:20 +01:00
if (!(audioClip == null)) {
2018-11-20 20:10:49 +01:00
player.SetMaterial(_footStepParticleMaterial);
2018-11-21 21:16:20 +01:00
2018-11-20 20:10:49 +01:00
player.audioSource.pitch = player.pitchOffset;
player.audioSource.PlayOneShot(audioClip, _audioVolume);
2018-11-21 21:16:20 +01:00
2018-11-20 20:10:49 +01:00
player.EmitFootstepParticle(audioClip.length);
}
}
}
}
}