add back Path rendering, add time multipliers based on latest GridNodeType changes, remove GridState.cs

+ small import/access level changes in Manager.cs
This commit is contained in:
Xevion
2020-11-24 22:00:33 -06:00
parent 944aaad6d6
commit 403756aaa9
5 changed files with 35 additions and 85 deletions

View File

@@ -1,5 +1,4 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using UnityEngine; using UnityEngine;
namespace Algorithms { namespace Algorithms {
@@ -9,7 +8,6 @@ namespace Algorithms {
private Stack<Node> _path; private Stack<Node> _path;
private List<Node> _openList; private List<Node> _openList;
private List<Node> _closedList; private List<Node> _closedList;
private List<GridState> _states;
public ChangeController ChangeController { get; private set; } public ChangeController ChangeController { get; private set; }
public Vector2Int Start { get; private set; } public Vector2Int Start { get; private set; }
@@ -17,7 +15,6 @@ namespace Algorithms {
public AStar(NodeGrid nodeGrid) { public AStar(NodeGrid nodeGrid) {
this._nodeGrid = nodeGrid; this._nodeGrid = nodeGrid;
_states = new List<GridState>();
ChangeController = new ChangeController(nodeGrid.RenderNodeTypes()); ChangeController = new ChangeController(nodeGrid.RenderNodeTypes());
} }
@@ -35,7 +32,6 @@ namespace Algorithms {
_openList = new List<Node>(); _openList = new List<Node>();
_closedList = new List<Node>(); _closedList = new List<Node>();
RecordState();
Node current = startNode; Node current = startNode;
// add start node to Open List // add start node to Open List
@@ -89,28 +85,11 @@ namespace Algorithms {
if (temp == null) return null; if (temp == null) return null;
do { do {
_path.Push(temp); _path.Push(temp);
ChangeController.AddChange(new Change(temp.Position.x, temp.Position.y, GridNodeType.Path, GridNodeType.Expanded));
temp = temp.Parent; temp = temp.Parent;
} while (temp != null && !temp.Equals(startNode)); } while (temp != null && !temp.Equals(startNode));
return _path; return _path;
} }
/// <summary>
/// Records the current state of the pathfinding algorithm in the grid.
/// </summary>
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)
);
}
/// <summary>
/// Returns the current list of grid states of the pathfinding algorithm.
/// </summary>
/// <returns>A list of GridState objects representing the pathfinding algorithm's progress</returns>
public List<GridState> GetStates() {
return this._states;
}
} }
} }

View File

@@ -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<Node> seen, IReadOnlyList<Node> expanded, Vector2Int start,
Vector2Int end, Stack<Node> 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;
}
}

View File

@@ -1,3 +0,0 @@
fileFormatVersion: 2
guid: 1a26aa7e514043e8911382088176258b
timeCreated: 1604887099

View File

@@ -14,12 +14,6 @@ public interface IPathfinding {
/// <returns>A List of NodeGridGrid objects representing the timeline of Pathfinding</returns> /// <returns>A List of NodeGridGrid objects representing the timeline of Pathfinding</returns>
Stack<Node> FindPath(Vector2Int start, Vector2Int end); Stack<Node> FindPath(Vector2Int start, Vector2Int end);
/// <summary>
/// Records the current state of the pathfinding algorithm. Internal usage only.
/// </summary>
void RecordState();
List<GridState> GetStates();
Vector2Int Start { get; } Vector2Int Start { get; }
Vector2Int End { get; } Vector2Int End { get; }
ChangeController ChangeController { get; } ChangeController ChangeController { get; }

View File

@@ -1,8 +1,10 @@
using System.Collections.Generic; using System;
using System.Collections.Generic;
using Algorithms; using Algorithms;
using LevelGeneration; using LevelGeneration;
using TMPro; using TMPro;
using UnityEngine; using UnityEngine;
using UnityEngine.Analytics;
/// <summary> /// <summary>
/// The primary controller of the entire application, managing state, events and sending commands /// 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)); // Gizmos.DrawWireCube(transform.position, new Vector3(size, size, size));
// } // }
/// <summary>
/// Returns the current time multiplier, based on the latest change in the path.
/// </summary>
/// <returns>A positive non-zero float representing how fast the current frame should be processed.</returns>
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() { public void Update() {
var increment = Time.deltaTime * speed; var increment = Time.deltaTime * speed * CurrentMultiplier();
if (clampIncrement > 0) if (clampIncrement > 0)
increment = Mathf.Clamp(increment, 0, _state.Count * Time.deltaTime / clampIncrement); increment = Mathf.Clamp(increment, 0, _state.Count * Time.deltaTime / clampIncrement);
_runtime += increment; _runtime += increment;
@@ -80,7 +111,7 @@ public class Manager : MonoBehaviour {
/// <summary> /// <summary>
/// Scales the GridController GameObject to fit within the Camera /// Scales the GridController GameObject to fit within the Camera
/// </summary> /// </summary>
public void Resize() { private void Resize() {
float ratioImage = (float) gridController.width / gridController.height; float ratioImage = (float) gridController.width / gridController.height;
float ratioScreen = mainCamera.aspect; float ratioScreen = mainCamera.aspect;