Implement sound effects

This commit is contained in:
RochesterX
2025-04-12 17:24:51 -04:00
parent e122129196
commit 274d87241f
20 changed files with 1829 additions and 4 deletions

67
Assets/AudioManager.cs Normal file
View File

@@ -0,0 +1,67 @@
using System.Collections.Generic;
using UnityEngine;
public class AudioManager : MonoBehaviour
{
public List<SoundEffect> soundEffects = new List<SoundEffect>();
public static AudioManager Instance;
private void Awake()
{
if (Instance == null)
{
Instance = this;
DontDestroyOnLoad(gameObject);
}
else
{
Destroy(gameObject);
}
foreach (Transform child in transform)
{
var soundEffect = new SoundEffect(child.name, child.GetComponent<AudioSource>());
if (soundEffect != null)
{
soundEffects.Add(soundEffect);
}
}
print(soundEffects);
}
public void PlaySound(string soundName)
{
if (soundName == "Punch")
{
soundEffects.Find(x => x.name == "Punch").audioSource.Play();
soundEffects.Find(x => x.name == "Punch 2").audioSource.Play();
soundEffects.Find(x => x.name == "Punch 3").audioSource.Play();
return;
}
foreach (var soundEffect in soundEffects)
{
if (soundEffect.name == soundName)
{
soundEffect.audioSource.Play();
return;
}
}
Debug.LogWarning($"Sound '{soundName}' not found!");
}
}
public class SoundEffect
{
public string name;
public AudioSource audioSource;
public SoundEffect(string name, AudioSource audioSource)
{
this.name = name;
this.audioSource = audioSource;
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: ab3338b5eb5fc46caac119dfaba50ae3

View File

@@ -327,6 +327,17 @@
"isComposite": false,
"isPartOfComposite": false
},
{
"name": "",
"id": "518c81e8-0fbd-417b-8e0d-1369f419f06d",
"path": "<HID::45E-2FD>/button12",
"interactions": "",
"processors": "",
"groups": ";Gamepad",
"action": "Join",
"isComposite": false,
"isPartOfComposite": false
},
{
"name": "",
"id": "579f259d-1883-4fa5-bb3f-e564431fd795",

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -20,7 +20,7 @@ GameObject:
- component: {fileID: 8121950008105218343}
- component: {fileID: 2813634395235005482}
- component: {fileID: 3838772665456314839}
m_Layer: 0
m_Layer: 9
m_Name: Bones
m_TagString: Player
m_Icon: {fileID: 0}
@@ -1945,7 +1945,7 @@ Transform:
m_GameObject: {fileID: 8242218317363719243}
serializedVersion: 2
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0.337, z: 0}
m_LocalPosition: {x: 0, y: 21.1, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []

View File

@@ -14,7 +14,7 @@ GameObject:
- component: {fileID: 3780107421726398174}
- component: {fileID: 2046340251710672081}
- component: {fileID: 7583491847306130808}
m_Layer: 0
m_Layer: 10
m_Name: Hat
m_TagString: Hat
m_Icon: {fileID: 0}

File diff suppressed because it is too large Load Diff

View File

@@ -217,6 +217,7 @@ public class GameManager : MonoBehaviour
while (!winner.GetComponent<UseItem>().IsHoldingItem())
{
hatObject.transform.position = winner.transform.position + Vector3.up * 3/2;
winner.GetComponent<UseItem>().PickUpItem(hatObject);
yield return null;
}
}

View File

@@ -17,6 +17,7 @@ public class HatRespawn : MonoBehaviour
void Update() // Checks if the hat has been inactive for too long
{
if (GameManager.Instance.gameOver) GetComponent<BoxCollider2D>().enabled = false;
if (isDropped && Time.time - lastInteractionTime > respawnTime)
{
RespawnHat();

View File

@@ -52,16 +52,19 @@ public class Damageable : MonoBehaviour
if (blockComponent.IsParrying()) // Player receives damage if punching a parrying player
{
damageSource.GetComponent<Damageable>().SuccessfulParry(gameObject, actualForce);
AudioManager.Instance.PlaySound("Parry");
return;
}
else // Player does less damage if punching a blocking player
{
AudioManager.Instance.PlaySound("Punch");
actualForce /= 4;
GetComponent<Rigidbody2D>().AddForce(((transform.position - damageSource.transform.position).normalized + Vector3.up * 2) * actualForce, ForceMode2D.Force);
}
}
else // Player does full damage to a non-blocking player
{
AudioManager.Instance.PlaySound("Punch");
GetComponent<Rigidbody2D>().AddForce(((transform.position - damageSource.transform.position).normalized + Vector3.up * 2) * actualForce * (1 + (damage / maxDamage) * 3), ForceMode2D.Force);
}
damage += actualForce;
@@ -88,6 +91,7 @@ public class Damageable : MonoBehaviour
GetComponent<Rigidbody2D>().AddForce(((transform.position - damageSource.transform.position).normalized + Vector3.up * 2) * force, ForceMode2D.Force);
damage += force;
damage = Mathf.Clamp(damage, 0f, maxDamage);
if (damage >= maxDamage)
{
Die();
@@ -105,6 +109,8 @@ public class Damageable : MonoBehaviour
}
animator.SetBool("die", true);
dying = true;
AudioManager.Instance.PlaySound("Death Simple");
}
}

View File

@@ -132,6 +132,7 @@ public class PlayerMovement : MonoBehaviour
if ((virtualButtonJumpLastFrame == 1f && isBasicallyGrounded && jumping == false) // Coyote Jump: Must have jump pressed this frame and be grounded in last time frame and not be actually jumping.
|| (jumpInputStillValid && Time.time - lastTimeJumpPressed <= jumpLenience && IsPhysicallyGrounded())) // Buffered Jump: Must have pressed jump in the last time frame and be jumping
{
AudioManager.Instance.PlaySound("Jump");
jumpPhysics = true;
jumping = true;
jumpInputStillValid = false;

View File

@@ -40,7 +40,7 @@ public class UseItem : MonoBehaviour
}
}
private void PickUpItem(GameObject item) // Player picks up hat and starts hold counter
public void PickUpItem(GameObject item) // Player picks up hat and starts hold counter
{
if (damageable.dying) return; // Prevent picking up items if the player is dying
if (HatRespawn.canBePickedUp == false) return; // Prevent picking up items if they are not interactable
@@ -58,6 +58,7 @@ public class UseItem : MonoBehaviour
{
GameManager.playerHoldTimes[gameObject] = 0f;
}
AudioManager.Instance.PlaySound("Pickup Hat");
GameManager.Instance.StopCoroutine("MoveHatToWinner");
}