Committed everything

This commit is contained in:
2021-06-30 21:39:19 +10:00
commit fcfa8e7213
525 changed files with 49440 additions and 0 deletions

View File

@@ -0,0 +1,216 @@
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 Start() {
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 != null && source.volume > 0f) {
source.volume -= amountPerSecond / 10f;
yield return new WaitForSeconds(0.1f);
}
}
//hybrid controls
public void PlayFadeIn(string name, float seconds, Mode mode = Mode.ONCE, float jumpStart = -1f, float jumpEnd = -1f) {
Play(name, mode, jumpStart, jumpEnd);
FadeIn(name, seconds);
}
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 GetLoaded(string name) {
return audioDictionary.ContainsKey(name);
}
public bool GetPlaying(string name) {
return audioDictionary[name].source.isPlaying;
}
public Mode GetMode(string name) {
return audioDictionary[name].mode;
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: d141fd75d0ff0454ea28e473c81cd70f
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,20 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Clickable : MonoBehaviour {
public string inkFilename;
void OnMouseDown() {
TextAsset asset;
asset = Resources.Load<TextAsset>("Story/" + inkFilename);
if (asset == null) {
Debug.LogError("Asset file is null");
return;
}
StoryController controller = FindObjectsOfType<StoryController>()[0];
controller.PushInkAsset(asset);
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: e593639a718308247bc5a0cd8866a1fc
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,378 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using Ink.Runtime;
public class StoryController : MonoBehaviour {
class StorySegment {
public TextAsset asset;
public Story story;
public Canvas canvas; //hold the clickable images
}
Stack<StorySegment> assetStack = new Stack<StorySegment>();
Dictionary<string, string> variables = new Dictionary<string, string>();
string textFull;
bool autoFinishText = false;
bool resetLineCoroutineRunning = false;
bool gameEnd = false;
[SerializeField]
TMP_Text textBox;
[SerializeField]
Canvas choiceCanvas;
[SerializeField]
Canvas clickableParentCanvas;
[SerializeField]
Canvas backgroundCanvas;
[SerializeField]
Canvas zoomedCanvas;
[SerializeField]
Image imagePrefab;
[SerializeField]
Button buttonPrefab;
[SerializeField]
GameObject clickablePrefab;
[SerializeField]
GameObject zoomedImagePrefab;
[Header("Start Point")]
[SerializeField]
TextAsset startPoint = null;
void Awake() {
//
}
void Start() {
//load the resources
AudioController audio = GameObject.FindObjectsOfType<AudioController>()[0];
audio.Load("letters", "Audio/letters");
//kick off
PushInkAsset(startPoint);
}
void Update() {
//TODO: scrolling text
}
void OnMouseDown() {
NextLine();
}
//controllers
public void PushInkAsset(TextAsset asset) {
StorySegment segment = new StorySegment();
segment.asset = asset;
segment.story = new Story(asset.text);
GameObject go = new GameObject();
go.name = "Layer Canvas";
go.transform.SetParent(clickableParentCanvas.gameObject.transform);
segment.canvas = go.AddComponent<Canvas>();
((RectTransform)(go.transform)).anchorMin = Vector2.zero;
((RectTransform)(go.transform)).anchorMax = new Vector2(1, 1);
((RectTransform)(go.transform)).offsetMin = Vector2.zero;
((RectTransform)(go.transform)).offsetMax = Vector2.zero;
//callbacks
segment.story.BindExternalFunction("SetVariable", (string key, string value) => {
variables[key] = value;
});
segment.story.BindExternalFunction("GetVariable", (string key) => {
if (variables.ContainsKey(key)) {
return variables[key];
} else {
return "";
}
});
segment.story.BindExternalFunction("SetBackground", (string fname, float seconds) => {
//destroy existing background after
if (backgroundCanvas.transform.childCount > 0) {
GameObject.Destroy(backgroundCanvas.transform.GetChild(0).gameObject, seconds);
}
Texture2D texture = Resources.Load<Texture2D>("Visuals/" + fname);
if (texture == null) {
return;
}
GameObject go = Instantiate(imagePrefab, backgroundCanvas.gameObject.transform).gameObject;
go.GetComponent<Image>().sprite = Sprite.Create(texture, new Rect(0.0f, 0.0f, texture.width, texture.height), new Vector2(0.5f,0.5f), 100.0f);
go.GetComponent<Image>().color = new Color(1, 1, 1, seconds == 0 ? 1 : 0);
if (seconds > 0) {
StartCoroutine(IncreaseAlphaColor(go.GetComponent<Image>(), seconds));
}
});
segment.story.BindExternalFunction("SetMusic", (string fname, float fadeOut, float fadeIn) => {
StartCoroutine(SetMusicCoroutine(fname, fadeOut, fadeIn));
});
segment.story.BindExternalFunction("PlaySound", (string fname) => {
AudioController audio = GameObject.FindObjectsOfType<AudioController>()[0];
if (!audio.GetLoaded(fname)) {
audio.Load(fname, "Audio/" + fname);
}
audio.Play(fname);
});
segment.story.BindExternalFunction("AddClickable", (string fname, string ink, float x, float y) => {
AddClickable(fname, ink, new Vector2(x, y));
});
segment.story.BindExternalFunction("RemoveClickable", (string name) => {
RemoveClickable(name);
});
segment.story.BindExternalFunction("AddZoomedImage", (string fname, float x, float y) => {
AddZoomedImage(fname, new Vector2(x, y));
});
segment.story.BindExternalFunction("RemoveZoomedImage", (string name) => {
RemoveZoomedImage(name);
});
if (assetStack.Count > 0) { //disable the lower canvas
assetStack.Peek().canvas.gameObject.SetActive(false);
}
assetStack.Push(segment);
RemoveCanvasChildren(choiceCanvas);
textBox.text = null;
textFull = "";
NextLine();
}
IEnumerator IncreaseAlphaColor(Image image, float seconds) {
while(image.color.a < 1) {
image.color = new Color(1, 1, 1, image.color.a + 0.1f / seconds);
yield return new WaitForSeconds(0.1f / seconds);
}
}
IEnumerator SetMusicCoroutine(string fname, float fadeOut, float fadeIn) {
AudioController audio = GameObject.FindObjectsOfType<AudioController>()[0];
if (audio.GetLoaded("music") && audio.GetPlaying("music")) {
audio.StopFadeOut("music", fadeOut);
yield return new WaitForSeconds(fadeOut);
audio.Unload("music");
}
audio.Load("music", "Audio/" + fname);
audio.PlayFadeIn("music", fadeIn, AudioController.Mode.LOOP);
}
public void PopInkAsset() {
if (assetStack.Count == 0) {
return;
}
GameObject.Destroy(assetStack.Peek().canvas.gameObject);
assetStack.Pop();
if (assetStack.Count > 0) {
assetStack.Peek().canvas.gameObject.SetActive(true); //re-enable the canvas
}
RemoveCanvasChildren(choiceCanvas);
textBox.text = null;
ResetLine(false);
}
void NextLine() {
if (resetLineCoroutineRunning || gameEnd) {
autoFinishText = true;
return;
}
if (assetStack.Count == 0) {
textFull = "Programming: Ratstail91\nArt: Crystal\nAudio: Silas / Silence";
StartCoroutine(ResetLineCallback(true));
gameEnd = true;
return;
}
if (!assetStack.Peek().story.canContinue) {
if (assetStack.Peek().story.currentChoices.Count == 0) {
PopInkAsset();
}
return;
}
ResetLine(true);
}
void ResetLine(bool cont) {
RemoveCanvasChildren(choiceCanvas);
if (assetStack.Count == 0) {
return;
}
//actually show the next part
if (cont) {
textFull = assetStack.Peek().story.Continue();
} else {
textFull = assetStack.Peek().story.currentText;
}
StartCoroutine(ResetLineCallback(cont));
}
IEnumerator ResetLineCallback(bool cont) {
resetLineCoroutineRunning = true;
textBox.text = "";
AudioController audio = GameObject.FindObjectsOfType<AudioController>()[0];
audio.Unpause("letters", AudioController.Mode.LOOP);
//scroll along the text
for (int i = 0; i < textFull.Length && !autoFinishText && cont; i++) {
textBox.text += textFull[i];
yield return new WaitForSeconds(0.05f);
//pause on a period
if (i < textFull.Length && textFull[i] == '.') {
yield return new WaitForSeconds(0.1f);
}
}
audio.Pause("letters");
textBox.text = textFull;
autoFinishText = false;
//if we're at a choice
if (assetStack.Count > 0 && assetStack.Peek().story.currentChoices.Count > 0) {
for (int i = 0; i < assetStack.Peek().story.currentChoices.Count; i++) {
Choice choice = assetStack.Peek().story.currentChoices[i];
Button button = DisplayButton(choice.text.Trim());
button.onClick.AddListener(() => { assetStack.Peek().story.ChooseChoiceIndex(choice.index); NextLine(); });
}
}
resetLineCoroutineRunning = false;
if (textFull == "") {
NextLine();
}
}
//remove children from a canvas
void RemoveCanvasChildren(Canvas canv) {
int childCount = canv.transform.childCount;
for (int i = childCount - 1; i >= 0; --i) {
GameObject.Destroy (canv.transform.GetChild(i).gameObject);
}
}
//create and display a button on choiceCanvas
Button DisplayButton(string text) {
Button button = Instantiate(buttonPrefab) as Button;
button.transform.SetParent(choiceCanvas.transform, false);
TMP_Text buttonText = button.GetComponentInChildren<TMP_Text>();
buttonText.text = text;
return button;
}
void AddClickable(string image, string ink, Vector2 position) {
if (assetStack.Count == 0) {
Debug.LogError("Can't add a clickable to an empty stack");
return;
}
Texture2D texture = Resources.Load<Texture2D>("Visuals/" + image);
if (texture == null) {
Debug.LogError("Clickable visual is null");
return;
}
GameObject go = Instantiate(clickablePrefab, position, Quaternion.identity, assetStack.Peek().canvas.gameObject.transform);
go.name = image;
go.GetComponent<Image>().sprite = Sprite.Create(texture, new Rect(0.0f, 0.0f, texture.width, texture.height), new Vector2(0.5f,0.5f), 100.0f);
go.GetComponent<Clickable>().inkFilename = ink;
if (go.GetComponent<Image>().sprite == null) {
Debug.LogError("Clickable sprite is null");
}
//BUGFIX: ink, wtf?
clickableParentCanvas.gameObject.SetActive(false);
clickableParentCanvas.gameObject.SetActive(true);
}
void RemoveClickable(string image) {
GameObject go = assetStack.Peek().canvas.gameObject.transform.Find(image)?.gameObject;
if (go != null) {
GameObject.Destroy(go);
} else {
Debug.LogError("Failed to find the GameObject " + image);
}
}
void AddZoomedImage(string image, Vector2 position) {
if (assetStack.Count == 0) {
Debug.LogError("Can't add a zoomed image to an empty stack");
return;
}
Texture2D texture = Resources.Load<Texture2D>("Visuals/" + image);
if (texture == null) {
Debug.LogError("zoomed image visual is null");
return;
}
GameObject go = Instantiate(zoomedImagePrefab, position, Quaternion.identity, zoomedCanvas.gameObject.transform);
go.name = image;
go.GetComponent<Image>().sprite = Sprite.Create(texture, new Rect(0.0f, 0.0f, texture.width, texture.height), new Vector2(0.5f,0.5f), 100.0f);
if (go.GetComponent<Image>().sprite == null) {
Debug.LogError("Zoomed image sprite is null");
}
//BUGFIX: ink, wtf?
zoomedCanvas.gameObject.SetActive(false);
zoomedCanvas.gameObject.SetActive(true);
}
void RemoveZoomedImage(string image) {
GameObject go = zoomedCanvas.gameObject.transform.Find(image)?.gameObject;
if (go != null) {
GameObject.Destroy(go);
} else {
Debug.LogError("Failed to find the GameObject " + image);
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 84ddd20bfb3bf5441b6e1ffad1a9be93
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: