Files
tbd-station-14/Content.Server/GameObjects/EntitySystems/BuckleSystem.cs
DrSmugleaf 5e2109a2d6 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
2020-07-24 14:22:28 +02:00

70 lines
2.1 KiB
C#

using Content.Server.GameObjects.Components.Buckle;
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
/// <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()
{
base.Initialize();
EntityQuery = new TypeEntityQuery(typeof(BuckleComponent));
UpdatesAfter.Add(typeof(InteractionSystem));
UpdatesAfter.Add(typeof(InputSystem));
SubscribeLocalEvent<MoveEvent>(MoveEvent);
}
public override void Update(float frameTime)
{
foreach (var entity in RelevantEntities)
{
if (!entity.TryGetComponent(out BuckleComponent buckle))
{
continue;
}
buckle.Update();
}
}
}
}