From 665604bafc2431b652789376a07a3c317a452738 Mon Sep 17 00:00:00 2001 From: Xevion Date: Sat, 14 Nov 2020 00:13:31 -0600 Subject: [PATCH] change NodeGrid/GridState to use 2d arrays for performance --- Paths/Assets/Scripts/Algorithms/NodeGrid.cs | 89 +++++--------------- Paths/Assets/Scripts/GridState.cs | 3 +- Paths/Assets/Shaders/GridShader Material.mat | 2 +- 3 files changed, 25 insertions(+), 69 deletions(-) diff --git a/Paths/Assets/Scripts/Algorithms/NodeGrid.cs b/Paths/Assets/Scripts/Algorithms/NodeGrid.cs index 4c058d9..cef4137 100644 --- a/Paths/Assets/Scripts/Algorithms/NodeGrid.cs +++ b/Paths/Assets/Scripts/Algorithms/NodeGrid.cs @@ -6,7 +6,7 @@ using Random = UnityEngine.Random; namespace Algorithms { public class NodeGrid { - private List> grid; + public readonly Node[,] Grid; public readonly int Width; public readonly int Height; public int CellCount => this.Width * this.Height; @@ -19,26 +19,22 @@ namespace Algorithms { throw new ArgumentOutOfRangeException(nameof(height), "The height of the grid must be a positive non-zero integer."); - grid = new List>(width); // Fill grid with width*height nodes, zero-indexed - for (int x = 0; x < width; x++) { - List list = new List(height); + Grid = new Node[width, height]; + for (int x = 0; x < width; x++) for (int y = 0; y < height; y++) - list.Add(new Node(new Vector2Int(x, y), true)); + Grid[x, y] = new Node(new Vector2Int(x, y), true); - grid.Add(list); - } - - Width = this.grid.Count; - Height = this.grid[0].Count; + Width = width; + Height = height; } - public NodeGrid(List> grid) { - this.grid = grid; + public NodeGrid(Node[,] grid) { + this.Grid = grid; - Height = this.grid[0].Count; - Width = this.grid.Count; + Height = this.Grid.GetLength(0); + Width = this.Grid.GetLength(1); } public List GetAdjacentNodesList(Node node) { @@ -47,10 +43,10 @@ namespace Algorithms { int col = node.Position.x; int row = node.Position.y; - if (row + 1 < Height) temp.Add(grid[col][row + 1]); - if (row - 1 >= 0) temp.Add(grid[col][row - 1]); - if (col - 1 >= 0) temp.Add(grid[col - 1][row]); - if (col + 1 < Width) temp.Add(grid[col + 1][row]); + if (row + 1 < Height) temp.Add(Grid[col, row + 1]); + if (row - 1 >= 0) temp.Add(Grid[col, row - 1]); + if (col - 1 >= 0) temp.Add(Grid[col - 1, row]); + if (col + 1 < Width) temp.Add(Grid[col + 1, row]); return temp; } @@ -60,54 +56,15 @@ namespace Algorithms { int row = node.Position.y; return new Node[] { - row + 1 < Height ? grid[col][row + 1] : null, - row - 1 >= 0 ? grid[col][row - 1] : null, - col - 1 >= 0 ? grid[col - 1][row] : null, - col + 1 < Width ? grid[col + 1][row] : null + row + 1 < Height ? Grid[col, row + 1] : null, + row - 1 >= 0 ? Grid[col, row - 1] : null, + col - 1 >= 0 ? Grid[col - 1, row] : null, + col + 1 < Width ? Grid[col + 1, row] : null }; } - /// - /// Tests whether a coordinate is valid on the NodeGrid - /// - /// The X (column) coordinate - /// The Y (row) coordinate - /// - public bool IsValid(int x, int y) { - return x > 0 && x < Width && y > 0 && y < Height; - } - - /// - /// Retrieves a node at a given coordinate. - /// - /// the X (column) coordinate - /// the Y (row) coordinate - /// 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(); - - return grid[x][y]; - } - public Node GetNode(Vector2Int position) { - return GetNode(position.x, position.y); - } - - /// - /// Finds one random walkable cell and turns it into a wall. - /// - public void AddRandomWall() { - while (true) { - int x = Random.Range(0, Width); - int y = Random.Range(0, Height); - - if (grid[x][y].Walkable) { - grid[x][y].Walkable = false; - return; - } - } + return Grid[position.x, position.y]; } public static float Manhattan(Node a, Node b) { @@ -135,9 +92,9 @@ namespace Algorithms { } public IEnumerable Iterator() { - for (int x = 0; x < this.grid.Count; x++) - for (int y = 0; y < this.grid[0].Count; y++) - yield return this.grid[x][y]; + for (int x = 0; x < Width; x++) + for (int y = 0; y < Height; y++) + yield return this.Grid[x, y]; } public IEnumerable Walls() { @@ -153,7 +110,7 @@ namespace Algorithms { /// /// A Node object. public Node GetRandomNode() { - return grid[Random.Range(0, Width)][Random.Range(0, Height)]; + return Grid[Random.Range(0, Width), Random.Range(0, Height)]; } } } \ No newline at end of file diff --git a/Paths/Assets/Scripts/GridState.cs b/Paths/Assets/Scripts/GridState.cs index 2b84ddc..00f0890 100644 --- a/Paths/Assets/Scripts/GridState.cs +++ b/Paths/Assets/Scripts/GridState.cs @@ -15,8 +15,7 @@ public class GridState { // Add walls and empty tiles for (int x = 0; x < grid.Width; x++) { for (int y = 0; y < grid.Height; y++) { - Node node = grid.GetNode(x, y); - Grid[x, y] = node.Walkable ? GridNodeType.Empty : GridNodeType.Wall; + Grid[x, y] = grid.Grid[x, y].Walkable ? GridNodeType.Empty : GridNodeType.Wall; } } diff --git a/Paths/Assets/Shaders/GridShader Material.mat b/Paths/Assets/Shaders/GridShader Material.mat index 77eb710..e2d9f46 100644 --- a/Paths/Assets/Shaders/GridShader Material.mat +++ b/Paths/Assets/Shaders/GridShader Material.mat @@ -64,7 +64,7 @@ Material: - _Glossiness: 0.5 - _GlossyReflections: 1 - _GridSize: 32 - - _LineSize: 0.05 + - _LineSize: 0 - _Metallic: 0 - _Mode: 0 - _OcclusionStrength: 1