Update trivial components to use auto comp states (#20539)

This commit is contained in:
DrSmugleaf
2023-09-28 16:20:29 -07:00
committed by GitHub
parent 14cfe44ece
commit a44fa86b68
158 changed files with 806 additions and 2866 deletions

View File

@@ -1,58 +1,56 @@
using Content.Shared.StepTrigger.Systems;
using Content.Shared.Whitelist;
using Robust.Shared.GameStates;
using Robust.Shared.Serialization;
namespace Content.Shared.StepTrigger.Components;
[RegisterComponent]
[NetworkedComponent]
[RegisterComponent, NetworkedComponent, AutoGenerateComponentState(true)]
[Access(typeof(StepTriggerSystem))]
public sealed partial class StepTriggerComponent : Component
{
/// <summary>
/// List of entities that are currently colliding with the entity.
/// </summary>
[ViewVariables]
public readonly HashSet<EntityUid> Colliding = new();
[ViewVariables, AutoNetworkedField(true)]
public HashSet<EntityUid> Colliding = new();
/// <summary>
/// The list of entities that are standing on this entity,
/// which shouldn't be able to trigger it again until stepping off.
/// </summary>
[ViewVariables]
public readonly HashSet<EntityUid> CurrentlySteppedOn = new();
[ViewVariables, AutoNetworkedField(true)]
public HashSet<EntityUid> CurrentlySteppedOn = new();
/// <summary>
/// Whether or not this component will currently try to trigger for entities.
/// </summary>
[DataField("active")]
[DataField, AutoNetworkedField]
public bool Active = true;
/// <summary>
/// Ratio of shape intersection for a trigger to occur.
/// </summary>
[DataField("intersectRatio")]
[DataField, AutoNetworkedField]
public float IntersectRatio = 0.3f;
/// <summary>
/// Entities will only be triggered if their speed exceeds this limit.
/// </summary>
[DataField("requiredTriggeredSpeed")]
public float RequiredTriggerSpeed = 3.5f;
[DataField, AutoNetworkedField]
public float RequiredTriggeredSpeed = 3.5f;
/// <summary>
/// If any entities occupy the blacklist on the same tile then steptrigger won't work.
/// </summary>
[DataField("blacklist")]
[DataField]
public EntityWhitelist? Blacklist;
/// <summary>
/// If this is true, steptrigger will still occur on entities that are in air / weightless. They do not
/// by default.
/// </summary>
[DataField("ignoreWeightless")]
public bool IgnoreWeightless = false;
[DataField]
public bool IgnoreWeightless;
}
[RegisterComponent]
@@ -61,23 +59,3 @@ public sealed partial class StepTriggerActiveComponent : Component
{
}
[Serializable, NetSerializable]
public sealed class StepTriggerComponentState : ComponentState
{
public float IntersectRatio { get; }
public float RequiredTriggerSpeed { get; }
public readonly HashSet<NetEntity> CurrentlySteppedOn;
public readonly HashSet<NetEntity> Colliding;
public readonly bool Active;
public StepTriggerComponentState(float intersectRatio, HashSet<NetEntity> currentlySteppedOn, HashSet<NetEntity> colliding, float requiredTriggerSpeed, bool active)
{
IntersectRatio = intersectRatio;
CurrentlySteppedOn = currentlySteppedOn;
RequiredTriggerSpeed = requiredTriggerSpeed;
Active = active;
Colliding = colliding;
}
}

View File

@@ -1,6 +1,5 @@
using Content.Shared.Gravity;
using Content.Shared.StepTrigger.Components;
using Robust.Shared.GameStates;
using Robust.Shared.Map.Components;
using Robust.Shared.Physics;
using Robust.Shared.Physics.Components;
@@ -16,8 +15,7 @@ public sealed class StepTriggerSystem : EntitySystem
public override void Initialize()
{
UpdatesOutsidePrediction = true;
SubscribeLocalEvent<StepTriggerComponent, ComponentGetState>(TriggerGetState);
SubscribeLocalEvent<StepTriggerComponent, ComponentHandleState>(TriggerHandleState);
SubscribeLocalEvent<StepTriggerComponent, AfterAutoHandleStateEvent>(TriggerHandleState);
SubscribeLocalEvent<StepTriggerComponent, StartCollideEvent>(OnStartCollide);
SubscribeLocalEvent<StepTriggerComponent, EndCollideEvent>(OnEndCollide);
@@ -103,7 +101,7 @@ public sealed class StepTriggerSystem : EntitySystem
// this is hard to explain
var intersect = Box2.Area(otherAabb.Intersect(ourAabb));
var ratio = Math.Max(intersect / Box2.Area(otherAabb), intersect / Box2.Area(ourAabb));
if (otherPhysics.LinearVelocity.Length() < component.RequiredTriggerSpeed
if (otherPhysics.LinearVelocity.Length() < component.RequiredTriggeredSpeed
|| component.CurrentlySteppedOn.Contains(otherUid)
|| ratio < component.IntersectRatio
|| !CanTrigger(uid, otherUid, component))
@@ -171,24 +169,8 @@ public sealed class StepTriggerSystem : EntitySystem
}
}
private void TriggerHandleState(EntityUid uid, StepTriggerComponent component, ref ComponentHandleState args)
private void TriggerHandleState(EntityUid uid, StepTriggerComponent component, ref AfterAutoHandleStateEvent args)
{
if (args.Current is not StepTriggerComponentState state)
return;
component.RequiredTriggerSpeed = state.RequiredTriggerSpeed;
component.IntersectRatio = state.IntersectRatio;
component.Active = state.Active;
var stepped = EnsureEntitySet<StepTriggerComponent>(state.CurrentlySteppedOn, uid);
var colliding = EnsureEntitySet<StepTriggerComponent>(state.CurrentlySteppedOn, uid);
component.CurrentlySteppedOn.Clear();
component.CurrentlySteppedOn.UnionWith(stepped);
component.Colliding.Clear();
component.Colliding.UnionWith(colliding);
if (component.Colliding.Count > 0)
{
EnsureComp<StepTriggerActiveComponent>(uid);
@@ -199,16 +181,6 @@ public sealed class StepTriggerSystem : EntitySystem
}
}
private void TriggerGetState(EntityUid uid, StepTriggerComponent component, ref ComponentGetState args)
{
args.State = new StepTriggerComponentState(
component.IntersectRatio,
GetNetEntitySet(component.CurrentlySteppedOn),
GetNetEntitySet(component.Colliding),
component.RequiredTriggerSpeed,
component.Active);
}
public void SetIntersectRatio(EntityUid uid, float ratio, StepTriggerComponent? component = null)
{
if (!Resolve(uid, ref component))
@@ -226,10 +198,10 @@ public sealed class StepTriggerSystem : EntitySystem
if (!Resolve(uid, ref component))
return;
if (MathHelper.CloseToPercent(component.RequiredTriggerSpeed, speed))
if (MathHelper.CloseToPercent(component.RequiredTriggeredSpeed, speed))
return;
component.RequiredTriggerSpeed = speed;
component.RequiredTriggeredSpeed = speed;
Dirty(uid, component);
}