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,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);
}
}
}