Advertisement
Viksy

Grid

Mar 16th, 2023
456
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.09 KB | None | 0 0
  1. using UnityEngine;
  2. using System;
  3. using CodeMonkey.Utils;
  4.  
  5. public class Grid<TGridObject>
  6. {
  7.     public event EventHandler<OnGridValueChangedEventArgs> onGridValueChanged;
  8.     public class OnGridValueChangedEventArgs : EventArgs
  9.     {
  10.         public int x;
  11.         public int y;
  12.     }
  13.  
  14.     private int width;
  15.     private int height;
  16.     private float cellSize;
  17.     private Vector3 originPosition;
  18.     private TGridObject[,] gridArray;
  19.  
  20.     public Grid(int width, int height, float cellSize, Vector3 originPosition, Func<Grid<TGridObject>, int, int, TGridObject> createGridObject)
  21.     {
  22.         this.width = width;
  23.         this.height = height;
  24.         this.cellSize = cellSize;
  25.         this.originPosition = originPosition;
  26.  
  27.         gridArray = new TGridObject[width, height];
  28.  
  29.         for (int x = 0; x < gridArray.GetLength(0); x++)
  30.         {
  31.             for (int y = 0; y < gridArray.GetLength(1); y++)
  32.             {
  33.                 gridArray[x, y] = createGridObject(this, x, y);
  34.             }
  35.         }
  36.  
  37.         bool showDebug = true;
  38.         if(showDebug)
  39.         {
  40.             TextMesh[,] debugTextArray = new TextMesh[width, height];
  41.  
  42.             for (int x = 0; x < gridArray.GetLength(0); x++)
  43.             {
  44.                 for (int y = 0; y < gridArray.GetLength(1); y++)
  45.                 {
  46.                     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);
  47.                     Debug.DrawLine(getWorldPosition(x, y), getWorldPosition(x, y + 1), Color.white, 100f);
  48.                     Debug.DrawLine(getWorldPosition(x, y), getWorldPosition(x + 1, y), Color.white, 100f);
  49.                 }
  50.             }
  51.  
  52.             Debug.DrawLine(getWorldPosition(0, height), getWorldPosition(width, height), Color.white, 100f);
  53.             Debug.DrawLine(getWorldPosition(width, 0), getWorldPosition(width, height), Color.white, 100f);
  54.  
  55.             onGridValueChanged += (object sender, OnGridValueChangedEventArgs eventArgs) =>
  56.             {
  57.                 debugTextArray[eventArgs.x, eventArgs.y].text = gridArray[eventArgs.x, eventArgs.y]?.ToString();
  58.             };
  59.         }
  60.        
  61.     }
  62.  
  63.     public int getWidth()
  64.     {
  65.         return width;
  66.     }
  67.  
  68.     public int getHeight()
  69.     {
  70.         return height;
  71.     }
  72.  
  73.     public float getCellSize()
  74.     {
  75.         return cellSize;
  76.     }
  77.  
  78.     private Vector3 getWorldPosition(int x, int y)
  79.     {
  80.         return new Vector3(x, 0, y) * cellSize + originPosition;
  81.     }
  82.  
  83.     private void getXY(Vector3 worldPosition, out int x, out int y)
  84.     {
  85.         x = Mathf.FloorToInt((worldPosition - originPosition).x / cellSize);
  86.         y = Mathf.FloorToInt((worldPosition - originPosition).y / cellSize);
  87.     }
  88.  
  89.     public void setGridObject(int x, int y, TGridObject value)
  90.     {
  91.         if(x >= 0 && y >= 0 && x < width && y < height)
  92.         {
  93.             gridArray[x, y] = value;
  94.             triggerGridObjectChanged(x, y);
  95.         }        
  96.     }
  97.    
  98.     public void triggerGridObjectChanged(int x, int y)
  99.     {
  100.         onGridValueChanged?.Invoke(this, new OnGridValueChangedEventArgs { x = x, y = y });
  101.     }
  102.  
  103.     public void setGridObject(Vector3 worldPosition, TGridObject value)
  104.     {
  105.         int x, y;
  106.         getXY(worldPosition, out x, out y);
  107.         setGridObject(x, y, value);
  108.     }
  109.  
  110.     public TGridObject getGridObject(int x, int y)
  111.     {
  112.         if (x >= 0 && y >= 0 && x < width && y < height)
  113.         {
  114.             return gridArray[x, y];
  115.         } else
  116.         {
  117.             return default(TGridObject);
  118.         }
  119.     }
  120.  
  121.     public TGridObject getGridObject(Vector3 worldPosition)
  122.     {
  123.         int x, y;
  124.         getXY(worldPosition, out x, out y);
  125.         return getGridObject(x, y);
  126.     }
  127.  
  128.     public Vector2Int validateGridPosition(Vector2Int gridPosition)
  129.     {
  130.         return new Vector2Int(
  131.             Mathf.Clamp(gridPosition.x, 0, width - 1),
  132.             Mathf.Clamp(gridPosition.y, 0, height - 1)
  133.             );
  134.     }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement