using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using Utils = KairoEngine.Utility.Utilities; using TMPro; namespace KairoEngine.Grids { public enum GridPlane { XY, XZ, YZ } public class Grid { private int width; private int height; private float cellSize; private Vector3 originPosition; private bool debug = false; private GridPlane plane = GridPlane.XY; private TGridObject[,] gridArray; private TextMesh[,] debugTextArray; public event EventHandler OnGridObjectChanged; public class OnGridObjectChangedEventArgs : EventArgs { public int x; public int y; } public Grid(int width, int height, float cellSize, Vector3 originPosition, Func, int, int, TGridObject> createGridObject, GridPlane plane = GridPlane.XY, bool debug = false) { this.width = width; this.height = height; this.cellSize = cellSize; this.originPosition = originPosition; this.gridArray = new TGridObject[width, height]; this.debug = debug; this.plane = plane; for (int x = 0; x < gridArray.GetLength(0); x++) { for (int y = 0; y < gridArray.GetLength(1); y++) { gridArray[x, y] = createGridObject(this, x, y); } } UpdateDebugView(); } public int GetWidth() => width; public int GetHeight() => height; public float GetCellSize() => cellSize; public Vector3 GetWorldPosition(int x, int y) { switch(plane) { case GridPlane.XY: return new Vector3(x, y, 0) * cellSize + originPosition; case GridPlane.XZ: return new Vector3(x, 0, y) * cellSize + originPosition; case GridPlane.YZ: return new Vector3(0, x, y) * cellSize + originPosition; default: return new Vector3(); } } public void GetGridPosition(Vector3 worldPosition, out int x, out int y) { x = 0; y = 0; switch(plane) { case GridPlane.XY: x = Mathf.FloorToInt((worldPosition - originPosition).x/ cellSize); y = Mathf.FloorToInt((worldPosition - originPosition).y / cellSize); break; case GridPlane.XZ: x = Mathf.FloorToInt((worldPosition - originPosition).x/ cellSize); y = Mathf.FloorToInt((worldPosition - originPosition).z / cellSize); break; case GridPlane.YZ: x = Mathf.FloorToInt((worldPosition - originPosition).y/ cellSize); y = Mathf.FloorToInt((worldPosition - originPosition).z / cellSize); break; default: break; } } public bool IsPositionInsideGrid(Vector2Int gridPosition) { if(gridPosition.x < 0) return false; if(gridPosition.x > width - 1) return false; if(gridPosition.y < 0) return false; if(gridPosition.y > height - 1) return false; return true; } public bool IsPositionInsideGrid(Vector3 worldPosition) { float maxWidth, maxHeight; switch(plane) { case GridPlane.XY: maxWidth = originPosition.x + (cellSize * width); maxHeight = originPosition.y + (cellSize * height); if(worldPosition.x > maxWidth) return false; if(worldPosition.x < originPosition.x) return false; if(worldPosition.y > maxHeight) return false; if(worldPosition.y < originPosition.y) return false; break; case GridPlane.XZ: maxWidth = originPosition.x + (cellSize * width); maxHeight = originPosition.z + (cellSize * height); if(worldPosition.x > maxWidth) return false; if(worldPosition.x < originPosition.x) return false; if(worldPosition.z > maxHeight) return false; if(worldPosition.z < originPosition.z) return false; break; case GridPlane.YZ: maxWidth = originPosition.y + (cellSize * width); maxHeight = originPosition.z + (cellSize * height); if(worldPosition.y > maxWidth) return false; if(worldPosition.y < originPosition.y) return false; if(worldPosition.z > maxHeight) return false; if(worldPosition.z < originPosition.z) return false; break; default: break; } return true; } public void SetObject(int x, int y, TGridObject value) { if(x >= 0 && y >= 0 && x < width && y < height) { gridArray[x, y] = value; if(debug) debugTextArray[x, y].text = gridArray[x, y].ToString(); TriggerGridObjectChanged(x, y); } } public void SetObject(Vector3 worldPosition, TGridObject value) { int x, y; GetGridPosition(worldPosition, out x, out y); SetObject(x, y, value); } public TGridObject GetObject(int x, int y) { if(x >= 0 && y >= 0 && x < width && y < height) { return gridArray[x, y]; } else return default(TGridObject); } public TGridObject GetObject(Vector3 worldPosition) { int x, y; GetGridPosition(worldPosition, out x, out y); return GetObject(x, y); } public Vector3 GetQuadSize() { switch(plane) { case GridPlane.XY: return new Vector3(1, 1, 0) * GetCellSize(); case GridPlane.XZ: return new Vector3(1, 0, 1) * GetCellSize(); case GridPlane.YZ: return new Vector3(0, 1, 1) * GetCellSize(); default: return new Vector3(0, 0, 0); } } public GridPlane GetGridPlane() => plane; public void TriggerGridObjectChanged(int x, int y) { if(OnGridObjectChanged != null) OnGridObjectChanged(this, new OnGridObjectChangedEventArgs {x = x, y = y}); UpdateDebugView(); } private void UpdateDebugView() { DestroyDebugText(); if(debug) { this.debugTextArray = new TextMesh[width, height]; for (int x = 0; x < gridArray.GetLength(0); x++) { for (int y = 0; y < gridArray.GetLength(1); y++) { debugTextArray[x, y] = Utils.CreateWorldText(gridArray[x,y]?.ToString(), null, GetWorldPosition(x, y) + GetQuadSize() * .5f, 12, Color.white, TextAnchor.MiddleCenter); Debug.DrawLine(GetWorldPosition(x, y), GetWorldPosition(x, y + 1), Color.white, 100f); Debug.DrawLine(GetWorldPosition(x, y), GetWorldPosition(x + 1, y), Color.white, 100f); } } Debug.DrawLine(GetWorldPosition(0, height), GetWorldPosition(width, height), Color.white, 100f); Debug.DrawLine(GetWorldPosition(width, 0), GetWorldPosition(width, height), Color.white, 100f); } } private void DestroyDebugText() { if(this.debugTextArray != null) { for (int x = 0; x < this.debugTextArray.GetLength(0); x++) { for (int y = 0; y < this.debugTextArray.GetLength(1); y++) { GameObject.Destroy(debugTextArray[x,y].gameObject); } } } } } }