mirror of
https://github.com/Xevion/Paths.git
synced 2025-12-13 00:12:20 -06:00
use Time.deltaTime based index calculations for changable precise state change code
This commit is contained in:
@@ -248,6 +248,7 @@ MonoBehaviour:
|
|||||||
m_EditorClassIdentifier:
|
m_EditorClassIdentifier:
|
||||||
gridController: {fileID: 2092623185}
|
gridController: {fileID: 2092623185}
|
||||||
debugText: {fileID: 1436240699}
|
debugText: {fileID: 1436240699}
|
||||||
|
speed: 7.18
|
||||||
--- !u!1 &1436240698
|
--- !u!1 &1436240698
|
||||||
GameObject:
|
GameObject:
|
||||||
m_ObjectHideFlags: 0
|
m_ObjectHideFlags: 0
|
||||||
|
|||||||
@@ -1,18 +1,14 @@
|
|||||||
using System;
|
using System.Collections.Generic;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using Algorithms;
|
using Algorithms;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
public class GridState {
|
public class GridState {
|
||||||
public readonly GridNodeType[,] Grid;
|
public readonly GridNodeType[,] Grid;
|
||||||
|
public readonly float Time;
|
||||||
// public List<List<GridNodeType>> Grid;
|
|
||||||
public float time;
|
|
||||||
|
|
||||||
public GridState(NodeGrid grid, IEnumerable<Node> seen, IEnumerable<Node> expanded, Vector2Int start,
|
public GridState(NodeGrid grid, IEnumerable<Node> seen, IEnumerable<Node> expanded, Vector2Int start,
|
||||||
Vector2Int end, IReadOnlyCollection<Node> path) {
|
Vector2Int end, IReadOnlyCollection<Node> path) {
|
||||||
time = Time.realtimeSinceStartup;
|
this.Time = UnityEngine.Time.realtimeSinceStartup;
|
||||||
|
|
||||||
Grid = new GridNodeType[grid.Width, grid.Height];
|
Grid = new GridNodeType[grid.Width, grid.Height];
|
||||||
|
|
||||||
@@ -42,17 +38,4 @@ public class GridState {
|
|||||||
Grid[start.x, start.y] = GridNodeType.Start;
|
Grid[start.x, start.y] = GridNodeType.Start;
|
||||||
Grid[end.x, end.y] = GridNodeType.End;
|
Grid[end.x, end.y] = GridNodeType.End;
|
||||||
}
|
}
|
||||||
|
|
||||||
// public IEnumerable<GridNodeType> GetNodes() {
|
|
||||||
// return Grid.SelectMany(nodeList => nodeList).ToList();
|
|
||||||
// }
|
|
||||||
|
|
||||||
// public string RenderGrid() {
|
|
||||||
// string result = "";
|
|
||||||
// foreach (List<GridNodeType> nodeTypes in Grid) {
|
|
||||||
// result = nodeTypes.Aggregate(result, (current, nodeType) => current + $"{(int) nodeType}") + "\n";
|
|
||||||
// }
|
|
||||||
|
|
||||||
// return result;
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
@@ -9,13 +9,21 @@ using UnityEngine;
|
|||||||
/// 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
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class Manager : MonoBehaviour {
|
public class Manager : MonoBehaviour {
|
||||||
public GridController gridController;
|
|
||||||
private IPathfinding _algorithm;
|
private IPathfinding _algorithm;
|
||||||
private List<GridState> _states;
|
private List<GridState> _states;
|
||||||
private int _curIndex;
|
private int _curIndex;
|
||||||
private Stack<Node> path;
|
private Stack<Node> path;
|
||||||
private float lastStart;
|
private float _lastStart;
|
||||||
|
private float _runtime;
|
||||||
|
|
||||||
|
public GridController gridController;
|
||||||
public TextMeshPro debugText;
|
public TextMeshPro debugText;
|
||||||
|
public float speed;
|
||||||
|
|
||||||
|
public int CurrentIndex {
|
||||||
|
get => (int) _runtime;
|
||||||
|
set => _runtime = value;
|
||||||
|
}
|
||||||
|
|
||||||
public void Start() {
|
public void Start() {
|
||||||
_states = new List<GridState>();
|
_states = new List<GridState>();
|
||||||
@@ -28,13 +36,15 @@ public class Manager : MonoBehaviour {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void Update() {
|
public void Update() {
|
||||||
if (_curIndex < _states.Count)
|
_runtime += Time.deltaTime * speed;
|
||||||
|
|
||||||
|
if (CurrentIndex < _states.Count)
|
||||||
this.LoadNextState();
|
this.LoadNextState();
|
||||||
else {
|
else {
|
||||||
try {
|
try {
|
||||||
lastStart = Time.realtimeSinceStartup;
|
_lastStart = Time.realtimeSinceStartup;
|
||||||
GeneratePath();
|
GeneratePath();
|
||||||
_curIndex = 0;
|
CurrentIndex = 0;
|
||||||
// _curIndex = path != null && path.Count > 30 ? 0 : _states.Count;
|
// _curIndex = path != null && path.Count > 30 ? 0 : _states.Count;
|
||||||
}
|
}
|
||||||
catch (ArgumentOutOfRangeException) {
|
catch (ArgumentOutOfRangeException) {
|
||||||
@@ -49,12 +59,12 @@ public class Manager : MonoBehaviour {
|
|||||||
// Vector2 start = nodeGrid.RandomPosition();
|
// Vector2 start = nodeGrid.RandomPosition();
|
||||||
Vector2Int start = new Vector2Int(30, 30);
|
Vector2Int start = new Vector2Int(30, 30);
|
||||||
Vector2Int end = nodeGrid.RandomPosition();
|
Vector2Int end = nodeGrid.RandomPosition();
|
||||||
|
|
||||||
|
|
||||||
int wallCount = (int) (gridController.size * gridController.size * 0.25);
|
int wallCount = (int) (gridController.size * gridController.size * 0.25);
|
||||||
for (int unused = 0; unused < wallCount; unused++)
|
for (int unused = 0; unused < wallCount; unused++)
|
||||||
nodeGrid.AddRandomWall();
|
nodeGrid.AddRandomWall();
|
||||||
|
|
||||||
nodeGrid.GetNode(start).Walkable = true;
|
nodeGrid.GetNode(start).Walkable = true;
|
||||||
nodeGrid.GetNode(end).Walkable = true;
|
nodeGrid.GetNode(end).Walkable = true;
|
||||||
|
|
||||||
@@ -64,13 +74,12 @@ public class Manager : MonoBehaviour {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void LoadNextState() {
|
private void LoadNextState() {
|
||||||
GridState state = _states[_curIndex];
|
GridState state = _states[CurrentIndex];
|
||||||
gridController.LoadGridState(state);
|
gridController.LoadGridState(state);
|
||||||
|
|
||||||
float change = state.time - lastStart;
|
float change = state.Time - _lastStart;
|
||||||
string pathCount = path != null ? $"{path.Count}" : "N/A";
|
string pathCount = path != null ? $"{path.Count}" : "N/A";
|
||||||
debugText.text =
|
debugText.text =
|
||||||
$"{change * 1000.0:F1}ms\n{this._curIndex:000} / {this._states.Count:000}\nPath: {pathCount} tiles";
|
$"{change * 1000.0:F1}ms\n{this.CurrentIndex:000} / {this._states.Count:000}\nPath: {pathCount} tiles";
|
||||||
_curIndex += 3;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user