コード例 #1
0
        private bool HandleDrop(ICommonSession?session, EntityCoordinates coords, EntityUid uid)
        {
            var ent = ((IPlayerSession?)session)?.AttachedEntity;

            if (ent == null || !ent.IsValid())
            {
                return(false);
            }

            if (!ent.TryGetComponent(out HandsComponent? handsComp))
            {
                return(false);
            }

            if (handsComp.ActiveHand == null || handsComp.GetActiveHand == null)
            {
                return(false);
            }

            // It's important to note that the calculations are done in map coordinates (they're absolute).
            // They're translated back to EntityCoordinates at the end.
            var entMap       = ent.Transform.MapPosition;
            var targetPos    = coords.ToMapPos(EntityManager);
            var dropVector   = targetPos - entMap.Position;
            var targetVector = Vector2.Zero;

            if (dropVector != Vector2.Zero)
            {
                var targetLength = MathF.Min(dropVector.Length, SharedInteractionSystem.InteractionRange - 0.001f); // InteractionRange is reduced due to InRange not dealing with floating point error
                var newCoords    = new MapCoordinates(dropVector.Normalized * targetLength + entMap.Position, entMap.MapId);
                var rayLength    = Get <SharedInteractionSystem>().UnobstructedDistance(entMap, newCoords, ignoredEnt: ent);
                targetVector = dropVector.Normalized * rayLength;
            }

            var resultMapCoordinates = new MapCoordinates(entMap.Position + targetVector, entMap.MapId);
            var resultEntCoordinates = EntityCoordinates.FromMap(coords.GetParent(EntityManager), resultMapCoordinates);

            handsComp.Drop(handsComp.ActiveHand, resultEntCoordinates);

            return(true);
        }