Advertisement
jwow22

Map Generation

Jan 4th, 2022
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.26 KB | None | 0 0
  1. public class Map : MonoBehaviour
  2. {
  3.     [Header("Spawning tiles")]
  4.     [SerializeField] private GameObject tilePrefab;
  5.     [SerializeField] public Vector2Int size;
  6.     [SerializeField] [Tooltip("Offset between each tile")]
  7.     private float offset;
  8.     [NonSerialized] public Tile[,] tileGrid;
  9.    
  10.     // Spawn size.x * size.y tiles
  11.     private void GenerateMap()
  12.     {
  13.         tileGrid = new Tile[size.x, size.y];
  14.         Transform parent = transform.GetChild(0);
  15.         for (int x = 0; x < size.x; x++)
  16.         {
  17.             for (int y = 0; y < size.y; y++)
  18.             {
  19.                 // Spawning tile
  20.                 Vector2 position = new Vector2(x, y) * offset;
  21.                 GameObject tileGameObject = Instantiate(tilePrefab, position, Quaternion.identity, parent);
  22.                 tileGameObject.name = $"Tile_{x}_{y}";
  23.                
  24.                 // Setting grid values
  25.                 tileGrid[x, y] = tileGameObject.GetComponent<Tile>();
  26.                 tileGrid[x, y].gridPosition = new Vector2Int(x, y);
  27.                 tileGrid[x, y].worldPosition = tileGameObject.transform.position;
  28.             }
  29.         }
  30.     }
  31.    
  32.     // Check if neighbour is valid (exists)
  33.     private bool IsEdgeTile(Tile tile, Vector2Int positionToCheck)
  34.     {
  35.         if (positionToCheck.x >= size.x)
  36.         {
  37.             tile.neighbourTiles[(int) Direction.Right] = tileGrid[0, positionToCheck.y];
  38.             return true;
  39.         }
  40.         if (positionToCheck.x < 0)
  41.         {
  42.             tile.neighbourTiles[(int) Direction.Left] = tileGrid[size.x - 1, positionToCheck.y];
  43.             return true;
  44.         }
  45.         if (positionToCheck.y >= size.y)
  46.         {
  47.             tile.neighbourTiles[(int) Direction.Up] = tileGrid[positionToCheck.x, 0];
  48.             return true;
  49.         }
  50.         if (positionToCheck.y < 0)
  51.         {
  52.             tile.neighbourTiles[(int) Direction.Down] = tileGrid[positionToCheck.x, size.y - 1];
  53.             return true;
  54.         }
  55.         return false;
  56.     }
  57.  
  58.     // Check all neighbours around singular tile
  59.     private void NeighboursAroundTile(Tile tile)
  60.     {
  61.         // Defining positions around tile to check
  62.         Vector2Int position = tile.gridPosition;
  63.        
  64.         // Four possible neighbour positions around tile
  65.         Vector2Int[] positions = new[]
  66.         {
  67.             new Vector2Int(position.x - 1, position.y), // left
  68.             new Vector2Int(position.x + 1, position.y), // right
  69.             new Vector2Int(position.x, position.y - 1), // down
  70.             new Vector2Int(position.x, position.y + 1)  // up
  71.         };
  72.  
  73.         // Check validity of all positions
  74.         for (int i = 0; i < positions.Length; i++)
  75.         {
  76.             if (!IsEdgeTile(tile, positions[i]))
  77.             {
  78.                 tile.neighbourTiles[i] = tileGrid[positions[i].x, positions[i].y];
  79.             }
  80.         }
  81.     }
  82.  
  83.     // Find neighbours of all tiles on the grid
  84.     private void FindAllNeighbours()
  85.     {
  86.         for (int x = 0; x < size.x; x++)
  87.         {
  88.             for (int y = 0; y < size.y; y++)
  89.             {
  90.                 NeighboursAroundTile(tileGrid[x, y]);
  91.             }
  92.         }
  93.     }
  94.  
  95.     private void Awake()
  96.     {
  97.         GenerateMap();
  98.         FindAllNeighbours();
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement