mirror of
https://github.com/Ratstail91/Keep-It-Alive.git
synced 2025-11-29 10:34:27 +11:00
Added audio, it's finished
This commit is contained in:
212
Assets/Scripts/AudioController.cs
Normal file
212
Assets/Scripts/AudioController.cs
Normal file
@@ -0,0 +1,212 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class AudioController : MonoBehaviour {
|
||||
//public structures
|
||||
public enum Mode {
|
||||
NONE,
|
||||
ONCE,
|
||||
LOOP,
|
||||
JUMP
|
||||
}
|
||||
|
||||
public struct AudioContainer {
|
||||
public AudioSource source;
|
||||
public Mode mode;
|
||||
public float jumpStart;
|
||||
public float jumpEnd;
|
||||
}
|
||||
|
||||
//internals
|
||||
Dictionary<string, AudioContainer> audioDictionary = new Dictionary<string, AudioContainer>();
|
||||
static bool initialized = false;
|
||||
|
||||
//monobehaviour methods
|
||||
void Awake() {
|
||||
if (initialized) {
|
||||
Destroy(gameObject);
|
||||
}
|
||||
|
||||
initialized = true;
|
||||
|
||||
DontDestroyOnLoad(gameObject);
|
||||
}
|
||||
|
||||
void Update() {
|
||||
foreach(KeyValuePair<string, AudioContainer> iter in audioDictionary) {
|
||||
//handle the jump points
|
||||
if (iter.Value.mode == Mode.JUMP && iter.Value.jumpStart >= 0f && iter.Value.jumpEnd > 0f) {
|
||||
if (iter.Value.source.time >= iter.Value.jumpEnd) {
|
||||
iter.Value.source.time = iter.Value.jumpStart;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void OnDestroy() {
|
||||
foreach(KeyValuePair<string, AudioContainer> iter in audioDictionary) {
|
||||
Resources.UnloadAsset(iter.Value.source.clip);
|
||||
Destroy(iter.Value.source);
|
||||
}
|
||||
}
|
||||
|
||||
//public access members
|
||||
public void Load(string name, string filename) {
|
||||
AudioContainer container = new AudioContainer();
|
||||
|
||||
container.source = gameObject.AddComponent(typeof(AudioSource)) as AudioSource;
|
||||
container.source.clip = Resources.Load<AudioClip>(filename) as AudioClip;
|
||||
container.source.volume = 0f;
|
||||
container.mode = Mode.NONE;
|
||||
|
||||
audioDictionary[name] = container;
|
||||
}
|
||||
|
||||
public bool Unload(string name) {
|
||||
if (!audioDictionary.ContainsKey(name)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
AudioContainer container = audioDictionary[name];
|
||||
|
||||
Resources.UnloadAsset(container.source.clip);
|
||||
Destroy(container.source);
|
||||
|
||||
audioDictionary.Remove(name);
|
||||
return true;
|
||||
}
|
||||
|
||||
//controls
|
||||
public void Play(string name, Mode mode = Mode.ONCE, float jumpStart = -1f, float jumpEnd = -1f) {
|
||||
AudioContainer container = audioDictionary[name];
|
||||
|
||||
container.source.Play();
|
||||
container.source.loop = mode == Mode.LOOP;
|
||||
container.source.volume = 1f;
|
||||
container.mode = mode;
|
||||
container.jumpStart = jumpStart;
|
||||
container.jumpEnd = jumpEnd;
|
||||
audioDictionary[name] = container;
|
||||
}
|
||||
|
||||
public void Pause(string name) {
|
||||
AudioContainer container = audioDictionary[name];
|
||||
|
||||
container.source.Pause();
|
||||
}
|
||||
|
||||
public void Unpause(string name, Mode mode = Mode.ONCE, float jumpStart = -1f, float jumpEnd = -1f) {
|
||||
AudioContainer container = audioDictionary[name];
|
||||
|
||||
if (container.source.isPlaying) {
|
||||
container.source.UnPause();
|
||||
} else {
|
||||
Play(name, mode, jumpStart, jumpEnd);
|
||||
}
|
||||
}
|
||||
|
||||
public void Stop(string name) {
|
||||
AudioContainer container = audioDictionary[name];
|
||||
|
||||
container.source.Stop();
|
||||
container.mode = Mode.NONE;
|
||||
|
||||
audioDictionary[name] = container;
|
||||
}
|
||||
|
||||
public void StopAll() {
|
||||
List<string> names = new List<string>();
|
||||
foreach(KeyValuePair<string, AudioContainer> iter in audioDictionary) {
|
||||
names.Add(iter.Key);
|
||||
}
|
||||
|
||||
foreach(string name in names) {
|
||||
Stop(name);
|
||||
}
|
||||
}
|
||||
|
||||
//fade controls
|
||||
public void FadeIn(string name, float seconds) {
|
||||
StartCoroutine(FadeInCallback(audioDictionary[name].source, 1f/seconds));
|
||||
}
|
||||
|
||||
IEnumerator FadeInCallback(AudioSource source, float amountPerSecond) {
|
||||
source.volume = 0;
|
||||
while (source.volume < 1f) {
|
||||
yield return new WaitForSeconds(0.1f);
|
||||
source.volume += amountPerSecond / 10f;
|
||||
}
|
||||
}
|
||||
|
||||
public void FadeOut(string name, float seconds) {
|
||||
StartCoroutine(FadeOutCallback(audioDictionary[name].source, 1f/seconds));
|
||||
}
|
||||
|
||||
IEnumerator FadeOutCallback(AudioSource source, float amountPerSecond) {
|
||||
while (source.volume > 0f) {
|
||||
yield return new WaitForSeconds(0.1f);
|
||||
source.volume -= amountPerSecond / 10f;
|
||||
}
|
||||
}
|
||||
|
||||
//hybrid controls
|
||||
public void PlayFadeIn(string name, float seconds, Mode mode = Mode.ONCE, float jumpStart = -1f, float jumpEnd = -1f) {
|
||||
FadeIn(name, seconds);
|
||||
Play(name, mode, jumpStart, jumpEnd);
|
||||
}
|
||||
|
||||
public void PauseFadeOut(string name, float seconds) {
|
||||
FadeOut(name, seconds);
|
||||
StartCoroutine(PauseFadeOutCallback(name, seconds));
|
||||
}
|
||||
|
||||
public void PauseFadeOutAll(float seconds, List<string> exclude = null) {
|
||||
foreach(KeyValuePair<string, AudioContainer> iter in audioDictionary) {
|
||||
if (exclude != null && exclude.Contains(iter.Key)) {
|
||||
continue;
|
||||
}
|
||||
FadeOut(iter.Key, seconds);
|
||||
StartCoroutine(PauseFadeOutCallback(iter.Key, seconds));
|
||||
}
|
||||
}
|
||||
|
||||
IEnumerator PauseFadeOutCallback(string name, float seconds) {
|
||||
yield return new WaitForSeconds(seconds);
|
||||
Pause(name);
|
||||
}
|
||||
|
||||
public void UnpauseFadeIn(string name, float seconds, Mode mode = Mode.ONCE, float jumpStart = -1f, float jumpEnd = -1f) {
|
||||
Unpause(name, mode, jumpStart, jumpEnd);
|
||||
FadeIn(name, seconds);
|
||||
}
|
||||
|
||||
public void StopFadeOut(string name, float seconds) {
|
||||
FadeOut(name, seconds);
|
||||
StartCoroutine(StopFadeOutCallback(name, seconds));
|
||||
}
|
||||
|
||||
public void StopFadeOutAll(float seconds, List<string> exclude = null) {
|
||||
foreach(KeyValuePair<string, AudioContainer> iter in audioDictionary) {
|
||||
if (exclude != null && exclude.Contains(iter.Key)) {
|
||||
continue;
|
||||
}
|
||||
FadeOut(iter.Key, seconds);
|
||||
StartCoroutine(StopFadeOutCallback(iter.Key, seconds));
|
||||
}
|
||||
}
|
||||
|
||||
IEnumerator StopFadeOutCallback(string name, float seconds) {
|
||||
yield return new WaitForSeconds(seconds);
|
||||
Stop(name);
|
||||
}
|
||||
|
||||
//status
|
||||
public bool GetPlaying(string name) {
|
||||
return audioDictionary[name].source.isPlaying;
|
||||
}
|
||||
|
||||
public Mode GetMode(string name) {
|
||||
return audioDictionary[name].mode;
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/AudioController.cs.meta
Normal file
11
Assets/Scripts/AudioController.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 93eaa725ebfe1c77a8286d5fea05229a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
18
Assets/Scripts/AudioMainMenu.cs
Normal file
18
Assets/Scripts/AudioMainMenu.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class AudioMainMenu : MonoBehaviour {
|
||||
AudioController controller;
|
||||
|
||||
void Start() {
|
||||
controller = GameObject.Find("AudioController").GetComponent<AudioController>();
|
||||
}
|
||||
|
||||
void Update() {
|
||||
//won't work in start, for some reason
|
||||
if (controller.GetPlaying("fire") == false) {
|
||||
controller.Play("fire", AudioController.Mode.LOOP);
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/AudioMainMenu.cs.meta
Normal file
11
Assets/Scripts/AudioMainMenu.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3d6a8f4dff66810239f482b0236a94d7
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
14
Assets/Scripts/AudioStartup.cs
Normal file
14
Assets/Scripts/AudioStartup.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class AudioStartup : MonoBehaviour {
|
||||
AudioController controller;
|
||||
void Awake() {
|
||||
controller = GetComponent<AudioController>();
|
||||
controller.Load("fire", "Fire");
|
||||
controller.Load("crackle", "Fire Cracks");
|
||||
controller.Load("cut", "Tree Cut");
|
||||
controller.Load("hunted", "Hunted");
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/AudioStartup.cs.meta
Normal file
11
Assets/Scripts/AudioStartup.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e9ef43588bdc8ffda9204b41ef5a58af
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -5,12 +5,16 @@ using UnityEngine.SceneManagement;
|
||||
|
||||
public class Claws : MonoBehaviour {
|
||||
SpriteRenderer spriteRenderer;
|
||||
AudioController audioController;
|
||||
|
||||
void Awake() {
|
||||
spriteRenderer = GetComponent<SpriteRenderer>();
|
||||
audioController = GameObject.Find("AudioController").GetComponent<AudioController>();
|
||||
|
||||
StartCoroutine(FadeOverTime(2f));
|
||||
StartCoroutine(SwitchScenesAfter("MainMenu", 5f));
|
||||
|
||||
audioController.StopAll();
|
||||
}
|
||||
|
||||
IEnumerator FadeOverTime(float delay) {
|
||||
|
||||
@@ -4,9 +4,14 @@ using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
|
||||
public class FireGoesOut : MonoBehaviour {
|
||||
AudioController audioController;
|
||||
|
||||
void Awake() {
|
||||
//TODO: play extinguished sound
|
||||
audioController = GameObject.Find("AudioController").GetComponent<AudioController>();
|
||||
|
||||
StartCoroutine(SwitchScenesAfter("MainMenu", 3f));
|
||||
|
||||
audioController.StopAll();
|
||||
}
|
||||
|
||||
IEnumerator SwitchScenesAfter(string sceneName, float delay) {
|
||||
|
||||
@@ -4,6 +4,9 @@ using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
|
||||
public class Character : MonoBehaviour {
|
||||
//references
|
||||
AudioController audioController;
|
||||
|
||||
//components
|
||||
SpriteRenderer spriteRenderer;
|
||||
Rigidbody2D rb;
|
||||
@@ -22,6 +25,8 @@ public class Character : MonoBehaviour {
|
||||
float huntingTime;
|
||||
|
||||
void Awake() {
|
||||
audioController = GameObject.Find("AudioController").GetComponent<AudioController>();
|
||||
|
||||
spriteRenderer = GetComponent<SpriteRenderer>();
|
||||
rb = GetComponent<Rigidbody2D>();
|
||||
fadeToBlack = GetComponent<FadeToBlack>();
|
||||
@@ -75,8 +80,13 @@ public class Character : MonoBehaviour {
|
||||
void HandleHunted() {
|
||||
if (fadeToBlack.brightness < 0f) {
|
||||
huntingTime -= Time.deltaTime;
|
||||
|
||||
if (!audioController.GetPlaying("hunted")) {
|
||||
audioController.Play("hunted");
|
||||
}
|
||||
} else {
|
||||
huntingTime = 3f;
|
||||
audioController.Stop("hunted");
|
||||
}
|
||||
|
||||
if (huntingTime <= 0f) {
|
||||
|
||||
@@ -5,6 +5,7 @@ using UnityEngine;
|
||||
public class Fire : MonoBehaviour {
|
||||
Animator animator;
|
||||
LevelController levelController;
|
||||
AudioController audioController;
|
||||
|
||||
[SerializeField]
|
||||
int size = 1;
|
||||
@@ -12,6 +13,7 @@ public class Fire : MonoBehaviour {
|
||||
void Awake() {
|
||||
animator = GetComponent<Animator>();
|
||||
levelController = GameObject.Find("Level Controller").GetComponent<LevelController>();
|
||||
audioController = GameObject.Find("AudioController").GetComponent<AudioController>();
|
||||
}
|
||||
|
||||
void Start() {
|
||||
@@ -38,6 +40,8 @@ public class Fire : MonoBehaviour {
|
||||
levelController.globalWood -= 1;
|
||||
levelController.globalLightLevel += 0.2f;
|
||||
levelController.globalLightLevel = Mathf.Clamp(levelController.globalLightLevel, 0f, 1f);
|
||||
|
||||
audioController.Play("crackle");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ using UnityEngine;
|
||||
|
||||
public class Tree : MonoBehaviour {
|
||||
static LevelController levelController;
|
||||
static AudioController audioController;
|
||||
static GameObject character;
|
||||
|
||||
SpriteRenderer spriteRenderer;
|
||||
@@ -19,6 +20,10 @@ public class Tree : MonoBehaviour {
|
||||
levelController = GameObject.Find("Level Controller").GetComponent<LevelController>();
|
||||
}
|
||||
|
||||
if (!audioController) {
|
||||
audioController = GameObject.Find("AudioController").GetComponent<AudioController>();
|
||||
}
|
||||
|
||||
if (!character) {
|
||||
character = GameObject.Find("Character");
|
||||
}
|
||||
@@ -28,6 +33,8 @@ public class Tree : MonoBehaviour {
|
||||
if (GamePad.GetState().Pressed(CButton.Y)) {
|
||||
life -= 1;
|
||||
|
||||
audioController.Play("cut");
|
||||
|
||||
//wiggle
|
||||
StartCoroutine(Wiggle());
|
||||
|
||||
|
||||
Reference in New Issue
Block a user