Files
wwdpublic/Content.Shared/Movement/Systems/FrictionContactsSystem.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

101 lines
3.3 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 FrictionContactsSystem : EntitySystem
{
[Dependency] private readonly SharedPhysicsSystem _physics = default!;
[Dependency] private readonly MovementSpeedModifierSystem _speedModifierSystem = default!;
// Comment copied from "original" SlowContactsSystem.cs (now SpeedModifierContactsSystem.cs)
// 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();
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<FrictionContactsComponent, StartCollideEvent>(OnEntityEnter);
SubscribeLocalEvent<FrictionContactsComponent, EndCollideEvent>(OnEntityExit);
SubscribeLocalEvent<FrictionContactsComponent, ComponentShutdown>(OnShutdown);
UpdatesAfter.Add(typeof(SharedPhysicsSystem));
}
private void OnEntityEnter(EntityUid uid, FrictionContactsComponent component, ref StartCollideEvent args)
{
var otherUid = args.OtherEntity;
if (!HasComp(otherUid, typeof(MovementSpeedModifierComponent)))
return;
_toUpdate.Add(otherUid);
}
private void OnEntityExit(EntityUid uid, FrictionContactsComponent component, ref EndCollideEvent args)
{
var otherUid = args.OtherEntity;
if (!HasComp(otherUid, typeof(MovementSpeedModifierComponent)))
return;
_toUpdate.Add(otherUid);
}
private void OnShutdown(EntityUid uid, FrictionContactsComponent component, ComponentShutdown args)
{
if (!TryComp(uid, out PhysicsComponent? phys))
return;
_toUpdate.UnionWith(_physics.GetContactingEntities(uid, phys));
}
public override void Update(float frameTime)
{
base.Update(frameTime);
foreach (var uid in _toUpdate)
{
ApplyFrictionChange(uid);
}
_toUpdate.Clear();
}
private void ApplyFrictionChange(EntityUid uid)
{
if (!EntityManager.TryGetComponent<PhysicsComponent>(uid, out var physicsComponent))
return;
if (!TryComp(uid, out MovementSpeedModifierComponent? speedModifier))
return;
FrictionContactsComponent? frictionComponent = TouchesFrictionContactsComponent(uid, physicsComponent);
if (frictionComponent == null)
{
_speedModifierSystem.ChangeFriction(uid, MovementSpeedModifierComponent.DefaultFriction, null, MovementSpeedModifierComponent.DefaultAcceleration, speedModifier);
}
else
{
_speedModifierSystem.ChangeFriction(uid, frictionComponent.MobFriction, frictionComponent.MobFrictionNoInput, frictionComponent.MobAcceleration, speedModifier);
}
}
private FrictionContactsComponent? TouchesFrictionContactsComponent(EntityUid uid, PhysicsComponent physicsComponent)
{
foreach (var ent in _physics.GetContactingEntities(uid, physicsComponent))
{
if (!TryComp(ent, out FrictionContactsComponent? frictionContacts))
continue;
return frictionContacts;
}
return null;
}
}