Events/RadiationStorm: Fancy radiation shader & SFX (#5612)
This commit is contained in:
@@ -12,10 +12,12 @@ namespace Content.Client.StationEvents
|
||||
private bool _decay;
|
||||
private float _radsPerSecond;
|
||||
private float _range;
|
||||
private TimeSpan _startTime;
|
||||
private TimeSpan _endTime;
|
||||
|
||||
public override float RadsPerSecond => _radsPerSecond;
|
||||
public override float Range => _range;
|
||||
public override TimeSpan StartTime => _startTime;
|
||||
public override TimeSpan EndTime => _endTime;
|
||||
public override bool Draw => _draw;
|
||||
public override bool Decay => _decay;
|
||||
@@ -33,6 +35,7 @@ namespace Content.Client.StationEvents
|
||||
_range = state.Range;
|
||||
_draw = state.Draw;
|
||||
_decay = state.Decay;
|
||||
_startTime = state.StartTime;
|
||||
_endTime = state.EndTime;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,152 +1,138 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Client.Graphics;
|
||||
using Robust.Client.Player;
|
||||
using Robust.Shared.Enums;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Timing;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Map;
|
||||
using Robust.Shared.Maths;
|
||||
using Robust.Shared.Timing;
|
||||
using Robust.Shared.Prototypes;
|
||||
|
||||
namespace Content.Client.StationEvents
|
||||
{
|
||||
[UsedImplicitly]
|
||||
public sealed class RadiationPulseOverlay : Overlay
|
||||
public class RadiationPulseOverlay : Overlay
|
||||
{
|
||||
[Dependency] private readonly IEntityManager _entityManager = default!;
|
||||
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
|
||||
[Dependency] private readonly IGameTiming _gameTiming = default!;
|
||||
[Dependency] private readonly IMapManager _mapManager = default!;
|
||||
[Dependency] private readonly IPlayerManager _playerManager = default!;
|
||||
[Dependency] private readonly IEyeManager _eyeManager = default!;
|
||||
|
||||
/// <summary>
|
||||
/// Current color of a pulse
|
||||
/// </summary>
|
||||
private readonly Dictionary<IEntity, Color> _colors = new();
|
||||
|
||||
/// <summary>
|
||||
/// Whether our alpha is increasing or decreasing and at what time does it flip (or stop)
|
||||
/// </summary>
|
||||
private readonly Dictionary<IEntity, (bool EasingIn, TimeSpan TransitionTime)> _transitions =
|
||||
new();
|
||||
|
||||
/// <summary>
|
||||
/// How much the alpha changes per second for each pulse
|
||||
/// </summary>
|
||||
private readonly Dictionary<IEntity, float> _alphaRateOfChange = new();
|
||||
|
||||
private TimeSpan _lastTick;
|
||||
private const float MaxDist = 15.0f;
|
||||
|
||||
public override OverlaySpace Space => OverlaySpace.WorldSpace;
|
||||
public override bool RequestScreenTexture => true;
|
||||
|
||||
private TimeSpan _lastTick = default;
|
||||
|
||||
private readonly ShaderInstance _baseShader;
|
||||
private readonly Dictionary<EntityUid, (ShaderInstance shd, RadiationShaderInstance instance)> _pulses = new();
|
||||
|
||||
public RadiationPulseOverlay()
|
||||
{
|
||||
IoCManager.InjectDependencies(this);
|
||||
_lastTick = _gameTiming.CurTime;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the current color for the entity,
|
||||
/// accounting for what its alpha should be and whether it should be transitioning in or out
|
||||
/// </summary>
|
||||
/// <param name="entity"></param>
|
||||
/// <param name="elapsedTime">frametime</param>
|
||||
/// <param name="endTime"></param>
|
||||
/// <returns></returns>
|
||||
private Color GetColor(IEntity entity, float elapsedTime, TimeSpan endTime)
|
||||
{
|
||||
var currentTime = _gameTiming.CurTime;
|
||||
|
||||
// New pulse
|
||||
if (!_colors.ContainsKey(entity))
|
||||
{
|
||||
UpdateTransition(entity, currentTime, endTime);
|
||||
}
|
||||
|
||||
var currentColor = _colors[entity];
|
||||
var alphaChange = _alphaRateOfChange[entity] * elapsedTime;
|
||||
|
||||
if (!_transitions[entity].EasingIn)
|
||||
{
|
||||
alphaChange *= -1;
|
||||
}
|
||||
|
||||
if (currentTime > _transitions[entity].TransitionTime)
|
||||
{
|
||||
UpdateTransition(entity, currentTime, endTime);
|
||||
}
|
||||
|
||||
_colors[entity] = _colors[entity].WithAlpha(currentColor.A + alphaChange);
|
||||
return _colors[entity];
|
||||
}
|
||||
|
||||
private void UpdateTransition(IEntity entity, TimeSpan currentTime, TimeSpan endTime)
|
||||
{
|
||||
bool easingIn;
|
||||
TimeSpan transitionTime;
|
||||
|
||||
if (!_transitions.TryGetValue(entity, out var transition))
|
||||
{
|
||||
// Start as false because it will immediately be flipped
|
||||
easingIn = false;
|
||||
transitionTime = (endTime - currentTime) / 2 + currentTime;
|
||||
}
|
||||
else
|
||||
{
|
||||
easingIn = transition.EasingIn;
|
||||
transitionTime = endTime;
|
||||
}
|
||||
|
||||
_transitions[entity] = (!easingIn, transitionTime);
|
||||
_colors[entity] = Color.LimeGreen.WithAlpha(0.0f);
|
||||
_alphaRateOfChange[entity] = 1.0f / (float) (transitionTime - currentTime).TotalSeconds;
|
||||
_baseShader = _prototypeManager.Index<ShaderPrototype>("Radiation").Instance().Duplicate();
|
||||
}
|
||||
|
||||
protected override void Draw(in OverlayDrawArgs args)
|
||||
{
|
||||
// PVS should control the overlay pretty well so the overlay doesn't get instantiated unless we're near one...
|
||||
var playerEntity = _playerManager.LocalPlayer?.ControlledEntity;
|
||||
RadiationQuery(args.Viewport.Eye);
|
||||
|
||||
if (playerEntity == null)
|
||||
{
|
||||
if (_pulses.Count == 0)
|
||||
return;
|
||||
}
|
||||
|
||||
var radiationPulses = _entityManager
|
||||
.EntityQuery<RadiationPulseComponent>(true)
|
||||
.ToList();
|
||||
|
||||
if (radiationPulses.Count == 0)
|
||||
{
|
||||
if (ScreenTexture == null)
|
||||
return;
|
||||
}
|
||||
|
||||
var elapsedTime = (float) (_gameTiming.CurTime - _lastTick).TotalSeconds;
|
||||
_lastTick = _gameTiming.CurTime;
|
||||
|
||||
var worldHandle = args.WorldHandle;
|
||||
var viewport = _eyeManager.GetWorldViewport();
|
||||
foreach (var grid in _mapManager.FindGridsIntersecting(playerEntity.Transform.MapID, viewport))
|
||||
var viewport = args.Viewport;
|
||||
|
||||
foreach ((var shd, var instance) in _pulses.Values)
|
||||
{
|
||||
worldHandle.SetTransform(grid.WorldMatrix);
|
||||
foreach (var pulse in radiationPulses)
|
||||
{
|
||||
if (!pulse.Draw || grid.Index != pulse.Owner.Transform.GridID) continue;
|
||||
// To be clear, this needs to use "inside-viewport" pixels.
|
||||
// In other words, specifically NOT IViewportControl.WorldToScreen (which uses outer coordinates).
|
||||
var tempCoords = viewport.WorldToLocal(instance.CurrentMapCoords);
|
||||
tempCoords.Y = viewport.Size.Y - tempCoords.Y;
|
||||
shd?.SetParameter("renderScale", viewport.RenderScale);
|
||||
shd?.SetParameter("positionInput", tempCoords);
|
||||
shd?.SetParameter("range", instance.Range);
|
||||
var life = (_lastTick - instance.Start) / (instance.End - instance.Start);
|
||||
shd?.SetParameter("life", (float) life);
|
||||
|
||||
var pulseTransform = pulse.Owner.Transform;
|
||||
// There's probably a very good reason not to do this.
|
||||
// Oh well!
|
||||
shd?.SetParameter("SCREEN_TEXTURE", viewport.RenderTarget.Texture);
|
||||
|
||||
var maxVisibleCircleArea = viewport.Enlarged(pulse.Range * 64);
|
||||
if (!maxVisibleCircleArea.Contains(pulseTransform.WorldPosition)) continue;
|
||||
|
||||
worldHandle.DrawCircle(
|
||||
pulseTransform.LocalPosition,
|
||||
pulse.Range,
|
||||
GetColor(pulse.Owner, pulse.Decay ? elapsedTime : 0, pulse.EndTime));
|
||||
}
|
||||
worldHandle.UseShader(shd);
|
||||
worldHandle.DrawRect(Box2.CenteredAround(instance.CurrentMapCoords, new Vector2(instance.Range, instance.Range) * 2f), Color.White);
|
||||
}
|
||||
}
|
||||
|
||||
//Queries all pulses on the map and either adds or removes them from the list of rendered pulses based on whether they should be drawn (in range? on the same z-level/map? pulse entity still exists?)
|
||||
private void RadiationQuery(IEye? currentEye)
|
||||
{
|
||||
if (currentEye == null)
|
||||
{
|
||||
_pulses.Clear();
|
||||
return;
|
||||
}
|
||||
|
||||
_lastTick = _gameTiming.CurTime;
|
||||
|
||||
var currentEyeLoc = currentEye.Position;
|
||||
|
||||
var pulses = _entityManager.EntityQuery<RadiationPulseComponent>();
|
||||
foreach (var pulse in pulses) //Add all pulses that are not added yet but qualify
|
||||
{
|
||||
var pulseEntity = pulse.Owner;
|
||||
|
||||
if (!_pulses.Keys.Contains(pulseEntity.Uid) && PulseQualifies(pulseEntity, currentEyeLoc))
|
||||
{
|
||||
_pulses.Add(
|
||||
pulseEntity.Uid,
|
||||
(
|
||||
_baseShader.Duplicate(),
|
||||
new RadiationShaderInstance(
|
||||
pulseEntity.Transform.MapPosition.Position,
|
||||
pulse.Range,
|
||||
pulse.StartTime,
|
||||
pulse.EndTime
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
var activeShaderIds = _pulses.Keys;
|
||||
foreach (var activePulseUid in activeShaderIds) //Remove all pulses that are added and no longer qualify
|
||||
{
|
||||
if (_entityManager.TryGetEntity(activePulseUid, out var pulseEntity) &&
|
||||
PulseQualifies(pulseEntity, currentEyeLoc) &&
|
||||
pulseEntity.TryGetComponent<RadiationPulseComponent>(out var pulse))
|
||||
{
|
||||
var shaderInstance = _pulses[activePulseUid];
|
||||
shaderInstance.instance.CurrentMapCoords = pulseEntity.Transform.MapPosition.Position;
|
||||
shaderInstance.instance.Range = pulse.Range;
|
||||
} else {
|
||||
_pulses[activePulseUid].shd.Dispose();
|
||||
_pulses.Remove(activePulseUid);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private bool PulseQualifies(IEntity pulseEntity, MapCoordinates currentEyeLoc)
|
||||
{
|
||||
return pulseEntity.Transform.MapID == currentEyeLoc.MapId && pulseEntity.Transform.Coordinates.InRange(_entityManager, EntityCoordinates.FromMap(_entityManager, pulseEntity.Transform.ParentUid, currentEyeLoc), MaxDist);
|
||||
}
|
||||
|
||||
private sealed record RadiationShaderInstance(Vector2 CurrentMapCoords, float Range, TimeSpan Start, TimeSpan End)
|
||||
{
|
||||
public Vector2 CurrentMapCoords = CurrentMapCoords;
|
||||
public float Range = Range;
|
||||
public TimeSpan Start = Start;
|
||||
public TimeSpan End = End;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user