diff --git a/Paths/Assets/Scripts/Algorithms/AStar.cs b/Paths/Assets/Scripts/Algorithms/AStar.cs index 3a70947..1c395ad 100644 --- a/Paths/Assets/Scripts/Algorithms/AStar.cs +++ b/Paths/Assets/Scripts/Algorithms/AStar.cs @@ -1,5 +1,4 @@ using System.Collections.Generic; -using System.Linq; using UnityEngine; namespace Algorithms { @@ -9,7 +8,6 @@ namespace Algorithms { private Stack _path; private List _openList; private List _closedList; - private List _states; public ChangeController ChangeController { get; private set; } public Vector2Int Start { get; private set; } @@ -17,7 +15,6 @@ namespace Algorithms { public AStar(NodeGrid nodeGrid) { this._nodeGrid = nodeGrid; - _states = new List(); ChangeController = new ChangeController(nodeGrid.RenderNodeTypes()); } @@ -35,7 +32,6 @@ namespace Algorithms { _openList = new List(); _closedList = new List(); - RecordState(); Node current = startNode; // add start node to Open List @@ -89,28 +85,11 @@ namespace Algorithms { if (temp == null) return null; do { _path.Push(temp); + ChangeController.AddChange(new Change(temp.Position.x, temp.Position.y, GridNodeType.Path, GridNodeType.Expanded)); temp = temp.Parent; } while (temp != null && !temp.Equals(startNode)); return _path; } - - /// - /// Records the current state of the pathfinding algorithm in the grid. - /// - public void RecordState() { - // TODO: Record pathfinding state information (stages, heuristic, statistical info) - this._states.Add( - new GridState(this._nodeGrid, this._openList.ToList(), this._closedList, Start, End, _path) - ); - } - - /// - /// Returns the current list of grid states of the pathfinding algorithm. - /// - /// A list of GridState objects representing the pathfinding algorithm's progress - public List GetStates() { - return this._states; - } } } \ No newline at end of file diff --git a/Paths/Assets/Scripts/GridState.cs b/Paths/Assets/Scripts/GridState.cs deleted file mode 100644 index 7bc7ceb..0000000 --- a/Paths/Assets/Scripts/GridState.cs +++ /dev/null @@ -1,51 +0,0 @@ -using System.Collections.Generic; -using System.Linq; -using Algorithms; -using UnityEngine; - -public struct GridState { - public readonly GridNodeType[,] Grid; - public readonly float Time; - - public GridState(NodeGrid grid, IReadOnlyList seen, IReadOnlyList expanded, Vector2Int start, - Vector2Int end, Stack path) { - this.Time = UnityEngine.Time.realtimeSinceStartup; - - Grid = new GridNodeType[grid.Width, grid.Height]; - - // Add walls and empty tiles - for (int x = 0; x < grid.Width; x++) { - for (int y = 0; y < grid.Height; y++) { - Grid[x, y] = grid.Grid[x, y].Walkable ? GridNodeType.Empty : GridNodeType.Wall; - } - } - - // Add 'seen' tiles - int length = seen.Count(); - for (int i = 0; i < length; i++) { - Node seenNode = seen[i]; - Grid[seenNode.Position.x, seenNode.Position.y] = GridNodeType.Seen; - } - - // Add 'expanded' tiles - length = expanded.Count(); - for (int i = 0; i < length; i++) { - Node expandedNode = expanded[i]; - Grid[expandedNode.Position.x, expandedNode.Position.y] = GridNodeType.Expanded; - } - - // Add 'path' tiles - if (path != null) { - Node[] pathArray = path.ToArray(); - length = pathArray.Length; - for (int i = 0; i < length; i++) { - Node pathNode = pathArray[i]; - Grid[pathNode.Position.x, pathNode.Position.y] = GridNodeType.Path; - } - } - - // Set start and end tiles - Grid[start.x, start.y] = GridNodeType.Start; - Grid[end.x, end.y] = GridNodeType.End; - } -} \ No newline at end of file diff --git a/Paths/Assets/Scripts/GridState.cs.meta b/Paths/Assets/Scripts/GridState.cs.meta deleted file mode 100644 index 14a2e8b..0000000 --- a/Paths/Assets/Scripts/GridState.cs.meta +++ /dev/null @@ -1,3 +0,0 @@ -fileFormatVersion: 2 -guid: 1a26aa7e514043e8911382088176258b -timeCreated: 1604887099 \ No newline at end of file diff --git a/Paths/Assets/Scripts/IPathfinding.cs b/Paths/Assets/Scripts/IPathfinding.cs index e8c7b5f..6707545 100644 --- a/Paths/Assets/Scripts/IPathfinding.cs +++ b/Paths/Assets/Scripts/IPathfinding.cs @@ -14,12 +14,6 @@ public interface IPathfinding { /// A List of NodeGridGrid objects representing the timeline of Pathfinding Stack FindPath(Vector2Int start, Vector2Int end); - /// - /// Records the current state of the pathfinding algorithm. Internal usage only. - /// - void RecordState(); - - List GetStates(); Vector2Int Start { get; } Vector2Int End { get; } ChangeController ChangeController { get; } diff --git a/Paths/Assets/Scripts/Manager.cs b/Paths/Assets/Scripts/Manager.cs index 1c2c9c9..81ea5b8 100644 --- a/Paths/Assets/Scripts/Manager.cs +++ b/Paths/Assets/Scripts/Manager.cs @@ -1,8 +1,10 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using Algorithms; using LevelGeneration; using TMPro; using UnityEngine; +using UnityEngine.Analytics; /// /// The primary controller of the entire application, managing state, events and sending commands @@ -36,8 +38,37 @@ public class Manager : MonoBehaviour { // Gizmos.DrawWireCube(transform.position, new Vector3(size, size, size)); // } + /// + /// Returns the current time multiplier, based on the latest change in the path. + /// + /// A positive non-zero float representing how fast the current frame should be processed. + private float CurrentMultiplier() { + if (_state.Index == -1) + return 1; + + switch (_state.CurrentChange.New) { + case GridNodeType.Path: + return 1/5f; + case GridNodeType.Empty: + break; + case GridNodeType.Wall: + break; + case GridNodeType.Start: + break; + case GridNodeType.End: + break; + case GridNodeType.Seen: + break; + case GridNodeType.Expanded: + break; + default: + throw new ArgumentOutOfRangeException(); + } + return 1; + } + public void Update() { - var increment = Time.deltaTime * speed; + var increment = Time.deltaTime * speed * CurrentMultiplier(); if (clampIncrement > 0) increment = Mathf.Clamp(increment, 0, _state.Count * Time.deltaTime / clampIncrement); _runtime += increment; @@ -80,7 +111,7 @@ public class Manager : MonoBehaviour { /// /// Scales the GridController GameObject to fit within the Camera /// - public void Resize() { + private void Resize() { float ratioImage = (float) gridController.width / gridController.height; float ratioScreen = mainCamera.aspect;