Files
Crash-Course/Assets/Scripts/Game/AudioManager.cs

114 lines
3.7 KiB
C#
Raw Normal View History

2025-04-12 17:24:51 -04:00
using System.Collections.Generic;
2025-04-18 15:54:50 -04:00
using UnityEngine;
using Game;
using Music;
using Player;
2025-04-16 19:57:54 -04:00
namespace Music
{
2025-04-18 15:54:50 -04:00
/// <summary>
/// This class manages the playback of sound effects in the game.
/// It provides functionality to play specific sounds by name and ensures a singleton instance for global access.
/// </summary>
public class AudioManager : MonoBehaviour
{
/// <summary>
/// A list of all sound effects managed by the AudioManager.
/// </summary>
public List<SoundEffect> soundEffects = new List<SoundEffect>();
2025-04-12 17:24:51 -04:00
2025-04-18 15:54:50 -04:00
/// <summary>
/// The singleton instance of the <see cref="AudioManager"/> class.
/// </summary>
public static AudioManager Instance;
2025-04-12 17:24:51 -04:00
2025-04-18 15:54:50 -04:00
/// <summary>
/// Initializes the singleton instance and loads all child AudioSources as sound effects.
/// </summary>
private void Awake()
2025-04-12 17:24:51 -04:00
{
2025-04-18 15:54:50 -04:00
// Ensure only one instance of the AudioManager exists
if (Instance == null)
{
Instance = this;
DontDestroyOnLoad(gameObject); // Persist across scenes
}
else
{
Destroy(gameObject); // Destroy duplicate instances
}
// Load all child AudioSources into the soundEffects list
foreach (Transform child in transform)
{
var soundEffect = new SoundEffect(child.name, child.GetComponent<AudioSource>());
if (soundEffect != null)
{
soundEffects.Add(soundEffect);
}
}
2025-04-12 17:24:51 -04:00
}
2025-04-18 15:54:50 -04:00
/// <summary>
/// Plays a sound effect by its name.
/// </summary>
/// <param name="soundName">The name of the sound effect to play.</param>
/// <remarks>
/// If the sound name is "Punch," it plays multiple punch-related sound effects.
/// If the sound is not found, a warning is logged to the console.
/// </remarks>
public void PlaySound(string soundName)
2025-04-12 17:24:51 -04:00
{
2025-04-18 15:54:50 -04:00
// Special case: Play multiple punch sound effects
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;
}
// Find and play the sound effect by name
foreach (var soundEffect in soundEffects)
2025-04-12 17:24:51 -04:00
{
2025-04-18 15:54:50 -04:00
if (soundEffect.name == soundName)
{
soundEffect.audioSource.Play();
return;
}
2025-04-12 17:24:51 -04:00
}
2025-04-18 15:54:50 -04:00
// Log a warning if the sound effect is not found
Debug.LogWarning($"Sound '{soundName}' not found!");
2025-04-12 17:24:51 -04:00
}
}
2025-04-18 15:54:50 -04:00
/// <summary>
/// Represents a sound effect, including its name and associated AudioSource.
/// </summary>
public class SoundEffect
2025-04-12 17:24:51 -04:00
{
2025-04-18 15:54:50 -04:00
/// <summary>
/// The name of the sound effect.
/// </summary>
public string name;
2025-04-12 17:24:51 -04:00
2025-04-18 15:54:50 -04:00
/// <summary>
/// The AudioSource component that plays the sound effect.
/// </summary>
public AudioSource audioSource;
2025-04-12 17:24:51 -04:00
2025-04-18 15:54:50 -04:00
/// <summary>
/// Initializes a new instance of the <see cref="SoundEffect"/> class.
/// </summary>
/// <param name="name">The name of the sound effect.</param>
/// <param name="audioSource">The AudioSource component for the sound effect.</param>
public SoundEffect(string name, AudioSource audioSource)
2025-04-12 17:24:51 -04:00
{
2025-04-18 15:54:50 -04:00
this.name = name;
this.audioSource = audioSource;
2025-04-12 17:24:51 -04:00
}
}
}