private static void CreateRectangularRoom(Coordinate currentCoord, int width, int height) //universal algorithm for rectangular-shaped room { Coordinate floorTile = currentCoord; List <Coordinate> room = new List <Coordinate>(width * height); for (int z = 0; z < height; z++) { for (int x = 0; x < width; x++) { floorTile.x += x; floorTile.z += z; room.Add(floorTile); floorTile = currentCoord; } } int xOffset = Random.Range(-(width - 1), 0); // - 1 to not move away from current coordinate int zOffset = Random.Range(-(height - 1), 0); // same here for (int i = 0; i < room.Count; i++) { floorTile = room[i]; floorTile.x += xOffset; floorTile.z += zOffset; if (!cellsCoordinates.Contains(floorTile)) { cellsCoordinates.Add(floorTile); RandomWorm.IncreaseCreatedCellsCount(); RandomWorm.UpdateMinMaxMapCoordinates(floorTile.x, floorTile.z); } } }
private static void MoveWorms() { while (!RandomWorm.IsEnoughCellsCreated(settings.cellsToCreateCount)) { if (worms.Count == 0) { worms.Add(new RandomWorm(cellsCoordinates[Random.Range(0, cellsCoordinates.Count)])); } for (int i = worms.Count - 1; i >= 0; i--) { //if work is done if (RandomWorm.IsEnoughCellsCreated(settings.cellsToCreateCount)) { break; } RandomWorm worm = worms[i]; bool isAlive = worm.Move(); //move Coordinate currentCoord = worm.GetCurrentCoord(); //create tile if (!cellsCoordinates.Contains(currentCoord)) { cellsCoordinates.Add(currentCoord); RandomWorm.IncreaseCreatedCellsCount(); } if (!isAlive) //if dead { worms.Remove(worm); RandomWorm.DecreaseWormsCount(); RandomWorm.SetChanceToDie(settings.initialChanceToDie, settings.increaseDieChanceBy); continue; } if (Random.value < settings.chanceToCreateWorm) { worms.Add(new RandomWorm(worm.GetCurrentCoord())); //worms.Add(new RandomWorm(cellsCoordinates[Random.Range(0, cellsCoordinates.Count)])); //settings.wormsCount++; RandomWorm.SetChanceToDie(settings.initialChanceToDie, settings.increaseDieChanceBy); } } } }
public static TileMapSettings PreprocessMap(TileMapSettings mapSettings, RandomWormsSettings settings) { // Random Generator Random.State initialState = Random.state; if (settings.useFixedSeed) { Random.InitState(settings.seed.GetHashCode()); } else { Random.InitState(Time.time.ToString().GetHashCode()); } RandomWorms.settings = settings; RandomWorms.worms = new List <RandomWorm>(); RandomWorms.cellsCoordinates = new List <Coordinate>(settings.cellsToCreateCount) { new Coordinate(0, 0) // Initial spawn cell }; RandomWorms.maxX = RandomWorms.maxZ = RandomWorms.minX = RandomWorms.minZ = 0; RandomWorms.worms.Add(new RandomWorm(settings.chanceToTurnAround, settings.chanceToTurn, settings.allowTurnAround)); for (int i = 1; i < settings.wormsCount; i++) { RandomWorms.worms.Add(new RandomWorm(RandomWorms.worms[0].GetCurrentCoord())); } RandomWorm.SetChanceToDie(settings.initialChanceToDie, settings.increaseDieChanceBy); RandomWorm.createRoomEvent += CreateRectangularRoom; MoveWorms(); RandomWorm.createRoomEvent -= CreateRectangularRoom; RandomWorm.GetMaxCoord(ref RandomWorms.minX, ref RandomWorms.maxX, ref RandomWorms.minZ, ref RandomWorms.maxZ); mapSettings.mapWidth = RandomWorms.width = maxX - minX + 1; //+1 for zero tile mapSettings.mapHeight = RandomWorms.height = maxZ - minZ + 1; //+1 for zero tile Random.state = initialState; return(mapSettings); }