Skip to content

Commit

Permalink
SlowContactsSystem to SpeedModifierContactsSystem mini rework (space-…
Browse files Browse the repository at this point in the history
…wizards#26110)

* rework

* update logic
  • Loading branch information
TheShuEd committed Mar 17, 2024
1 parent 209846b commit c35ff87
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 34 deletions.
4 changes: 2 additions & 2 deletions Content.Server/Chemistry/TileReactions/SpillTileReaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ public FixedPoint2 TileReact(TileRef tile, ReagentPrototype reagent, FixedPoint2
var step = entityManager.EnsureComponent<StepTriggerComponent>(puddleUid);
entityManager.EntitySysManager.GetEntitySystem<StepTriggerSystem>().SetRequiredTriggerSpeed(puddleUid, _requiredSlipSpeed, step);

var slow = entityManager.EnsureComponent<SlowContactsComponent>(puddleUid);
var slow = entityManager.EnsureComponent<SpeedModifierContactsComponent>(puddleUid);
var speedModifier = 1 - reagent.Viscosity;
entityManager.EntitySysManager.GetEntitySystem<SlowContactsSystem>().ChangeModifiers(puddleUid, speedModifier, slow);
entityManager.EntitySysManager.GetEntitySystem<SpeedModifierContactsSystem>().ChangeModifiers(puddleUid, speedModifier, slow);

return reactVolume;
}
Expand Down
8 changes: 4 additions & 4 deletions Content.Server/Fluids/EntitySystems/PuddleSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public sealed partial class PuddleSystem : SharedPuddleSystem
[Dependency] private readonly SharedPopupSystem _popups = default!;
[Dependency] private readonly SolutionContainerSystem _solutionContainerSystem = default!;
[Dependency] private readonly StepTriggerSystem _stepTrigger = default!;
[Dependency] private readonly SlowContactsSystem _slowContacts = default!;
[Dependency] private readonly SpeedModifierContactsSystem _speedModContacts = default!;
[Dependency] private readonly TileFrictionController _tile = default!;

[ValidatePrototypeId<ReagentPrototype>]
Expand Down Expand Up @@ -435,13 +435,13 @@ private void UpdateSlow(EntityUid uid, Solution solution)

if (maxViscosity > 0)
{
var comp = EnsureComp<SlowContactsComponent>(uid);
var comp = EnsureComp<SpeedModifierContactsComponent>(uid);
var speed = 1 - maxViscosity;
_slowContacts.ChangeModifiers(uid, speed, comp);
_speedModContacts.ChangeModifiers(uid, speed, comp);
}
else
{
RemComp<SlowContactsComponent>(uid);
RemComp<SpeedModifierContactsComponent>(uid);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ namespace Content.Shared.Movement.Components;
/// Exists just to listen to a single event. What a life.
/// </summary>
[NetworkedComponent, RegisterComponent] // must be networked to properly predict adding & removal
public sealed partial class SlowedByContactComponent : Component
public sealed partial class SpeedModifiedByContactComponent : Component
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ namespace Content.Shared.Movement.Components;

[NetworkedComponent, RegisterComponent]
[AutoGenerateComponentState]
[Access(typeof(SlowContactsSystem))]
public sealed partial class SlowContactsComponent : Component
[Access(typeof(SpeedModifierContactsSystem))]
public sealed partial class SpeedModifierContactsComponent : Component
{
[DataField("walkSpeedModifier"), ViewVariables(VVAccess.ReadWrite)]
[AutoNetworkedField]
Expand Down
2 changes: 1 addition & 1 deletion Content.Shared/Movement/Systems/FrictionContactsSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public sealed class FrictionContactsSystem : EntitySystem
[Dependency] private readonly SharedPhysicsSystem _physics = default!;
[Dependency] private readonly MovementSpeedModifierSystem _speedModifierSystem = default!;

// Comment copied from "original" SlowContactsSystem.cs
// 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace Content.Shared.Movement.Systems;

public sealed class SlowContactsSystem : EntitySystem
public sealed class SpeedModifierContactsSystem : EntitySystem
{
[Dependency] private readonly SharedPhysicsSystem _physics = default!;
[Dependency] private readonly MovementSpeedModifierSystem _speedModifierSystem = default!;
Expand All @@ -18,10 +18,10 @@ public sealed class SlowContactsSystem : EntitySystem
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<SlowContactsComponent, StartCollideEvent>(OnEntityEnter);
SubscribeLocalEvent<SlowContactsComponent, EndCollideEvent>(OnEntityExit);
SubscribeLocalEvent<SlowedByContactComponent, RefreshMovementSpeedModifiersEvent>(MovementSpeedCheck);
SubscribeLocalEvent<SlowContactsComponent, ComponentShutdown>(OnShutdown);
SubscribeLocalEvent<SpeedModifierContactsComponent, StartCollideEvent>(OnEntityEnter);
SubscribeLocalEvent<SpeedModifierContactsComponent, EndCollideEvent>(OnEntityExit);
SubscribeLocalEvent<SpeedModifiedByContactComponent, RefreshMovementSpeedModifiersEvent>(MovementSpeedCheck);
SubscribeLocalEvent<SpeedModifierContactsComponent, ComponentShutdown>(OnShutdown);

UpdatesAfter.Add(typeof(SharedPhysicsSystem));
}
Expand All @@ -39,18 +39,18 @@ public override void Update(float frameTime)

foreach (var ent in _toRemove)
{
RemComp<SlowedByContactComponent>(ent);
RemComp<SpeedModifiedByContactComponent>(ent);
}

_toUpdate.Clear();
}

public void ChangeModifiers(EntityUid uid, float speed, SlowContactsComponent? component = null)
public void ChangeModifiers(EntityUid uid, float speed, SpeedModifierContactsComponent? component = null)
{
ChangeModifiers(uid, speed, speed, component);
}

public void ChangeModifiers(EntityUid uid, float walkSpeed, float sprintSpeed, SlowContactsComponent? component = null)
public void ChangeModifiers(EntityUid uid, float walkSpeed, float sprintSpeed, SpeedModifierContactsComponent? component = null)
{
if (!Resolve(uid, ref component))
{
Expand All @@ -62,7 +62,7 @@ public void ChangeModifiers(EntityUid uid, float walkSpeed, float sprintSpeed, S
_toUpdate.UnionWith(_physics.GetContactingEntities(uid));
}

private void OnShutdown(EntityUid uid, SlowContactsComponent component, ComponentShutdown args)
private void OnShutdown(EntityUid uid, SpeedModifierContactsComponent component, ComponentShutdown args)
{
if (!TryComp(uid, out PhysicsComponent? phys))
return;
Expand All @@ -71,48 +71,56 @@ private void OnShutdown(EntityUid uid, SlowContactsComponent component, Componen
_toUpdate.UnionWith(_physics.GetContactingEntities(uid, phys));
}

private void MovementSpeedCheck(EntityUid uid, SlowedByContactComponent component, RefreshMovementSpeedModifiersEvent args)
private void MovementSpeedCheck(EntityUid uid, SpeedModifiedByContactComponent component, RefreshMovementSpeedModifiersEvent args)
{
if (!EntityManager.TryGetComponent<PhysicsComponent>(uid, out var physicsComponent))
return;

var walkSpeed = 1.0f;
var sprintSpeed = 1.0f;
var walkSpeed = 0.0f;
var sprintSpeed = 0.0f;

bool remove = true;
var entries = 0;
foreach (var ent in _physics.GetContactingEntities(uid, physicsComponent))
{
if (!TryComp<SlowContactsComponent>(ent, out var slowContactsComponent))
if (!TryComp<SpeedModifierContactsComponent>(ent, out var slowContactsComponent))
continue;

if (slowContactsComponent.IgnoreWhitelist != null && slowContactsComponent.IgnoreWhitelist.IsValid(uid))
continue;

walkSpeed = Math.Min(walkSpeed, slowContactsComponent.WalkSpeedModifier);
sprintSpeed = Math.Min(sprintSpeed, slowContactsComponent.SprintSpeedModifier);
walkSpeed += slowContactsComponent.WalkSpeedModifier;
sprintSpeed += slowContactsComponent.SprintSpeedModifier;
remove = false;
entries++;
}

args.ModifySpeed(walkSpeed, sprintSpeed);
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, SlowContactsComponent component, ref EndCollideEvent args)
private void OnEntityExit(EntityUid uid, SpeedModifierContactsComponent component, ref EndCollideEvent args)
{
var otherUid = args.OtherEntity;
_toUpdate.Add(otherUid);
}

private void OnEntityEnter(EntityUid uid, SlowContactsComponent component, ref StartCollideEvent args)
private void OnEntityEnter(EntityUid uid, SpeedModifierContactsComponent component, ref StartCollideEvent args)
{
var otherUid = args.OtherEntity;
if (!HasComp<MovementSpeedModifierComponent>(otherUid))
return;

EnsureComp<SlowedByContactComponent>(otherUid);
EnsureComp<SpeedModifiedByContactComponent>(otherUid);
_toUpdate.Add(otherUid);
}
}
6 changes: 3 additions & 3 deletions Resources/Prototypes/Entities/Objects/Misc/kudzu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
- type: AtmosExposed
growthTickChance: 0.3
spreadChance: 0.4
- type: SlowContacts
- type: SpeedModifierContacts
walkSpeedModifier: 0.2
sprintSpeedModifier: 0.2
ignoreWhitelist:
Expand Down Expand Up @@ -123,7 +123,7 @@
- type: Kudzu
spriteVariants: 5
spreadChance: 0.01
- type: SlowContacts
- type: SpeedModifierContacts
walkSpeedModifier: 0.8
sprintSpeedModifier: 0.8
ignoreWhitelist:
Expand Down Expand Up @@ -237,7 +237,7 @@
Heat: 3
growthTickChance: 0.3
- type: AtmosExposed
- type: SlowContacts
- type: SpeedModifierContacts
walkSpeedModifier: 0.3
sprintSpeedModifier: 0.3
ignoreWhitelist:
Expand Down
2 changes: 1 addition & 1 deletion Resources/Prototypes/Entities/Objects/Misc/spider_web.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
Flammable: [Touch]
Extinguish: [Touch]
- type: SpiderWebObject
- type: SlowContacts
- type: SpeedModifierContacts
walkSpeedModifier: 0.5
sprintSpeedModifier: 0.5
ignoreWhitelist:
Expand Down
2 changes: 1 addition & 1 deletion Resources/Prototypes/Entities/Tiles/water.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
Quantity: 100
- type: DrainableSolution
solution: pool
- type: SlowContacts
- type: SpeedModifierContacts
walkSpeedModifier: 0.5
sprintSpeedModifier: 0.5
- type: Physics
Expand Down

0 comments on commit c35ff87

Please sign in to comment.