From 7f493f682503f5186308de7b8f74b5b49233cfe4 Mon Sep 17 00:00:00 2001 From: chai <215380520@qq.com> Date: Thu, 2 Nov 2023 11:51:31 +0800 Subject: +init --- GameCode/PlayerCharacterAudio.cs | 94 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 GameCode/PlayerCharacterAudio.cs (limited to 'GameCode/PlayerCharacterAudio.cs') diff --git a/GameCode/PlayerCharacterAudio.cs b/GameCode/PlayerCharacterAudio.cs new file mode 100644 index 0000000..2e951ef --- /dev/null +++ b/GameCode/PlayerCharacterAudio.cs @@ -0,0 +1,94 @@ +using UnityEngine; + +public class PlayerCharacterAudio : MonoBehaviour +{ + public Hp playerHp; + + public AutoRevive playerReviveComponent; + + public AudioSource stepAudioSource; + + public AudioSource fxAudioSource; + + public AudioSource dmgAudioSource; + + public PlayerMovement targetController; + + public float fadeTime = 0.3f; + + public float sprintPitch = 1.1f; + + private PlayerWeaponAudio weaponAudio; + + private float initialVolume; + + private float initialPitch; + + private float fadeSpeed; + + private void Start() + { + initialPitch = stepAudioSource.pitch; + initialVolume = stepAudioSource.volume; + stepAudioSource.volume = 0f; + stepAudioSource.priority = 110; + fadeSpeed = initialVolume / fadeTime; + playerHp.OnKillOrKnockout.AddListener(OnDeath); + playerHp.OnReceiveDamage.AddListener(OnDmg); + playerReviveComponent.onReviveTrigger.AddListener(OnRevive); + } + + public void OnWeaponEquip(ManualAttack weapon) + { + weaponAudio = weapon.GetComponent(); + weaponAudio.autoWeapon.onAttack.AddListener(PlayAttackSound); + } + + private void PlayAttackSound() + { + fxAudioSource.priority = 5; + fxAudioSource.pitch = Random.Range(1f - weaponAudio.pitchRange, 1f + weaponAudio.pitchRange); + fxAudioSource.PlayOneShot(weaponAudio.AttackSound.GetRandomClip(), weaponAudio.volume); + } + + private void OnDeath() + { + dmgAudioSource.priority = 5; + dmgAudioSource.pitch = 1f; + dmgAudioSource.PlayOneShot(ThronefallAudioManager.Instance.audioContent.PlayerDeath.GetRandomClip(), 1f); + } + + private void OnDmg(bool b) + { + dmgAudioSource.priority = 5; + dmgAudioSource.pitch = Random.Range(0.9f, 1.1f); + dmgAudioSource.PlayOneShot(ThronefallAudioManager.Instance.audioContent.PlayerDamage.GetRandomClip(), 0.65f); + } + + private void OnRevive() + { + fxAudioSource.priority = 5; + fxAudioSource.pitch = 1f; + fxAudioSource.PlayOneShot(ThronefallAudioManager.Instance.audioContent.PlayerRevive, 0.8f); + } + + private void Update() + { + if (targetController.Sprinting) + { + stepAudioSource.pitch = sprintPitch; + } + else + { + stepAudioSource.pitch = initialPitch; + } + if (targetController.Moving && (playerHp == null || !playerHp.KnockedOut)) + { + stepAudioSource.volume = Mathf.MoveTowards(stepAudioSource.volume, initialVolume, fadeSpeed * Time.unscaledDeltaTime); + } + else + { + stepAudioSource.volume = Mathf.MoveTowards(stepAudioSource.volume, 0f, fadeSpeed * Time.unscaledDeltaTime); + } + } +} -- cgit v1.1-26-g67d0