diff --git a/Paths/Assets/Scenes/Default.unity b/Paths/Assets/Scenes/Default.unity index 98ecd58..ce99e3e 100644 --- a/Paths/Assets/Scenes/Default.unity +++ b/Paths/Assets/Scenes/Default.unity @@ -248,6 +248,7 @@ MonoBehaviour: m_EditorClassIdentifier: gridController: {fileID: 2092623185} debugText: {fileID: 1436240699} + speed: 7.18 --- !u!1 &1436240698 GameObject: m_ObjectHideFlags: 0 diff --git a/Paths/Assets/Scripts/GridState.cs b/Paths/Assets/Scripts/GridState.cs index 588af43..2b84ddc 100644 --- a/Paths/Assets/Scripts/GridState.cs +++ b/Paths/Assets/Scripts/GridState.cs @@ -1,18 +1,14 @@ -using System; -using System.Collections.Generic; -using System.Linq; +using System.Collections.Generic; using Algorithms; using UnityEngine; public class GridState { public readonly GridNodeType[,] Grid; - - // public List> Grid; - public float time; + public readonly float Time; public GridState(NodeGrid grid, IEnumerable seen, IEnumerable expanded, Vector2Int start, Vector2Int end, IReadOnlyCollection path) { - time = Time.realtimeSinceStartup; + this.Time = UnityEngine.Time.realtimeSinceStartup; Grid = new GridNodeType[grid.Width, grid.Height]; @@ -42,17 +38,4 @@ public class GridState { Grid[start.x, start.y] = GridNodeType.Start; Grid[end.x, end.y] = GridNodeType.End; } - - // public IEnumerable GetNodes() { - // return Grid.SelectMany(nodeList => nodeList).ToList(); - // } - - // public string RenderGrid() { - // string result = ""; - // foreach (List nodeTypes in Grid) { - // result = nodeTypes.Aggregate(result, (current, nodeType) => current + $"{(int) nodeType}") + "\n"; - // } - - // return result; - // } } \ No newline at end of file diff --git a/Paths/Assets/Scripts/Manager.cs b/Paths/Assets/Scripts/Manager.cs index a8b7136..abb9093 100644 --- a/Paths/Assets/Scripts/Manager.cs +++ b/Paths/Assets/Scripts/Manager.cs @@ -9,13 +9,21 @@ using UnityEngine; /// The primary controller of the entire application, managing state, events and sending commands /// public class Manager : MonoBehaviour { - public GridController gridController; private IPathfinding _algorithm; private List _states; private int _curIndex; private Stack path; - private float lastStart; + private float _lastStart; + private float _runtime; + + public GridController gridController; public TextMeshPro debugText; + public float speed; + + public int CurrentIndex { + get => (int) _runtime; + set => _runtime = value; + } public void Start() { _states = new List(); @@ -28,13 +36,15 @@ public class Manager : MonoBehaviour { } public void Update() { - if (_curIndex < _states.Count) + _runtime += Time.deltaTime * speed; + + if (CurrentIndex < _states.Count) this.LoadNextState(); else { try { - lastStart = Time.realtimeSinceStartup; + _lastStart = Time.realtimeSinceStartup; GeneratePath(); - _curIndex = 0; + CurrentIndex = 0; // _curIndex = path != null && path.Count > 30 ? 0 : _states.Count; } catch (ArgumentOutOfRangeException) { @@ -49,12 +59,12 @@ public class Manager : MonoBehaviour { // Vector2 start = nodeGrid.RandomPosition(); Vector2Int start = new Vector2Int(30, 30); Vector2Int end = nodeGrid.RandomPosition(); - + int wallCount = (int) (gridController.size * gridController.size * 0.25); for (int unused = 0; unused < wallCount; unused++) nodeGrid.AddRandomWall(); - + nodeGrid.GetNode(start).Walkable = true; nodeGrid.GetNode(end).Walkable = true; @@ -64,13 +74,12 @@ public class Manager : MonoBehaviour { } private void LoadNextState() { - GridState state = _states[_curIndex]; + GridState state = _states[CurrentIndex]; gridController.LoadGridState(state); - float change = state.time - lastStart; + float change = state.Time - _lastStart; string pathCount = path != null ? $"{path.Count}" : "N/A"; debugText.text = - $"{change * 1000.0:F1}ms\n{this._curIndex:000} / {this._states.Count:000}\nPath: {pathCount} tiles"; - _curIndex += 3; + $"{change * 1000.0:F1}ms\n{this.CurrentIndex:000} / {this._states.Count:000}\nPath: {pathCount} tiles"; } } \ No newline at end of file