Grid.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using CodeMonkey.Utils;
  5. using UnityEngine;
  6. public class Grid<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 y;
  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 Grid(int width, int height, float cellSize, Vector3 originPosition, Func<Grid<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 y = 0; y < gridArray.GetLength(1); y++) {
  29. gridArray[x, y] = createGridObject(this, x, y);
  30. }
  31. }
  32. if (showDebug) {
  33. debugTextArray = new TextMesh[width, height];
  34. for (int x = 0; x < gridArray.GetLength(0); x++) {
  35. for (int y = 0; y < gridArray.GetLength(1); y++) {
  36. debugTextArray[x, y] = UtilsClass.CreateWorldText(gridArray[x, y]?.ToString(), null, GetWorldPosition(x, y) + new Vector3(cellSize, cellSize) * 0.5f, 20, Color.white, TextAnchor.MiddleCenter);
  37. Debug.DrawLine(GetWorldPosition(x, y), GetWorldPosition(x, y + 1), Color.white, 100);
  38. Debug.DrawLine(GetWorldPosition(x, y), GetWorldPosition(x + 1, y), Color.white, 100);
  39. }
  40. }
  41. Debug.DrawLine(GetWorldPosition(0, height), GetWorldPosition(width, height), Color.white, 100);
  42. Debug.DrawLine(GetWorldPosition(width, 0), GetWorldPosition(width, height), Color.white, 100);
  43. OnGridObjectChanged += (object sender, OnGridObjectChangedEventArgs eventArgs) => {
  44. debugTextArray[eventArgs.x, eventArgs.y].text = gridArray[eventArgs.x, eventArgs.y]?.ToString();
  45. };
  46. }
  47. }
  48. public int GetWidth() {
  49. return width;
  50. }
  51. public int GetHeight() {
  52. return height;
  53. }
  54. public float GetCellSize() {
  55. return cellSize;
  56. }
  57. public Vector3 GetWorldPosition(int x, int y) {
  58. return new Vector3(x, y) * cellSize + originPosition;
  59. }
  60. public void GetXY(Vector3 worldPosition, out int x, out int y) {
  61. x = Mathf.FloorToInt((worldPosition - originPosition).x / cellSize);
  62. y = Mathf.FloorToInt((worldPosition - originPosition).y / cellSize);
  63. }
  64. public void SetGridObject(int x, int y, TGridObject value) {
  65. if (x >= 0 && y >= 0 && x < width && y < height) {
  66. gridArray[x, y] = value;
  67. if (OnGridObjectChanged != null) OnGridObjectChanged(this, new OnGridObjectChangedEventArgs { x = x, y = y });
  68. }
  69. }
  70. public void TriggerGridObjectChanged(int x, int y) {
  71. if (OnGridObjectChanged != null) OnGridObjectChanged(this, new OnGridObjectChangedEventArgs { x = x, y = y });
  72. }
  73. public void SetGridObject(Vector3 worldPosition, TGridObject value) {
  74. int x, y;
  75. GetXY(worldPosition, out x, out y);
  76. SetGridObject(x, y, value);
  77. }
  78. public TGridObject GetGridObject(int x, int y) {
  79. if (x >= 0 && y >= 0 && x < width && y < height) {
  80. return gridArray[x, y];
  81. } else {
  82. return default(TGridObject);
  83. }
  84. }
  85. public TGridObject GetGridObject(Vector3 worldPosition) {
  86. int x, y;
  87. GetXY(worldPosition, out x, out y);
  88. return GetGridObject(x, y);
  89. }
  90. }