private Rect2 GetBoundingArea(IEnumerable <PathChunkArea> areas) { var firstArea = areas.ElementAt(0); var boundingRect = new Rect2(firstArea.ChunkOffset, new Vector2(firstArea.HorizontalChunkCount, firstArea.VerticalChunkCount)); foreach (var area in areas) { boundingRect = boundingRect.Merge(new Rect2(area.ChunkOffset, new Vector2(area.HorizontalChunkCount, area.VerticalChunkCount))); } // boundingRect = boundingRect.Grow(1f); return(boundingRect); }
/** * Returns a Rect2 containing all the tilemaps object given. */ private static Rect2 GetTilemapsLimits(Generic.List <TileMap> tilemaps) { // Prepare the return rect. Rect2 limits = new Rect2(); // Loop through the tilemaps. foreach (TileMap tilemap in tilemaps) { // Merge the rect2 objects. limits = limits.Merge(tilemap.GetUsedRect()); } // Return the limits. return(limits); }
public void Build(string lvlName, Dictionary <int, Array <Vector2> > plan) { Name = lvlName; Plan = plan; //GD.Print("Check 1"); UsedRect = new Rect2(); Env = new TileMap() { Name = "Env", CellSize = new Vector2(60, 60), TileSet = Preloads.EnvTileSet, CollisionLayer = 2, }; //GD.Print("Check 2"); Danger = new TileMap() { Name = "Danger", CellSize = new Vector2(60, 60), TileSet = Preloads.DangerTileSet, CollisionLayer = 5, }; //GD.Print("Check 3"); Env.AddToGroup("Environment", true); Danger.AddToGroup("Danger", true); //GD.Print("Check 4"); AddChild(Env); Env.Owner = this; AddChild(Danger); Danger.Owner = this; foreach (int i in plan.Keys) { switch (i) { case 0: case 1: //GD.Print("Check 5 1"); foreach (Vector2 tile in plan[i]) { Env.SetCell((int)tile.x, (int)tile.y, i); } UsedRect = Env.GetUsedRect(); break; case 2: //GD.Print("Check 5 2"); try { foreach (Vector2 tile in plan[2]) { Danger.SetCell((int)tile.x, (int)tile.y, 2); } UsedRect = UsedRect.Merge(Danger.GetUsedRect()); } catch (Exception ex) { GD.Print($"{ex.Message}"); } break; case 3: //GD.Print("Check 5 3"); try { foreach (Vector2 tile in plan[3]) { Node2D coin = (Node2D)Preloads.Coin.Instance(); coin.Name = $"Coin_{tile.x}_{tile.y}"; coin.Position = tile * 60f; //GD.Print($"{coin.Name} {coin.Position}"); AddChild(coin); coin.Owner = this; UsedRect = UsedRect.Expand(tile); } //GD.Print($"{Coins.GetChildCount()}"); } catch (Exception ex) { GD.Print($"{ex.Message}"); } break; case 4: //GD.Print("Check 5 4"); try { foreach (Vector2 tile in plan[4]) { Node2D flag = (Node2D)Preloads.Flag.Instance(); flag.Name = $"Flag_{tile.x}_{tile.y}"; flag.Position = tile * 60f; //GD.Print($"{flag.Name} {flag.Position}"); AddChild(flag); flag.Owner = this; if (Flag == null) { Flag = flag as Flag; } UsedRect = UsedRect.Expand(tile); } } catch (Exception ex) { GD.Print($"{ex.Message}"); } break; } } //GD.Print("Check 6"); Env.UpdateBitmaskRegion(); }