summaryrefslogtreecommitdiff
path: root/Pathfinding Visualizer/Assets/CodeMonkey/Utils/FunctionUpdater.cs
diff options
context:
space:
mode:
authorSimon O'Shea <osheas1@wit.edu>2023-07-31 18:45:01 -0400
committerSimon O'Shea <osheas1@wit.edu>2023-07-31 18:45:01 -0400
commit5f9e8f979deb94e9219113ed09b49142c040cb57 (patch)
treea67bd606205f7fb28e48d351028b3478bc49145b /Pathfinding Visualizer/Assets/CodeMonkey/Utils/FunctionUpdater.cs
parent4699ce72871eaa6ee27c01971383b9d1fced1946 (diff)
import successful, grid has visuals
cell values and grid lines are displayed. no interaction available yet.
Diffstat (limited to 'Pathfinding Visualizer/Assets/CodeMonkey/Utils/FunctionUpdater.cs')
-rw-r--r--Pathfinding Visualizer/Assets/CodeMonkey/Utils/FunctionUpdater.cs157
1 files changed, 157 insertions, 0 deletions
diff --git a/Pathfinding Visualizer/Assets/CodeMonkey/Utils/FunctionUpdater.cs b/Pathfinding Visualizer/Assets/CodeMonkey/Utils/FunctionUpdater.cs
new file mode 100644
index 0000000..307a7d29
--- /dev/null
+++ b/Pathfinding Visualizer/Assets/CodeMonkey/Utils/FunctionUpdater.cs
@@ -0,0 +1,157 @@
+/*
+ ------------------- Code Monkey -------------------
+
+ Thank you for downloading the Code Monkey Utilities
+ I hope you find them useful in your projects
+ If you have any questions use the contact form
+ Cheers!
+
+ unitycodemonkey.com
+ --------------------------------------------------
+ */
+
+using System;
+using UnityEngine;
+using System.Collections.Generic;
+
+namespace CodeMonkey.Utils {
+
+ /*
+ * Calls function on every Update until it returns true
+ * */
+ public class FunctionUpdater {
+
+ /*
+ * Class to hook Actions into MonoBehaviour
+ * */
+ private class MonoBehaviourHook : MonoBehaviour {
+
+ public Action OnUpdate;
+
+ private void Update() {
+ if (OnUpdate != null) OnUpdate();
+ }
+
+ }
+
+ private static List<FunctionUpdater> updaterList; // Holds a reference to all active updaters
+ private static GameObject initGameObject; // Global game object used for initializing class, is destroyed on scene change
+
+ private static void InitIfNeeded() {
+ if (initGameObject == null) {
+ initGameObject = new GameObject("FunctionUpdater_Global");
+ updaterList = new List<FunctionUpdater>();
+ }
+ }
+
+
+
+
+ public static FunctionUpdater Create(Action updateFunc) {
+ return Create(() => { updateFunc(); return false; }, "", true, false);
+ }
+
+ public static FunctionUpdater Create(Action updateFunc, string functionName) {
+ return Create(() => { updateFunc(); return false; }, functionName, true, false);
+ }
+
+ public static FunctionUpdater Create(Func<bool> updateFunc) {
+ return Create(updateFunc, "", true, false);
+ }
+
+ public static FunctionUpdater Create(Func<bool> updateFunc, string functionName) {
+ return Create(updateFunc, functionName, true, false);
+ }
+
+ public static FunctionUpdater Create(Func<bool> updateFunc, string functionName, bool active) {
+ return Create(updateFunc, functionName, active, false);
+ }
+
+ public static FunctionUpdater Create(Func<bool> updateFunc, string functionName, bool active, bool stopAllWithSameName) {
+ InitIfNeeded();
+
+ if (stopAllWithSameName) {
+ StopAllUpdatersWithName(functionName);
+ }
+
+ GameObject gameObject = new GameObject("FunctionUpdater Object " + functionName, typeof(MonoBehaviourHook));
+ FunctionUpdater functionUpdater = new FunctionUpdater(gameObject, updateFunc, functionName, active);
+ gameObject.GetComponent<MonoBehaviourHook>().OnUpdate = functionUpdater.Update;
+
+ updaterList.Add(functionUpdater);
+ return functionUpdater;
+ }
+
+ private static void RemoveUpdater(FunctionUpdater funcUpdater) {
+ InitIfNeeded();
+ updaterList.Remove(funcUpdater);
+ }
+
+ public static void DestroyUpdater(FunctionUpdater funcUpdater) {
+ InitIfNeeded();
+ if (funcUpdater != null) {
+ funcUpdater.DestroySelf();
+ }
+ }
+
+ public static void StopUpdaterWithName(string functionName) {
+ InitIfNeeded();
+ for (int i = 0; i < updaterList.Count; i++) {
+ if (updaterList[i].functionName == functionName) {
+ updaterList[i].DestroySelf();
+ return;
+ }
+ }
+ }
+
+ public static void StopAllUpdatersWithName(string functionName) {
+ InitIfNeeded();
+ for (int i = 0; i < updaterList.Count; i++) {
+ if (updaterList[i].functionName == functionName) {
+ updaterList[i].DestroySelf();
+ i--;
+ }
+ }
+ }
+
+
+
+
+
+ private GameObject gameObject;
+ private string functionName;
+ private bool active;
+ private Func<bool> updateFunc; // Destroy Updater if return true;
+
+ public FunctionUpdater(GameObject gameObject, Func<bool> updateFunc, string functionName, bool active) {
+ this.gameObject = gameObject;
+ this.updateFunc = updateFunc;
+ this.functionName = functionName;
+ this.active = active;
+ }
+
+ public void Pause() {
+ active = false;
+ }
+
+ public void Resume() {
+ active = true;
+ }
+
+ private void Update() {
+ if (!active) return;
+ if (updateFunc()) {
+ DestroySelf();
+ }
+ }
+
+ public void DestroySelf() {
+ RemoveUpdater(this);
+ if (gameObject != null) {
+ UnityEngine.Object.Destroy(gameObject);
+ }
+ }
+
+ }
+
+} \ No newline at end of file