GridXZ.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using CodeMonkey.Utils;
  5. using UnityEngine;
  6. public class GridXZ<TGridObject> {
  7. public bool showDebug = true;
  8. public const int HEAT_MAP_MAX_VALUE = 100;
  9. public const int HEAT_MAP_MIN_VALUE = 0;
  10. public event EventHandler<OnGridObjectChangedEventArgs> OnGridObjectChanged;
  11. public class OnGridObjectChangedEventArgs : EventArgs {
  12. public int x;
  13. public int z;
  14. }
  15. private int width;
  16. private int height;
  17. private float cellSize;
  18. private TGridObject[,] gridArray;
  19. private TextMesh[,] debugTextArray;
  20. private Vector3 originPosition;
  21. public GridXZ(int width, int height, float cellSize, Vector3 originPosition, Func<GridXZ<TGridObject>, int, int, TGridObject> createGridObject) {
  22. this.width = width;
  23. this.height = height;
  24. this.cellSize = cellSize;
  25. this.originPosition = originPosition;
  26. gridArray = new TGridObject[width, height];
  27. for (int x = 0; x < gridArray.GetLength(0); x++) {
  28. for (int z = 0; z < gridArray.GetLength(1); z++) {
  29. gridArray[x, z] = createGridObject(this, x, z);
  30. }
  31. }
  32. if (showDebug) {
  33. debugTextArray = new TextMesh[width, height];
  34. for (int x = 0; x < gridArray.GetLength(0); x++) {
  35. for (int z = 0; z < gridArray.GetLength(1); z++) {
  36. 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);
  37. debugTextArray[x, z].transform.Rotate(new Vector3(75f, 0f, 0f));
  38. Debug.DrawLine(GetWorldPosition(x, z), GetWorldPosition(x, z + 1), Color.white, 20);
  39. Debug.DrawLine(GetWorldPosition(x, z), GetWorldPosition(x + 1, z), Color.white, 20);
  40. }
  41. }
  42. Debug.DrawLine(GetWorldPosition(0, height), GetWorldPosition(width, height), Color.white, 20);
  43. Debug.DrawLine(GetWorldPosition(width, 0), GetWorldPosition(width, height), Color.white, 20);
  44. OnGridObjectChanged += (object sender, OnGridObjectChangedEventArgs eventArgs) => {
  45. debugTextArray[eventArgs.x, eventArgs.z].text = gridArray[eventArgs.x, eventArgs.z]?.ToString();
  46. };
  47. }
  48. }
  49. public int GetWidth() {
  50. return width;
  51. }
  52. public int GetHeight() {
  53. return height;
  54. }
  55. public float GetCellSize() {
  56. return cellSize;
  57. }
  58. public Vector3 GetWorldPosition(int x, int z) {
  59. return new Vector3(x, 0, z) * cellSize + originPosition;
  60. }
  61. public void GetXZ(Vector3 worldPosition, out int x, out int z) {
  62. x = Mathf.FloorToInt((worldPosition - originPosition).x / cellSize);
  63. z = Mathf.FloorToInt((worldPosition - originPosition).z / cellSize);
  64. }
  65. public void SetGridObject(int x, int z, TGridObject value) {
  66. if (x >= 0 && z >= 0 && x < width && z < height) {
  67. gridArray[x, z] = value;
  68. if (OnGridObjectChanged != null) OnGridObjectChanged(this, new OnGridObjectChangedEventArgs { x = x, z = z });
  69. }
  70. }
  71. public void TriggerGridObjectChanged(int x, int z) {
  72. if (OnGridObjectChanged != null) OnGridObjectChanged(this, new OnGridObjectChangedEventArgs { x = x, z = z });
  73. }
  74. public void SetGridObject(Vector3 worldPosition, TGridObject value) {
  75. int x, z;
  76. GetXZ(worldPosition, out x, out z);
  77. SetGridObject(x, z, value);
  78. }
  79. public TGridObject GetGridObject(int x, int z) {
  80. if (x >= 0 && z >= 0 && x < width && z < height) {
  81. return gridArray[x, z];
  82. } else {
  83. return default(TGridObject);
  84. }
  85. }
  86. public TGridObject GetGridObject(Vector3 worldPosition) {
  87. int x, z;
  88. GetXZ(worldPosition, out x, out z);
  89. return GetGridObject(x, z);
  90. }
  91. }