Open-sourced some code

This commit is contained in:
2019-03-08 09:54:14 +11:00
commit 645272872c
142 changed files with 3028 additions and 0 deletions
@@ -0,0 +1,110 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
namespace DialogSystem {
public class DialogCanvas : MonoBehaviour {
//public access references
public Image dialogPanel;
public TextMeshProUGUI dialogText;
//internal variables
bool visible;
List<string> internalTextList;
//fine-tuning the interface
float speed = 0.25f;
float speedCharCount = 0;
void Start() {
SetVisible(false);
}
void Update() {
if (!PauseManager.Instance.Paused && visible) {
HandleInput();
}
}
void FixedUpdate() {
if (visible) {
ProcessSpeed();
ProcessText();
}
}
public void SetVisible(bool b) {
visible = b;
dialogPanel.gameObject.SetActive(visible);
dialogText.gameObject.SetActive(visible);
}
public bool GetVisible() {
return visible;
}
public void SetText(List<string> stringList, bool setVisible = true) {
internalTextList = stringList;
dialogText.text = "";
SetVisible(setVisible);
}
void HandleInput() {
if (GamePad.GetState().Pressed(CButton.A)) {
if (internalTextList[0].Length > speedCharCount) {
//skip the text scroll
speedCharCount = internalTextList[0].Length;
} else {
//skip to the next "page"
internalTextList.RemoveAt(0);
speedCharCount = 0;
//if there isn't another "page", go dormant
if (internalTextList.Count == 0) {
SetVisible(false);
}
}
}
}
void ProcessSpeed() {
switch(ConfigurationManager.Instance.textSpeed) {
case "Fast":
speed = 1f;
break;
case "Normal":
speed = 0.25f;
break;
case "Slow":
speed = 0.1f;
break;
}
}
void ProcessText() {
//if the list of text has run out
if (internalTextList.Count == 0) {
SetVisible(false);
return;
}
SetVisible(true);
//only show the first "speedCharCount" characters
string thisLine = internalTextList[0];
if (speedCharCount >= thisLine.Length) {
dialogText.text = thisLine;
return;
}
speedCharCount += speed;
thisLine = thisLine.Substring(0, (int)Mathf.Floor(speedCharCount));
dialogText.text = thisLine;
}
}
}
+76
View File
@@ -0,0 +1,76 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class HUDCanvas : MonoBehaviour {
//public access members
public Sprite[] sparkSprites;
public Sprite[] flameSprites;
//public properties
int _sparkLevel = 0;
public int SparkLevel {
get {
return _sparkLevel;
}
set {
_sparkLevel = value;
if (_sparkLevel < 0) {
_sparkLevel = 0;
}
if (_sparkLevel > sparkSprites.Length * (flameSprites.Length - 1)) {
_sparkLevel = sparkSprites.Length * (flameSprites.Length - 1);
}
}
}
public int FlameLevel {
get {
return _sparkLevel / sparkSprites.Length;
}
set {
_sparkLevel = value * sparkSprites.Length + _sparkLevel % sparkSprites.Length;
while(_sparkLevel < 0) {
_sparkLevel += sparkSprites.Length;
}
while(_sparkLevel > sparkSprites.Length * (flameSprites.Length - 1)) {
_sparkLevel -= 1;
}
}
}
//internal members
Image[] childImages;
void Start() {
childImages = GetComponentsInChildren<Image>();
}
void Update() {
HandleGraphics();
DebugHandleInput();
}
void HandleGraphics() {
childImages[0].sprite = sparkSprites[SparkLevel % sparkSprites.Length];
childImages[1].sprite = flameSprites[SparkLevel / sparkSprites.Length];
}
void DebugHandleInput() {
if (Input.GetKeyDown("1")) {
SparkLevel--;
}
if (Input.GetKeyDown("2")) {
SparkLevel++;
}
if (Input.GetKeyDown("3")) {
FlameLevel--;
}
if (Input.GetKeyDown("4")) {
FlameLevel++;
}
}
}
@@ -0,0 +1,23 @@
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
namespace MenuSystem {
public class ConfirmDelete : MenuOption {
//public access members
public string fileName;
public GameObject loadCanvas;
public GameObject confirmationCanvas;
public override void Execute() {
File.Delete(fileName);
confirmationCanvas.SetActive(false);
loadCanvas.SetActive(true);
}
public override void Scroll(float x) {
//DO NOTHING
}
}
}
@@ -0,0 +1,30 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
namespace MenuSystem {
public class ConfirmLoad : MenuOption {
//public access members
public SaveFileManager.SaveSlot saveData;
public string fileName;
void Update() {
if (transform.childCount > 0) {
//BUGFIX: I don't know why this is disabled here
transform.GetChild(0).GetComponent<Image>().enabled = true;
}
}
public override void Execute() {
SaveFileManager.saveSlotFileName = fileName;
SaveFileManager.LoadedSaveSlot = saveData;
SceneManager.LoadScene("Gameplay");
}
public override void Scroll(float x) {
//DO NOTHING
}
}
}
@@ -0,0 +1,40 @@
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.UI;
namespace MenuSystem {
public class LoadCanvas : MonoBehaviour {
//public access members
public GameObject saveSlotPrefab;
public GameObject confirmationCanvas;
void OnEnable() {
//load the save objects
DirectoryInfo info = new DirectoryInfo(Application.persistentDataPath);
//create the load menu
foreach(var file in info.GetFiles("*.sav")) {
GameObject saveSlot = Instantiate(saveSlotPrefab) as GameObject;
saveSlot.transform.SetParent(transform); //BUGFIX: unity bug
saveSlot.GetComponent<SaveSlot>().SetSaveSlotInfo(SaveFileManager.LoadData(file.FullName));
saveSlot.GetComponent<SaveSlot>().fileName = file.FullName;
saveSlot.GetComponent<SaveSlot>().loadCanvas = transform.gameObject;
saveSlot.GetComponent<SaveSlot>().confirmationCanvas = confirmationCanvas;
}
//Move the "back" option to the bottom
transform.GetChild(0).SetAsLastSibling();
}
void OnDisable() {
foreach (Transform child in transform) {
if (child.GetComponent<SaveSlot>() != null) {
GameObject.Destroy(child.gameObject);
}
}
}
}
}
@@ -0,0 +1,50 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
namespace MenuSystem {
public class SaveSlot : MenuOption {
public string fileName;
public GameObject loadCanvas;
public GameObject confirmationCanvas;
//private access members
SaveFileManager.SaveSlot saveData;
public void SetSaveSlotInfo(SaveFileManager.SaveSlot saveSlot) {
TextMeshProUGUI[] texts = GetComponentsInChildren<TextMeshProUGUI>(true);
texts[0].text = saveSlot.currentLocation;
TimeSpan time = TimeSpan.FromSeconds(saveSlot.secondsPlaying);
texts[1].text = time.ToString(@"hh\:mm\:ss");
saveData = saveSlot;
}
public override void Execute() {
//show a confirmation canvas
confirmationCanvas.transform.GetChild(0).GetComponent<ConfirmLoad>().saveData = saveData;
confirmationCanvas.transform.GetChild(0).GetComponent<ConfirmLoad>().fileName = fileName;
confirmationCanvas.transform.GetChild(1).GetComponent<ConfirmDelete>().fileName = fileName;
//BUGFIX: move the cursor somewhere safe
transform.Find("Cursor").SetParent(confirmationCanvas.transform.GetChild(0));
//BUGFIX: wait a frame to show the confirmation page, to make sure the cursor is safe
StartCoroutine(DoStuffNextFrame());
}
IEnumerator DoStuffNextFrame() {
yield return null;
loadCanvas.SetActive(false);
confirmationCanvas.SetActive(true);
}
public override void Scroll(float x) {
//DO NOTHING
}
}
}
@@ -0,0 +1,12 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace MenuSystem {
class Quit : MenuOption {
override public void Execute() {
Application.Quit();
Debug.Log("Quit called");
}
}
}
@@ -0,0 +1,12 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
namespace MenuSystem {
class StartGame : MenuOption {
override public void Execute() {
SceneManager.LoadScene("Gameplay");
}
}
}
+144
View File
@@ -0,0 +1,144 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace MenuSystem {
public class MenuCanvas : MonoBehaviour {
//public access members
public Image cursorImage;
public bool floating = false;
//private members
MenuOption[] menuOptions = null;
//fine-tuned input
const float deadZone = 0.25f;
const float inputDelay = 0.5f;
const float scrollDelay = 0.1f;
float lastInputX = float.NegativeInfinity;
float lastInputY = float.NegativeInfinity;
void OnEnable() {
menuOptions = GetComponentsInChildren<MenuOption>();
//set up the cursor
menuOptions[0].hover = true;
RectTransform rt = cursorImage.GetComponent<RectTransform>();
rt.SetParent(menuOptions[0].transform);
rt.offsetMin = new Vector2(0, 0);
rt.offsetMax = new Vector2(0, 0);
HandleGraphics();
}
void OnDisable() {
//BUGFIX: reset this menu tree if closing the menu
foreach (Transform child in transform) {
child.gameObject.SetActive(true);
}
}
void Update() {
//BUGFIX: remove all non-active elements
menuOptions = Array.FindAll(menuOptions, (MenuOption option) => option.gameObject.active);
HandleInput();
HandleGraphics();
}
void HandleInput() {
//press a button
if (GamePad.GetState().Pressed(CButton.A)) {
foreach(MenuOption option in menuOptions) {
if (option.hover) {
option.Execute();
}
}
return;
}
//scroll left or right
if (Mathf.Abs(GamePad.GetAxis(CAxis.LX)) >= deadZone && Time.unscaledTime - lastInputX > scrollDelay) {
lastInputX = Time.unscaledTime;
foreach(MenuOption option in menuOptions) {
if (option.hover) {
option.Scroll(GamePad.GetAxis(CAxis.LX));
}
}
return;
}
//keyboard/gamepad scroll up
if (GamePad.GetAxis(CAxis.LY) < -deadZone && Time.unscaledTime - lastInputY > inputDelay) {
lastInputY = Time.unscaledTime;
//move the cursor one up
for (int i = 1; i < menuOptions.Length; i++) {
//find the current hovered item
if (menuOptions[i].hover) {
//move the hover up
menuOptions[i].hover = false;
menuOptions[i-1].hover = true;
break;
}
}
}
//keyboard/gamepad scroll down
if (GamePad.GetAxis(CAxis.LY) > deadZone && Time.unscaledTime - lastInputY > inputDelay) {
lastInputY = Time.unscaledTime;
//move the cursor one down
for (int i = 0; i < menuOptions.Length - 1; i++) {
//find the current hovered item
if (menuOptions[i].hover) {
//move the hover down
menuOptions[i].hover = false;
menuOptions[i+1].hover = true;
break;
}
}
}
//deadzone reset lastInputX
if (Mathf.Abs(GamePad.GetAxis(CAxis.LX)) < deadZone) {
lastInputX = float.NegativeInfinity;
}
//deadzone reset lastInputY
if (Mathf.Abs(GamePad.GetAxis(CAxis.LY)) < deadZone) {
lastInputY = float.NegativeInfinity;
}
}
void HandleGraphics() {
bool hoverSet = false;
//update the graphics
foreach(MenuOption option in menuOptions) {
if (option.hover) {
if (hoverSet) {
option.hover = false;
continue;
}
RectTransform rt = cursorImage.GetComponent<RectTransform>();
rt.SetParent(option.transform);
rt.anchorMin = new Vector2(0, 0);
rt.anchorMax = new Vector2(1, 1);
rt.offsetMin = new Vector2(0, 0);
rt.offsetMax = new Vector2(0, 0);
hoverSet = true;
}
}
if (floating) {
float offset = cursorImage.transform.position.y - Screen.height / 2;
transform.position = new Vector2(transform.position.x, transform.position.y - offset);
}
}
}
}
@@ -0,0 +1,16 @@
using UnityEngine;
namespace MenuSystem {
public class MenuOption : MonoBehaviour {
//if this option is being hovered over, logically
public bool hover = false;
public virtual void Execute() {
//DO NOTHING
}
public virtual void Scroll(float x) {
//DO NOTHING
}
}
}
@@ -0,0 +1,37 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
namespace MenuSystem {
public class TextSpeed : MenuOption {
//component references
ConfigurationManager configManager;
TextMeshProUGUI text;
void Start() {
configManager = ConfigurationManager.Instance;
text = GetComponent<TextMeshProUGUI>();
configManager.textSpeed = (configManager.textSpeed != null && configManager.textSpeed != "" ? configManager.textSpeed : "Normal");
text.text = configManager.textSpeed;
}
override public void Execute() {
switch(configManager.textSpeed) {
case "Normal":
configManager.textSpeed = "Slow";
break;
case "Slow":
configManager.textSpeed = "Fast";
break;
case "Fast":
configManager.textSpeed = "Normal";
break;
}
text.text = configManager.textSpeed;
}
}
}
@@ -0,0 +1,40 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
namespace MenuSystem {
public class Volume : MenuOption {
//component references
ConfigurationManager configManager;
TextMeshProUGUI text;
void Start() {
configManager = ConfigurationManager.Instance;
text = GetComponent<TextMeshProUGUI>();
configManager.volume = (configManager.volume != null && configManager.volume != 0 ? configManager.volume : 100f);
text.text = configManager.volume.ToString();
UpdateMasterVolume();
}
public override void Execute() {
//DO NOTHING
}
public override void Scroll(float x) {
//DO NOTHING
configManager.volume += x;
configManager.volume = Mathf.Clamp(configManager.volume, 0f, 100f);
text.text = configManager.volume.ToString();
UpdateMasterVolume();
}
void UpdateMasterVolume() {
AudioListener.volume = configManager.volume / 100f;
}
}
}
@@ -0,0 +1,13 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
namespace MenuSystem {
public class QuitToMainMenu : MenuOption {
public override void Execute() {
SceneManager.LoadScene("MainMenu");
PauseManager.Instance.Paused = false;
}
}
}
@@ -0,0 +1,11 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace MenuSystem {
public class ReturnToGame : MenuOption {
public override void Execute() {
PauseManager.Instance.Paused = false;
}
}
}
@@ -0,0 +1,17 @@
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using TMPro;
namespace MenuSystem {
public class Heading : MonoBehaviour {
void Start() {
TextMeshProUGUI text = GetComponent<TextMeshProUGUI>();
if (!File.Exists(SaveFileManager.saveSlotFileName)) {
text.text = "Save The Game?";
}
}
}
}
@@ -0,0 +1,15 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace MenuSystem {
public class HideMeIf : MonoBehaviour {
public SaveHandler saveHandler;
void OnEnable() {
if (!saveHandler.saveMenuAvailable) {
gameObject.SetActive(false);
}
}
}
}
@@ -0,0 +1,27 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace MenuSystem {
public class SaveGame : MenuOption {
public SaveHandler saveHandler;
public Canvas oldMenu;
public Canvas newMenu;
public override void Execute() {
//calculate playtime
SaveFileManager.LoadedSaveSlot.secondsPlaying += Time.time - saveHandler.startTime;
saveHandler.startTime = Time.time;
//save the game
SaveFileManager.SaveData(SaveFileManager.LoadedSaveSlot, SaveFileManager.saveSlotFileName);
oldMenu.gameObject.SetActive(false);
newMenu.gameObject.SetActive(true);
}
public override void Scroll(float x) {
//DO NOTHING
}
}
}
@@ -0,0 +1,16 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace MenuSystem {
class SwitchMenus : MenuOption {
public Canvas oldMenu;
public Canvas newMenu;
override public void Execute() {
oldMenu.gameObject.SetActive(false);
newMenu.gameObject.SetActive(true);
}
}
}