Files
wwdpublic/Content.Shared/Movement/Systems/SpeedModifierContactsSystem.cs
SimpleStation14 3b2eb1348e Mirror: SlowContactsSystem to SpeedModifierContactsSystem mini rework (#235)
## Mirror of PR #26110: [SlowContactsSystem to
SpeedModifierContactsSystem mini
rework](https://github.com/space-wizards/space-station-14/pull/26110)
from <img src="https://avatars.githubusercontent.com/u/10567778?v=4"
alt="space-wizards" width="22"/>
[space-wizards](https://github.com/space-wizards)/[space-station-14](https://github.com/space-wizards/space-station-14)

###### `c35ff87e146f0cda7c10782f4ab80b784b51e5fe`

PR opened by <img
src="https://avatars.githubusercontent.com/u/96445749?v=4"
width="16"/><a href="https://github.com/TheShuEd"> TheShuEd</a> at
2024-03-14 09:45:33 UTC

---

PR changed 9 files with 42 additions and 34 deletions.

The PR had the following labels:
- Status: Needs Review


---

<details open="true"><summary><h1>Original Body</h1></summary>

> ## About the PR
> system and components are renamed. It can now be used including to
accelerate entities
> 
> ## Why / Balance
> more reusability for the system
> 
> ## Technical details
> Now the system calculates the arithmetic mean of all the entities that
affects your speed.


</details>

Co-authored-by: SimpleStation14 <Unknown>
2024-05-12 00:33:33 -04:00

127 lines
4.2 KiB
C#

using Content.Shared.Movement.Components;
using Robust.Shared.Physics.Components;
using Robust.Shared.Physics.Events;
using Robust.Shared.Physics.Systems;
namespace Content.Shared.Movement.Systems;
public sealed class SpeedModifierContactsSystem : EntitySystem
{
[Dependency] private readonly SharedPhysicsSystem _physics = default!;
[Dependency] private readonly MovementSpeedModifierSystem _speedModifierSystem = default!;
// TODO full-game-save
// Either these need to be processed before a map is saved, or slowed/slowing entities need to update on init.
private HashSet<EntityUid> _toUpdate = new();
private HashSet<EntityUid> _toRemove = new();
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<SpeedModifierContactsComponent, StartCollideEvent>(OnEntityEnter);
SubscribeLocalEvent<SpeedModifierContactsComponent, EndCollideEvent>(OnEntityExit);
SubscribeLocalEvent<SpeedModifiedByContactComponent, RefreshMovementSpeedModifiersEvent>(MovementSpeedCheck);
SubscribeLocalEvent<SpeedModifierContactsComponent, ComponentShutdown>(OnShutdown);
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);
}
_toUpdate.Clear();
}
public void ChangeModifiers(EntityUid uid, float speed, SpeedModifierContactsComponent? component = null)
{
ChangeModifiers(uid, speed, speed, component);
}
public void ChangeModifiers(EntityUid uid, float walkSpeed, float sprintSpeed, SpeedModifierContactsComponent? component = null)
{
if (!Resolve(uid, ref component))
{
return;
}
component.WalkSpeedModifier = walkSpeed;
component.SprintSpeedModifier = sprintSpeed;
Dirty(component);
_toUpdate.UnionWith(_physics.GetContactingEntities(uid));
}
private void OnShutdown(EntityUid uid, SpeedModifierContactsComponent component, ComponentShutdown args)
{
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));
}
private void MovementSpeedCheck(EntityUid uid, SpeedModifiedByContactComponent component, RefreshMovementSpeedModifiersEvent args)
{
if (!EntityManager.TryGetComponent<PhysicsComponent>(uid, out var physicsComponent))
return;
var walkSpeed = 0.0f;
var sprintSpeed = 0.0f;
bool remove = true;
var entries = 0;
foreach (var ent in _physics.GetContactingEntities(uid, physicsComponent))
{
if (!TryComp<SpeedModifierContactsComponent>(ent, out var slowContactsComponent))
continue;
if (slowContactsComponent.IgnoreWhitelist != null && slowContactsComponent.IgnoreWhitelist.IsValid(uid))
continue;
walkSpeed += slowContactsComponent.WalkSpeedModifier;
sprintSpeed += slowContactsComponent.SprintSpeedModifier;
remove = false;
entries++;
}
if (entries > 0)
{
walkSpeed /= entries;
sprintSpeed /= entries;
args.ModifySpeed(walkSpeed, sprintSpeed);
}
// no longer colliding with anything
if (remove)
_toRemove.Add(uid);
}
private void OnEntityExit(EntityUid uid, SpeedModifierContactsComponent component, ref EndCollideEvent args)
{
var otherUid = args.OtherEntity;
_toUpdate.Add(otherUid);
}
private void OnEntityEnter(EntityUid uid, SpeedModifierContactsComponent component, ref StartCollideEvent args)
{
var otherUid = args.OtherEntity;
if (!HasComp<MovementSpeedModifierComponent>(otherUid))
return;
EnsureComp<SpeedModifiedByContactComponent>(otherUid);
_toUpdate.Add(otherUid);
}
}