diff --git a/Paths/Assets/Scripts/ChangeController.cs b/Paths/Assets/Scripts/ChangeController.cs index 1f084b9..db6c5e8 100644 --- a/Paths/Assets/Scripts/ChangeController.cs +++ b/Paths/Assets/Scripts/ChangeController.cs @@ -92,7 +92,7 @@ public class ChangeController { Reset(); // resetting by copying values instead of mutating might be easier. else { for (int i = 0; i < n; i++) { - Change cur = _changes[--Index]; + Change cur = _changes[Index--]; // post decrement as we apply the current Change's old, not the previous Current[cur.X, cur.Y] = cur.Old; SetDirty(new Vector2Int(cur.X, cur.Y)); } diff --git a/Paths/Assets/Scripts/Manager.cs b/Paths/Assets/Scripts/Manager.cs index 81ea5b8..c1e3407 100644 --- a/Paths/Assets/Scripts/Manager.cs +++ b/Paths/Assets/Scripts/Manager.cs @@ -3,8 +3,9 @@ using System.Collections.Generic; using Algorithms; using LevelGeneration; using TMPro; +using UnityEditor; using UnityEngine; -using UnityEngine.Analytics; +using UnityEngine.UI; /// /// The primary controller of the entire application, managing state, events and sending commands @@ -16,12 +17,17 @@ public class Manager : MonoBehaviour { private Stack _path; private float _runtime; + public float speed; + public float clampIncrement; + public bool moving = true; + public Camera mainCamera; public GameObject gridObject; public GridController gridController; public TextMeshPro debugText; - public float speed; - public float clampIncrement; + public Slider progressSlider; + private float? _moveTo; + private bool _allowPausing = true; private int CurrentIndex { get => (int) _runtime; @@ -31,6 +37,8 @@ public class Manager : MonoBehaviour { public void Start() { GeneratePath(); Resize(); + + progressSlider.onValueChanged.AddListener((value) => MoveToSlider(value)); } // public void OnDrawGizmos() { @@ -45,10 +53,10 @@ public class Manager : MonoBehaviour { private float CurrentMultiplier() { if (_state.Index == -1) return 1; - + switch (_state.CurrentChange.New) { case GridNodeType.Path: - return 1/5f; + return 1 / 5f; case GridNodeType.Empty: break; case GridNodeType.Wall: @@ -64,24 +72,41 @@ public class Manager : MonoBehaviour { default: throw new ArgumentOutOfRangeException(); } + return 1; } - - public void Update() { - var increment = Time.deltaTime * speed * CurrentMultiplier(); - if (clampIncrement > 0) - increment = Mathf.Clamp(increment, 0, _state.Count * Time.deltaTime / clampIncrement); - _runtime += increment; + public void Update() { + // Toggle pause with space + if (_allowPausing && Input.GetKeyDown(KeyCode.Space)) { + moving = !moving; + } + + // Increment index if unpaused and not clicking (implying slider may be interacted with) + if (moving && !Input.GetMouseButton(0)) { + var increment = Time.deltaTime * speed * CurrentMultiplier(); + if (clampIncrement > 0) + increment = Mathf.Clamp(increment, 0, _state.Count * Time.deltaTime / clampIncrement); + _runtime += increment; + } + + // Load next state in grid or update text if (CurrentIndex < _state.Count) LoadNextState(); else { + // No new states to load, generate new grid GeneratePath(); - CurrentIndex = 0; } + + // Update progress slider silently + progressSlider.SetValueWithoutNotify(_runtime / _state.Count); } + /// + /// Generates a new grid and runs pathfinding. + /// private void GeneratePath() { + CurrentIndex = 0; var nodeGrid = new NodeGrid(gridController.width, gridController.height); Vector2Int start = nodeGrid.RandomPosition(); @@ -98,6 +123,9 @@ public class Manager : MonoBehaviour { _state = _algorithm.ChangeController; } + /// + /// Loads the appropriate grid state into the shader via the ChangeController instance. + /// private void LoadNextState() { _state.MoveTo(CurrentIndex); gridController.LoadDirtyGridState(_state.Current, _state.DirtyFlags); diff --git a/Paths/Assets/Shaders/GridShader.shader b/Paths/Assets/Shaders/GridShader.shader index 9f97a04..fe4a044 100644 --- a/Paths/Assets/Shaders/GridShader.shader +++ b/Paths/Assets/Shaders/GridShader.shader @@ -41,7 +41,7 @@ Shader "PDT Shaders/TestGrid" static const float4 _gridColors[7] = { float4(255 / 255.0, 255 / 255.0, 255 / 255.0, 1.0), // Empty - float4(0 / 255.0, 0 / 255.0, 0 / 255.0, 1.0), // Wall + float4(5 / 255.0, 5 / 255.0, 5 / 255.0, 1.0), // Wall float4(0 / 255.0, 255 / 255.0, 0 / 255.0, 1.0), // Start float4(255 / 255.0, 0 / 255.0, 0 / 255.0, 1.0), // End float4(252 / 255.0, 236 / 255.0, 3 / 255.0, 1.0), // Seen