Fix not staying buckled to a strap when both the buckled and the strap go into the same container

This commit is contained in:
DrSmugleaf
2020-07-08 15:30:48 +02:00
parent 8f9af88bb2
commit f5a9f91de4
2 changed files with 90 additions and 23 deletions

View File

@@ -72,6 +72,8 @@ namespace Content.Server.GameObjects.Components.Mobs
[ViewVariables] [ViewVariables]
protected override bool Buckled => BuckledTo != null; protected override bool Buckled => BuckledTo != null;
public bool ContainerChanged { get; private set; }
[ViewVariables] [ViewVariables]
public int Size => _size; public int Size => _size;
@@ -86,6 +88,30 @@ namespace Content.Server.GameObjects.Components.Mobs
} }
} }
private void ReAttach(StrapComponent strap)
{
var ownTransform = Owner.Transform;
var strapTransform = strap.Owner.Transform;
ownTransform.GridPosition = strapTransform.GridPosition;
ownTransform.AttachParent(strapTransform);
switch (strap.Position)
{
case StrapPosition.None:
ownTransform.WorldRotation = strapTransform.WorldRotation;
break;
case StrapPosition.Stand:
StandingStateHelper.Standing(Owner);
ownTransform.WorldRotation = strapTransform.WorldRotation;
break;
case StrapPosition.Down:
StandingStateHelper.Down(Owner);
ownTransform.WorldRotation = Angle.South;
break;
}
}
private bool TryBuckle(IEntity user, IEntity to) private bool TryBuckle(IEntity user, IEntity to)
{ {
if (user == null || user == to) if (user == null || user == to)
@@ -176,26 +202,7 @@ namespace Content.Server.GameObjects.Components.Mobs
appearance.SetData(BuckleVisuals.Buckled, true); appearance.SetData(BuckleVisuals.Buckled, true);
} }
var ownTransform = Owner.Transform; ReAttach(strap);
var strapTransform = strap.Owner.Transform;
ownTransform.GridPosition = strapTransform.GridPosition;
ownTransform.AttachParent(strapTransform);
switch (strap.Position)
{
case StrapPosition.None:
ownTransform.WorldRotation = strapTransform.WorldRotation;
break;
case StrapPosition.Stand:
StandingStateHelper.Standing(Owner);
ownTransform.WorldRotation = strapTransform.WorldRotation;
break;
case StrapPosition.Down:
StandingStateHelper.Down(Owner);
ownTransform.WorldRotation = Angle.South;
break;
}
BuckledTo = strap; BuckledTo = strap;
BuckleStatus(); BuckleStatus();
@@ -283,14 +290,38 @@ namespace Content.Server.GameObjects.Components.Mobs
return TryBuckle(user, to); return TryBuckle(user, to);
} }
private void InsertIntoContainer(EntInsertedIntoContainerMessage message) private void InsertIntoContainer(ContainerModifiedMessage message)
{ {
if (message.Entity != Owner) if (message.Entity != Owner)
{ {
return; return;
} }
ContainerChanged = true;
}
public void Update()
{
if (!ContainerChanged || BuckledTo == null)
{
return;
}
var contained = ContainerHelpers.TryGetContainer(Owner, out var ownContainer);
var strapContained = ContainerHelpers.TryGetContainer(BuckledTo.Owner, out var strapContainer);
if (contained != strapContained || ownContainer != strapContainer)
{
TryUnbuckle(Owner, true); TryUnbuckle(Owner, true);
return;
}
if (!contained && !strapContained)
{
ReAttach(BuckledTo);
}
ContainerChanged = false;
} }
public override void ExposeData(ObjectSerializer serializer) public override void ExposeData(ObjectSerializer serializer)
@@ -311,6 +342,7 @@ namespace Content.Server.GameObjects.Components.Mobs
base.Initialize(); base.Initialize();
_entityManager.EventBus.SubscribeEvent<EntInsertedIntoContainerMessage>(EventSource.Local, this, InsertIntoContainer); _entityManager.EventBus.SubscribeEvent<EntInsertedIntoContainerMessage>(EventSource.Local, this, InsertIntoContainer);
_entityManager.EventBus.SubscribeEvent<EntRemovedFromContainerMessage>(EventSource.Local, this, InsertIntoContainer);
} }
protected override void Startup() protected override void Startup()
@@ -331,7 +363,8 @@ namespace Content.Server.GameObjects.Components.Mobs
strap.Remove(this); strap.Remove(this);
} }
BuckledTo = null; TryUnbuckle(Owner, true);
_buckleTime = default; _buckleTime = default;
BuckleStatus(); BuckleStatus();
} }

View File

@@ -0,0 +1,34 @@
using Content.Server.GameObjects.Components.Mobs;
using Content.Server.GameObjects.EntitySystems.Click;
using Robust.Server.GameObjects.EntitySystems;
using Robust.Shared.GameObjects;
using Robust.Shared.GameObjects.Systems;
namespace Content.Server.GameObjects.EntitySystems
{
public class BuckleSystem : EntitySystem
{
public override void Initialize()
{
base.Initialize();
EntityQuery = new TypeEntityQuery(typeof(BuckleComponent));
UpdatesAfter.Add(typeof(InteractionSystem));
UpdatesAfter.Add(typeof(InputSystem));
}
public override void Update(float frameTime)
{
foreach (var entity in RelevantEntities)
{
if (!entity.TryGetComponent(out BuckleComponent buckle))
{
continue;
}
buckle.Update();
}
}
}
}