private void Start() { Vector3 bottomLeft = GetBottomLeftOfGrid() + new Vector3(0.5f, 0.5f); grid = new TileContainer[rows][]; // Fill grid with plain tiles for (int ii = 0; ii < rows; ii++) { grid[ii] = new TileContainer[columns]; for (int jj = 0; jj < columns; jj++) { GameObject tileObj = Instantiate(plainTile, bottomLeft + new Vector3(jj, ii), Quaternion.identity, transform); TileContainer tile = tileObj.GetComponent <TileContainer>(); grid[ii][jj] = tile; // Add tile above and tile to the left to this tile's neighbors, and add this tile to their neighbors if (ii > 0) { tile.AddNeighbor(grid[ii - 1][jj]); } if (jj > 0) { tile.AddNeighbor(grid[ii][jj - 1]); } } } // Place initial farmhouses and crop tiles based on serialized offset values grid[farmhouseOffsetY][farmhouseOffsetX].ConvertToFarmhouse(0); grid[farmhouseOffsetY + 1][farmhouseOffsetX].ConvertToCrop(0); grid[farmhouseOffsetY][farmhouseOffsetX + 1].ConvertToCrop(0); grid[farmhouseOffsetY][columns - farmhouseOffsetX - 1].ConvertToFarmhouse(1); grid[farmhouseOffsetY + 1][columns - farmhouseOffsetX - 1].ConvertToCrop(1); grid[farmhouseOffsetY][columns - farmhouseOffsetX - 2].ConvertToCrop(1); grid[rows - farmhouseOffsetY - 1][farmhouseOffsetX].ConvertToFarmhouse(2); grid[rows - farmhouseOffsetY - 2][farmhouseOffsetX].ConvertToCrop(2); grid[rows - farmhouseOffsetY - 1][farmhouseOffsetX + 1].ConvertToCrop(2); grid[rows - farmhouseOffsetY - 1][columns - farmhouseOffsetX - 1].ConvertToFarmhouse(3); grid[rows - farmhouseOffsetY - 2][columns - farmhouseOffsetX - 1].ConvertToCrop(3); grid[rows - farmhouseOffsetY - 1][columns - farmhouseOffsetX - 2].ConvertToCrop(3); }
// Adds the given tile to this tile's list of neighbors and vice versa, ignoring duplicates public void AddNeighbor(TileContainer tile) { if (neighbors.Contains(tile)) { return; } neighbors.Add(tile); tile.AddNeighbor(this); }