| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using CodeMonkey.Utils;
- using UnityEngine;
- public class GridXZ<TGridObject> {
- public bool showDebug = true;
- public const int HEAT_MAP_MAX_VALUE = 100;
- public const int HEAT_MAP_MIN_VALUE = 0;
- public event EventHandler<OnGridObjectChangedEventArgs> OnGridObjectChanged;
- public class OnGridObjectChangedEventArgs : EventArgs {
- public int x;
- public int z;
- }
- private int width;
- private int height;
- private float cellSize;
- private TGridObject[,] gridArray;
- private TextMesh[,] debugTextArray;
- private Vector3 originPosition;
- public GridXZ(int width, int height, float cellSize, Vector3 originPosition, Func<GridXZ<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 z = 0; z < gridArray.GetLength(1); z++) {
- gridArray[x, z] = createGridObject(this, x, z);
- }
- }
- if (showDebug) {
- debugTextArray = new TextMesh[width, height];
- for (int x = 0; x < gridArray.GetLength(0); x++) {
- for (int z = 0; z < gridArray.GetLength(1); z++) {
- debugTextArray[x, z] = UtilsClass.CreateWorldText(gridArray[x, z]?.ToString(), null, GetWorldPosition(x, z) + new Vector3(cellSize, 0, cellSize) * 0.5f, 20, Color.white, TextAnchor.MiddleCenter);
- debugTextArray[x, z].transform.Rotate(new Vector3(75f, 0f, 0f));
- Debug.DrawLine(GetWorldPosition(x, z), GetWorldPosition(x, z + 1), Color.white, 20);
- Debug.DrawLine(GetWorldPosition(x, z), GetWorldPosition(x + 1, z), Color.white, 20);
- }
- }
- Debug.DrawLine(GetWorldPosition(0, height), GetWorldPosition(width, height), Color.white, 20);
- Debug.DrawLine(GetWorldPosition(width, 0), GetWorldPosition(width, height), Color.white, 20);
- OnGridObjectChanged += (object sender, OnGridObjectChangedEventArgs eventArgs) => {
- debugTextArray[eventArgs.x, eventArgs.z].text = gridArray[eventArgs.x, eventArgs.z]?.ToString();
- };
- }
- }
- public int GetWidth() {
- return width;
- }
- public int GetHeight() {
- return height;
- }
- public float GetCellSize() {
- return cellSize;
- }
- public Vector3 GetWorldPosition(int x, int z) {
- return new Vector3(x, 0, z) * cellSize + originPosition;
- }
- public void GetXZ(Vector3 worldPosition, out int x, out int z) {
- x = Mathf.FloorToInt((worldPosition - originPosition).x / cellSize);
- z = Mathf.FloorToInt((worldPosition - originPosition).z / cellSize);
- }
- public void SetGridObject(int x, int z, TGridObject value) {
- if (x >= 0 && z >= 0 && x < width && z < height) {
- gridArray[x, z] = value;
- if (OnGridObjectChanged != null) OnGridObjectChanged(this, new OnGridObjectChangedEventArgs { x = x, z = z });
- }
- }
- public void TriggerGridObjectChanged(int x, int z) {
- if (OnGridObjectChanged != null) OnGridObjectChanged(this, new OnGridObjectChangedEventArgs { x = x, z = z });
- }
- public void SetGridObject(Vector3 worldPosition, TGridObject value) {
- int x, z;
- GetXZ(worldPosition, out x, out z);
- SetGridObject(x, z, value);
- }
- public TGridObject GetGridObject(int x, int z) {
- if (x >= 0 && z >= 0 && x < width && z < height) {
- return gridArray[x, z];
- } else {
- return default(TGridObject);
- }
- }
- public TGridObject GetGridObject(Vector3 worldPosition) {
- int x, z;
- GetXZ(worldPosition, out x, out z);
- return GetGridObject(x, z);
- }
- }
|