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
This commit is contained in:
DrSmugleaf
2020-07-24 14:22:28 +02:00
committed by GitHub
parent be3db34d1c
commit 5e2109a2d6
3 changed files with 59 additions and 10 deletions

View File

@@ -2,6 +2,7 @@
using System; using System;
using Content.Server.GameObjects.Components.Mobs; using Content.Server.GameObjects.Components.Mobs;
using Content.Server.GameObjects.Components.Strap; using Content.Server.GameObjects.Components.Strap;
using Content.Server.GameObjects.EntitySystems;
using Content.Server.Interfaces; using Content.Server.Interfaces;
using Content.Server.Mobs; using Content.Server.Mobs;
using Content.Server.Utility; using Content.Server.Utility;
@@ -21,6 +22,7 @@ using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Interfaces.Timing; using Robust.Shared.Interfaces.Timing;
using Robust.Shared.IoC; using Robust.Shared.IoC;
using Robust.Shared.Localization; using Robust.Shared.Localization;
using Robust.Shared.Map;
using Robust.Shared.Maths; using Robust.Shared.Maths;
using Robust.Shared.Serialization; using Robust.Shared.Serialization;
using Robust.Shared.ViewVariables; using Robust.Shared.ViewVariables;
@@ -58,6 +60,8 @@ namespace Content.Server.GameObjects.Components.Buckle
[ViewVariables] [ViewVariables]
private TimeSpan _buckleTime; private TimeSpan _buckleTime;
public Vector2? BuckleOffset { get; private set; }
private StrapComponent? _buckledTo; private StrapComponent? _buckledTo;
/// <summary> /// <summary>
@@ -116,7 +120,6 @@ namespace Content.Server.GameObjects.Components.Buckle
var ownTransform = Owner.Transform; var ownTransform = Owner.Transform;
var strapTransform = strap.Owner.Transform; var strapTransform = strap.Owner.Transform;
ownTransform.GridPosition = strapTransform.GridPosition;
ownTransform.AttachParent(strapTransform); ownTransform.AttachParent(strapTransform);
switch (strap.Position) switch (strap.Position)
@@ -129,14 +132,21 @@ namespace Content.Server.GameObjects.Components.Buckle
ownTransform.WorldRotation = strapTransform.WorldRotation; ownTransform.WorldRotation = strapTransform.WorldRotation;
break; break;
case StrapPosition.Down: case StrapPosition.Down:
StandingStateHelper.Down(Owner); StandingStateHelper.Down(Owner, force: true);
ownTransform.WorldRotation = Angle.South; ownTransform.WorldRotation = Angle.South;
break; break;
} }
// Assign BuckleOffset first, before causing a MoveEvent to fire
if (strapTransform.WorldRotation.GetCardinalDir() == Direction.North) 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); appearance.SetData(BuckleVisuals.Buckled, true);
} }
ReAttach(strap);
BuckledTo = strap; BuckledTo = strap;
ReAttach(strap);
BuckleStatus(); BuckleStatus();
return true; return true;
@@ -315,8 +325,8 @@ namespace Content.Server.GameObjects.Components.Buckle
if (Owner.Transform.Parent == BuckledTo.Owner.Transform) if (Owner.Transform.Parent == BuckledTo.Owner.Transform)
{ {
Owner.Transform.DetachParent();
Owner.Transform.WorldRotation = BuckledTo.Owner.Transform.WorldRotation; Owner.Transform.WorldRotation = BuckledTo.Owner.Transform.WorldRotation;
Owner.Transform.DetachParent();
} }
BuckledTo = null; BuckledTo = null;

View File

@@ -1,14 +1,46 @@
using Content.Server.GameObjects.Components.Buckle; using Content.Server.GameObjects.Components.Buckle;
using Content.Server.GameObjects.Components.Mobs;
using Content.Server.GameObjects.EntitySystems.Click; using Content.Server.GameObjects.EntitySystems.Click;
using JetBrains.Annotations;
using Robust.Server.GameObjects.EntitySystems; using Robust.Server.GameObjects.EntitySystems;
using Robust.Shared.GameObjects; using Robust.Shared.GameObjects;
using Robust.Shared.GameObjects.Components.Transform;
using Robust.Shared.GameObjects.Systems; using Robust.Shared.GameObjects.Systems;
using Robust.Shared.Interfaces.Map;
using Robust.Shared.IoC;
namespace Content.Server.GameObjects.EntitySystems namespace Content.Server.GameObjects.EntitySystems
{ {
[UsedImplicitly]
public class BuckleSystem : EntitySystem public class BuckleSystem : EntitySystem
{ {
#pragma warning disable 649
[Dependency] private readonly IMapManager _mapManager;
#pragma warning restore 649
/// <summary>
/// Checks if a buckled entity should be unbuckled from moving
/// too far from its strap.
/// </summary>
/// <param name="moveEvent">The move event of a buckled entity.</param>
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() public override void Initialize()
{ {
base.Initialize(); base.Initialize();
@@ -17,6 +49,8 @@ namespace Content.Server.GameObjects.EntitySystems
UpdatesAfter.Add(typeof(InteractionSystem)); UpdatesAfter.Add(typeof(InteractionSystem));
UpdatesAfter.Add(typeof(InputSystem)); UpdatesAfter.Add(typeof(InputSystem));
SubscribeLocalEvent<MoveEvent>(MoveEvent);
} }
public override void Update(float frameTime) public override void Update(float frameTime)

View File

@@ -17,11 +17,16 @@ namespace Content.Server.Mobs
/// <param name="entity">The mob in question</param> /// <param name="entity">The mob in question</param>
/// <param name="playSound">Whether to play a sound when falling down or not</param> /// <param name="playSound">Whether to play a sound when falling down or not</param>
/// <param name="dropItems">Whether to make the mob drop all the items on his hands</param> /// <param name="dropItems">Whether to make the mob drop all the items on his hands</param>
/// <param name="force">Whether or not to check if the entity can fall.</param>
/// <returns>False if the mob was already downed or couldn't set the state</returns> /// <returns>False if the mob was already downed or couldn't set the state</returns>
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) || if (!force && !EffectBlockerSystem.CanFall(entity))
!entity.TryGetComponent(out AppearanceComponent appearance)) {
return false;
}
if (!entity.TryGetComponent(out AppearanceComponent appearance))
{ {
return false; return false;
} }