Advertisement
Zgragselus

Grid

Oct 5th, 2024
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.43 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. /// <summary>
  4. /// Templated 2D grid class.
  5. /// </summary>
  6. /// <typeparam name="T">Grid data type</typeparam>
  7. public class Grid<T>
  8. {
  9.     /// <summary>
  10.     /// Holds the grid data.
  11.     /// </summary>
  12.     T[] data;
  13.  
  14.     /// <summary>
  15.     /// Size of the grid.
  16.     /// </summary>
  17.     public Vector2Int Size { get; private set; }
  18.  
  19.     /// <summary>
  20.     /// Grid offset.
  21.     /// </summary>
  22.     public Vector2Int Offset { get; set; }
  23.  
  24.     /// <summary>
  25.     /// Default constructor.
  26.     /// </summary>
  27.     /// <param name="size">Size of the grid</param>
  28.     /// <param name="offset">Offset of the grid</param>
  29.     public Grid(Vector2Int size, Vector2Int offset)
  30.     {
  31.         Size = size;
  32.         Offset = offset;
  33.  
  34.         data = new T[size.x * size.y];
  35.     }
  36.  
  37.     /// <summary>
  38.     /// Get index of grid data based on 2D position.
  39.     /// </summary>
  40.     /// <param name="position">2D position to get index of</param>
  41.     /// <returns>Index of grid data</returns>
  42.     public int GetIndex(Vector2Int position)
  43.     {
  44.         return position.x + position.y * Size.x;
  45.     }
  46.  
  47.     /// <summary>
  48.     /// Is the given position inside the grid?
  49.     /// </summary>
  50.     /// <param name="position">2D position to check</param>
  51.     /// <returns>True when inside the grid, false otherwise</returns>
  52.     public bool IsInside(Vector2Int position)
  53.     {
  54.         return position.x >= 0 && position.x < Size.x && position.y >= 0 && position.y < Size.y;
  55.     }
  56.  
  57.     /// <summary>
  58.     /// Get data at the given position (with x and y coordinates).
  59.     /// </summary>
  60.     /// <param name="x">X coordinate</param>
  61.     /// <param name="y">Y coordinate</param>
  62.     /// <returns>Value at the given position</returns>
  63.     public T this[int x, int y]
  64.     {
  65.         get
  66.         {
  67.             return this[new Vector2Int(x, y)];
  68.         }
  69.  
  70.         set
  71.         {
  72.             this[new Vector2Int(x, y)] = value;
  73.         }
  74.     }
  75.  
  76.     /// <summary>
  77.     /// Get data at the given position (with Vector2Int).
  78.     /// </summary>
  79.     /// <param name="position">Position to get data of</param>
  80.     /// <returns>Value at the given position</returns>
  81.     public T this[Vector2Int position]
  82.     {
  83.         get
  84.         {
  85.             position += Offset;
  86.             return data[GetIndex(position)];
  87.         }
  88.  
  89.         set
  90.         {
  91.             position += Offset;
  92.             data[GetIndex(position)] = value;
  93.         }
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement