2021-11-28 20:25:36 -06:00
|
|
|
using Content.Shared.Movement.Components;
|
2022-09-14 17:26:26 +10:00
|
|
|
using Robust.Shared.Physics.Components;
|
|
|
|
|
using Robust.Shared.Physics.Events;
|
|
|
|
|
using Robust.Shared.Physics.Systems;
|
2021-11-28 20:25:36 -06:00
|
|
|
|
2022-06-24 17:44:30 +10:00
|
|
|
namespace Content.Shared.Movement.Systems;
|
2021-11-28 20:25:36 -06:00
|
|
|
|
2024-03-17 05:27:22 +03: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();
|
2024-03-17 05:27:22 +03:00
|
|
|
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)
|
|
|
|
|
{
|
2024-03-17 05:27:22 +03:00
|
|
|
RemComp<SpeedModifiedByContactComponent>(ent);
|
2022-08-26 01:34:05 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_toUpdate.Clear();
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 05:27:22 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2024-03-17 05:27:22 +03: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;
|
2024-03-19 23:27:02 -04:00
|
|
|
Dirty(uid, component);
|
2023-06-12 05:04:42 +12:00
|
|
|
_toUpdate.UnionWith(_physics.GetContactingEntities(uid));
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 05:27:22 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2024-03-17 05:27:22 +03: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;
|
|
|
|
|
|
2024-03-17 05:27:22 +03:00
|
|
|
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;
|
2024-03-17 05:27:22 +03:00
|
|
|
var entries = 0;
|
2023-05-09 19:21:26 +12:00
|
|
|
foreach (var ent in _physics.GetContactingEntities(uid, physicsComponent))
|
2021-11-28 20:25:36 -06:00
|
|
|
{
|
2024-03-17 05:27:22 +03: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))
|
2022-10-01 21:36:44 -04:00
|
|
|
continue;
|
|
|
|
|
|
2024-03-17 05:27:22 +03:00
|
|
|
walkSpeed += slowContactsComponent.WalkSpeedModifier;
|
|
|
|
|
sprintSpeed += slowContactsComponent.SprintSpeedModifier;
|
2022-08-26 01:34:05 +12:00
|
|
|
remove = false;
|
2024-03-17 05:27:22 +03:00
|
|
|
entries++;
|
2021-11-28 20:25:36 -06:00
|
|
|
}
|
|
|
|
|
|
2024-03-17 05:27:22 +03: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
|
|
|
}
|
|
|
|
|
|
2024-03-17 05:27:22 +03:00
|
|
|
private void OnEntityExit(EntityUid uid, SpeedModifierContactsComponent component, ref EndCollideEvent args)
|
2021-11-28 20:25:36 -06:00
|
|
|
{
|
2023-05-09 19:21:26 +12:00
|
|
|
var otherUid = args.OtherEntity;
|
2023-06-12 05:04:42 +12:00
|
|
|
_toUpdate.Add(otherUid);
|
2021-11-28 20:25:36 -06:00
|
|
|
}
|
|
|
|
|
|
2024-03-17 05:27:22 +03:00
|
|
|
private void OnEntityEnter(EntityUid uid, SpeedModifierContactsComponent component, ref StartCollideEvent args)
|
2021-11-28 20:25:36 -06:00
|
|
|
{
|
2023-05-09 19:21:26 +12: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;
|
2022-06-24 17:44:30 +10:00
|
|
|
|
2024-03-17 05:27:22 +03:00
|
|
|
EnsureComp<SpeedModifiedByContactComponent>(otherUid);
|
2022-08-26 01:34:05 +12:00
|
|
|
_toUpdate.Add(otherUid);
|
2021-11-28 20:25:36 -06:00
|
|
|
}
|
|
|
|
|
}
|