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(); }
/// <summary> /// Takes in room tile data and sets all of the corresponding tiles on the tilemaps to the correct values. Also deals with mutateable tile logic. /// </summary> /// <param name="roomDataList">A list of the rooms tile data to draw tiles from.</param> /// <param name="offset">The offset of the current room on the tilemap</param> void ConstructTileMapFromData(List <TileDataCustom> roomDataList, Vector2 offset) { int xOffset = (int)offset.x * 32; int yOffset = (int)offset.y * 16; List <TileDataCustom> mutateableInCurrentRoom = new List <TileDataCustom>(); for (int i = 0; i < roomDataList.Count; i++) { int tileId = roomDataList[i].TileId; int x = (int)roomDataList[i].TileLocationX + xOffset; int y = (int)roomDataList[i].TileLocationY + yOffset; //If tile not empty if (tileId != 65535) { //If current tile data is not a mutateable tile, spawn it regularly //If not, add it to the mutatabeable list to be dealt with later if (!roomDataList[i].Mutateable) { tileMap.SetTile(x, y, tileId, brushID, eTileFlags.None); } else { mutateableInCurrentRoom.Add(roomDataList[i]); } } } //For every mutateable tile found in this room, check to see whether to spawn it or not for (int i = 0; i < mutateableInCurrentRoom.Count; i++) { //If tile is mutateable, roll a dice and see whether to set it or not [TEMP] //If not mutateable, just set it immediately if (mutateableInCurrentRoom[i].Mutateable) { int tileId = mutateableInCurrentRoom[i].TileId; int x = (int)mutateableInCurrentRoom[i].TileLocationX + xOffset; int y = (int)mutateableInCurrentRoom[i].TileLocationY + yOffset; //If random int is under the chance threshold, spawn the mutateable tile on the tilemap int rand = Random.Range(0, 11); if (rand < (int)(mutateableChance * 10)) { tileMap.SetTile(x, y, tileId, brushID, eTileFlags.None); } } } }
private void SetDoorState(bool state) { if (state == _oldState) { return; } var top = DoorLocation; var bot = new Vector2(top.x, top.y + 1); var tileTop = WallTileId; // Default is closed var tileBot = FloorTileId; if (Locked) { tileTop = LockedWallTileId; tileBot = LockedFloorTileId; } else if (state) { tileTop = OpenWallTileId; tileBot = OpenFloorTileId; } DoorTilemap.SetTile(bot, tileTop); DoorTilemap.SetTile(top, tileBot); if (InvertTiles) { SetFlipForTile(DoorTilemap, bot); SetFlipForTile(DoorTilemap, top); } _oldState = state; DoorTilemap.UpdateMeshImmediate(); }
//Loops through the passed in tile data and sets the tiles on the tilemap to the associated tile ID's void ConstructTileMapFromData(List <TileDataCustom> roomDataList) { for (int i = 0; i < roomDataList.Count; i++) { //Get the tile location on the tilemap grid by using the data stored in the TileDataCustom objects int tileId = roomDataList[i].TileId; int x = (int)roomDataList[i].TileLocationX; int y = (int)roomDataList[i].TileLocationY; //If the tile is not an empty tile (id 65535), set the tile on the tilemap to the corresponding ID if (roomDataList[i].TileId != 65535) { tileMap.SetTile(x, y, tileId, 2, eTileFlags.None); } } //Update the mesh once all tile data is set tileMap.UpdateMesh(); }
private void TryToPlaceDirt() { if (MathHelper.GetRandomInt(0, 100) < 95) { return; } var randomTile = GetRandomPlaceableTilePosition(); if (randomTile.HasValue) { var flag = GetRandomTileRotationFlag(); _doodadsBackgroundTilemap.SetTile( randomTile.Value, GetRandomDirtTile(), Tileset.k_BrushId_Default, flag ); _doodadsBackgroundTilemap.UpdateMeshImmediate(); } }