public Transform[,] GenerateTiles() { this.mapSettings = Constants.MapSettings; // calculate how many tiles of each kind are required int numOfTiles = mapSettings.MapSize.X * mapSettings.MapSize.Y; float sum = mapSettings.CalculateTilesSum(); if (sum <= 0) { throw new InvalidOperationException("There are no tile types specified. Unable to create map"); } int water = (int)(mapSettings.WaterPercent / sum * numOfTiles); int grass = (int)(mapSettings.GrassPercent / sum * numOfTiles); int desert = (int)(mapSettings.DesertPercent / sum * numOfTiles); int mountain = (int)(mapSettings.MountainPercent / sum * numOfTiles); int forest = (int)(mapSettings.ForestPercent / sum * numOfTiles); // fix possible rounding error if (numOfTiles != (water + grass + desert + mountain + forest)) { water = numOfTiles - (grass + desert + mountain + forest); // TODO : can water be negativ? } // add all tiles to a list List <Transform> transforms = CreateListOfAllTiles(water, grass, desert, mountain, forest); // shuffle the list of tiles ShuffleList(transforms); // creat an array of random tiles Transform[,] tiles = ConverToArray(transforms); return(tiles); }
public void AllTileTypesNull() { MapSettings[] settings = new MapSettings[] { CreateSettings(50, 50, 0, 0, 0, 0, 0), }; int[][] tilesExpected = new int[][] { new int[] { 20, 20, 20, 20, 20 }, }; Assert.Throws <InvalidOperationException>(() => ExecuteTest(settings, tilesExpected)); }
private static MapSettings CreateSettings(int x, int y, float water, float grass, float desert, float mountain, float forest) { MapSettings settings = new MapSettings(); settings.MapSize = new Coord(x, y); settings.WaterPercent = water; settings.GrassPercent = grass; settings.DesertPercent = desert; settings.MountainPercent = mountain; settings.ForestPercent = forest; return(settings); }
public void GenerateLargeMaps() { MapSettings[] settings = new MapSettings[] { CreateSettings(100, 100, 1, 1, 1, 1, 1), CreateSettings(100, 1, 1, 1, 1, 1, 1), CreateSettings(1, 100, 1, 1, 1, 1, 1), }; int[][] tilesExpected = new int[][] { new int[] { 2000, 2000, 2000, 2000, 2000 }, new int[] { 20, 20, 20, 20, 20 }, new int[] { 20, 20, 20, 20, 20 }, }; ExecuteTest(settings, tilesExpected); }
public void GenerateThreeTerrains() { MapSettings[] settings = new MapSettings[] { CreateSettings(10, 10, 1, 1, 1, 0, 0), CreateSettings(10, 10, 0, 1, 1, 1, 0), CreateSettings(10, 10, 0, 0, 1, 1, 1), CreateSettings(10, 10, 1, 0, 0, 1, 1), CreateSettings(10, 10, 1, 1, 0, 0, 1) }; int[][] tilesExpected = new int[][] { new int[] { 34, 33, 33, 0, 0 }, new int[] { 1, 33, 33, 33, 0 }, new int[] { 1, 0, 33, 33, 33 }, new int[] { 34, 0, 0, 33, 33 }, new int[] { 34, 33, 0, 0, 33 }, }; ExecuteTest(settings, tilesExpected); }
public void GenerateTwoTerrains() { MapSettings[] settings = new MapSettings[] { CreateSettings(10, 10, 1, 1, 0, 0, 0), CreateSettings(10, 10, 0, 1, 1, 0, 0), CreateSettings(10, 10, 0, 0, 1, 1, 0), CreateSettings(10, 10, 0, 0, 0, 1, 1), CreateSettings(10, 10, 1, 0, 0, 0, 1) }; int[][] tilesExpected = new int[][] { new int[] { 50, 50, 0, 0, 0 }, new int[] { 0, 50, 50, 0, 0 }, new int[] { 0, 0, 50, 50, 0 }, new int[] { 0, 0, 0, 50, 50 }, new int[] { 50, 0, 0, 0, 50 }, }; ExecuteTest(settings, tilesExpected); }