From 0ca9ac59f9f24932ddf9f0bde9af4270cd5f18e2 Mon Sep 17 00:00:00 2001 From: Ed <96445749+TheShuEd@users.noreply.github.com> Date: Fri, 15 Nov 2024 18:18:04 +0300 Subject: [PATCH] magic visibility --- .../MagicSpell/CP14ClientMagicVisionSystem.cs | 78 +++++++++++++++++++ .../_CP14/MagicSpell/CP14MagicVisionSystem.cs | 24 ++++++ .../MagicSpell/CP14SharedMagicVisionSystem.cs | 21 +++++ .../CP14MagicVisionMarkerComponent.cs | 17 ++++ Resources/Prototypes/_CP14/Entities/TEST.yml | 14 ++++ 5 files changed, 154 insertions(+) create mode 100644 Content.Client/_CP14/MagicSpell/CP14ClientMagicVisionSystem.cs create mode 100644 Content.Server/_CP14/MagicSpell/CP14MagicVisionSystem.cs create mode 100644 Content.Shared/_CP14/MagicSpell/CP14SharedMagicVisionSystem.cs create mode 100644 Content.Shared/_CP14/MagicSpell/Components/CP14MagicVisionMarkerComponent.cs create mode 100644 Resources/Prototypes/_CP14/Entities/TEST.yml diff --git a/Content.Client/_CP14/MagicSpell/CP14ClientMagicVisionSystem.cs b/Content.Client/_CP14/MagicSpell/CP14ClientMagicVisionSystem.cs new file mode 100644 index 0000000000..b68e9d3d7f --- /dev/null +++ b/Content.Client/_CP14/MagicSpell/CP14ClientMagicVisionSystem.cs @@ -0,0 +1,78 @@ +using Content.Shared._CP14.MagicSpell; +using Content.Shared._CP14.MagicSpell.Components; +using Robust.Client.GameObjects; +using Robust.Client.Timing; +using Robust.Shared.Console; + +namespace Content.Client._CP14.MagicSpell; + +public sealed class CP14ClientMagicVisionSystem : CP14SharedMagicVisionSystem +{ + [Dependency] private readonly IClientGameTiming _timing = default!; + public bool MagicVisible { get; set; } + + private TimeSpan _nextUpdate = TimeSpan.Zero; + + public override void Initialize() + { + base.Initialize(); + + SubscribeLocalEvent(OnStartup); + } + + public override void Update(float frameTime) + { + base.Update(frameTime); + + if (_timing.CurTime < _nextUpdate) + return; + + _nextUpdate = _timing.CurTime + TimeSpan.FromSeconds(1f); + + var query = EntityQueryEnumerator(); + while (query.MoveNext(out var uid, out var marker, out var sprite)) + { + UpdateVisibility((uid, marker), sprite); + } + } + + private void OnStartup(Entity ent, ref ComponentStartup args) + { + if (!TryComp(ent, out var sprite)) + return; + + ent.Comp.SpawnTime = _timing.CurTime; + ent.Comp.EndTime = _timing.CurTime + ent.Comp.VisibilityTime; + + UpdateVisibility(ent, sprite); + } + + private void UpdateVisibility(Entity ent, SpriteComponent sprite) + { + sprite.Visible = MagicVisible; + + + + if (MagicVisible == false) + return; + + var progress = Math.Clamp((_timing.CurTime.TotalSeconds - ent.Comp.SpawnTime.TotalSeconds) / (ent.Comp.EndTime.TotalSeconds - ent.Comp.SpawnTime.TotalSeconds), 0, 1); + var alpha = 1 - progress; + Log.Info($"{ent.Owner.Id} - {alpha.ToString()}"); + sprite.Color = Color.White.WithAlpha((float)alpha); + } +} + +internal sealed class ShowMagicCommand : LocalizedCommands +{ + [Dependency] private readonly IEntitySystemManager _entitySystemManager = default!; + + public override string Command => "cp14_showmagic"; + + public override string Help => "Toggle visibility of magic traces"; + + public override void Execute(IConsoleShell shell, string argStr, string[] args) + { + _entitySystemManager.GetEntitySystem().MagicVisible ^= true; + } +} diff --git a/Content.Server/_CP14/MagicSpell/CP14MagicVisionSystem.cs b/Content.Server/_CP14/MagicSpell/CP14MagicVisionSystem.cs new file mode 100644 index 0000000000..8ff429df95 --- /dev/null +++ b/Content.Server/_CP14/MagicSpell/CP14MagicVisionSystem.cs @@ -0,0 +1,24 @@ +using Content.Shared._CP14.MagicSpell; +using Content.Shared._CP14.MagicSpell.Components; +using Robust.Shared.Timing; + +namespace Content.Server._CP14.MagicSpell; + +public sealed class CP14MagicVisionSystem : CP14SharedMagicVisionSystem +{ + [Dependency] private readonly IGameTiming _timing = default!; + + public override void Update(float frameTime) + { + base.Update(frameTime); + + var query = EntityQueryEnumerator(); + while (query.MoveNext(out var uid, out var marker)) + { + if (_timing.CurTime < marker.EndTime) + continue; + + QueueDel(uid); + } + } +} diff --git a/Content.Shared/_CP14/MagicSpell/CP14SharedMagicVisionSystem.cs b/Content.Shared/_CP14/MagicSpell/CP14SharedMagicVisionSystem.cs new file mode 100644 index 0000000000..d57db5df4d --- /dev/null +++ b/Content.Shared/_CP14/MagicSpell/CP14SharedMagicVisionSystem.cs @@ -0,0 +1,21 @@ +using Content.Shared._CP14.MagicSpell.Components; +using Robust.Shared.Timing; + +namespace Content.Shared._CP14.MagicSpell; + +public abstract class CP14SharedMagicVisionSystem : EntitySystem +{ + [Dependency] private readonly IGameTiming _timing = default!; + public override void Initialize() + { + base.Initialize(); + + SubscribeLocalEvent(OnMapInit); + } + + private void OnMapInit(Entity ent, ref MapInitEvent args) + { + ent.Comp.SpawnTime = _timing.CurTime; + ent.Comp.EndTime = _timing.CurTime + ent.Comp.VisibilityTime; + } +} diff --git a/Content.Shared/_CP14/MagicSpell/Components/CP14MagicVisionMarkerComponent.cs b/Content.Shared/_CP14/MagicSpell/Components/CP14MagicVisionMarkerComponent.cs new file mode 100644 index 0000000000..38655e69a4 --- /dev/null +++ b/Content.Shared/_CP14/MagicSpell/Components/CP14MagicVisionMarkerComponent.cs @@ -0,0 +1,17 @@ +namespace Content.Shared._CP14.MagicSpell.Components; + +/// +/// Controls the visibility of this entity to the client, based on the length of time it has existed and the client's ability to see the magic +/// +[RegisterComponent, AutoGenerateComponentPause] +public sealed partial class CP14MagicVisionMarkerComponent : Component +{ + [DataField, AutoPausedField] + public TimeSpan SpawnTime = TimeSpan.Zero; + + [DataField, AutoPausedField] + public TimeSpan EndTime = TimeSpan.Zero; + + [DataField] + public TimeSpan VisibilityTime = TimeSpan.FromMinutes(2f); +} diff --git a/Resources/Prototypes/_CP14/Entities/TEST.yml b/Resources/Prototypes/_CP14/Entities/TEST.yml new file mode 100644 index 0000000000..879ba6d95b --- /dev/null +++ b/Resources/Prototypes/_CP14/Entities/TEST.yml @@ -0,0 +1,14 @@ +- type: entity + id: CP14MagicTrace + categories: [ ForkFiltered ] + components: + - type: Sprite + drawdepth: Effects + snapCardinals: true + sprite: _CP14/Effects/fire.rsi + layers: + - state: full + shader: unshaded + - type: Clickable + - type: CP14MagicVisionMarker + visibilityTime: 30 \ No newline at end of file