From 5e2109a2d6275edc47cac9ccc679d41df90dfc17 Mon Sep 17 00:00:00 2001 From: DrSmugleaf Date: Fri, 24 Jul 2020 14:22:28 +0200 Subject: [PATCH] Add unbuckling entities that are moved away from their straps (#1437) * Add unbuckling entities that move away from their straps * Remove range constraint from movement unbuckling check * Fix setting the wrong positions when buckling * Fix beds making you sleep for the rest of your life --- .../Components/Buckle/BuckleComponent.cs | 22 ++++++++---- .../GameObjects/EntitySystems/BuckleSystem.cs | 36 ++++++++++++++++++- Content.Server/Mobs/StandingStateHelper.cs | 11 ++++-- 3 files changed, 59 insertions(+), 10 deletions(-) diff --git a/Content.Server/GameObjects/Components/Buckle/BuckleComponent.cs b/Content.Server/GameObjects/Components/Buckle/BuckleComponent.cs index 2a4c406dac..275fe52859 100644 --- a/Content.Server/GameObjects/Components/Buckle/BuckleComponent.cs +++ b/Content.Server/GameObjects/Components/Buckle/BuckleComponent.cs @@ -2,6 +2,7 @@ using System; using Content.Server.GameObjects.Components.Mobs; using Content.Server.GameObjects.Components.Strap; +using Content.Server.GameObjects.EntitySystems; using Content.Server.Interfaces; using Content.Server.Mobs; using Content.Server.Utility; @@ -21,6 +22,7 @@ using Robust.Shared.Interfaces.GameObjects; using Robust.Shared.Interfaces.Timing; using Robust.Shared.IoC; using Robust.Shared.Localization; +using Robust.Shared.Map; using Robust.Shared.Maths; using Robust.Shared.Serialization; using Robust.Shared.ViewVariables; @@ -58,6 +60,8 @@ namespace Content.Server.GameObjects.Components.Buckle [ViewVariables] private TimeSpan _buckleTime; + public Vector2? BuckleOffset { get; private set; } + private StrapComponent? _buckledTo; /// @@ -116,7 +120,6 @@ namespace Content.Server.GameObjects.Components.Buckle var ownTransform = Owner.Transform; var strapTransform = strap.Owner.Transform; - ownTransform.GridPosition = strapTransform.GridPosition; ownTransform.AttachParent(strapTransform); switch (strap.Position) @@ -129,14 +132,21 @@ namespace Content.Server.GameObjects.Components.Buckle ownTransform.WorldRotation = strapTransform.WorldRotation; break; case StrapPosition.Down: - StandingStateHelper.Down(Owner); + StandingStateHelper.Down(Owner, force: true); ownTransform.WorldRotation = Angle.South; break; } + // Assign BuckleOffset first, before causing a MoveEvent to fire if (strapTransform.WorldRotation.GetCardinalDir() == Direction.North) { - ownTransform.WorldPosition += (0, 0.15f); + BuckleOffset = (0, 0.15f); + ownTransform.WorldPosition = strapTransform.WorldPosition + BuckleOffset!.Value; + } + else + { + BuckleOffset = Vector2.Zero; + ownTransform.WorldPosition = strapTransform.WorldPosition; } } @@ -257,9 +267,9 @@ namespace Content.Server.GameObjects.Components.Buckle appearance.SetData(BuckleVisuals.Buckled, true); } - ReAttach(strap); - BuckledTo = strap; + + ReAttach(strap); BuckleStatus(); return true; @@ -315,8 +325,8 @@ namespace Content.Server.GameObjects.Components.Buckle if (Owner.Transform.Parent == BuckledTo.Owner.Transform) { - Owner.Transform.DetachParent(); Owner.Transform.WorldRotation = BuckledTo.Owner.Transform.WorldRotation; + Owner.Transform.DetachParent(); } BuckledTo = null; diff --git a/Content.Server/GameObjects/EntitySystems/BuckleSystem.cs b/Content.Server/GameObjects/EntitySystems/BuckleSystem.cs index 501ab2b9bb..2b9712f6d1 100644 --- a/Content.Server/GameObjects/EntitySystems/BuckleSystem.cs +++ b/Content.Server/GameObjects/EntitySystems/BuckleSystem.cs @@ -1,14 +1,46 @@ using Content.Server.GameObjects.Components.Buckle; -using Content.Server.GameObjects.Components.Mobs; using Content.Server.GameObjects.EntitySystems.Click; +using JetBrains.Annotations; using Robust.Server.GameObjects.EntitySystems; using Robust.Shared.GameObjects; +using Robust.Shared.GameObjects.Components.Transform; using Robust.Shared.GameObjects.Systems; +using Robust.Shared.Interfaces.Map; +using Robust.Shared.IoC; namespace Content.Server.GameObjects.EntitySystems { + [UsedImplicitly] public class BuckleSystem : EntitySystem { +#pragma warning disable 649 + [Dependency] private readonly IMapManager _mapManager; +#pragma warning restore 649 + + /// + /// Checks if a buckled entity should be unbuckled from moving + /// too far from its strap. + /// + /// The move event of a buckled entity. + private void MoveEvent(MoveEvent moveEvent) + { + if (!moveEvent.Sender.TryGetComponent(out BuckleComponent buckle) || + buckle.BuckledTo == null || + !buckle.BuckleOffset.HasValue) + { + return; + } + + var bucklePosition = buckle.BuckledTo.Owner.Transform.GridPosition.Offset(buckle.BuckleOffset.Value); + + if (moveEvent.NewPosition.InRange(_mapManager, bucklePosition, 0.2f)) + { + return; + } + + buckle.TryUnbuckle(buckle.Owner, true); + } + public override void Initialize() { base.Initialize(); @@ -17,6 +49,8 @@ namespace Content.Server.GameObjects.EntitySystems UpdatesAfter.Add(typeof(InteractionSystem)); UpdatesAfter.Add(typeof(InputSystem)); + + SubscribeLocalEvent(MoveEvent); } public override void Update(float frameTime) diff --git a/Content.Server/Mobs/StandingStateHelper.cs b/Content.Server/Mobs/StandingStateHelper.cs index b70c971a66..4bd0d2ae1d 100644 --- a/Content.Server/Mobs/StandingStateHelper.cs +++ b/Content.Server/Mobs/StandingStateHelper.cs @@ -17,11 +17,16 @@ namespace Content.Server.Mobs /// The mob in question /// Whether to play a sound when falling down or not /// Whether to make the mob drop all the items on his hands + /// Whether or not to check if the entity can fall. /// False if the mob was already downed or couldn't set the state - public static bool Down(IEntity entity, bool playSound = true, bool dropItems = true) + public static bool Down(IEntity entity, bool playSound = true, bool dropItems = true, bool force = false) { - if (!EffectBlockerSystem.CanFall(entity) || - !entity.TryGetComponent(out AppearanceComponent appearance)) + if (!force && !EffectBlockerSystem.CanFall(entity)) + { + return false; + } + + if (!entity.TryGetComponent(out AppearanceComponent appearance)) { return false; }