I have strange behavior with a map generator script.
This code below calls the code after it:
void GenerateTiles (GameObject map) {
tiles = new GameObject [mapWidth, mapHeight];
towerHeights = new float [mapWidth, mapHeight];
mapCenter = new Coord (mapWidth / 2, mapHeight / 2);
int towerFrequency = (int) (mapWidth * mapHeight * towerPercent);
int obstacleCount = 0;
for (int i = 0; i < towerFrequency; i ++)
{
Coord coord = GetRandomCoord ();
float height = Random.Range (minTowerHeight, maxTowerHeight);
towerHeights [coord.x, coord.y] = height;
if (height > 0.25f)
{
obstacleCount ++;
}
if (!SweepMap (obstacleCount) || !CompareCoordinates (coord, mapCenter))
{
height = 0.25f;
towerHeights [coord.x, coord.y] = height;
obstacleCount --;
}
if (!CompareCoordinates (coord, mapCenter))
{
GameObject newTower = Instantiate (tower, CoordToPosition (coord), Quaternion.identity) as GameObject;
newTower.transform.localScale = new Vector3 (1 - outlinePercentage, height, 1 - outlinePercentage);
newTower.transform.parent = map.transform;
tiles [coord.x, coord.y] = newTower;
}
}
for (int x = 0; x < mapWidth; x ++)
{
for (int y = 0; y < mapHeight; y ++)
{
if (tiles [x, y] == null)
{
GameObject newTile = Instantiate (tile, CoordToPosition (new Coord (x, y)), Quaternion.Euler (90, 0, 0)) as GameObject;
newTile.transform.localScale = new Vector3 (1 - outlinePercentage, 1 - outlinePercentage, 1);
newTile.transform.parent = map.transform;
tiles [x, y] = newTile;
}
}
}
}
Code called at various times:
bool SweepMap (int o) {
bool [,] mapFlags = new bool [mapWidth, mapHeight];
mapFlags [mapCenter.x, mapCenter.y] = true;
Queue <Coord> queue = new Queue <Coord> ();
queue.Enqueue (mapCenter);
int accessibleTiles = 1;
while (queue.Count > 0)
{
Coord tile = queue.Dequeue ();
for (int x = -1; x < 2; x ++)
{
for (int y = -1; y < 2; y ++)
{
int neighborX = tile.x + x;
int neighborY = tile.y + y;
if (x != 0 || y != 0)
{
if (neighborX > 0 && neighborX < mapWidth && neighborY > 0 && neighborY < mapHeight)
{
if (!mapFlags [neighborX, neighborY])
{
if (!CheckNeighbor (neighborX, neighborY))
{
queue.Enqueue (new Coord (neighborX, neighborY));
accessibleTiles ++;
}
mapFlags [neighborX, neighborY] = true;
}
}
}
}
}
}
int targetTiles = mapWidth * mapHeight - o;
if (accessibleTiles == targetTiles)
{
return true;
}
return false;
}
bool CheckNeighbor (int x, int y) {
if (towerHeights [x, y] > 0.25f)
{
return true;
}
return false;
}
Coord GetRandomCoord () {
Coord randomCoord = shuffledCoords.Dequeue ();
shuffledCoords.Enqueue (randomCoord);
return randomCoord;
}
Vector3 CoordToPosition (Coord coord) {
float x = (float) (coord.x - mapWidth / 2);
float y = (float) (coord.y - mapHeight / 2);
return new Vector3 (x, 0.01f, y);
}
What happens is all the towers are scaled down, meaning it thinks everything is a threat to an enclosed space. Any help is appreciated.
You can try the script by downloading this.