basic grid class implemented
can set / un-set values on each cell. concrete foundation implemented
This commit is contained in:
@@ -228,7 +228,7 @@ Camera:
|
|||||||
far clip plane: 1000
|
far clip plane: 1000
|
||||||
field of view: 60
|
field of view: 60
|
||||||
orthographic: 1
|
orthographic: 1
|
||||||
orthographic size: 20
|
orthographic size: 60
|
||||||
m_Depth: -1
|
m_Depth: -1
|
||||||
m_CullingMask:
|
m_CullingMask:
|
||||||
serializedVersion: 2
|
serializedVersion: 2
|
||||||
|
|||||||
@@ -10,15 +10,26 @@ public class LogicGrid
|
|||||||
private int height;
|
private int height;
|
||||||
private float cellSize;
|
private float cellSize;
|
||||||
|
|
||||||
|
private Vector3 originPosition;
|
||||||
|
|
||||||
|
// Actual Grid Array
|
||||||
private int[,] gridArray;
|
private int[,] gridArray;
|
||||||
|
|
||||||
public LogicGrid(int width, int height, float cellSize)
|
// Debug Grid Array used for updating the text-objects
|
||||||
|
private TextMesh[,] debugTextArray;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public LogicGrid(int width, int height, float cellSize, Vector3 originPosition)
|
||||||
{
|
{
|
||||||
this.width = width;
|
this.width = width;
|
||||||
this.height = height;
|
this.height = height;
|
||||||
this.cellSize = cellSize;
|
this.cellSize = cellSize;
|
||||||
|
|
||||||
|
this.originPosition = originPosition;
|
||||||
|
|
||||||
gridArray = new int[width, height];
|
gridArray = new int[width, height];
|
||||||
|
debugTextArray = new TextMesh[width, height];
|
||||||
|
|
||||||
// Used to place cell values in middle of cell
|
// Used to place cell values in middle of cell
|
||||||
Vector3 textOffset = new Vector3(cellSize/2, cellSize/2);
|
Vector3 textOffset = new Vector3(cellSize/2, cellSize/2);
|
||||||
@@ -27,25 +38,70 @@ public class LogicGrid
|
|||||||
for(int x = 0; x < gridArray.GetLength(0); x++)
|
for(int x = 0; x < gridArray.GetLength(0); x++)
|
||||||
for(int y = 0; y < gridArray.GetLength(1); y++)
|
for(int y = 0; y < gridArray.GetLength(1); y++)
|
||||||
{
|
{
|
||||||
// Create text that shows the value of the number in the given cell.
|
// Create text that prints the value of a given cell inside of that cell.
|
||||||
CodeMonkey.Utils.UtilsClass.CreateWorldText(gridArray[x, y].ToString(), null, GetWorldPosition(x, y) + textOffset, 20, Color.white, TextAnchor.MiddleCenter);
|
debugTextArray[x, y] = 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)
|
// 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 + 1, y), Color.white, 100f);
|
||||||
Debug.DrawLine(GetWorldPosition(x, y), GetWorldPosition(x, y + 1), Color.white, 100f);
|
Debug.DrawLine(GetWorldPosition(x, y), GetWorldPosition(x, y + 1), Color.white, 100f);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Draw top-line for whole grid
|
// Draw top-line for whole grid, draw right-line for whole grid
|
||||||
Debug.DrawLine(GetWorldPosition(0, height), GetWorldPosition(width, height), Color.white, 100f);
|
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);
|
Debug.DrawLine(GetWorldPosition(width, 0), GetWorldPosition(width, height), Color.white, 100f);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// enables us to scale grid. cells would otherwise be 1x1 (very small)
|
|
||||||
|
// Enables us to convert the array's (x,y) pair into the user's world-space (also used to scale grid in constructor)
|
||||||
private Vector3 GetWorldPosition(int x, int y)
|
private Vector3 GetWorldPosition(int x, int y)
|
||||||
{
|
{
|
||||||
return new Vector3(x, y) * this.cellSize;
|
return new Vector3(x, y) * this.cellSize + originPosition;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void getXY(Vector3 worldPosition, out int x, out int y)
|
||||||
|
{
|
||||||
|
x = Mathf.FloorToInt((worldPosition - originPosition).x / cellSize);
|
||||||
|
y = Mathf.FloorToInt((worldPosition - originPosition).y / cellSize);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Set cell value based on world-location (enables user interaction)
|
||||||
|
public void SetValue(Vector3 worldPosition, int value)
|
||||||
|
{
|
||||||
|
int x, y;
|
||||||
|
getXY(worldPosition, out x, out y);
|
||||||
|
SetValue(x, y, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set cell value based on array index
|
||||||
|
public void SetValue(int x, int y, int value)
|
||||||
|
{
|
||||||
|
if((x >= 0 && x < width) && (y >= 0 && y < height))
|
||||||
|
{
|
||||||
|
// Set the value in the real grid
|
||||||
|
gridArray[x, y] = value;
|
||||||
|
|
||||||
|
// Set the value in our debug mesh so that our Text on-screen gets updated
|
||||||
|
debugTextArray[x, y].text = gridArray[x, y].ToString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get cell's value based on array index
|
||||||
|
public int GetValue(int x, int y)
|
||||||
|
{
|
||||||
|
if ((x >= 0 && x < width) && (y >= 0 && y < height))
|
||||||
|
{
|
||||||
|
return gridArray[x, y];
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Takes a world position, converts to array index, returns value held
|
||||||
|
public int GetValue(Vector3 worldPosition)
|
||||||
|
{
|
||||||
|
int x, y;
|
||||||
|
getXY(worldPosition, out x, out y);
|
||||||
|
return GetValue(x, y);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,12 +4,28 @@ using UnityEngine;
|
|||||||
|
|
||||||
public class TestingScript : MonoBehaviour
|
public class TestingScript : MonoBehaviour
|
||||||
{
|
{
|
||||||
|
private LogicGrid grid;
|
||||||
// Start is called before the first frame update
|
// Start is called before the first frame update
|
||||||
private void Start()
|
private void Start()
|
||||||
{
|
{
|
||||||
LogicGrid grid = new LogicGrid(4, 2, 10f);
|
//Vector3 origin = new Vector3(0, 0);
|
||||||
|
grid = new LogicGrid(4, 2, 10f, new Vector3(0, 0));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void Update()
|
||||||
|
{
|
||||||
|
if(Input.GetMouseButtonDown(0))
|
||||||
|
{
|
||||||
|
grid.SetValue(CodeMonkey.Utils.UtilsClass.GetMouseWorldPosition(), 1);
|
||||||
|
}
|
||||||
|
if (Input.GetMouseButtonDown(1))
|
||||||
|
{
|
||||||
|
grid.SetValue(CodeMonkey.Utils.UtilsClass.GetMouseWorldPosition(), 0);
|
||||||
|
}
|
||||||
|
if (Input.GetMouseButtonDown(2))
|
||||||
|
{
|
||||||
|
Debug.Log(grid.GetValue(CodeMonkey.Utils.UtilsClass.GetMouseWorldPosition()).ToString());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user