Add simple audio system

This commit is contained in:
WillJeynes
2026-08-25 17:22:12 +01:00
parent 4c3f94dba1
commit c92d5a633a
20 changed files with 346 additions and 23 deletions
+38
View File
@@ -0,0 +1,38 @@
using System.Collections.Generic;
using UnityEngine;
using Utils;
using Utils.Utils;
public class AudioManager : Singleton<AudioManager>
{
private AudioSource audioSource;
[SerializeField]
private List<GameEntry<AudioClip>> clips = new List<GameEntry<AudioClip>>();
[SerializeField]
private List<AudioClip> speech = new List<AudioClip>();
private
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
audioSource = GetComponent<AudioSource>();
audioSource.pitch = 1;
}
public void PlaySound(string type)
{
audioSource.pitch = 1f;
audioSource.PlayOneShot(clips.ByKey(type));
}
public void PlaySpeechSound(bool pitchDown)
{
int index = Random.Range(0, speech.Count);
audioSource.pitch = pitchDown ? 0.5f:1f;
audioSource.PlayOneShot(speech[index], 1.5f);
}
}
+2
View File
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 083c97d5b5c3da8408586386bf771491
+8 -5
View File
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using DefaultNamespace;
using JetBrains.Annotations;
@@ -133,11 +134,11 @@ public class DialogManager : Singleton<DialogManager>
option.GetComponent<Button>().onClick.AddListener(() =>
{
AudioManager.Instance.PlaySound("WordAdd");
if (kvp.Value is JValue)
{
AddQuestionInfill(currentItem, kvp.Key + "?");
Debug.LogError("ready");
currentItem = null;
AskButton.SetActive(true);
FullQuestion = kvp.Value.ToString();
@@ -161,6 +162,7 @@ public class DialogManager : Singleton<DialogManager>
public void BackButtonPress()
{
AudioManager.Instance.PlaySound("WordRemove");
if (questionStack.Count == 0)
{
reset();
@@ -173,9 +175,9 @@ public class DialogManager : Singleton<DialogManager>
Destroy(infil);
//this might be buggy, or might miraculuisly work
//if options start dissapearing randomly, look here
if (item.StartsWith("NOREC_"))
//aaaaaaaaaaaaaaaaaaaaaaaa
//aaaaaaaaaaaaaaaaaaaaaaaa
if (parent.ContainsKey(item) && parent[item] is JObject && (parent[item] as JObject).ContainsKey("QUESTION"))
{
recursiveDepth--;
FullQuestionPrefixes.Pop();
@@ -186,6 +188,7 @@ public class DialogManager : Singleton<DialogManager>
public void SubmitButtonPress()
{
AudioManager.Instance.PlaySound("Select");
//formulate query string in format PERPSECTIVE%ITEM#PROPERTY
//slightly wack way of doing it, hoping it is future proof
while (FullQuestionPrefixes.Count > 0)
+3 -2
View File
@@ -2,14 +2,15 @@ using UnityEngine;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
using Utils;
using Utils.Utils;
public class LevelLoader : Singleton<LevelLoader>
{
[SerializeField]
private List<GameObjectEntry> DoorPrefabs;
private List<GameEntry<GameObject>> DoorPrefabs;
[SerializeField]
private List<GameObjectEntry> GuardPrefabs;
private List<GameEntry<GameObject>> GuardPrefabs;
[SerializeField]
private List<TextAsset> levels;
+6 -1
View File
@@ -20,6 +20,8 @@ public class SpeechManager : Singleton<SpeechManager>
[CanBeNull] private string fulltext;
private Coroutine ccoroutine;
private string speaker;
public string Status = "TALKING";
@@ -67,10 +69,11 @@ public class SpeechManager : Singleton<SpeechManager>
private void NextText()
{
AudioManager.Instance.PlaySound("Select");
var item = textBuffer.Dequeue();
ccoroutine = StartCoroutine(TypeTextUncapped(item.Key));
var speaker = item.Value;
speaker = item.Value;
if (speaker == "PLAYER")
{
@@ -98,11 +101,13 @@ public class SpeechManager : Singleton<SpeechManager>
string textBuffer = null;
char[] chars = line.ToCharArray();
int i = 0;
while (i < chars.Length)
{
if (timer < Time.deltaTime)
{
AudioManager.Instance.PlaySpeechSound(speaker != "PLAYER");
textBuffer += chars[i];
mainText.text = textBuffer;
timer += interval;
+21 -15
View File
@@ -4,24 +4,30 @@ using UnityEngine;
namespace Utils
{
[Serializable]
public class GameObjectEntry
namespace Utils
{
public string key;
public GameObject value;
}
public static class GameObjectEntryListExtensions
{
public static GameObject ByKey(this List<GameObjectEntry> entries, string key)
[Serializable]
public class GameEntry<T>
{
foreach (var entry in entries)
{
if (entry.key == key)
return entry.value;
}
public string key;
public T value;
}
return null;
public static class GameObjectEntryListExtensions
{
public static T ByKey<T>(
this List<GameEntry<T>> entries,
string key
) where T : class
{
foreach (var entry in entries)
{
if (entry.key == key)
return entry.value;
}
return null;
}
}
}