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); }
/////////////////////////// /////////////////////////// ////PRIVATE FUNCS BELOW//// /////////////////////////// /////////////////////////// /// <summary> /// For a given TileMap, gets its walls as a list of SegmentShape2Ds by getting perimData's EdgeCollections for the /// TileMap directly above the given TileMap. /// </summary> /// <param name="tileMaps">TileMapList of TileMaps.</param> /// <param name="thisTileMap">The TileMap that this function is getting walls for.</param> /// <param name="perimData">Data of all perimeters of all TileMaps in the currently loaded world.</param> /// <returns>A List of SegmentShape2Ds that contain every segment that make up every wall in thisTileMap.</returns> private static List<SegmentShape2D> _GetWallSegments(TileMapList tileMaps, TileMap thisTileMap, PerimeterData perimData) { var allSegments = new List<SegmentShape2D>(); if (thisTileMap == tileMaps.Last()) return allSegments; //there are never walls on the highest TileMap int nextIndex = thisTileMap.ZIndex + 1; TileMap nextTileMap = tileMaps[nextIndex]; int maxTileGroups = perimData.GetMaxTileGroup(nextTileMap); for (int tileGroup = 0; tileGroup < maxTileGroups; tileGroup++) { int maxHoleGroups = perimData.GetMaxHoleGroup(nextTileMap, tileGroup); for (int holeGroup = 0; holeGroup < maxHoleGroups; holeGroup++) { EdgeCollection<TileEdge> wallColl = perimData.GetEdgeCollection(nextTileMap, tileGroup, holeGroup); IEnumerable<SegmentShape2D> thisWallSegments = _ShiftSegmentsDown(_EdgeCollToSegments(wallColl)); allSegments.AddRange(thisWallSegments); } } return allSegments; }
Dictionary <HoleGroupKey, int>, Dictionary <TileGroupKey, int>) BuildLedges(this TileMapList tileMaps, PerimeterData perimData) { if (tileMaps is null) { throw new ArgumentNullException(nameof(tileMaps)); } if (perimData is null) { throw new ArgumentNullException(nameof(perimData)); } var ledgeCollMap = new Dictionary <LedgeCollKey, EdgeCollection <TileEdge> >(); var ledgeGroupMap = new Dictionary <LedgeGroupKey, int>(); var holeGroupMap = new Dictionary <HoleGroupKey, int>(); var tileGroupMap = new Dictionary <TileGroupKey, int>(); foreach (TileMap tileMap in tileMaps.Values) { int maxTileGroups = perimData.GetMaxTileGroup(tileMap); tileGroupMap.Add(new TileGroupKey(tileMap), maxTileGroups); for (int tileGroup = 0; tileGroup < maxTileGroups; tileGroup++) { int maxHoleGroups = perimData.GetMaxHoleGroup(tileMap, tileGroup); holeGroupMap.Add(new HoleGroupKey(tileMap, tileGroup), maxHoleGroups); for (int holeGroup = 0; holeGroup < maxHoleGroups; holeGroup++) { EdgeCollection <TileEdge> thisPerim = perimData.GetEdgeCollection(tileMap, tileGroup, holeGroup); (ledgeCollMap, ledgeGroupMap) = _FillLedges(ledgeCollMap, ledgeGroupMap, tileMaps, tileMap, thisPerim, tileGroup, holeGroup); } } } return(ledgeCollMap, ledgeGroupMap, holeGroupMap, tileGroupMap); }