From a2a895de4eb1b22bf99f7cee3e2cdaf65a92f2c0 Mon Sep 17 00:00:00 2001 From: Rane <60792108+Elijahrane@users.noreply.github.com> Date: Sat, 22 Oct 2022 18:06:55 -0400 Subject: [PATCH] seperate Stealth and StealthOnMove (#12035) --- .../Stealth/Components/StealthComponent.cs | 18 ++------ .../Components/StealthOnMoveComponent.cs | 22 ++++++++++ Content.Shared/Stealth/SharedStealthSystem.cs | 41 +++++++++++++++++-- .../Structures/Storage/Closets/big_boxes.yml | 3 +- 4 files changed, 65 insertions(+), 19 deletions(-) create mode 100644 Content.Shared/Stealth/Components/StealthOnMoveComponent.cs diff --git a/Content.Shared/Stealth/Components/StealthComponent.cs b/Content.Shared/Stealth/Components/StealthComponent.cs index d97e76b9fb..be7bbe2f54 100644 --- a/Content.Shared/Stealth/Components/StealthComponent.cs +++ b/Content.Shared/Stealth/Components/StealthComponent.cs @@ -5,8 +5,9 @@ using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom; namespace Content.Shared.Stealth.Components; /// /// Add this component to an entity that you want to be cloaked. -/// It overlays a shader on the entity to give them an invisibility cloaked effect -/// It also turns the entity invisible +/// It overlays a shader on the entity to give them an invisibility cloaked effect. +/// It also turns the entity invisible. +/// Use other components (like StealthOnMove) to modify this component's visibility based on certain conditions. /// [RegisterComponent, NetworkedComponent] [Access(typeof(SharedStealthSystem))] @@ -35,6 +36,7 @@ public sealed class StealthComponent : Component /// outside of this range simply act as a buffer for the visual effect (i.e., a delay before turning invisible). To /// get the actual current visibility, use + /// If you don't have anything else updating the stealth, this will just stay at a constant value, which can be useful. /// [DataField("lastVisibility")] [Access(typeof(SharedStealthSystem), Other = AccessPermissions.None)] @@ -48,18 +50,6 @@ public sealed class StealthComponent : Component [DataField("lastUpdate", customTypeSerializer:typeof(TimeOffsetSerializer))] public TimeSpan? LastUpdated; - /// - /// Rate that effects how fast an entity's visibility passively changes. - /// - [DataField("passiveVisibilityRate")] - public readonly float PassiveVisibilityRate = -0.15f; - - /// - /// Rate for movement induced visibility changes. Scales with distance moved. - /// - [DataField("movementVisibilityRate")] - public readonly float MovementVisibilityRate = 0.2f; - /// /// Minimum visibility. Note that the visual effect caps out at -1, but this value is allowed to be larger or smaller. /// diff --git a/Content.Shared/Stealth/Components/StealthOnMoveComponent.cs b/Content.Shared/Stealth/Components/StealthOnMoveComponent.cs new file mode 100644 index 0000000000..c4f6face5b --- /dev/null +++ b/Content.Shared/Stealth/Components/StealthOnMoveComponent.cs @@ -0,0 +1,22 @@ +namespace Content.Shared.Stealth.Components +{ + /// + /// When added to an entity with stealth component, this component will change the visibility + /// based on the entity's (lack of) movement. + /// + [RegisterComponent] + public sealed class StealthOnMoveComponent : Component + { + /// + /// Rate that effects how fast an entity's visibility passively changes. + /// + [DataField("passiveVisibilityRate")] + public readonly float PassiveVisibilityRate = -0.15f; + + /// + /// Rate for movement induced visibility changes. Scales with distance moved. + /// + [DataField("movementVisibilityRate")] + public readonly float MovementVisibilityRate = 0.2f; + } +} diff --git a/Content.Shared/Stealth/SharedStealthSystem.cs b/Content.Shared/Stealth/SharedStealthSystem.cs index 9390d6eb22..b46344b609 100644 --- a/Content.Shared/Stealth/SharedStealthSystem.cs +++ b/Content.Shared/Stealth/SharedStealthSystem.cs @@ -15,7 +15,8 @@ public abstract class SharedStealthSystem : EntitySystem SubscribeLocalEvent(OnStealthGetState); SubscribeLocalEvent(OnStealthHandleState); - SubscribeLocalEvent(OnMove); + SubscribeLocalEvent(OnMove); + SubscribeLocalEvent(OnGetVisibilityModifiers); SubscribeLocalEvent(OnPaused); SubscribeLocalEvent(OnInit); SubscribeLocalEvent(OnExamine); @@ -87,7 +88,7 @@ public abstract class SharedStealthSystem : EntitySystem component.LastUpdated = cast.LastUpdated; } - private void OnMove(EntityUid uid, StealthComponent component, ref MoveEvent args) + private void OnMove(EntityUid uid, StealthOnMoveComponent component, ref MoveEvent args) { if (args.FromStateHandling) return; @@ -96,7 +97,13 @@ public abstract class SharedStealthSystem : EntitySystem return; var delta = component.MovementVisibilityRate * (args.NewPosition.Position - args.OldPosition.Position).Length; - ModifyVisibility(uid, delta, component); + ModifyVisibility(uid, delta); + } + + private void OnGetVisibilityModifiers(EntityUid uid, StealthOnMoveComponent component, GetVisibilityModifiersEvent args) + { + var mod = args.SecondsSinceUpdate * component.PassiveVisibilityRate; + args.FlatModifier += mod; } /// @@ -150,6 +157,32 @@ public abstract class SharedStealthSystem : EntitySystem return component.LastVisibility; var deltaTime = _timing.CurTime - component.LastUpdated.Value; - return Math.Clamp(component.LastVisibility + (float) deltaTime.TotalSeconds * component.PassiveVisibilityRate, component.MinVisibility, component.MaxVisibility); + + var ev = new GetVisibilityModifiersEvent(uid, component, (float) deltaTime.TotalSeconds, 0f); + RaiseLocalEvent(uid, ev, false); + + return Math.Clamp(component.LastVisibility + ev.FlatModifier, component.MinVisibility, component.MaxVisibility); + } + + /// + /// Used to run through any stealth effecting components on the entity. + /// + private sealed class GetVisibilityModifiersEvent : EntityEventArgs + { + public readonly StealthComponent Stealth = default!; + public readonly float SecondsSinceUpdate; + + /// + /// Calculate this and add to it. Do not divide, multiply, or overwrite. + /// The sum will be added to the stealth component's visibility. + /// + public float FlatModifier; + + public GetVisibilityModifiersEvent(EntityUid uid, StealthComponent stealth, float secondsSinceUpdate, float flatModifier) + { + Stealth = stealth; + SecondsSinceUpdate = secondsSinceUpdate; + FlatModifier = flatModifier; + } } } diff --git a/Resources/Prototypes/Entities/Structures/Storage/Closets/big_boxes.yml b/Resources/Prototypes/Entities/Structures/Storage/Closets/big_boxes.yml index e39a1a03e9..aca016f6e4 100644 --- a/Resources/Prototypes/Entities/Structures/Storage/Closets/big_boxes.yml +++ b/Resources/Prototypes/Entities/Structures/Storage/Closets/big_boxes.yml @@ -68,6 +68,7 @@ state: agentbox state_open: cardboard_open - type: Stealth + - type: StealthOnMove passiveVisibilityRate: -0.24 movementVisibilityRate: 0.10 @@ -75,7 +76,7 @@ - type: entity id: GhostBox parent: StealthBox - suffix: + suffix: name: ghost box description: Beware! components: