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)); }
private void CheckForDirtAndCleanIfPossible() { if (_currentCapacity == 0) { return; } // Check tile below me var realWorldPosition = transform.position; var vLocPos = _doodadsBackgroundTilemap.transform.InverseTransformPoint(realWorldPosition); var gridX = BrushUtil.GetGridX(vLocPos, _doodadsBackgroundTilemap.CellSize); var gridY = BrushUtil.GetGridY(vLocPos, _doodadsBackgroundTilemap.CellSize); var gridPosition = TilemapUtils.GetGridPosition( _doodadsBackgroundTilemap, new Vector2(realWorldPosition.x, realWorldPosition.y) ); gridPosition = new Vector2(gridX, gridY); var tileData = _doodadsBackgroundTilemap.GetTileData(gridPosition); // Remove the tile below me! if (IsDirtTile(tileData)) { _doodadsBackgroundTilemap.Erase(gridPosition); _doodadsBackgroundTilemap.UpdateMesh(); _currentCapacity--; } }
protected void DoColorPaintAction() { var go = Fsm.GetOwnerDefaultTarget(gameObject); if (UpdateCache(go)) { STETilemap tilemap = cachedComponent as STETilemap; Vector2 center; if ((ePositionType)positionType.Value == ePositionType.LocalPosition) { center = position.Value; } else// if ((ePositionType)positionType.Value == ePositionType.GridPosition) { center = TilemapUtils.GetTileCenterPosition(tilemap, (int)position.Value.x, (int)position.Value.y); } if (applyToTilemapGroup.Value && tilemap.ParentTilemapGroup) { tilemap.ParentTilemapGroup.IterateTilemapWithAction( (STETilemap tmap) => { TilemapVertexPaintUtils.VertexPaintCircle(tmap, center, radius.Value, tileColor.Value, (eBlendMode)blendingMode.Value, (eTileColorPaintMode)tileColorPaintMode.Value == eTileColorPaintMode.Vertex, intensityCurve.curve); tmap.UpdateMesh(); } ); } else { TilemapVertexPaintUtils.VertexPaintCircle(tilemap, center, radius.Value, tileColor.Value, (eBlendMode)blendingMode.Value, (eTileColorPaintMode)tileColorPaintMode.Value == eTileColorPaintMode.Vertex, intensityCurve.curve); tilemap.UpdateMesh(); } } }
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(); }
protected void DoColorPaintAction() { var go = Fsm.GetOwnerDefaultTarget(gameObject); if (UpdateCache(go)) { STETilemap tilemap = cachedComponent as STETilemap; tilemap.RemoveColorChannel(); tilemap.UpdateMesh(); } }
public static void DeserializeTilemap(TilemapSerializedData data, STETilemap tilemap) { foreach (TilemapSerializedData.TileData dataItem in data.tileDataList) { tilemap.SetTileData(dataItem.gridX, dataItem.gridY, dataItem.tileData); // NOT SURE ABOUT THIS LINE. } tilemap.UpdateMesh(); //TilemapUtils.IterateTilemapWithAction(tilemap, action); //return data; }
private void DrawTileToLayers() { bool fx = false; bool fy = false; int rot = 0; for (int i = 0; i < this.pyxelTilemap.layers.Length; i++) { STETilemap map = pyxelEdit.transform.Find(this.pyxelTilemap.layers[i].name).GetComponent <STETilemap>(); for (int t = 0; t < this.pyxelTilemap.layers[i].tiles.Length; t++) { int tileID = this.pyxelTilemap.layers[i].tiles[t].tile; rot = this.pyxelTilemap.layers[i].tiles[t].rot; fx = this.pyxelTilemap.layers[i].tiles[t].flipX; fy = (rot == 2) ? true : false; if (rot == 2) { fx = !fx; } int x = this.pyxelTilemap.layers[i].tiles[t].x; int y = (this.pyxelTilemap.tileshigh - this.pyxelTilemap.layers[i].tiles[t].y) - 1; if (tileID < 0) { map.Erase(x, y); } else { TileData tileData = new TileData(); tileData.brushId = 0; tileData.tileId = this.pyxelTilemap.layers[i].tiles[t].tile; tileData.flipHorizontal = fx; tileData.flipVertical = fy; map.SetTileData(x, y, tileData.BuildData()); } } map.UpdateMesh(); } }
public override void OnEnter() { var go = Fsm.GetOwnerDefaultTarget(gameObject); if (UpdateCache(go)) { STETilemap tilemap = cachedComponent as STETilemap; int gridX; int gridY; if ((ePositionType)positionType.Value == ePositionType.LocalPosition) { gridX = TilemapUtils.GetGridX(tilemap, startPaintingPosition.Value); gridY = TilemapUtils.GetGridY(tilemap, startPaintingPosition.Value); } else// if ((ePositionType)positionType.Value == ePositionType.GridPosition) { gridX = (int)startPaintingPosition.Value.x; gridY = (int)startPaintingPosition.Value.y; } if (tileSelection.selectionData.Length == 1) { tilemap.SetTileData(gridX, gridY, (uint)tileSelection.selectionData[0]); } else { int w = tileSelection.rowLength; int h = tileSelection.columnLength; int xf = -(w >> 1) + 1 - (w & 1); int yf = -(h >> 1) + 1 - (h & 1); for (int y = h - 1, idx = 0; y >= 0; --y) { for (int x = 0; x < w; ++x, ++idx) { tilemap.SetTileData(gridX + x + xf, gridY + y + yf, (uint)tileSelection.selectionData[idx]); } } } tilemap.UpdateMesh(); } Finish(); }
protected void DoColorPaintAction() { var go = Fsm.GetOwnerDefaultTarget(gameObject); if (UpdateCache(go)) { STETilemap tilemap = cachedComponent as STETilemap; if ((ePositionType)positionType.Value == ePositionType.LocalPosition) { tilemap.SetTileColor(position.Value, tileColor.Value, (eBlendMode)blendingMode.Value); } else// if ((ePositionType)positionType.Value == ePositionType.GridPosition) { tilemap.SetTileColor((int)position.Value.x, (int)position.Value.y, tileColor.Value, (eBlendMode)blendingMode.Value); } tilemap.UpdateMesh(); } }
public override void OnEnter() { var go = Fsm.GetOwnerDefaultTarget(gameObject); if (UpdateCache(go)) { STETilemap tilemap = cachedComponent as STETilemap; if (forceUpdate.Value) { tilemap.UpdateMeshImmediate(); } else { tilemap.UpdateMesh(); } } Finish(); }
//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(); }
protected override void DoPaintAction() { var go = Fsm.GetOwnerDefaultTarget(gameObject); if (UpdateCache(go)) { STETilemap tilemap = cachedComponent as STETilemap; if ((ePositionType)positionType.Value == ePositionType.LocalPosition) { TilemapDrawingUtils.DrawDot(tilemap as STETilemap, startPaintingPosition.Value, tileSelection.Get2DTileDataArray(), randomizePattern.Value); } else// if ((ePositionType)positionType.Value == ePositionType.GridPosition) { int x0 = (int)startPaintingPosition.Value.x; int y0 = (int)startPaintingPosition.Value.y; TilemapDrawingUtils.DrawDot(tilemap as STETilemap, x0, y0, tileSelection.Get2DTileDataArray(), randomizePattern.Value); } tilemap.UpdateMesh(); } }
protected override void DoPaintAction() { var go = Fsm.GetOwnerDefaultTarget(gameObject); if (UpdateCache(go)) { STETilemap tilemap = cachedComponent as STETilemap; int gridX; int gridY; if ((ePositionType)positionType.Value == ePositionType.LocalPosition) { gridX = TilemapUtils.GetGridX(tilemap, startPaintingPosition.Value); gridY = TilemapUtils.GetGridY(tilemap, startPaintingPosition.Value); } else// if ((ePositionType)positionType.Value == ePositionType.GridPosition) { gridX = (int)startPaintingPosition.Value.x; gridY = (int)startPaintingPosition.Value.y; } TilemapDrawingUtils.FloodFill(tilemap as STETilemap, gridX, gridY, tileSelection.Get2DTileDataArray(), randomizePattern.Value); tilemap.UpdateMesh(); } }
public static void ImportTmxIntoTheScene(string tmxFilePath, Tileset tileset) { string tmxFileName = Path.GetFileNameWithoutExtension(tmxFilePath); TmxTilemap tilemap = TmxTilemap.LoadFromFile(tmxFilePath); if (tilemap.DicTilesetTex2D.Values.Count == 0) { return; } GameObject tilemapGroupObj = new GameObject(tmxFileName); TilemapGroup tilemapGroup = tilemapGroupObj.AddComponent <TilemapGroup>(); int orderInLayer = 0; foreach (TmxLayer layer in tilemap.Map.Layers) { GameObject tilemapObj = new GameObject(layer.Name); tilemapObj.transform.SetParent(tilemapGroupObj.transform); STETilemap tilemapBhv = tilemapObj.AddComponent <STETilemap>(); tilemapBhv.Tileset = tileset; tilemapBhv.OrderInLayer = orderInLayer++; tilemapBhv.IsVisible = layer.Visible; tilemapBhv.TintColor = new Color(1f, 1f, 1f, layer.Opacity); for (int gx = 0; gx < layer.Width; gx++) { for (int gy = 0; gy < layer.Height; gy++) { int tileIdx = gy * layer.Width + gx; TmxLayerTile tile = layer.Tiles[tileIdx]; //skip non valid tiles if (tile.GId == 0) { continue; } int tileId = tilemap.GetTileAbsoluteId(tile); uint tileData = tileId >= 0 ? (uint)tileId : Tileset.k_TileData_Empty; if (tileData != Tileset.k_TileData_Empty) { // add tile flags if ((tile.GId & k_FLIPPED_HORIZONTALLY_FLAG) != 0) { tileData |= Tileset.k_TileFlag_FlipH; } if ((tile.GId & k_FLIPPED_VERTICALLY_FLAG) != 0) { tileData |= Tileset.k_TileFlag_FlipV; } if ((tile.GId & k_FLIPPED_DIAGONALLY_FLAG) != 0) { tileData |= Tileset.k_TileFlag_Rot90; } //convert from tiled flip diagonal to rot90 if ((tileData & Tileset.k_TileFlag_Rot90) != 0) { if (((tile.GId & k_FLIPPED_HORIZONTALLY_FLAG) != 0) != ((tile.GId & k_FLIPPED_VERTICALLY_FLAG) != 0)) { tileData ^= Tileset.k_TileFlag_FlipH; } else { tileData ^= Tileset.k_TileFlag_FlipV; } } } tilemapBhv.SetTileData(gx, layer.Height - gy - 1, tileData); } } tilemapBhv.UpdateMesh(); } tilemapGroup.Refresh(); }