From c35ff87e146f0cda7c10782f4ab80b784b51e5fe Mon Sep 17 00:00:00 2001 From: Ed <96445749+TheShuEd@users.noreply.github.com> Date: Sun, 17 Mar 2024 05:27:22 +0300 Subject: [PATCH] SlowContactsSystem to SpeedModifierContactsSystem mini rework (#26110) * rework * update logic --- .../TileReactions/SpillTileReaction.cs | 4 +- .../Fluids/EntitySystems/PuddleSystem.cs | 8 ++-- ....cs => SpeedModifiedByContactComponent.cs} | 2 +- ...t.cs => SpeedModifierContactsComponent.cs} | 4 +- .../Systems/FrictionContactsSystem.cs | 2 +- ...stem.cs => SpeedModifierContactsSystem.cs} | 46 +++++++++++-------- .../Entities/Objects/Misc/kudzu.yml | 6 +-- .../Entities/Objects/Misc/spider_web.yml | 2 +- Resources/Prototypes/Entities/Tiles/water.yml | 2 +- 9 files changed, 42 insertions(+), 34 deletions(-) rename Content.Shared/Movement/Components/{SlowedByContactComponent.cs => SpeedModifiedByContactComponent.cs} (80%) rename Content.Shared/Movement/Components/{SlowContactsComponent.cs => SpeedModifierContactsComponent.cs} (83%) rename Content.Shared/Movement/Systems/{SlowContactsSystem.cs => SpeedModifierContactsSystem.cs} (59%) diff --git a/Content.Server/Chemistry/TileReactions/SpillTileReaction.cs b/Content.Server/Chemistry/TileReactions/SpillTileReaction.cs index 49fdaa5c7ee738..8f8b0626a28a22 100644 --- a/Content.Server/Chemistry/TileReactions/SpillTileReaction.cs +++ b/Content.Server/Chemistry/TileReactions/SpillTileReaction.cs @@ -45,9 +45,9 @@ public FixedPoint2 TileReact(TileRef tile, ReagentPrototype reagent, FixedPoint2 var step = entityManager.EnsureComponent(puddleUid); entityManager.EntitySysManager.GetEntitySystem().SetRequiredTriggerSpeed(puddleUid, _requiredSlipSpeed, step); - var slow = entityManager.EnsureComponent(puddleUid); + var slow = entityManager.EnsureComponent(puddleUid); var speedModifier = 1 - reagent.Viscosity; - entityManager.EntitySysManager.GetEntitySystem().ChangeModifiers(puddleUid, speedModifier, slow); + entityManager.EntitySysManager.GetEntitySystem().ChangeModifiers(puddleUid, speedModifier, slow); return reactVolume; } diff --git a/Content.Server/Fluids/EntitySystems/PuddleSystem.cs b/Content.Server/Fluids/EntitySystems/PuddleSystem.cs index c03e6f593c6d54..e3481f98da3b7f 100644 --- a/Content.Server/Fluids/EntitySystems/PuddleSystem.cs +++ b/Content.Server/Fluids/EntitySystems/PuddleSystem.cs @@ -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] @@ -435,13 +435,13 @@ private void UpdateSlow(EntityUid uid, Solution solution) if (maxViscosity > 0) { - var comp = EnsureComp(uid); + var comp = EnsureComp(uid); var speed = 1 - maxViscosity; - _slowContacts.ChangeModifiers(uid, speed, comp); + _speedModContacts.ChangeModifiers(uid, speed, comp); } else { - RemComp(uid); + RemComp(uid); } } diff --git a/Content.Shared/Movement/Components/SlowedByContactComponent.cs b/Content.Shared/Movement/Components/SpeedModifiedByContactComponent.cs similarity index 80% rename from Content.Shared/Movement/Components/SlowedByContactComponent.cs rename to Content.Shared/Movement/Components/SpeedModifiedByContactComponent.cs index eafefefee180b3..4f791e13cab0fd 100644 --- a/Content.Shared/Movement/Components/SlowedByContactComponent.cs +++ b/Content.Shared/Movement/Components/SpeedModifiedByContactComponent.cs @@ -7,6 +7,6 @@ namespace Content.Shared.Movement.Components; /// Exists just to listen to a single event. What a life. /// [NetworkedComponent, RegisterComponent] // must be networked to properly predict adding & removal -public sealed partial class SlowedByContactComponent : Component +public sealed partial class SpeedModifiedByContactComponent : Component { } diff --git a/Content.Shared/Movement/Components/SlowContactsComponent.cs b/Content.Shared/Movement/Components/SpeedModifierContactsComponent.cs similarity index 83% rename from Content.Shared/Movement/Components/SlowContactsComponent.cs rename to Content.Shared/Movement/Components/SpeedModifierContactsComponent.cs index 00cbc55d198049..73bb0690fdc1a2 100644 --- a/Content.Shared/Movement/Components/SlowContactsComponent.cs +++ b/Content.Shared/Movement/Components/SpeedModifierContactsComponent.cs @@ -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] diff --git a/Content.Shared/Movement/Systems/FrictionContactsSystem.cs b/Content.Shared/Movement/Systems/FrictionContactsSystem.cs index b104c549e69c04..b086bc0e05f575 100644 --- a/Content.Shared/Movement/Systems/FrictionContactsSystem.cs +++ b/Content.Shared/Movement/Systems/FrictionContactsSystem.cs @@ -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 _toUpdate = new(); diff --git a/Content.Shared/Movement/Systems/SlowContactsSystem.cs b/Content.Shared/Movement/Systems/SpeedModifierContactsSystem.cs similarity index 59% rename from Content.Shared/Movement/Systems/SlowContactsSystem.cs rename to Content.Shared/Movement/Systems/SpeedModifierContactsSystem.cs index 1ee145075f6b84..f9f6b82bb182b9 100644 --- a/Content.Shared/Movement/Systems/SlowContactsSystem.cs +++ b/Content.Shared/Movement/Systems/SpeedModifierContactsSystem.cs @@ -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!; @@ -18,10 +18,10 @@ public sealed class SlowContactsSystem : EntitySystem public override void Initialize() { base.Initialize(); - SubscribeLocalEvent(OnEntityEnter); - SubscribeLocalEvent(OnEntityExit); - SubscribeLocalEvent(MovementSpeedCheck); - SubscribeLocalEvent(OnShutdown); + SubscribeLocalEvent(OnEntityEnter); + SubscribeLocalEvent(OnEntityExit); + SubscribeLocalEvent(MovementSpeedCheck); + SubscribeLocalEvent(OnShutdown); UpdatesAfter.Add(typeof(SharedPhysicsSystem)); } @@ -39,18 +39,18 @@ public override void Update(float frameTime) foreach (var ent in _toRemove) { - RemComp(ent); + RemComp(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)) { @@ -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; @@ -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(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(ent, out var slowContactsComponent)) + if (!TryComp(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(otherUid)) return; - EnsureComp(otherUid); + EnsureComp(otherUid); _toUpdate.Add(otherUid); } } diff --git a/Resources/Prototypes/Entities/Objects/Misc/kudzu.yml b/Resources/Prototypes/Entities/Objects/Misc/kudzu.yml index 5de1f0b7f935d0..657201912e8652 100644 --- a/Resources/Prototypes/Entities/Objects/Misc/kudzu.yml +++ b/Resources/Prototypes/Entities/Objects/Misc/kudzu.yml @@ -82,7 +82,7 @@ - type: AtmosExposed growthTickChance: 0.3 spreadChance: 0.4 - - type: SlowContacts + - type: SpeedModifierContacts walkSpeedModifier: 0.2 sprintSpeedModifier: 0.2 ignoreWhitelist: @@ -123,7 +123,7 @@ - type: Kudzu spriteVariants: 5 spreadChance: 0.01 - - type: SlowContacts + - type: SpeedModifierContacts walkSpeedModifier: 0.8 sprintSpeedModifier: 0.8 ignoreWhitelist: @@ -237,7 +237,7 @@ Heat: 3 growthTickChance: 0.3 - type: AtmosExposed - - type: SlowContacts + - type: SpeedModifierContacts walkSpeedModifier: 0.3 sprintSpeedModifier: 0.3 ignoreWhitelist: diff --git a/Resources/Prototypes/Entities/Objects/Misc/spider_web.yml b/Resources/Prototypes/Entities/Objects/Misc/spider_web.yml index af65ac95a8a7c8..e1f4d086d4594a 100644 --- a/Resources/Prototypes/Entities/Objects/Misc/spider_web.yml +++ b/Resources/Prototypes/Entities/Objects/Misc/spider_web.yml @@ -72,7 +72,7 @@ Flammable: [Touch] Extinguish: [Touch] - type: SpiderWebObject - - type: SlowContacts + - type: SpeedModifierContacts walkSpeedModifier: 0.5 sprintSpeedModifier: 0.5 ignoreWhitelist: diff --git a/Resources/Prototypes/Entities/Tiles/water.yml b/Resources/Prototypes/Entities/Tiles/water.yml index e2c0421aef3b66..2fd1c8547deb67 100644 --- a/Resources/Prototypes/Entities/Tiles/water.yml +++ b/Resources/Prototypes/Entities/Tiles/water.yml @@ -32,7 +32,7 @@ Quantity: 100 - type: DrainableSolution solution: pool - - type: SlowContacts + - type: SpeedModifierContacts walkSpeedModifier: 0.5 sprintSpeedModifier: 0.5 - type: Physics