Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System;
- using CodeMonkey.Utils;
- public class Grid<TGridObject>
- {
- public event EventHandler<OnGridValueChangedEventArgs> onGridValueChanged;
- public class OnGridValueChangedEventArgs : EventArgs
- {
- public int x;
- public int y;
- }
- private int width;
- private int height;
- private float cellSize;
- private Vector3 originPosition;
- private TGridObject[,] gridArray;
- public Grid(int width, int height, float cellSize, Vector3 originPosition, Func<Grid<TGridObject>, int, int, TGridObject> createGridObject)
- {
- this.width = width;
- this.height = height;
- this.cellSize = cellSize;
- this.originPosition = originPosition;
- gridArray = new TGridObject[width, height];
- 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);
- }
- }
- bool showDebug = true;
- if(showDebug)
- {
- TextMesh[,] 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] = UtilsClass.CreateWorldText(gridArray[x, y]?.ToString(), null, getWorldPosition(x, y) + new Vector3(cellSize, 0, cellSize) * 0.5f, 15, Color.white, TextAnchor.MiddleCenter, TextAlignment.Center);
- 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);
- onGridValueChanged += (object sender, OnGridValueChangedEventArgs eventArgs) =>
- {
- debugTextArray[eventArgs.x, eventArgs.y].text = gridArray[eventArgs.x, eventArgs.y]?.ToString();
- };
- }
- }
- public int getWidth()
- {
- return width;
- }
- public int getHeight()
- {
- return height;
- }
- public float getCellSize()
- {
- return cellSize;
- }
- private Vector3 getWorldPosition(int x, int y)
- {
- return new Vector3(x, 0, y) * 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);
- }
- public void setGridObject(int x, int y, TGridObject value)
- {
- if(x >= 0 && y >= 0 && x < width && y < height)
- {
- gridArray[x, y] = value;
- triggerGridObjectChanged(x, y);
- }
- }
- public void triggerGridObjectChanged(int x, int y)
- {
- onGridValueChanged?.Invoke(this, new OnGridValueChangedEventArgs { x = x, y = y });
- }
- public void setGridObject(Vector3 worldPosition, TGridObject value)
- {
- int x, y;
- getXY(worldPosition, out x, out y);
- setGridObject(x, y, value);
- }
- public TGridObject getGridObject(int x, int y)
- {
- if (x >= 0 && y >= 0 && x < width && y < height)
- {
- return gridArray[x, y];
- } else
- {
- return default(TGridObject);
- }
- }
- public TGridObject getGridObject(Vector3 worldPosition)
- {
- int x, y;
- getXY(worldPosition, out x, out y);
- return getGridObject(x, y);
- }
- public Vector2Int validateGridPosition(Vector2Int gridPosition)
- {
- return new Vector2Int(
- Mathf.Clamp(gridPosition.x, 0, width - 1),
- Mathf.Clamp(gridPosition.y, 0, height - 1)
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement