magic visibility

This commit is contained in:
Ed
2024-11-15 18:18:04 +03:00
parent 5bcc378bbe
commit 0ca9ac59f9
5 changed files with 154 additions and 0 deletions

View File

@@ -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<CP14MagicVisionMarkerComponent, ComponentStartup>(OnStartup);
}
public override void Update(float frameTime)
{
base.Update(frameTime);
if (_timing.CurTime < _nextUpdate)
return;
_nextUpdate = _timing.CurTime + TimeSpan.FromSeconds(1f);
var query = EntityQueryEnumerator<CP14MagicVisionMarkerComponent, SpriteComponent>();
while (query.MoveNext(out var uid, out var marker, out var sprite))
{
UpdateVisibility((uid, marker), sprite);
}
}
private void OnStartup(Entity<CP14MagicVisionMarkerComponent> ent, ref ComponentStartup args)
{
if (!TryComp<SpriteComponent>(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<CP14MagicVisionMarkerComponent> 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<CP14ClientMagicVisionSystem>().MagicVisible ^= true;
}
}

View File

@@ -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<CP14MagicVisionMarkerComponent>();
while (query.MoveNext(out var uid, out var marker))
{
if (_timing.CurTime < marker.EndTime)
continue;
QueueDel(uid);
}
}
}

View File

@@ -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<CP14MagicVisionMarkerComponent, MapInitEvent>(OnMapInit);
}
private void OnMapInit(Entity<CP14MagicVisionMarkerComponent> ent, ref MapInitEvent args)
{
ent.Comp.SpawnTime = _timing.CurTime;
ent.Comp.EndTime = _timing.CurTime + ent.Comp.VisibilityTime;
}
}

View File

@@ -0,0 +1,17 @@
namespace Content.Shared._CP14.MagicSpell.Components;
/// <summary>
/// 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
/// </summary>
[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);
}

View File

@@ -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