I made a script that randomly spawns either land or water and than smooths that out using cellular automata. When I run unity, it only spawns water though. Do any of you have any idea what is causing this problem in my script?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;
public class worldGen : MonoBehaviour
{
public Tile water;
public Tile ground;
int[,] waterCount;
int[,] groundCount;
public Tilemap tilemap;
public int width = 100;
public int height = 100;
public Tile[] tileTypes;
void Start()
{
genRandomTiles();
cellularAutomata();
smoothTiles();
}
void genRandomTiles()
{
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
int rand = Random.Range(0, tileTypes.Length);
Vector3Int pos = new Vector3Int(x, y, 0);
tilemap.SetTile(pos, tileTypes[rand]);
}
}
}
void cellularAutomata()
{
waterCount = new int[width, height];
groundCount = new int[width, height];
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
List<TileBase> neighbours = new List<TileBase>();
for (int i = -1; i < 2; i++)
{
for (int j = -1; j < 2; j++)
{
int neighbour_x = x + i;
int neighbour_y = y + j;
if (i == 0 && j == 0)
{
}
else
{
Vector3Int pos = new Vector3Int(neighbour_x, neighbour_y, 0);
neighbours.Add(tilemap.GetTile(pos));
}
}
}
for (int i = 0; i < neighbours.Count; i++)
{
if (neighbours[i] = water)
{
waterCount[x,y]++;
}
else if (neighbours[i] = ground)
{
groundCount[x,y]++;
}
}
}
}
}
void smoothTiles()
{
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
Vector3Int pos = new Vector3Int(x, y, 0);
if(waterCount[x,y] > 4)
{
tilemap.SetTile(pos, water);
}
else if (groundCount[x,y] > 4)
{
tilemap.SetTile(pos, ground);
}
}
}
}
}