public void AddTileGenerationInfo(TileGenerationInfo info) { tileGenerationInfoList.Add(info); }
/// Level generation logic void CreateTiles() { Dictionary <Tile.TileType, int> tileCount = new Dictionary <Tile.TileType, int>(); Dictionary <Tile.TileType, int> tileMinimumGuarantees = new Dictionary <Tile.TileType, int>(); foreach (var tileGenerationInfo in levelDefinition.tileGenerationInfoList) { // add each minimum guarantee tileMinimumGuarantees.Add(tileGenerationInfo.tileType, tileGenerationInfo.guaranteeAtLeast); // pre-populate dictionary tileCount.Add(tileGenerationInfo.tileType, 0); } for (int i = 0; i < MapHeight; i++) { for (int j = 0; j < MapWidth; j++) { Tile.TileType _type = ChooseNextTileType(j, i); Coordinate coordinateToCreate = new Coordinate(j, i); CreateOneTile(coordinateToCreate, _type); if (_type != Tile.TileType.EMPTY) { tileCount[_type]++; } } } // add tile strips int groupNameIndex = 0; foreach (var stripInfo in levelDefinition.tileGenerationInfoList.Where(i => i.isStripType == true)) { for (int i = 0; i < stripInfo.numStrips; i++) { // generate name for tile group TileGroup tileGroup = new TileGroup(stripInfo.tileType.ToString() + " " + groupNameIndex); // get even distribution position int stripDepth = ((MapHeight - NumSkyTiles - numRewardTilesAtBottom) / (int)stripInfo.numStrips * (i + 1)); // i + 1 so that a strip doesn't appear at the very top int stripDepthOffset = 0; for (int j = 0; j < MapWidth; j++) { // random offset stripDepthOffset += UnityEngine.Random.Range(-1, 2); int depthOfStripTile = stripDepth + stripDepthOffset + NumSkyTiles; Coordinate stripTileCoordinate = new Coordinate(j, Mathf.Min(depthOfStripTile, MapHeight - numRewardTilesAtBottom - 1)); // increase tile count for tile added tileCount[stripInfo.tileType]++; // decrease tile count for tile removed tileCount[TileTypeToEnumTileType(tileGrid.GetTileAt(stripTileCoordinate).GetType())]--; // replace the tile Tile t = ReplaceOneTile(stripTileCoordinate, stripInfo.tileType); t.waveDefinition = levelDefinition.waveDefinitionList[groupNameIndex]; tileGroup.tileLocations.Add(stripTileCoordinate); } TileGroups.Add(tileGroup); groupNameIndex++; } } // check if guaranteed tile quantities were met, and add more if not foreach (var minGuarantee in tileMinimumGuarantees) { if (minGuarantee.Value > 0) { while (minGuarantee.Value > tileCount[minGuarantee.Key]) { // choose a place in the grid to add one TileGenerationInfo tileGenInfo = levelDefinition.GetTileGenerationInfoFromType(minGuarantee.Key); if ((tileGenInfo.depthRangeStart + NumSkyTiles) >= MapHeight) { Debug.LogError("Tile Depth Range Start set to beyond depth of grid"); } int randX = UnityEngine.Random.Range(0, MapWidth); int randY = UnityEngine.Random.Range( tileGenInfo.depthRangeStart + NumSkyTiles, Mathf.Min(tileGenInfo.depthRangeEnd + NumSkyTiles, MapHeight - 1)); // verify that you're not replacing over another tile with a minimum guarantee, or a strip Tile tileBeingReplaced = tileGrid.GetTileAt(new Coordinate(randX, randY)); TileGenerationInfo overwriteCandidateInfo = levelDefinition.GetTileGenerationInfoFromType(tileBeingReplaced.GetTileType()); if (overwriteCandidateInfo.guaranteeAtLeast > 0 || overwriteCandidateInfo.isStripType == true) { continue; } // increase tile count for tile added tileCount[minGuarantee.Key]++; // decrease tile count for tile removed tileCount[TileTypeToEnumTileType(tileGrid.GetTileAt(new Coordinate(randX, randY)).GetType())]--; // replace the tile ReplaceOneTile(new Coordinate(randX, randY), minGuarantee.Key); } } } }