/// <summary>
        /// Set all walls and ledges that are not on the same z index as some entity to ignore it.
        /// Passed from entity MessageHub subscription and is fired when EntityTracker detects that an entity has changed z index.
        /// </summary>
        /// <param name="msg"></param>
        private void _HandleEntityChangedZIndex(EntityChangedZIndexMessage msg)
        {
            RID entityRID = msg.GetEntityRID();
            int zIndex    = msg.GetZIndex();

            GD.PrintS("requested that entity with id: " + entityRID.GetId() + " be moved to collide with segments on z index: " + zIndex);

            foreach (TileMap tileMap in this._tileMapToWallSB2Ds.Keys)
            {
                if (tileMap.ZIndex == zIndex)
                {
                    Physics2DServer.BodyRemoveCollisionException(this._tileMapToWallSB2Ds[tileMap], entityRID);
                }
                else
                {
                    Physics2DServer.BodyAddCollisionException(this._tileMapToWallSB2Ds[tileMap], entityRID);
                }
            }
            foreach (TileMap tileMap in this._tileMapToLedgeSB2Ds.Keys)
            {
                if (tileMap.ZIndex == zIndex)
                {
                    Physics2DServer.BodyRemoveCollisionException(this._tileMapToLedgeSB2Ds[tileMap], entityRID);
                    GD.PrintS("removed collision with sb2d with id: " + this._tileMapToLedgeSB2Ds[tileMap].GetId());
                }
                else
                {
                    Physics2DServer.BodyAddCollisionException(this._tileMapToLedgeSB2Ds[tileMap], entityRID);
                }
            }
        }
        /// <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");
        }
        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);
        }