Files
OldThink/Content.Shared/Movement/Systems/SpeedModifierContactsSystem.cs

127 lines
4.2 KiB
C#
Raw Permalink Normal View History

2021-11-28 20:25:36 -06:00
using Content.Shared.Movement.Components;
using Robust.Shared.Physics.Components;
using Robust.Shared.Physics.Events;
using Robust.Shared.Physics.Systems;
2021-11-28 20:25:36 -06:00
namespace Content.Shared.Movement.Systems;
2021-11-28 20:25:36 -06:00
public sealed class SpeedModifierContactsSystem : EntitySystem
2021-11-28 20:25:36 -06:00
{
[Dependency] private readonly SharedPhysicsSystem _physics = default!;
[Dependency] private readonly MovementSpeedModifierSystem _speedModifierSystem = default!;
2023-06-12 05:04:42 +12:00
// TODO full-game-save
// Either these need to be processed before a map is saved, or slowed/slowing entities need to update on init.
2022-08-26 01:34:05 +12:00
private HashSet<EntityUid> _toUpdate = new();
private HashSet<EntityUid> _toRemove = new();
2021-11-28 20:25:36 -06:00
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<SpeedModifierContactsComponent, StartCollideEvent>(OnEntityEnter);
SubscribeLocalEvent<SpeedModifierContactsComponent, EndCollideEvent>(OnEntityExit);
SubscribeLocalEvent<SpeedModifiedByContactComponent, RefreshMovementSpeedModifiersEvent>(MovementSpeedCheck);
SubscribeLocalEvent<SpeedModifierContactsComponent, ComponentShutdown>(OnShutdown);
2022-08-26 01:34:05 +12:00
UpdatesAfter.Add(typeof(SharedPhysicsSystem));
}
public override void Update(float frameTime)
{
base.Update(frameTime);
_toRemove.Clear();
foreach (var ent in _toUpdate)
{
_speedModifierSystem.RefreshMovementSpeedModifiers(ent);
}
foreach (var ent in _toRemove)
{
RemComp<SpeedModifiedByContactComponent>(ent);
2022-08-26 01:34:05 +12:00
}
_toUpdate.Clear();
}
public void ChangeModifiers(EntityUid uid, float speed, SpeedModifierContactsComponent? component = null)
2022-08-26 01:34:05 +12:00
{
2023-06-03 22:34:43 +03:00
ChangeModifiers(uid, speed, speed, component);
2021-11-28 20:25:36 -06:00
}
public void ChangeModifiers(EntityUid uid, float walkSpeed, float sprintSpeed, SpeedModifierContactsComponent? component = null)
2021-11-28 20:25:36 -06:00
{
2023-06-03 22:34:43 +03:00
if (!Resolve(uid, ref component))
{
2021-11-28 20:25:36 -06:00
return;
2023-06-03 22:34:43 +03:00
}
component.WalkSpeedModifier = walkSpeed;
component.SprintSpeedModifier = sprintSpeed;
Dirty(uid, component);
2023-06-12 05:04:42 +12:00
_toUpdate.UnionWith(_physics.GetContactingEntities(uid));
}
private void OnShutdown(EntityUid uid, SpeedModifierContactsComponent component, ComponentShutdown args)
2023-06-12 05:04:42 +12:00
{
if (!TryComp(uid, out PhysicsComponent? phys))
return;
// Note that the entity may not be getting deleted here. E.g., glue puddles.
_toUpdate.UnionWith(_physics.GetContactingEntities(uid, phys));
2022-08-26 01:34:05 +12:00
}
private void MovementSpeedCheck(EntityUid uid, SpeedModifiedByContactComponent component, RefreshMovementSpeedModifiersEvent args)
2022-08-26 01:34:05 +12:00
{
2021-11-28 20:25:36 -06:00
if (!EntityManager.TryGetComponent<PhysicsComponent>(uid, out var physicsComponent))
return;
var walkSpeed = 0.0f;
var sprintSpeed = 0.0f;
2021-11-28 20:25:36 -06:00
2022-08-26 01:34:05 +12:00
bool remove = true;
var entries = 0;
foreach (var ent in _physics.GetContactingEntities(uid, physicsComponent))
2021-11-28 20:25:36 -06:00
{
if (!TryComp<SpeedModifierContactsComponent>(ent, out var slowContactsComponent))
2021-11-28 20:25:36 -06:00
continue;
2022-10-26 19:22:12 -04:00
if (slowContactsComponent.IgnoreWhitelist != null && slowContactsComponent.IgnoreWhitelist.IsValid(uid))
continue;
walkSpeed += slowContactsComponent.WalkSpeedModifier;
sprintSpeed += slowContactsComponent.SprintSpeedModifier;
2022-08-26 01:34:05 +12:00
remove = false;
entries++;
2021-11-28 20:25:36 -06:00
}
if (entries > 0)
{
walkSpeed /= entries;
sprintSpeed /= entries;
args.ModifySpeed(walkSpeed, sprintSpeed);
}
2022-08-26 01:34:05 +12:00
// no longer colliding with anything
if (remove)
_toRemove.Add(uid);
2021-11-28 20:25:36 -06:00
}
private void OnEntityExit(EntityUid uid, SpeedModifierContactsComponent component, ref EndCollideEvent args)
2021-11-28 20:25:36 -06:00
{
var otherUid = args.OtherEntity;
2023-06-12 05:04:42 +12:00
_toUpdate.Add(otherUid);
2021-11-28 20:25:36 -06:00
}
private void OnEntityEnter(EntityUid uid, SpeedModifierContactsComponent component, ref StartCollideEvent args)
2021-11-28 20:25:36 -06:00
{
var otherUid = args.OtherEntity;
2022-08-26 01:34:05 +12:00
if (!HasComp<MovementSpeedModifierComponent>(otherUid))
2021-11-28 20:25:36 -06:00
return;
EnsureComp<SpeedModifiedByContactComponent>(otherUid);
2022-08-26 01:34:05 +12:00
_toUpdate.Add(otherUid);
2021-11-28 20:25:36 -06:00
}
}