/// <summary> /// Creates and returns a dictionary that maps a TileMap's StaticBody2D's RID to said TileMap's walls or ledges. /// </summary> /// <param name="tileMaps">List of TileMaps.</param> /// <param name="tileMapToSB2Ds">Map of TileMap to its SB2D, whether its origin was a wall or ledge.</param> /// <param name="perimData">Data of all perimeters of all TileMaps.</param> /// <param name="ledgeData">Data of all ledges of all TileMaps.</param> /// <param name="option">Whether this function is being used to construct segments for walls or ledges.</param> /// <returns>A dictionary that maps a TileMap's SB2D RID to said TileMap's walls.</returns> public static Dictionary<RID, List<SegmentShape2D>> ConstructSB2DSegments(this TileMapList tileMaps, Dictionary<TileMap, RID> tileMapToSB2Ds, PerimeterData perimData, LedgeData ledgeData, StaticBodyOrigin option) { if (tileMaps is null) throw new ArgumentNullException(nameof(tileMaps)); if (tileMapToSB2Ds is null) throw new ArgumentNullException(nameof(tileMapToSB2Ds)); if (perimData is null) throw new ArgumentNullException(nameof(perimData)); if (ledgeData is null) throw new ArgumentNullException(nameof(ledgeData)); var sb2dToSegments = new Dictionary<RID, List<SegmentShape2D>>(); foreach (TileMap tileMap in tileMaps.Values) { RID wall = tileMapToSB2Ds[tileMap]; List<SegmentShape2D> segments = (option == StaticBodyOrigin.Wall) ? _GetWallSegments(tileMaps, tileMap, perimData) : _GetLedgeSegments(tileMaps, tileMap, ledgeData); sb2dToSegments.Add(wall, segments); foreach (SegmentShape2D segment in segments) { Physics2DServer.BodyAddShape(wall, segment.GetRid()); } } return sb2dToSegments; }
LedgeData ledgeData) BuildTileNodes(IEnumerable <Node> worldChildren) { if (worldChildren is null) { throw new ArgumentNullException(nameof(worldChildren)); } TileMapList tileMaps = _FillTilemapsArray(worldChildren); var perimData = new PerimeterData(tileMaps); var ledgeData = new LedgeData(perimData, tileMaps); return(tileMaps, perimData, ledgeData); }
/// <summary> /// Should be called whenever loaded world changes. /// Resets all wall and ledge physics bodies and prepares new ones to be filled by TileController. /// </summary> /// <param name="tileMaps">List of TileMaps which are guaranteed to have a unique Z index.</param> /// <param name="perimData">Data of all perimeters of all TileMaps.</param> /// <param name="ledgeData">Data of all ledges of all TileMaps.</param> /// <param name="worldSpace">Current world space.</param> public void LoadWorld(TileMapList tileMaps, PerimeterData perimData, LedgeData ledgeData, RID worldSpace) { if (tileMaps == null) { throw new ArgumentNullException(nameof(tileMaps), "Attempted to load world with no tilemaps."); } this._tileMapToFloorArea2Ds = tileMaps.ConstructTileMapFloorMap(worldSpace); this._tileMapToWallSB2Ds = tileMaps.ConstructTileMapCollisionMap(worldSpace); this._tileMapToLedgeSB2Ds = tileMaps.ConstructTileMapCollisionMap(worldSpace); this._floorArea2DToPolygons = tileMaps.ConstructFloorPartitions(this._tileMapToFloorArea2Ds, perimData); this._wallSB2DToSegments = tileMaps.ConstructSB2DSegments(this._tileMapToWallSB2Ds, perimData, ledgeData, CollisionConstructor.StaticBodyOrigin.Wall); this._ledgeSB2DToSegments = tileMaps.ConstructSB2DSegments(this._tileMapToLedgeSB2Ds, perimData, ledgeData, CollisionConstructor.StaticBodyOrigin.Ledge); this._toEntityHub.Publish(new FloorsConstructedMessage(this, this._tileMapToFloorArea2Ds)); foreach (TileMap tileMap in tileMaps.Values) { GD.PrintS("TileMapToFloorArea2Ds RID for tilemap with zIndex " + tileMap.ZIndex + ": " + this._tileMapToFloorArea2Ds[tileMap].GetId()); //GD.PrintS("TileMapToWallSB2Ds RID for tilemap with zIndex " + tileMap.ZIndex + ": " + this.tileMapToWallSB2Ds[tileMap].GetId()); //GD.PrintS("TileMapToLedgeSB2Ds RID for tilemap with zIndex " + tileMap.ZIndex + ": " + this.tileMapToLedgeSB2Ds[tileMap].GetId()); RID floorRID = this._tileMapToFloorArea2Ds[tileMap]; RID wallRID = this._tileMapToWallSB2Ds[tileMap]; RID ledgeRID = this._tileMapToLedgeSB2Ds[tileMap]; GD.PrintS("floorArea2DToPolygons count: " + this._floorArea2DToPolygons[floorRID].Count); GD.PrintS("wallSB2DToSegments count: " + this._wallSB2DToSegments[wallRID].Count); GD.PrintS("ledgeSB2DToSegments count: " + this._ledgeSB2DToSegments[ledgeRID].Count); this._area2DMonitors[floorRID.GetId()] = new Area2DMonitor(floorRID, tileMap.ZIndex, this._floorArea2DToPolygons[floorRID], this._toEntityHub); Physics2DServer.AreaSetMonitorCallback(floorRID, this._area2DMonitors[floorRID.GetId()], nameof(Area2DMonitor.OnAreaCallback)); } this._toEntityHub.Publish(new AreaMonitorsSetupMessage(this)); GD.PrintS("published areamonitor msg"); }
/// <summary> /// For a given TileMap, gets its ledges as a list of SegmentShape2Ds by iterating through LedgeData for: /// 1. The given TileMap's own ledges /// 2. Ledges that have been superimposed from every lower TileMap to the given TileMap /// </summary> /// <param name="tileMaps">List of TileMaps.</param> /// <param name="thisTileMap">The TileMap this function is getting ledges for.</param> /// <param name="ledgeData">Data of all ledges of all TileMaps.</param> /// <returns>A list of SegmentShape2Ds that contain every segment of every ledge that is on thisTileMap.</returns> private static List<SegmentShape2D> _GetLedgeSegments(TileMapList tileMaps, TileMap thisTileMap, LedgeData ledgeData) { var allSegments = new List<SegmentShape2D>(); for (int zIndex = 0; zIndex <= thisTileMap.ZIndex; zIndex++) { TileMap baseTileMap = tileMaps[zIndex]; int maxTileGroups = ledgeData.GetMaxTileGroup(baseTileMap); for (int tileGroup = 0; tileGroup < maxTileGroups; tileGroup++) { int maxHoleGroups = ledgeData.GetMaxHoleGroup(baseTileMap, tileGroup); for (int holeGroup = 0; holeGroup < maxHoleGroups; holeGroup++) { int maxLedgeGroups = ledgeData.GetMaxLedgeGroup(baseTileMap, tileGroup, holeGroup, thisTileMap); for (int ledgeGroup = 0; ledgeGroup < maxLedgeGroups; ledgeGroup++) { EdgeCollection<TileEdge> ledgeColl = ledgeData.GetLedgeCollection(baseTileMap, tileGroup, holeGroup, thisTileMap, ledgeGroup); IEnumerable<SegmentShape2D> thisLedgeSegments = _EdgeCollToSegments(ledgeColl); allSegments.AddRange(thisLedgeSegments); } } } } return allSegments; }