From f2328a7b4bd9b39e431807825f57992bbebdf4a9 Mon Sep 17 00:00:00 2001 From: Xevion Date: Mon, 9 Nov 2020 14:52:27 -0600 Subject: [PATCH] successful implementation of algorithm usage in Manager, random wall/start/end position placement, needs major feature and code cleanup --- Paths/Assets/Scripts/Algorithms/AStar.cs | 6 ++-- Paths/Assets/Scripts/Algorithms/Node.cs | 4 +++ Paths/Assets/Scripts/Algorithms/NodeGrid.cs | 17 +++++++-- Paths/Assets/Scripts/GridController.cs | 12 ++++--- Paths/Assets/Scripts/GridController.cs.meta | 12 +++++-- Paths/Assets/Scripts/GridState.cs | 24 ++++++++----- Paths/Assets/Scripts/Manager.cs | 39 +++++++++++++++++++++ Paths/Assets/Scripts/Manager.cs.meta | 11 ++++++ Paths/Assets/Shaders/GridShader.shader | 6 ++-- 9 files changed, 109 insertions(+), 22 deletions(-) create mode 100644 Paths/Assets/Scripts/Manager.cs create mode 100644 Paths/Assets/Scripts/Manager.cs.meta diff --git a/Paths/Assets/Scripts/Algorithms/AStar.cs b/Paths/Assets/Scripts/Algorithms/AStar.cs index f3d410d..b1f908a 100644 --- a/Paths/Assets/Scripts/Algorithms/AStar.cs +++ b/Paths/Assets/Scripts/Algorithms/AStar.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using System.Linq; -using System.Numerics; +using UnityEngine; +using Vector2 = System.Numerics.Vector2; namespace Algorithms { public class AStar : IPathfinding { @@ -22,7 +23,6 @@ namespace Algorithms { public Stack FindPath(Vector2 Start, Vector2 End) { this._start = Start; this._end = End; - RecordState(); var start = new Node(Start, true); var end = new Node(End, true); @@ -31,6 +31,8 @@ namespace Algorithms { _path = new Stack(); _openList = new List(); _closedList = new List(); + + RecordState(); Node current = start; // add start node to Open List diff --git a/Paths/Assets/Scripts/Algorithms/Node.cs b/Paths/Assets/Scripts/Algorithms/Node.cs index 622dbd7..40ef8c1 100644 --- a/Paths/Assets/Scripts/Algorithms/Node.cs +++ b/Paths/Assets/Scripts/Algorithms/Node.cs @@ -29,5 +29,9 @@ namespace Algorithms { Weight = weight; Walkable = walkable; } + + public override string ToString() { + return $"Node({Position.X}, {Position.Y}, {Walkable})"; + } } } \ No newline at end of file diff --git a/Paths/Assets/Scripts/Algorithms/NodeGrid.cs b/Paths/Assets/Scripts/Algorithms/NodeGrid.cs index 7465665..212f315 100644 --- a/Paths/Assets/Scripts/Algorithms/NodeGrid.cs +++ b/Paths/Assets/Scripts/Algorithms/NodeGrid.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Numerics; +using Random = UnityEngine.Random; namespace Algorithms { public class NodeGrid { @@ -64,14 +65,26 @@ namespace Algorithms { /// A Node object /// when the coordinate given does not exist on the grid public Node GetNode(int x, int y) { - if(!IsValid(x, y)) - throw new ArgumentOutOfRangeException(); + // if(!IsValid(x, y)) + // throw new ArgumentOutOfRangeException(); return this.grid[x][y]; } + public void FlipRandomWall() { + this.grid[Random.Range(0, this.Width - 1)][Random.Range(0, this.Height - 1)].Walkable = false; + } + public static float Manhattan(Node first, Node second) { return Math.Abs(first.Position.X - second.Position.X) + Math.Abs(first.Position.Y - second.Position.Y); } + + /// + /// Returns a random Vector2 position within the grid. + /// + /// a valid Vector2 position within the grid + public Vector2 RandomPosition() { + return new Vector2(Random.Range(0, Width - 1), Random.Range(0, Height - 1)); + } } } \ No newline at end of file diff --git a/Paths/Assets/Scripts/GridController.cs b/Paths/Assets/Scripts/GridController.cs index 3a5b75a..8563eb7 100644 --- a/Paths/Assets/Scripts/GridController.cs +++ b/Paths/Assets/Scripts/GridController.cs @@ -9,12 +9,13 @@ public enum PropertyName { } /// -/// A simple Grid Rendering Controller using MeshRenderer. +/// A simple Grid Rendering system controlling a Shader through a ComputeBuffer /// public class GridController : MonoBehaviour { - public Material gridMaterial; - public int size = 32; - + public Material gridMaterial; // Maintain reference to the Grid Material the Shader is implanted upon + public int size = 32; // Size of the grid, width and height + + // Value management private int[] _values; private ComputeBuffer _buffer; @@ -60,7 +61,7 @@ public class GridController : MonoBehaviour { } /// - /// Loads a GridState + /// Loads a GridState into the Grid Shader's StructuredBuffer /// /// public void LoadGridState(GridState gridState) { @@ -68,6 +69,7 @@ public class GridController : MonoBehaviour { foreach(int x in Enumerable.Range(0, gridState.Grid.Count - 1)) foreach(int y in Enumerable.Range(0, gridState.Grid[0].Count - 1)) this.SetValue(x, y, (int) gridState.Grid[x][y]); + UpdateShader(PropertyName.Values); } /// diff --git a/Paths/Assets/Scripts/GridController.cs.meta b/Paths/Assets/Scripts/GridController.cs.meta index 6cfeec3..2f77d40 100644 --- a/Paths/Assets/Scripts/GridController.cs.meta +++ b/Paths/Assets/Scripts/GridController.cs.meta @@ -1,3 +1,11 @@ -fileFormatVersion: 2 +fileFormatVersion: 2 guid: ae862ef271614573a2ecacb6d9fa2078 -timeCreated: 1604939699 \ No newline at end of file +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 200 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Paths/Assets/Scripts/GridState.cs b/Paths/Assets/Scripts/GridState.cs index 800497d..eb68736 100644 --- a/Paths/Assets/Scripts/GridState.cs +++ b/Paths/Assets/Scripts/GridState.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Linq; using System.Numerics; using Algorithms; @@ -19,26 +20,33 @@ public class GridState { } // Add 'seen' tiles - foreach (Node seenNode in seen) { + foreach (Node seenNode in seen) this.Grid[(int) seenNode.Position.X][(int) seenNode.Position.Y] = GridNodeType.Seen; - } // Add 'expanded' tiles - foreach (Node expandedNode in expanded) { + foreach (Node expandedNode in expanded) this.Grid[(int) expandedNode.Position.X][(int) expandedNode.Position.Y] = GridNodeType.Expanded; - } - // Set start and end tiles - this.Grid[(int) start.X][(int) start.Y] = GridNodeType.Start; - this.Grid[(int) end.X][(int) end.Y] = GridNodeType.End; // Add 'path' tiles if (path != null) foreach (Node pathNode in path) this.Grid[(int) pathNode.Position.X][(int) pathNode.Position.Y] = GridNodeType.Path; + + // Set start and end tiles + this.Grid[(int) start.X][(int) start.Y] = GridNodeType.Start; + this.Grid[(int) end.X][(int) end.Y] = GridNodeType.End; } public IEnumerable GetNodes() { return this.Grid.SelectMany(nodeList => nodeList).ToList(); } + + public string RenderGrid() { + string result = ""; + foreach (List nodeTypes in this.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 new file mode 100644 index 0000000..13ec6aa --- /dev/null +++ b/Paths/Assets/Scripts/Manager.cs @@ -0,0 +1,39 @@ +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using System.Numerics; +using Algorithms; +using Vector2 = System.Numerics.Vector2; +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; + + public void Start() { + var nodeGrid = new NodeGrid(gridController.size, gridController.size); + _algorithm = new AStar(nodeGrid); + + Vector2 start = new Vector2(0, 0); + Vector2 end = new Vector2(16, 16); + + foreach(int index in Enumerable.Range(0, 150)) + nodeGrid.FlipRandomWall(); + + Stack path = _algorithm.FindPath(start, end); + + List states = _algorithm.GetStates(); + gridController.LoadGridState(states[states.Count - 1]); + Debug.Log(states[states.Count - 1].RenderGrid()); + Debug.Log(start); + Debug.Log(end); + + // StartCoroutine(LateStart()); + } + + // IEnumerator LateStart() { + // } +} \ No newline at end of file diff --git a/Paths/Assets/Scripts/Manager.cs.meta b/Paths/Assets/Scripts/Manager.cs.meta new file mode 100644 index 0000000..ccde7f7 --- /dev/null +++ b/Paths/Assets/Scripts/Manager.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: ca142250b9964eb1adf66ac7c5cc3e3e +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 300 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Paths/Assets/Shaders/GridShader.shader b/Paths/Assets/Shaders/GridShader.shader index 1247c0b..ae5c99c 100644 --- a/Paths/Assets/Shaders/GridShader.shader +++ b/Paths/Assets/Shaders/GridShader.shader @@ -33,8 +33,8 @@ Shader "PDT Shaders/TestGrid" { float4 _ActiveColor; static const float4 _gridColors[7] = { - float4(255 / 255.0, 255 / 255.0, 255 / 255.0, 1.0), // Empty - float4(0 / 255.0, 0 / 255.0, 0 / 255.0, 1.0), // Wall + float4(255 / 255.0, 255 / 255.0, 255 / 255.0, 0.0), // Empty + float4(50 / 255.0, 50 / 255.0, 50 / 255.0, 1.0), // Wall float4(0 / 255.0, 255 / 255.0, 0 / 255.0, 1.0), // Start float4(255 / 255.0, 0 / 255.0, 0 / 255.0, 1.0), // End float4(252 / 255.0, 236 / 255.0, 3 / 255.0, 1.0), // Seen @@ -47,7 +47,7 @@ Shader "PDT Shaders/TestGrid" { // DX11 needed to run shader at high grid sizes #ifdef SHADER_API_D3D11 - StructuredBuffer _values; + StructuredBuffer _values; #else float _values[1024]; #endif