Re-organize all projects (#4166)
This commit is contained in:
71
Content.Client/Flash/FlashOverlay.cs
Normal file
71
Content.Client/Flash/FlashOverlay.cs
Normal file
@@ -0,0 +1,71 @@
|
||||
using Content.Client.Viewport;
|
||||
using Robust.Client.Graphics;
|
||||
using Robust.Client.State;
|
||||
using Robust.Shared.Enums;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Maths;
|
||||
using Robust.Shared.Prototypes;
|
||||
using Robust.Shared.Timing;
|
||||
using SixLabors.ImageSharp.PixelFormats;
|
||||
|
||||
namespace Content.Client.Flash
|
||||
{
|
||||
public class FlashOverlay : Overlay
|
||||
{
|
||||
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
|
||||
[Dependency] private readonly IClyde _displayManager = default!;
|
||||
[Dependency] private readonly IGameTiming _gameTiming = default!;
|
||||
[Dependency] private readonly IStateManager _stateManager = default!;
|
||||
|
||||
public override OverlaySpace Space => OverlaySpace.ScreenSpace;
|
||||
private readonly ShaderInstance _shader;
|
||||
private double _startTime = -1;
|
||||
private double _lastsFor = 1;
|
||||
private Texture? _screenshotTexture;
|
||||
|
||||
public FlashOverlay()
|
||||
{
|
||||
IoCManager.InjectDependencies(this);
|
||||
_shader = _prototypeManager.Index<ShaderPrototype>("FlashedEffect").Instance().Duplicate();
|
||||
}
|
||||
|
||||
public void ReceiveFlash(double duration)
|
||||
{
|
||||
if (_stateManager.CurrentState is IMainViewportState state)
|
||||
{
|
||||
state.Viewport.Viewport.Screenshot(image =>
|
||||
{
|
||||
var rgba32Image = image.CloneAs<Rgba32>(SixLabors.ImageSharp.Configuration.Default);
|
||||
_screenshotTexture = _displayManager.LoadTextureFromImage(rgba32Image);
|
||||
});
|
||||
}
|
||||
|
||||
_startTime = _gameTiming.CurTime.TotalSeconds;
|
||||
_lastsFor = duration;
|
||||
}
|
||||
|
||||
protected override void Draw(in OverlayDrawArgs args)
|
||||
{
|
||||
var percentComplete = (float) ((_gameTiming.CurTime.TotalSeconds - _startTime) / _lastsFor);
|
||||
if (percentComplete >= 1.0f)
|
||||
return;
|
||||
|
||||
var screenSpaceHandle = args.ScreenHandle;
|
||||
screenSpaceHandle.UseShader(_shader);
|
||||
_shader.SetParameter("percentComplete", percentComplete);
|
||||
|
||||
var screenSize = UIBox2.FromDimensions((0, 0), _displayManager.ScreenSize);
|
||||
|
||||
if (_screenshotTexture != null)
|
||||
{
|
||||
screenSpaceHandle.DrawTextureRect(_screenshotTexture, screenSize);
|
||||
}
|
||||
}
|
||||
|
||||
protected override void DisposeBehavior()
|
||||
{
|
||||
base.Dispose();
|
||||
_screenshotTexture = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
62
Content.Client/Flash/FlashableComponent.cs
Normal file
62
Content.Client/Flash/FlashableComponent.cs
Normal file
@@ -0,0 +1,62 @@
|
||||
#nullable enable
|
||||
using System;
|
||||
using Content.Shared.Flash;
|
||||
using Robust.Client.Graphics;
|
||||
using Robust.Client.Player;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Timing;
|
||||
|
||||
namespace Content.Client.Flash
|
||||
{
|
||||
[RegisterComponent]
|
||||
public sealed class FlashableComponent : SharedFlashableComponent
|
||||
{
|
||||
private TimeSpan _startTime;
|
||||
private double _duration;
|
||||
|
||||
public override void HandleComponentState(ComponentState? curState, ComponentState? nextState)
|
||||
{
|
||||
if (curState == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var playerManager = IoCManager.Resolve<IPlayerManager>();
|
||||
if (playerManager.LocalPlayer != null && playerManager.LocalPlayer.ControlledEntity != Owner)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var newState = (FlashComponentState) curState;
|
||||
if (newState.Time == default)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Few things here:
|
||||
// 1. If a shorter duration flash is applied then don't do anything
|
||||
// 2. If the client-side time is later than when the flash should've ended don't do anything
|
||||
var currentTime = IoCManager.Resolve<IGameTiming>().CurTime.TotalSeconds;
|
||||
var newEndTime = newState.Time.TotalSeconds + newState.Duration;
|
||||
var currentEndTime = _startTime.TotalSeconds + _duration;
|
||||
|
||||
if (currentEndTime > newEndTime)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (currentTime > newEndTime)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_startTime = newState.Time;
|
||||
_duration = newState.Duration;
|
||||
|
||||
var overlayManager = IoCManager.Resolve<IOverlayManager>();
|
||||
var overlay = overlayManager.GetOverlay<FlashOverlay>();
|
||||
overlay.ReceiveFlash(_duration);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user