/// <summary> /// Converts an EdgeCollection to a List of SegmentShape2Ds that make up that EdgeCollection, /// whether it is an open or closed loop. /// </summary> /// <param name="edgeColl">EdgeCollection to be converted to a list of segments</param> /// <returns>A list of segments that make up the edges in the input EdgeCollection</returns> private static IEnumerable<SegmentShape2D> _EdgeCollToSegments(EdgeCollection<TileEdge> edgeColl) { var segments = new List<SegmentShape2D>(); List<Vector2> simplifiedPolygon = edgeColl.GetSimplifiedPerim(); for (int thisIndex = 1; thisIndex < simplifiedPolygon.Count; thisIndex++) { int prevIndex = thisIndex - 1; var segment = new SegmentShape2D { A = simplifiedPolygon[prevIndex], B = simplifiedPolygon[thisIndex], }; segments.Add(segment); } return segments; }
ConstructFloorPartitions(this TileMapList tileMaps, Dictionary <TileMap, RID> tileMapToFloorArea2Ds, PerimeterData perimData) { if (tileMaps is null) { throw new ArgumentNullException(nameof(tileMaps)); } if (tileMapToFloorArea2Ds is null) { throw new ArgumentNullException(nameof(tileMapToFloorArea2Ds)); } if (perimData is null) { throw new ArgumentNullException(nameof(perimData)); } var floorArea2DToPolygons = new Dictionary <RID, List <ConvexPolygonShape2D> >(); foreach (TileMap tileMap in tileMaps.Values) { RID area2dRID = tileMapToFloorArea2Ds[tileMap]; floorArea2DToPolygons[area2dRID] = new List <ConvexPolygonShape2D>(); Physics2DServer.AreaSetCollisionLayer(area2dRID, LayersFuncs.GetLayersValue(Terrain)); Physics2DServer.AreaSetCollisionMask(area2dRID, LayersFuncs.GetLayersValue(PlayerEntity, NpcEntity)); int maxTileGroups = perimData.GetMaxTileGroup(tileMap); for (int tileGroup = 0; tileGroup < maxTileGroups; tileGroup++) { int maxHoleGroups = perimData.GetMaxHoleGroup(tileMap, tileGroup); var allPerims = new List <Vector2> [maxHoleGroups]; for (int holeGroup = 0; holeGroup < maxHoleGroups; holeGroup++) { //put all perims (outer and hole) from one tile group into a single array of lists (gods help me) for partitioning EdgeCollection <TileEdge> edgeColl = perimData.GetEdgeCollection(tileMap, tileGroup, holeGroup); allPerims[holeGroup] = new List <Vector2>(edgeColl.GetSimplifiedPerim()); } List <ConvexPolygonShape2D> partitionedRectangles = _PartitionPolygonToRectangles(allPerims); foreach (ConvexPolygonShape2D shape in partitionedRectangles) { Physics2DServer.AreaAddShape(area2dRID, shape.GetRid()); GD.PrintS("added shape " + shape.GetRid().GetId() + " to area: " + area2dRID.GetId()); } floorArea2DToPolygons[area2dRID].AddRange(partitionedRectangles); } } return(floorArea2DToPolygons); }
/// <summary> /// Simplifies the outer perim using EdgeCollections simplification algorithm. /// </summary> private Vector2[] _SimplifyOuterPerim() { var edgeCollection = new EdgeCollection <PolyEdge>(); for (int i = 0; i < this.outerPerimNodes.Count; i++) { var thisCoord = new Vector2(this.outerPerimNodes[i].x, this.outerPerimNodes[i].y); var nextCoord = new Vector2(this.outerPerimNodes[(i + 1) % this.outerPerimNodes.Count].x, this.outerPerimNodes[(i + 1) % this.outerPerimNodes.Count].y); if (thisCoord == nextCoord) { continue; } var polyEdge = new PolyEdge(thisCoord, nextCoord); edgeCollection.Add(polyEdge); } return(edgeCollection.GetSimplifiedPerim().ToArray()); }