Advertisement
kissemisse

Base Grid

Mar 14th, 2021
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.25 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. public class BaseGrid : MonoBehaviour {
  4.  
  5.     // This is where the grid is instantiated and other scripts can access it
  6.    
  7.     public CellPoint[,] grid;
  8.    
  9.     [SerializeField]
  10.     private int width, height;
  11.  
  12.     [SerializeField] private float distanceBetweenCells = 10f;
  13.  
  14.     [SerializeField] private GameObject cell;
  15.  
  16.     public Transform GetCellTransform(int x, int z) {
  17.         return grid[x, z].transform;
  18.     }
  19.  
  20.     private void Awake() {
  21.         grid = new CellPoint[width, height];
  22.  
  23.         float xPos = 0, zPos = 0;
  24.         for(int i = 0; i < width; i++) {
  25.             for(int j = 0; j < height; j++) {
  26.                 GameObject newCell = Instantiate(cell, new Vector3(xPos, 0, zPos), Quaternion.identity, transform);
  27.                 newCell.GetComponent<CellPoint>().SetCellCords(i,j); // TODO
  28.                 grid[i, j] = newCell.GetComponent<CellPoint>();
  29.  
  30.                 zPos += distanceBetweenCells;
  31.             }
  32.             xPos += distanceBetweenCells;
  33.             zPos = 0;
  34.         }
  35.     }
  36.  
  37.     public CellType[] GetCellNeighbourTypes(int cellX, int cellZ) {
  38.         // 0 up, 1 right, 2 down, 3 left
  39.         CellType[] returnCells =  { CellType.None, CellType.None, CellType.None, CellType.None};
  40.         // TODO add if's to remove errors
  41.         returnCells[0] = grid[cellX, cellZ + 1].GetCurrentCellType();
  42.         returnCells[1] = grid[cellX, cellZ - 1 ].GetCurrentCellType();
  43.         returnCells[2] = grid[cellX - 1, cellZ].GetCurrentCellType();
  44.         returnCells[3] = grid[cellX + 1, cellZ].GetCurrentCellType();
  45.  
  46.         return returnCells;
  47.     }
  48.  
  49.     public CellType[] GetCellNeighbourTypes(CellPoint cellPoint) {
  50.         // 0 up, 1 right, 2 down, 3 left
  51.         CellType[] returnCells =  { CellType.None, CellType.None, CellType.None, CellType.None};
  52.         // TODO add if's to remove errors
  53.  
  54.         int cellX = cellPoint.GetCellX();
  55.         int cellZ = cellPoint.GetCellZ();
  56.        
  57.         returnCells[0] = grid[cellX, cellZ + 1].GetCurrentCellType();
  58.         returnCells[1] = grid[cellX, cellZ - 1 ].GetCurrentCellType();
  59.         returnCells[2] = grid[cellX - 1, cellZ].GetCurrentCellType();
  60.         returnCells[3] = grid[cellX + 1, cellZ].GetCurrentCellType();
  61.  
  62.         return returnCells;
  63.     }
  64.  
  65.     public CellPoint[] GetCellNeighbours(int cellX, int cellZ) {
  66.         // 0 up, 1 right, 2 down, 3 left
  67.         CellPoint[] returnCells =  { null, null, null, null };
  68.         // TODO add if's to remove errors
  69.         returnCells[0] = grid[cellX, cellZ + 1];
  70.         returnCells[1] = grid[cellX, cellZ - 1];
  71.         returnCells[2] = grid[cellX - 1, cellZ];
  72.         returnCells[3] = grid[cellX + 1, cellZ];
  73.  
  74.         return returnCells;
  75.     }
  76.  
  77.     public CellPoint[] GetCellNeighbours(CellPoint cellPoint) {
  78.         // 0 up, 1 right, 2 down, 3 left
  79.         CellPoint[] returnCells =  { null, null, null, null };
  80.  
  81.         int cellX = cellPoint.GetCellX();
  82.         int cellZ = cellPoint.GetCellZ();
  83.        
  84.         // TODO add if's to remove errors
  85.         returnCells[0] = grid[cellX, cellZ + 1];
  86.         returnCells[1] = grid[cellX, cellZ - 1];
  87.         returnCells[2] = grid[cellX - 1, cellZ];
  88.         returnCells[3] = grid[cellX + 1, cellZ];
  89.  
  90.         return returnCells;
  91.     }
  92. }
  93.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement