2021-03-09 11:22:48 -08:00
|
|
|
#nullable enable
|
2020-11-06 15:15:02 +01:00
|
|
|
using Content.Server.GameObjects.Components.Buckle;
|
|
|
|
|
using Content.Server.GameObjects.Components.Strap;
|
2020-07-08 15:30:48 +02:00
|
|
|
using Content.Server.GameObjects.EntitySystems.Click;
|
2021-03-09 11:22:48 -08:00
|
|
|
using Content.Shared.Interfaces.GameObjects.Components;
|
2020-07-24 14:22:28 +02:00
|
|
|
using JetBrains.Annotations;
|
2021-02-11 01:13:03 -08:00
|
|
|
using Robust.Server.GameObjects;
|
2020-11-06 15:15:02 +01:00
|
|
|
using Robust.Shared.Containers;
|
2021-02-11 01:13:03 -08:00
|
|
|
using Robust.Shared.GameObjects;
|
2020-07-08 15:30:48 +02:00
|
|
|
|
|
|
|
|
namespace Content.Server.GameObjects.EntitySystems
|
|
|
|
|
{
|
2020-07-24 14:22:28 +02:00
|
|
|
[UsedImplicitly]
|
2020-08-13 22:17:12 +10:00
|
|
|
internal sealed class BuckleSystem : EntitySystem
|
2020-07-08 15:30:48 +02:00
|
|
|
{
|
|
|
|
|
public override void Initialize()
|
|
|
|
|
{
|
|
|
|
|
base.Initialize();
|
|
|
|
|
|
|
|
|
|
UpdatesAfter.Add(typeof(InteractionSystem));
|
|
|
|
|
UpdatesAfter.Add(typeof(InputSystem));
|
2020-11-06 15:15:02 +01:00
|
|
|
|
2021-05-27 11:51:14 +01:00
|
|
|
SubscribeLocalEvent<BuckleComponent, MoveEvent>(MoveEvent);
|
2021-03-09 11:22:48 -08:00
|
|
|
|
2021-05-27 11:51:14 +01:00
|
|
|
SubscribeLocalEvent<StrapComponent, RotateEvent>(RotateEvent);
|
2021-03-09 11:22:48 -08:00
|
|
|
|
2021-05-27 11:51:14 +01:00
|
|
|
SubscribeLocalEvent<BuckleComponent, EntInsertedIntoContainerMessage>(ContainerModifiedBuckle);
|
|
|
|
|
SubscribeLocalEvent<StrapComponent, EntInsertedIntoContainerMessage>(ContainerModifiedStrap);
|
2021-04-09 16:08:12 +02:00
|
|
|
|
2021-05-27 11:51:14 +01:00
|
|
|
SubscribeLocalEvent<BuckleComponent, EntRemovedFromContainerMessage>(ContainerModifiedBuckle);
|
|
|
|
|
SubscribeLocalEvent<StrapComponent, EntRemovedFromContainerMessage>(ContainerModifiedStrap);
|
2021-04-09 16:08:12 +02:00
|
|
|
|
2021-05-27 11:51:14 +01:00
|
|
|
SubscribeLocalEvent<BuckleComponent, AttackHandEvent>(HandleAttackHand);
|
2021-04-09 16:08:12 +02:00
|
|
|
}
|
|
|
|
|
|
2021-05-22 21:06:40 -07:00
|
|
|
private void HandleAttackHand(EntityUid uid, BuckleComponent component, AttackHandEvent args)
|
2021-03-09 11:22:48 -08:00
|
|
|
{
|
|
|
|
|
args.Handled = component.TryUnbuckle(args.User);
|
2020-11-06 12:38:24 +01:00
|
|
|
}
|
|
|
|
|
|
2020-12-18 20:12:53 +01:00
|
|
|
public override void Update(float frameTime)
|
|
|
|
|
{
|
2021-03-09 11:22:48 -08:00
|
|
|
foreach (var comp in ComponentManager.EntityQuery<BuckleComponent>())
|
2020-12-18 20:12:53 +01:00
|
|
|
{
|
|
|
|
|
comp.Update();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-27 11:51:14 +01:00
|
|
|
private void MoveEvent(EntityUid uid, BuckleComponent buckle, MoveEvent ev)
|
2020-11-06 12:38:24 +01:00
|
|
|
{
|
2020-11-06 15:15:02 +01:00
|
|
|
var strap = buckle.BuckledTo;
|
|
|
|
|
|
|
|
|
|
if (strap == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var strapPosition = strap.Owner.Transform.Coordinates.Offset(buckle.BuckleOffset);
|
|
|
|
|
|
|
|
|
|
if (ev.NewPosition.InRange(EntityManager, strapPosition, 0.2f))
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
buckle.TryUnbuckle(buckle.Owner, true);
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-27 11:51:14 +01:00
|
|
|
private void RotateEvent(EntityUid uid, StrapComponent strap, RotateEvent ev)
|
2020-11-06 15:15:02 +01:00
|
|
|
{
|
2021-05-27 11:51:14 +01:00
|
|
|
// On rotation of a strap, reattach all buckled entities.
|
|
|
|
|
// This fixes buckle offsets and draw depths.
|
|
|
|
|
foreach (var buckledEntity in strap.BuckledEntities)
|
2020-11-06 12:38:24 +01:00
|
|
|
{
|
2021-05-27 11:51:14 +01:00
|
|
|
if (!buckledEntity.TryGetComponent(out BuckleComponent? buckled))
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
buckled.ReAttach(strap);
|
|
|
|
|
buckled.Dirty();
|
2020-11-06 15:15:02 +01:00
|
|
|
}
|
2021-05-27 11:51:14 +01:00
|
|
|
}
|
2020-11-06 15:15:02 +01:00
|
|
|
|
2021-05-27 11:51:14 +01:00
|
|
|
private void ContainerModifiedBuckle(EntityUid uid, BuckleComponent buckle, ContainerModifiedMessage message)
|
|
|
|
|
{
|
|
|
|
|
ContainerModifiedReAttach(buckle, buckle.BuckledTo);
|
|
|
|
|
}
|
|
|
|
|
private void ContainerModifiedStrap(EntityUid uid, StrapComponent strap, ContainerModifiedMessage message)
|
|
|
|
|
{
|
|
|
|
|
foreach (var buckledEntity in strap.BuckledEntities)
|
2020-11-06 15:15:02 +01:00
|
|
|
{
|
2021-05-27 11:51:14 +01:00
|
|
|
if (!buckledEntity.TryGetComponent(out BuckleComponent? buckled))
|
2020-11-06 15:15:02 +01:00
|
|
|
{
|
2021-05-27 11:51:14 +01:00
|
|
|
continue;
|
2020-11-06 15:15:02 +01:00
|
|
|
}
|
2021-05-27 11:51:14 +01:00
|
|
|
|
|
|
|
|
ContainerModifiedReAttach(buckled, strap);
|
2020-11-06 12:38:24 +01:00
|
|
|
}
|
2020-07-08 15:30:48 +02:00
|
|
|
}
|
|
|
|
|
|
2020-11-06 15:15:02 +01:00
|
|
|
private void ContainerModifiedReAttach(BuckleComponent buckle, StrapComponent? strap)
|
2020-07-08 15:30:48 +02:00
|
|
|
{
|
2020-11-06 15:15:02 +01:00
|
|
|
if (strap == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-13 20:25:04 +13:00
|
|
|
var contained = buckle.Owner.TryGetContainer(out var ownContainer);
|
|
|
|
|
var strapContained = strap.Owner.TryGetContainer(out var strapContainer);
|
2020-11-06 15:15:02 +01:00
|
|
|
|
|
|
|
|
if (contained != strapContained || ownContainer != strapContainer)
|
|
|
|
|
{
|
|
|
|
|
buckle.TryUnbuckle(buckle.Owner, true);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!contained)
|
2020-07-08 15:30:48 +02:00
|
|
|
{
|
2020-11-06 15:15:02 +01:00
|
|
|
buckle.ReAttach(strap);
|
2020-07-08 15:30:48 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|