ECS strap component (#12627)

This commit is contained in:
DrSmugleaf
2022-11-18 22:08:28 +01:00
committed by GitHub
parent b749d0d275
commit 5aeb2ac4a0
15 changed files with 997 additions and 993 deletions

View File

@@ -1,102 +1,89 @@
using Content.Shared.DragDrop;
using Content.Shared.Interaction;
using Robust.Shared.GameStates;
using Robust.Shared.Serialization;
namespace Content.Shared.Buckle.Components
namespace Content.Shared.Buckle.Components;
public enum StrapPosition
{
public enum StrapPosition
/// <summary>
/// (Default) Makes no change to the buckled mob
/// </summary>
None = 0,
/// <summary>
/// Makes the mob stand up
/// </summary>
Stand,
/// <summary>
/// Makes the mob lie down
/// </summary>
Down
}
[NetworkedComponent]
[Access(typeof(SharedBuckleSystem))]
public abstract class SharedStrapComponent : Component
{
/// <summary>
/// The change in position to the strapped mob
/// </summary>
[DataField("position")]
public StrapPosition Position { get; set; } = StrapPosition.None;
/// <summary>
/// The entity that is currently buckled here
/// </summary>
public readonly HashSet<EntityUid> BuckledEntities = new();
/// <summary>
/// The distance above which a buckled entity will be automatically unbuckled.
/// Don't change it unless you really have to
/// </summary>
[DataField("maxBuckleDistance", required: false)]
public float MaxBuckleDistance = 0.1f;
/// <summary>
/// Gets and clamps the buckle offset to MaxBuckleDistance
/// </summary>
public Vector2 BuckleOffset => Vector2.Clamp(
BuckleOffsetUnclamped,
Vector2.One * -MaxBuckleDistance,
Vector2.One * MaxBuckleDistance);
/// <summary>
/// The buckled entity will be offset by this amount from the center of the strap object.
/// If this offset it too big, it will be clamped to <see cref="MaxBuckleDistance"/>
/// </summary>
[DataField("buckleOffset", required: false)]
[Access(Other = AccessPermissions.ReadWrite)]
public Vector2 BuckleOffsetUnclamped = Vector2.Zero;
}
[Serializable, NetSerializable]
public sealed class StrapComponentState : ComponentState
{
/// <summary>
/// The change in position that this strap makes to the strapped mob
/// </summary>
public StrapPosition Position;
public float MaxBuckleDistance;
public Vector2 BuckleOffsetClamped;
public HashSet<EntityUid> BuckledEntities;
public StrapComponentState(StrapPosition position, Vector2 offset, HashSet<EntityUid> buckled, float maxBuckleDistance)
{
/// <summary>
/// (Default) Makes no change to the buckled mob
/// </summary>
None = 0,
/// <summary>
/// Makes the mob stand up
/// </summary>
Stand,
/// <summary>
/// Makes the mob lie down
/// </summary>
Down
}
[NetworkedComponent()]
public abstract class SharedStrapComponent : Component, IDragDropOn
{
/// <summary>
/// The change in position to the strapped mob
/// </summary>
[DataField("position")]
public StrapPosition Position { get; set; } = StrapPosition.None;
/// <summary>
/// The entity that is currently buckled here, synced from <see cref="BuckleComponent.BuckledTo"/>
/// </summary>
public readonly HashSet<EntityUid> BuckledEntities = new();
/// <summary>
/// The distance above which a buckled entity will be automatically unbuckled.
/// Don't change it unless you really have to
/// </summary>
[DataField("maxBuckleDistance", required: false)]
public float MaxBuckleDistance = 0.1f;
/// <summary>
/// Gets and clamps the buckle offset to MaxBuckleDistance
/// </summary>
public Vector2 BuckleOffset => Vector2.Clamp(
BuckleOffsetUnclamped,
Vector2.One * -MaxBuckleDistance,
Vector2.One * MaxBuckleDistance);
/// <summary>
/// The buckled entity will be offset by this amount from the center of the strap object.
/// If this offset it too big, it will be clamped to <see cref="MaxBuckleDistance"/>
/// </summary>
[DataField("buckleOffset", required: false)]
public Vector2 BuckleOffsetUnclamped = Vector2.Zero;
bool IDragDropOn.CanDragDropOn(DragDropEvent eventArgs)
{
if (!IoCManager.Resolve<IEntityManager>().TryGetComponent(eventArgs.Dragged, out SharedBuckleComponent? buckleComponent)) return false;
bool Ignored(EntityUid entity) => entity == eventArgs.User || entity == eventArgs.Dragged || entity == eventArgs.Target;
return EntitySystem.Get<SharedInteractionSystem>().InRangeUnobstructed(eventArgs.Target, eventArgs.Dragged, buckleComponent.Range, predicate: Ignored);
}
public abstract bool DragDropOn(DragDropEvent eventArgs);
}
[Serializable, NetSerializable]
public sealed class StrapComponentState : ComponentState
{
/// <summary>
/// The change in position that this strap makes to the strapped mob
/// </summary>
public StrapPosition Position;
public float MaxBuckleDistance;
public Vector2 BuckleOffsetClamped;
public HashSet<EntityUid> BuckledEntities;
public StrapComponentState(StrapPosition position, Vector2 offset, HashSet<EntityUid> buckled, float maxBuckleDistance)
{
Position = position;
BuckleOffsetClamped = offset;
BuckledEntities = buckled;
MaxBuckleDistance = maxBuckleDistance;
}
}
[Serializable, NetSerializable]
public enum StrapVisuals : byte
{
RotationAngle,
BuckledState,
State
Position = position;
BuckleOffsetClamped = offset;
BuckledEntities = buckled;
MaxBuckleDistance = maxBuckleDistance;
}
}
[Serializable, NetSerializable]
public enum StrapVisuals : byte
{
RotationAngle,
State
}

View File

@@ -0,0 +1,107 @@
using Content.Shared.Buckle.Components;
using Content.Shared.Interaction.Events;
using Content.Shared.Movement.Events;
using Content.Shared.Standing;
using Content.Shared.Throwing;
using Content.Shared.Vehicle.Components;
using Robust.Shared.Map;
using Robust.Shared.Physics.Events;
namespace Content.Shared.Buckle;
public abstract partial class SharedBuckleSystem
{
private void InitializeBuckle()
{
SubscribeLocalEvent<SharedBuckleComponent, PreventCollideEvent>(PreventCollision);
SubscribeLocalEvent<SharedBuckleComponent, DownAttemptEvent>(HandleDown);
SubscribeLocalEvent<SharedBuckleComponent, StandAttemptEvent>(HandleStand);
SubscribeLocalEvent<SharedBuckleComponent, ThrowPushbackAttemptEvent>(HandleThrowPushback);
SubscribeLocalEvent<SharedBuckleComponent, UpdateCanMoveEvent>(HandleMove);
SubscribeLocalEvent<SharedBuckleComponent, ChangeDirectionAttemptEvent>(OnBuckleChangeDirectionAttempt);
}
private void PreventCollision(EntityUid uid, SharedBuckleComponent component, ref PreventCollideEvent args)
{
if (args.BodyB.Owner != component.LastEntityBuckledTo)
return;
if (component.Buckled || component.DontCollide)
args.Cancelled = true;
}
private void HandleDown(EntityUid uid, SharedBuckleComponent component, DownAttemptEvent args)
{
if (component.Buckled)
args.Cancel();
}
private void HandleStand(EntityUid uid, SharedBuckleComponent component, StandAttemptEvent args)
{
if (component.Buckled)
args.Cancel();
}
private void HandleThrowPushback(EntityUid uid, SharedBuckleComponent component, ThrowPushbackAttemptEvent args)
{
if (component.Buckled)
args.Cancel();
}
private void HandleMove(EntityUid uid, SharedBuckleComponent component, UpdateCanMoveEvent args)
{
if (component.LifeStage > ComponentLifeStage.Running)
return;
if (component.Buckled &&
!HasComp<VehicleComponent>(Transform(uid).ParentUid)) // buckle+vehicle shitcode
args.Cancel();
}
private void OnBuckleChangeDirectionAttempt(EntityUid uid, SharedBuckleComponent component, ChangeDirectionAttemptEvent args)
{
if (component.Buckled)
args.Cancel();
}
public bool IsBuckled(EntityUid uid, SharedBuckleComponent? component = null)
{
return Resolve(uid, ref component, false) && component.Buckled;
}
/// <summary>
/// Reattaches this entity to the strap, modifying its position and rotation.
/// </summary>
/// <param name="buckleId">The entity to reattach.</param>
/// <param name="strap">The strap to reattach to.</param>
/// <param name="buckle">The buckle component of the entity to reattach.</param>
public void ReAttach(EntityUid buckleId, SharedStrapComponent strap, SharedBuckleComponent? buckle = null)
{
if (!Resolve(buckleId, ref buckle, false))
return;
var ownTransform = Transform(buckleId);
var strapTransform = Transform(strap.Owner);
ownTransform.Coordinates = new EntityCoordinates(strapTransform.Owner, strap.BuckleOffset);
// Buckle subscribes to move for <reasons> so this might fail.
// TODO: Make buckle not do that.
if (ownTransform.ParentUid != strapTransform.Owner)
return;
ownTransform.LocalRotation = Angle.Zero;
switch (strap.Position)
{
case StrapPosition.None:
break;
case StrapPosition.Stand:
_standing.Stand(buckleId);
break;
case StrapPosition.Down:
_standing.Down(buckleId, false, false);
break;
}
}
}

View File

@@ -0,0 +1,93 @@
using Content.Shared.Buckle.Components;
using Content.Shared.DragDrop;
using Content.Shared.Interaction;
using Robust.Shared.GameStates;
namespace Content.Shared.Buckle;
public abstract partial class SharedBuckleSystem
{
[Dependency] private readonly SharedInteractionSystem _interactions = default!;
private void InitializeStrap()
{
SubscribeLocalEvent<SharedStrapComponent, MoveEvent>(OnStrapRotate);
SubscribeLocalEvent<SharedStrapComponent, ComponentHandleState>(OnStrapHandleState);
SubscribeLocalEvent<SharedStrapComponent, CanDragDropOnEvent>(OnStrapCanDragDropOn);
}
private void OnStrapHandleState(EntityUid uid, SharedStrapComponent component, ref ComponentHandleState args)
{
if (args.Current is not StrapComponentState state)
return;
component.Position = state.Position;
component.BuckleOffsetUnclamped = state.BuckleOffsetClamped;
component.BuckledEntities.Clear();
component.BuckledEntities.UnionWith(state.BuckledEntities);
component.MaxBuckleDistance = state.MaxBuckleDistance;
}
private void OnStrapRotate(EntityUid uid, SharedStrapComponent component, ref MoveEvent args)
{
// TODO: This looks dirty af.
// On rotation of a strap, reattach all buckled entities.
// This fixes buckle offsets and draw depths.
// This is mega cursed. Please somebody save me from Mr Buckle's wild ride.
// Oh god I'm back here again. Send help.
// Consider a chair that has a player strapped to it. Then the client receives a new server state, showing
// that the player entity has moved elsewhere, and the chair has rotated. If the client applies the player
// state, then the chairs transform comp state, and then the buckle state. The transform state will
// forcefully teleport the player back to the chair (client-side only). This causes even more issues if the
// chair was teleporting in from nullspace after having left PVS.
//
// One option is to just never trigger re-buckles during state application.
// another is to.. just not do this? Like wtf is this code. But I CBF with buckle atm.
if (GameTiming.ApplyingState || args.NewRotation == args.OldRotation)
return;
foreach (var buckledEntity in component.BuckledEntities)
{
if (!EntityManager.TryGetComponent(buckledEntity, out SharedBuckleComponent? buckled))
{
continue;
}
if (!buckled.Buckled || buckled.LastEntityBuckledTo != uid)
{
Logger.Error($"A moving strap entity {ToPrettyString(uid)} attempted to re-parent an entity that does not 'belong' to it {ToPrettyString(buckledEntity)}");
continue;
}
ReAttach(buckledEntity, component, buckle: buckled);
Dirty(buckled);
}
}
protected bool StrapCanDragDropOn(
EntityUid strapId,
EntityUid user,
EntityUid target,
EntityUid buckleId,
SharedStrapComponent? strap = null,
SharedBuckleComponent? buckle = null)
{
if (!Resolve(strapId, ref strap, false) ||
!Resolve(buckleId, ref buckle, false))
{
return false;
}
bool Ignored(EntityUid entity) => entity == user || entity == buckleId || entity == target;
return _interactions.InRangeUnobstructed(target, buckleId, buckle.Range, predicate: Ignored);
}
private void OnStrapCanDragDropOn(EntityUid uid, SharedStrapComponent strap, CanDragDropOnEvent args)
{
args.CanDrop = StrapCanDragDropOn(args.Target, args.User, args.Target, args.Dragged, strap);
args.Handled = true;
}
}

View File

@@ -1,16 +1,9 @@
using Content.Shared.Buckle.Components;
using Content.Shared.Interaction.Events;
using Content.Shared.Movement.Events;
using Content.Shared.Standing;
using Content.Shared.Throwing;
using Content.Shared.Vehicle.Components;
using Robust.Shared.Map;
using Robust.Shared.Physics.Events;
using Robust.Shared.Timing;
namespace Content.Shared.Buckle;
public abstract class SharedBuckleSystem : EntitySystem
public abstract partial class SharedBuckleSystem : EntitySystem
{
[Dependency] protected readonly IGameTiming GameTiming = default!;
@@ -20,141 +13,7 @@ public abstract class SharedBuckleSystem : EntitySystem
{
base.Initialize();
SubscribeLocalEvent<SharedStrapComponent, MoveEvent>(OnStrapRotate);
SubscribeLocalEvent<SharedBuckleComponent, PreventCollideEvent>(PreventCollision);
SubscribeLocalEvent<SharedBuckleComponent, DownAttemptEvent>(HandleDown);
SubscribeLocalEvent<SharedBuckleComponent, StandAttemptEvent>(HandleStand);
SubscribeLocalEvent<SharedBuckleComponent, ThrowPushbackAttemptEvent>(HandleThrowPushback);
SubscribeLocalEvent<SharedBuckleComponent, UpdateCanMoveEvent>(HandleMove);
SubscribeLocalEvent<SharedBuckleComponent, ChangeDirectionAttemptEvent>(OnBuckleChangeDirectionAttempt);
}
private void OnStrapRotate(EntityUid uid, SharedStrapComponent component, ref MoveEvent args)
{
// TODO: This looks dirty af.
// On rotation of a strap, reattach all buckled entities.
// This fixes buckle offsets and draw depths.
// This is mega cursed. Please somebody save me from Mr Buckle's wild ride.
// Oh god I'm back here again. Send help.
// Consider a chair that has a player strapped to it. Then the client receives a new server state, showing
// that the player entity has moved elsewhere, and the chair has rotated. If the client applies the player
// state, then the chairs transform comp state, and then the buckle state. The transform state will
// forcefully teleport the player back to the chair (client-side only). This causes even more issues if the
// chair was teleporting in from nullspace after having left PVS.
//
// One option is to just never trigger re-buckles during state application.
// another is to.. just not do this? Like wtf is this code. But I CBF with buckle atm.
if (GameTiming.ApplyingState || args.NewRotation == args.OldRotation)
return;
foreach (var buckledEntity in component.BuckledEntities)
{
if (!EntityManager.TryGetComponent(buckledEntity, out SharedBuckleComponent? buckled))
{
continue;
}
if (!buckled.Buckled || buckled.LastEntityBuckledTo != uid)
{
Logger.Error($"A moving strap entity {ToPrettyString(uid)} attempted to re-parent an entity that does not 'belong' to it {ToPrettyString(buckledEntity)}");
continue;
}
ReAttach(buckledEntity, component, buckle: buckled);
Dirty(buckled);
}
}
public bool IsBuckled(EntityUid uid, SharedBuckleComponent? component = null)
{
return Resolve(uid, ref component, false) && component.Buckled;
}
private void OnBuckleChangeDirectionAttempt(EntityUid uid, SharedBuckleComponent component, ChangeDirectionAttemptEvent args)
{
if (component.Buckled)
args.Cancel();
}
private void HandleMove(EntityUid uid, SharedBuckleComponent component, UpdateCanMoveEvent args)
{
if (component.LifeStage > ComponentLifeStage.Running)
return;
if (component.Buckled &&
!HasComp<VehicleComponent>(Transform(uid).ParentUid)) // buckle+vehicle shitcode
args.Cancel();
}
private void HandleStand(EntityUid uid, SharedBuckleComponent component, StandAttemptEvent args)
{
if (component.Buckled)
{
args.Cancel();
}
}
private void HandleDown(EntityUid uid, SharedBuckleComponent component, DownAttemptEvent args)
{
if (component.Buckled)
{
args.Cancel();
}
}
private void HandleThrowPushback(EntityUid uid, SharedBuckleComponent component, ThrowPushbackAttemptEvent args)
{
if (!component.Buckled) return;
args.Cancel();
}
private void PreventCollision(EntityUid uid, SharedBuckleComponent component, ref PreventCollideEvent args)
{
if (args.BodyB.Owner != component.LastEntityBuckledTo)
return;
if (component.Buckled || component.DontCollide)
{
args.Cancelled = true;
}
}
/// <summary>
/// Reattaches this entity to the strap, modifying its position and rotation.
/// </summary>
/// <param name="buckleId">The entity to reattach.</param>
/// <param name="strap">The strap to reattach to.</param>
/// <param name="buckle">The buckle component of the entity to reattach.</param>
public void ReAttach(EntityUid buckleId, SharedStrapComponent strap, SharedBuckleComponent? buckle = null)
{
if (!Resolve(buckleId, ref buckle, false))
return;
var ownTransform = Transform(buckleId);
var strapTransform = Transform(strap.Owner);
ownTransform.Coordinates = new EntityCoordinates(strapTransform.Owner, strap.BuckleOffset);
// Buckle subscribes to move for <reasons> so this might fail.
// TODO: Make buckle not do that.
if (ownTransform.ParentUid != strapTransform.Owner)
return;
ownTransform.LocalRotation = Angle.Zero;
switch (strap.Position)
{
case StrapPosition.None:
break;
case StrapPosition.Stand:
_standing.Stand(buckleId);
break;
case StrapPosition.Down:
_standing.Down(buckleId, false, false);
break;
}
InitializeBuckle();
InitializeStrap();
}
}