void AddIsland(int islandSize) { if (islandSize < 2) { islandSize = 2; } List <Vector2Int> tiles = new List <Vector2Int>(); List <Vector2Int> possTiles = new List <Vector2Int>(); IslandData currentIsland = new IslandData(); Vector2Int loc = new Vector2Int(Random.Range(-worldSize, worldSize), Random.Range(-worldSize, worldSize)); int x = 0; while (usedTiles.Contains(loc)) { if (x >= 10) { worldSize++; x = 0; Debug.Log("X"); } Vector3 spot = new Vector3(Random.Range(-1f, 1f), 0, Random.Range(-1f, 1f)) * worldSize; loc = HexGrid.RoundToGrid(spot); x++; } tiles.Add(loc); foreach (Vector2Int v in HexGrid.FindAdjacentGridLocs(loc)) { if (!tiles.Contains(v) && !possTiles.Contains(v) && !usedTiles.Contains(v)) { possTiles.Add(v); } } while (tiles.Count < islandSize && possTiles.Count > 0) { Vector2Int tile = possTiles[Random.Range(0, possTiles.Count)]; foreach (Vector2Int v in HexGrid.FindAdjacentGridLocs(tile)) { if (!tiles.Contains(v) && !possTiles.Contains(v) && !usedTiles.Contains(v)) { possTiles.Add(v); } } possTiles.Remove(tile); tiles.Add(tile); } if (tiles.Count == islandSize) { foreach (Vector2Int v in HexGrid.FindOutline(tiles)) { tiles.Add(v); } List <Vector2Int> remove = new List <Vector2Int>(); remove.AddRange(tiles); remove.AddRange(HexGrid.FindOutline(remove)); remove.AddRange(HexGrid.FindOutline(remove)); remove.AddRange(HexGrid.FindOutline(remove)); foreach (Vector2Int v in remove) { usedTiles.Add(v); } foreach (Vector2Int v in tiles) { currentIsland.tiles.Add(new WorldTile(v)); currentIsland.gridLocs.Add(v); } foreach (Vector2Int v in tiles) { foreach (Vector2Int adj in HexGrid.FindAdjacentGridLocs(v)) { if (tiles.Contains(adj)) { currentIsland.tiles[currentIsland.gridLocs.IndexOf(v)].connections.Add(adj); } } } currentIsland.CalcHeights(); islands.Add(currentIsland); } else { AddIsland(islandSize); } }