From 5f9e8f979deb94e9219113ed09b49142c040cb57 Mon Sep 17 00:00:00 2001 From: Simon O'Shea Date: Mon, 31 Jul 2023 18:45:01 -0400 Subject: import successful, grid has visuals cell values and grid lines are displayed. no interaction available yet. --- Pathfinding Visualizer/Assets/Scripts/LogicGrid.cs | 28 +++++++++++++++++++--- .../Assets/Scripts/TestingScript.cs | 2 +- 2 files changed, 26 insertions(+), 4 deletions(-) (limited to 'Pathfinding Visualizer/Assets/Scripts') diff --git a/Pathfinding Visualizer/Assets/Scripts/LogicGrid.cs b/Pathfinding Visualizer/Assets/Scripts/LogicGrid.cs index e72f5fe..92eb7b8 100644 --- a/Pathfinding Visualizer/Assets/Scripts/LogicGrid.cs +++ b/Pathfinding Visualizer/Assets/Scripts/LogicGrid.cs @@ -8,22 +8,44 @@ public class LogicGrid { private int width; private int height; + private float cellSize; + private int[,] gridArray; - public LogicGrid(int width, int height) + public LogicGrid(int width, int height, float cellSize) { this.width = width; this.height = height; + this.cellSize = cellSize; gridArray = new int[width, height]; - Debug.Log(width + " " + height); + // Used to place cell values in middle of cell + Vector3 textOffset = new Vector3(cellSize/2, cellSize/2); + // Visualize the grid for(int x = 0; x < gridArray.GetLength(0); x++) for(int y = 0; y < gridArray.GetLength(1); y++) { - Debug.Log(x + ", " + y); + // Create text that shows the value of the number in the given cell. + CodeMonkey.Utils.UtilsClass.CreateWorldText(gridArray[x, y].ToString(), null, GetWorldPosition(x, y) + textOffset, 20, Color.white, TextAnchor.MiddleCenter); + + // Give the cells a visual representation (must enable gizmos to see) + Debug.DrawLine(GetWorldPosition(x, y), GetWorldPosition(x + 1, y), Color.white, 100f); + Debug.DrawLine(GetWorldPosition(x, y), GetWorldPosition(x, y + 1), Color.white, 100f); } + // Draw top-line for whole grid + Debug.DrawLine(GetWorldPosition(0, height), GetWorldPosition(width, height), Color.white, 100f); + // Draw right-line for whole grid + Debug.DrawLine(GetWorldPosition(width, 0), GetWorldPosition(width, height), Color.white, 100f); + } + + // enables us to scale grid. cells would otherwise be 1x1 (very small) + private Vector3 GetWorldPosition(int x, int y) + { + return new Vector3(x, y) * this.cellSize; + } + } diff --git a/Pathfinding Visualizer/Assets/Scripts/TestingScript.cs b/Pathfinding Visualizer/Assets/Scripts/TestingScript.cs index a83c36b..51ee102 100644 --- a/Pathfinding Visualizer/Assets/Scripts/TestingScript.cs +++ b/Pathfinding Visualizer/Assets/Scripts/TestingScript.cs @@ -7,7 +7,7 @@ public class TestingScript : MonoBehaviour // Start is called before the first frame update private void Start() { - LogicGrid grid = new LogicGrid(4, 2); + LogicGrid grid = new LogicGrid(4, 2, 10f); } -- cgit v1.3.1-4-g156e