private void LoadTilemap(string name) { // Build background // Load map data TiledMap tiledMap = new TiledMap(); tiledMap.LoadFromJSON(name); width = tiledMap.width; // Build visible tilemap with collisions normalTilemap.collisionMapping = new int[] { 1, // [0] block 0, // [1] duck 2, // [2] spike up 3, // [3] coin 4, // ... 0, 0, 0, 0 }; normalTilemap.Build(tiledMap, "background"); normalTilemap.transform.position = new Vector3(offsetX, 0); // Build duckland tilemap without colliders duckWorldTilemap.Build(tiledMap, "background", false); duckWorldTilemap.SetLayer(LayerMask.NameToLayer("DuckWorld")); duckWorldTilemap.transform.position = new Vector3(offsetX, 0); TiledMap.Layer bgLayer = tiledMap.layers["background"]; // Build special colliders for (int y = 0; y < bgLayer.height; ++y) { for (int x = 0; x < bgLayer.width; ++x) { int t = bgLayer.AtYup(x, y); // Note: t starts at index 1 Vector3 pos = new Vector3(offsetX + x + 0.5f, y + 0.5f); if (t == 3) { Instantiate(spikePrefab, pos, Quaternion.identity); } } } // Build collectibles TiledMap.Layer objectLayer = tiledMap.layers["objects"]; for (int y = 0; y < objectLayer.height; ++y) { for (int x = 0; x < objectLayer.width; ++x) { int t = objectLayer.AtYup(x, y); if (t == 4) { Instantiate(collectiblePrefab, new Vector3(offsetX + x + 0.5f, y + 0.5f), Quaternion.identity); } else if (t == 5) { Instantiate(cancerPrefab, new Vector3(offsetX + x + 0.5f, y + 0.5f), Quaternion.identity); } else if (t == 6) { Instantiate(goalPrefab, new Vector3(offsetX + x + 0.5f, y + 0.5f), Quaternion.identity); } else if (t == 7) { Instantiate(heartPrefab, new Vector3(offsetX + x + 0.5f, y + 0.5f), Quaternion.identity); } } } }
public Tilemap <TileInfo> Generate() { if (!this.MuteAllOutput) { Console.WriteLine("------------------------------"); Console.WriteLine("Generating level with radius {0} ({1} tiles)...", this.Radius, Extensions.TileCountForRadius(this.Radius)); } var timer = SteppedStopwatch.StartNew(); timer.Mute = this.MuteAllOutput || !this.LogGenerationDetails; #region initialise var tempMap = new Tilemap <GeneratingTileInfo>(this.Radius); timer.WriteStepToConsole("Made mutable tilemap ......... {0}"); var tiles = tempMap.ToList(); timer.WriteStepToConsole("Enumerated tiles ............. {0}"); //var tilesSpiral = tempMap.TilesSpiralOutward.ToList(); //var tilesDistance = tiles.OrderBy( // t => (Settings.Game.Level.TileToPosition * t.Xy).LengthSquared // ).ToList(); //var infos = new List<GeneratingTileInfo>(tiles.Count); foreach (var tile in tiles) { var info = new GeneratingTileInfo(); //infos.Add(info); tempMap[tile] = info; } timer.WriteStepToConsole("Filled mutable tiles ......... {0}"); #endregion var centerTile = new GeneratingTile(tempMap, 0, 0); #region open walls centerTile.OpenRandomSpanningTree(this.MinCorridorWidth, this.MaxCorridorWidth); timer.WriteStepToConsole("Opened random spanning tree .. {0}"); if (this.OpennessCore > 0 || this.OpennessRim > 0) { tiles.OpenRandomWalls(t => GameMath.Lerp( this.OpennessCore, this.OpennessRim, (float)t.Radius / this.Radius), this.MinCorridorWidth, this.MaxCorridorWidth); timer.WriteStepToConsole( string.Format("Opened {0:0}%-{1:0}% random walls ", this.OpennessCore * 100, this.OpennessRim * 100) .PadRight(30, '.') + " {0}"); } #endregion #region make level geometry tiles.MakeWalls(); timer.WriteStepToConsole("Generated level walls ........ {0}"); tiles.MakeFloors(); timer.WriteStepToConsole("Generated level floors ....... {0}"); #endregion #region light level var lightBuffer1 = new Tilemap <float>(this.Radius); var lightBuffer2 = new Tilemap <float>(this.Radius); lightBuffer1.SetRandom(); lightBuffer1[0, 0] = 0.5f; lightBuffer1.DilateTo(tempMap, lightBuffer2); foreach (var tile in lightBuffer2.Where(t => t.Radius == this.Radius)) { lightBuffer2[tile] = 1f; } lightBuffer2.SmoothTo(tempMap, lightBuffer1); //lightBuffer1.SmoothTo(tempMap, lightBuffer2); lightBuffer1.SetBrightness(tempMap); timer.WriteStepToConsole("Generated level lightness .... {0}"); #endregion #region build var result = tempMap.Build(); timer.WriteStepToConsole("Build immutable tilemap ...... {0}"); #endregion if (!this.MuteAllOutput) { timer.Mute = false; timer.WriteTotalToConsole("Generated level in {0}"); Console.WriteLine("------------------------------"); } return(result); }
public void LoadTilemap() { tilemap = new Tilemap(gameObject); tilemap.Load(mapAsset); tilemap.Build(); }