successful implementation of algorithm usage in Manager, random wall/start/end position placement, needs major feature and code cleanup

This commit is contained in:
Xevion
2020-11-09 14:52:27 -06:00
parent 3591d16832
commit f2328a7b4b
9 changed files with 109 additions and 22 deletions

View File

@@ -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<Node> 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<Node>();
_openList = new List<Node>();
_closedList = new List<Node>();
RecordState();
Node current = start;
// add start node to Open List

View File

@@ -29,5 +29,9 @@ namespace Algorithms {
Weight = weight;
Walkable = walkable;
}
public override string ToString() {
return $"Node({Position.X}, {Position.Y}, {Walkable})";
}
}
}

View File

@@ -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 {
/// <returns>A Node object</returns>
/// <exception cref="ArgumentOutOfRangeException">when the coordinate given does not exist on the grid</exception>
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);
}
/// <summary>
/// Returns a random Vector2 position within the grid.
/// </summary>
/// <returns>a valid Vector2 position within the grid</returns>
public Vector2 RandomPosition() {
return new Vector2(Random.Range(0, Width - 1), Random.Range(0, Height - 1));
}
}
}

View File

@@ -9,12 +9,13 @@ public enum PropertyName {
}
/// <summary>
/// A simple Grid Rendering Controller using MeshRenderer.
/// A simple Grid Rendering system controlling a Shader through a ComputeBuffer
/// </summary>
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 {
}
/// <summary>
/// Loads a GridState
/// Loads a GridState into the Grid Shader's StructuredBuffer
/// </summary>
/// <param name="gridState"></param>
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);
}
/// <summary>

View File

@@ -1,3 +1,11 @@
fileFormatVersion: 2
fileFormatVersion: 2
guid: ae862ef271614573a2ecacb6d9fa2078
timeCreated: 1604939699
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 200
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -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<GridNodeType> GetNodes() {
return this.Grid.SelectMany(nodeList => nodeList).ToList();
}
public string RenderGrid() {
string result = "";
foreach (List<GridNodeType> nodeTypes in this.Grid) {
result = nodeTypes.Aggregate(result, (current, nodeType) => current + $"{(int) nodeType}") + "\n";
}
return result;
}
}

View File

@@ -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;
/// <summary>
/// The primary controller of the entire application, managing state, events and sending commands
/// </summary>
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<Node> path = _algorithm.FindPath(start, end);
List<GridState> 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() {
// }
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: ca142250b9964eb1adf66ac7c5cc3e3e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 300
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -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<float> _values;
StructuredBuffer<int> _values;
#else
float _values[1024];
#endif