//Reads CSV file data and compiles it into a list of tile data objects that can be used to generate a tilemap public void ParseTileMapDataFromCSV(string path) { //Clears the current tilemap to prevent overlapping tile data tileMap.ClearMap(); //Create an empty list of room data objects List <TileDataCustom> roomDataList = new List <TileDataCustom>(); //Create and configure the text/CSV readers and reference the users selected filepath to read from TextReader textReader; CsvReader csv; textReader = File.OpenText(path); csv = new CsvReader(textReader); csv.Configuration.RegisterClassMap <RoomTileDataMap>(); csv.Configuration.HeaderValidated = null; //Use the CSV reader to read the data from the file and convert it into tile data csv.Read(); IEnumerable <TileDataCustom> enumerable = csv.GetRecords <TileDataCustom>(); roomDataList = enumerable.ToList(); textReader.Close(); //Pass the tile data into the tilemap construction function ConstructTileMapFromData(roomDataList); }
public void GenerateBackgroundTilemap(STETilemap existingTilemap) { //Clear the current background tilemap thisTilemap.ClearMap(); //If background type chosen is perlin, run the perlin background generation functionality if (backgroundType == BackgroundType.perlin) { //random seed for variation float rand = Random.Range(0f, 999999f); //Loop through and assign tiles based on whether their perlin value at the given tile (with the seed/scale taken into account) is above a set threshold //If its above the threshold, set it as a tile, if not, leave it blank //This leads to organic and natural looking curves of tiles and a nice looking randomised background for (int x = 0; x < existingTilemap.GridWidth; x++) { for (int y = 0; y < existingTilemap.GridHeight; y++) { float xCoord = rand + (float)x / (float)existingTilemap.GridWidth * perlinScale; float yCoord = rand + (float)y / (float)existingTilemap.GridHeight * perlinScale; float p = Mathf.PerlinNoise(xCoord, yCoord); if (p > perlinThreshold) { thisTilemap.SetTile(x, y, 0, brushId, eTileFlags.None); } } } } //If the background mode is set from the main map, set any tile behind a foreground tile to a rubbly/destroyed tile, and the rest to regular background tiles else if (backgroundType == BackgroundType.fromMainMap) { for (int x = 0; x < existingTilemap.GridWidth; x++) { for (int y = 0; y < existingTilemap.GridHeight; y++) { uint raw = existingTilemap.GetTileData(x, y); TileData processed = new TileData(raw); if (processed.tileId != 65535) { thisTilemap.SetTile(x, y, 0, brushId, eTileFlags.None); } else { thisTilemap.SetTile(x, y, 0, brushId, eTileFlags.None); } } } } //Update the background mesh after all generation is complete thisTilemap.UpdateMesh(); }
public override void OnEnter() { var go = Fsm.GetOwnerDefaultTarget(gameObject); if (UpdateCache(go)) { STETilemap tilemap = cachedComponent as STETilemap; tilemap.ClearMap(); } Finish(); }
public IEnumerator GenerateLevelEnumerator() { //Clear and re-initialise all lists, dictionaries, and current tilemaps in order to generate a fresh level clutteredTilePositions = new List <Vector2>(); DestructibleTerrainHandler.ClearAllClutterFromMap(); DestructibleTerrainHandler.associatedClutterDictionary = new Dictionary <Vector2, GameObject>(); tileMap.ClearMap(); //Call for a new room layout from the DMW algorithm drunkManWalking.GenerateLevel(); //Loop through each room from the DMW layout and request a room of that type from the room database master. foreach (KeyValuePair <Vector2, PCG.Room> entry in drunkManWalking.RoomDictionary) { //Creates a room that we return from the master class. Requires a theme, difficulty and type (open and closed points). room = RoomDatabaseMaster.Instance.GetRoomData(theme, RoomDatabaseMaster.Instance.GetRoomType(entry.Value.U, entry.Value.R, entry.Value.D, entry.Value.L), 0f); //With this room returned, call the construction function with the tile data supplied to create the room on the tilemap itself ConstructTileMapFromData(room.tileData, entry.Key); //If this generation is happening at runtime, update the mesh after each loop and wait until the end of the current frame before looping again //This is mainly to split up generation into a 'loading' process instead of just stalling until it is all completed if (Application.isPlaying) { tileMap.UpdateMesh(); yield return(new WaitForEndOfFrame()); } } //Generate the background tilemap based off the main one backgroundTilemap.GenerateBackgroundTilemap(tileMap); //Begin the clutter pass to spawn contextual props on the generated level #if UNITY_EDITOR if (Application.isPlaying || spawnClutterInEditor) { ClutterPass(theme); } #else ClutterPass(theme); #endif //Update the mesh one final time now that all generation is complete tileMap.UpdateMesh(); roomGenerationComplete = true; yield return(new WaitForSeconds(0)); }