diff --git a/Paths/Assets/Scripts/Algorithms/AStar.cs b/Paths/Assets/Scripts/Algorithms/AStar.cs
index aafc771..58aeeb3 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 {
@@ -15,7 +14,7 @@ namespace Algorithms {
public Vector2Int End { get; private set; }
public AStar(NodeGrid nodeGrid) {
- this.NodeGrid = nodeGrid;
+ NodeGrid = nodeGrid;
ChangeController = new ChangeController(nodeGrid.RenderNodeTypes());
}
@@ -78,7 +77,7 @@ namespace Algorithms {
return null;
}
- // Fix start position being overriden
+ // Fix start and end position being overriden
ChangeController.RemovePositions(start, 1);
// if all good, return path
@@ -86,7 +85,8 @@ 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));
+ ChangeController.AddChange(new Change(temp.Position.x, temp.Position.y, GridNodeType.Path,
+ GridNodeType.Expanded));
temp = temp.Parent;
} while (temp != null && !temp.Equals(startNode));
@@ -100,14 +100,14 @@ namespace Algorithms {
while (_openList.Count > 0) {
Node node = _openList[0];
_openList.RemoveAt(0);
-
+
node.Reset();
}
-
+
while (_closedList.Count > 0) {
Node node = _closedList[0];
_closedList.RemoveAt(0);
-
+
node.Reset();
}
}
diff --git a/Paths/Assets/Scripts/ChangeController.cs b/Paths/Assets/Scripts/ChangeController.cs
index db6c5e8..f59dd62 100644
--- a/Paths/Assets/Scripts/ChangeController.cs
+++ b/Paths/Assets/Scripts/ChangeController.cs
@@ -64,7 +64,7 @@ public class ChangeController {
///
- /// Move the ChangeController's current index forward by index.
+ /// Move the ChangeController's current index forward by a certain number of times.
/// Positive values only, wil only result in forward movement (if any).
///
/// The number of times to move forward.
@@ -80,7 +80,7 @@ public class ChangeController {
}
///
- /// Move the ChangeController's current index backward by index.
+ /// Move the ChangeController's current index backward by a certain number of times.
/// Positive values only, will only result in backward movement (if any).
///
/// The number of times to move backward.
diff --git a/Paths/Assets/Scripts/CustomSlider.cs b/Paths/Assets/Scripts/CustomSlider.cs
index cda0ebe..5567601 100644
--- a/Paths/Assets/Scripts/CustomSlider.cs
+++ b/Paths/Assets/Scripts/CustomSlider.cs
@@ -2,7 +2,7 @@
using UnityEngine.UI;
public class CustomSlider: Slider {
- public bool IsPressed { get; private set; }
+ public new bool IsPressed { get; private set; }
public override void OnPointerDown(PointerEventData eventData)
{
diff --git a/Paths/Assets/Scripts/GridController.cs b/Paths/Assets/Scripts/GridController.cs
index 9ce74a1..f4ef545 100644
--- a/Paths/Assets/Scripts/GridController.cs
+++ b/Paths/Assets/Scripts/GridController.cs
@@ -141,9 +141,9 @@ public class GridController : MonoBehaviour {
public Vector2Int GetGridPosition(Vector3 worldPosition) {
Vector3 localScale = transform.localScale;
Vector2 gridPosition = (worldPosition + (localScale / 2f)) / new Vector2(localScale.x, localScale.y);
- return new Vector2Int(
+ return Size - new Vector2Int(
(int) (gridPosition.x * width),
- (int) (gridPosition.y * height)) - Size - Vector2Int.one;
+ (int) (gridPosition.y * height)) - Vector2Int.one;
}
///
@@ -154,9 +154,9 @@ public class GridController : MonoBehaviour {
public Vector3 GetWorldPosition(Vector2Int gridPosition) {
Transform ttransform = transform;
Vector3 localScale = ttransform.localScale;
- Vector2 bottomLeft = ttransform.position - (localScale / 2f);
+ Vector2 topRight = ttransform.position + (localScale / 2f);
var singleSquare = new Vector2(localScale.x / width, localScale.y / height);
- Vector2 worldPosition = bottomLeft + (singleSquare * gridPosition) + (singleSquare / 2f);
+ Vector2 worldPosition = topRight - (singleSquare * (gridPosition + Vector2Int.one)) + (singleSquare / 2f);
return worldPosition;
}
}
\ No newline at end of file
diff --git a/Paths/Assets/Scripts/IPathfinding.cs b/Paths/Assets/Scripts/IPathfinding.cs
index b1d0c2a..84e93c7 100644
--- a/Paths/Assets/Scripts/IPathfinding.cs
+++ b/Paths/Assets/Scripts/IPathfinding.cs
@@ -17,4 +17,5 @@ public interface IPathfinding {
Vector2Int Start { get; }
Vector2Int End { get; }
ChangeController ChangeController { get; }
+ void Cleanup();
}
\ No newline at end of file
diff --git a/Paths/Assets/Scripts/Manager.cs b/Paths/Assets/Scripts/Manager.cs
deleted file mode 100644
index 385a102..0000000
--- a/Paths/Assets/Scripts/Manager.cs
+++ /dev/null
@@ -1,180 +0,0 @@
-using System;
-using System.Collections.Generic;
-using Algorithms;
-using LevelGeneration;
-using TMPro;
-using UnityEditor;
-using UnityEngine;
-using UnityEngine.UI;
-
-///
-/// The primary controller of the entire application, managing state, events and sending commands
-///
-public class Manager : MonoBehaviour {
- private IPathfinding _algorithm;
- private ChangeController _state;
- private int _curIndex;
- 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 Slider progressSlider;
- private float? _moveTo;
-
- private int CurrentIndex {
- get => (int) _runtime;
- set => _runtime = value;
- }
-
- public void Start() {
- GeneratePath();
- Resize();
-
- progressSlider.onValueChanged.AddListener((value) => MoveToSlider(value));
- }
-
- ///
- /// Update the animation progress to the slider's (new) position.
- ///
- /// The new position on the slider.
- private void MoveToSlider(float @new) {
- _runtime = @new * _state.Count;
- }
-
-
-
- public void OnDrawGizmos() {
- if (!Application.isPlaying) return;
-
- Vector3 mouse = mainCamera.ScreenToWorldPoint(Input.mousePosition);
- Vector3 localScale = gridObject.transform.localScale;
- Vector2Int gridPosition = GetGridPosition(mouse, localScale);
- Vector2Int realGridPosition = gridController.Size - gridPosition - Vector2Int.one;
-
- var style = new GUIStyle();
- style.normal.textColor = Color.blue;
- Gizmos.color = Color.blue;
-
- Gizmos.DrawWireCube(GetWorldPosition(gridPosition, localScale), localScale / (Vector2) gridController.Size);
- Handles.Label(mouse, String.Format("{0}{1}",
- gridPosition,
- _algorithm.NodeGrid.IsValid(gridPosition)
- ? $"\n{_state.Current[realGridPosition.x, realGridPosition.y]}"
- : ""
- ), style);
- }
-
- ///
- /// 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() {
- // Toggle pause with space
- if (Input.GetKeyDown(KeyCode.Space)) {
- moving = !moving;
- }
-
- if (Input.GetKeyDown(KeyCode.G)) {
- GeneratePath();
- return;
- }
-
- // 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();
-
- // 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();
- Vector2Int start = new Vector2Int(3, 6);
- Vector2Int end = nodeGrid.RandomPosition();
-
- nodeGrid.ApplyGenerator(new RandomPlacement(0.3f, true, true));
-
- nodeGrid.GetNode(start).Walkable = true;
- nodeGrid.GetNode(end).Walkable = true;
-
- _algorithm = new AStar(nodeGrid);
- _path = _algorithm.FindPath(start, end);
- _state = _algorithm.ChangeController;
- }
-
- ///
- /// Loads the appropriate grid state into the shader via the ChangeController instance.
- ///
- private void LoadNextState() {
- _state.MoveTo(Math.Max(1, CurrentIndex)); // use Math.max to ensure both start/end nodes are always rendered
- gridController.LoadDirtyGridState(_state.Current, _state.DirtyFlags);
-
- string pathCount = _path != null ? $"{_path.Count}" : "N/A";
- debugText.text = $"{_state.CurrentRuntime * 1000.0:F1}ms\n" +
- $"{this.CurrentIndex:000} / {_state.Count:000}\n" +
- $"Path: {pathCount} tiles";
- }
-
- ///
- /// Scales the GridController GameObject to fit within the Camera
- ///
- private void Resize() {
- float ratioImage = (float) gridController.width / gridController.height;
- float ratioScreen = mainCamera.aspect;
-
- var orthographicSize = mainCamera.orthographicSize;
- var image = new Vector2(gridController.width, gridController.height);
- var screen = new Vector2(2 * orthographicSize * mainCamera.aspect, orthographicSize * 2);
-
- gridObject.transform.localScale = ratioScreen > ratioImage
- ? new Vector3(image.x * screen.y / image.y, screen.y, 0.001f)
- : new Vector3(screen.x, image.y * screen.x / image.x, 0.001f);
- }
-}
\ No newline at end of file
diff --git a/Paths/Assets/Scripts/Manager.cs.meta b/Paths/Assets/Scripts/Manager.cs.meta
deleted file mode 100644
index ccde7f7..0000000
--- a/Paths/Assets/Scripts/Manager.cs.meta
+++ /dev/null
@@ -1,11 +0,0 @@
-fileFormatVersion: 2
-guid: ca142250b9964eb1adf66ac7c5cc3e3e
-MonoImporter:
- externalObjects: {}
- serializedVersion: 2
- defaultReferences: []
- executionOrder: 300
- icon: {instanceID: 0}
- userData:
- assetBundleName:
- assetBundleVariant:
diff --git a/Paths/Assets/Scripts/UIController.cs b/Paths/Assets/Scripts/UIController.cs
index bc47bb9..14c5730 100644
--- a/Paths/Assets/Scripts/UIController.cs
+++ b/Paths/Assets/Scripts/UIController.cs
@@ -1,9 +1,9 @@
using System;
using System.Collections.Generic;
using Algorithms;
+using TMPro;
using UnityEditor;
using UnityEngine;
-using UnityEngine.UI;
///
@@ -39,7 +39,9 @@ public class UIController : MonoBehaviour {
// UI & important App references
public CustomSlider progressSlider;
public GridController gridController;
- public Manager manager;
+ public Camera mainCamera;
+ public TextMeshPro debugText;
+ public GameObject gridObject;
// Animation State, Click Management
private Vector2Int _lastClickLocation;
@@ -72,13 +74,13 @@ public class UIController : MonoBehaviour {
_end = _grid.RandomPosition();
_runtime = 0;
- manager.Resize();
+ Resize();
progressSlider.onValueChanged.AddListener((value) => MoveToSlider(value));
}
private void Update() {
if (Input.GetMouseButton(0)) {
- Vector3 worldMouse = manager.mainCamera.ScreenToWorldPoint(Input.mousePosition);
+ Vector3 worldMouse = mainCamera.ScreenToWorldPoint(Input.mousePosition);
Vector2Int position = gridController.GetGridPosition(worldMouse);
// Initial click, remember what they clicked
@@ -224,7 +226,7 @@ public class UIController : MonoBehaviour {
gridController.LoadDirtyGridState(_state.Current, _state.DirtyFlags);
string pathCount = _path != null ? $"{_path.Count}" : "N/A";
- manager.debugText.text = $"{_state.CurrentRuntime * 1000.0:F1}ms\n" +
+ debugText.text = $"{_state.CurrentRuntime * 1000.0:F1}ms\n" +
$"{CurrentIndex + 1:000} / {_state.Count:000}\n" +
$"Path: {pathCount} tiles";
}
@@ -262,8 +264,8 @@ public class UIController : MonoBehaviour {
public void OnDrawGizmos() {
if (!Application.isPlaying) return;
- Vector3 mouse = manager.mainCamera.ScreenToWorldPoint(Input.mousePosition);
- Vector3 localScale = manager.gridObject.transform.localScale;
+ Vector3 mouse = mainCamera.ScreenToWorldPoint(Input.mousePosition);
+ Vector3 localScale = gridObject.transform.localScale;
Vector2Int gridPosition = gridController.GetGridPosition(mouse);
var style = new GUIStyle();
@@ -287,4 +289,20 @@ public class UIController : MonoBehaviour {
if (_state != null)
_runtime = @new * _state.Count;
}
+
+ ///
+ /// Scales the GridController GameObject to fit within the Camera
+ ///
+ public void Resize() {
+ float ratioImage = (float) gridController.width / gridController.height;
+ float ratioScreen = mainCamera.aspect;
+
+ var orthographicSize = mainCamera.orthographicSize;
+ var image = new Vector2(gridController.width, gridController.height);
+ var screen = new Vector2(2 * orthographicSize * mainCamera.aspect, orthographicSize * 2);
+
+ gridObject.transform.localScale = ratioScreen > ratioImage
+ ? new Vector3(image.x * screen.y / image.y, screen.y, 0.001f)
+ : new Vector3(screen.x, image.y * screen.x / image.x, 0.001f);
+ }
}
\ No newline at end of file