Drunk Shader (#8910)
Co-authored-by: Kara D <lunarautomaton6@gmail.com> Co-authored-by: metalgearsloth <comedian_vs_clown@hotmail.com>
This commit is contained in:
81
Content.Client/Drunk/DrunkOverlay.cs
Normal file
81
Content.Client/Drunk/DrunkOverlay.cs
Normal file
@@ -0,0 +1,81 @@
|
||||
using Content.Shared.Drunk;
|
||||
using Content.Shared.StatusEffect;
|
||||
using Robust.Client.Graphics;
|
||||
using Robust.Client.Player;
|
||||
using Robust.Shared.Enums;
|
||||
using Robust.Shared.Prototypes;
|
||||
using Robust.Shared.Timing;
|
||||
|
||||
namespace Content.Client.Drunk;
|
||||
|
||||
public sealed class DrunkOverlay : Overlay
|
||||
{
|
||||
[Dependency] private readonly IEntityManager _entityManager = default!;
|
||||
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
|
||||
[Dependency] private readonly IPlayerManager _playerManager = default!;
|
||||
[Dependency] private readonly IEntitySystemManager _sysMan = default!;
|
||||
|
||||
public override OverlaySpace Space => OverlaySpace.WorldSpace;
|
||||
public override bool RequestScreenTexture => true;
|
||||
private readonly ShaderInstance _drunkShader;
|
||||
|
||||
public float CurrentBoozePower = 0.0f;
|
||||
|
||||
private const float VisualThreshold = 10.0f;
|
||||
private const float PowerDivisor = 250.0f;
|
||||
|
||||
private float _visualScale = 0;
|
||||
|
||||
public DrunkOverlay()
|
||||
{
|
||||
IoCManager.InjectDependencies(this);
|
||||
_drunkShader = _prototypeManager.Index<ShaderPrototype>("Drunk").InstanceUnique();
|
||||
}
|
||||
|
||||
protected override void FrameUpdate(FrameEventArgs args)
|
||||
{
|
||||
var playerEntity = _playerManager.LocalPlayer?.ControlledEntity;
|
||||
|
||||
if (playerEntity == null)
|
||||
return;
|
||||
|
||||
if (!_entityManager.HasComponent<DrunkComponent>(playerEntity)
|
||||
|| !_entityManager.TryGetComponent<StatusEffectsComponent>(playerEntity, out var status))
|
||||
return;
|
||||
|
||||
var statusSys = _sysMan.GetEntitySystem<StatusEffectsSystem>();
|
||||
if (!statusSys.TryGetTime(playerEntity.Value, SharedDrunkSystem.DrunkKey, out var time, status))
|
||||
return;
|
||||
|
||||
var timeLeft = (float) (time.Value.Item2 - time.Value.Item1).TotalSeconds;
|
||||
CurrentBoozePower += (timeLeft - CurrentBoozePower) * args.DeltaSeconds / 16f;
|
||||
}
|
||||
|
||||
protected override bool BeforeDraw(in OverlayDrawArgs args)
|
||||
{
|
||||
_visualScale = BoozePowerToVisual(CurrentBoozePower);
|
||||
return _visualScale > 0;
|
||||
}
|
||||
|
||||
protected override void Draw(in OverlayDrawArgs args)
|
||||
{
|
||||
if (ScreenTexture == null)
|
||||
return;
|
||||
|
||||
var handle = args.WorldHandle;
|
||||
_drunkShader.SetParameter("SCREEN_TEXTURE", ScreenTexture);
|
||||
_drunkShader.SetParameter("boozePower", _visualScale);
|
||||
handle.UseShader(_drunkShader);
|
||||
handle.DrawRect(args.WorldBounds, Color.White);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts the # of seconds the drunk effect lasts for (booze power) to a percentage
|
||||
/// used by the actual shader.
|
||||
/// </summary>
|
||||
/// <param name="boozePower"></param>
|
||||
private float BoozePowerToVisual(float boozePower)
|
||||
{
|
||||
return Math.Clamp((boozePower - VisualThreshold) / PowerDivisor, 0.0f, 1.0f);
|
||||
}
|
||||
}
|
||||
53
Content.Client/Drunk/DrunkSystem.cs
Normal file
53
Content.Client/Drunk/DrunkSystem.cs
Normal file
@@ -0,0 +1,53 @@
|
||||
using Content.Shared.Drunk;
|
||||
using Robust.Client.GameObjects;
|
||||
using Robust.Client.Graphics;
|
||||
using Robust.Client.Player;
|
||||
|
||||
namespace Content.Client.Drunk;
|
||||
|
||||
public sealed class DrunkSystem : SharedDrunkSystem
|
||||
{
|
||||
[Dependency] private readonly IPlayerManager _player = default!;
|
||||
[Dependency] private readonly IOverlayManager _overlayMan = default!;
|
||||
|
||||
private DrunkOverlay _overlay = default!;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
SubscribeLocalEvent<DrunkComponent, ComponentInit>(OnDrunkInit);
|
||||
SubscribeLocalEvent<DrunkComponent, ComponentShutdown>(OnDrunkShutdown);
|
||||
|
||||
SubscribeLocalEvent<DrunkComponent, PlayerAttachedEvent>(OnPlayerAttached);
|
||||
SubscribeLocalEvent<DrunkComponent, PlayerDetachedEvent>(OnPlayerDetached);
|
||||
|
||||
_overlay = new();
|
||||
}
|
||||
|
||||
private void OnPlayerAttached(EntityUid uid, DrunkComponent component, PlayerAttachedEvent args)
|
||||
{
|
||||
_overlayMan.AddOverlay(_overlay);
|
||||
}
|
||||
|
||||
private void OnPlayerDetached(EntityUid uid, DrunkComponent component, PlayerDetachedEvent args)
|
||||
{
|
||||
_overlay.CurrentBoozePower = 0;
|
||||
_overlayMan.RemoveOverlay(_overlay);
|
||||
}
|
||||
|
||||
private void OnDrunkInit(EntityUid uid, DrunkComponent component, ComponentInit args)
|
||||
{
|
||||
if (_player.LocalPlayer?.ControlledEntity == uid)
|
||||
_overlayMan.AddOverlay(_overlay);
|
||||
}
|
||||
|
||||
private void OnDrunkShutdown(EntityUid uid, DrunkComponent component, ComponentShutdown args)
|
||||
{
|
||||
if (_player.LocalPlayer?.ControlledEntity == uid)
|
||||
{
|
||||
_overlay.CurrentBoozePower = 0;
|
||||
_overlayMan.RemoveOverlay(_overlay);
|
||||
}
|
||||
}
|
||||
}
|
||||
7
Content.Client/Speech/EntitySystems/SlurredSystem.cs
Normal file
7
Content.Client/Speech/EntitySystems/SlurredSystem.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
using Content.Shared.Speech.EntitySystems;
|
||||
|
||||
namespace Content.Client.Speech.EntitySystems;
|
||||
|
||||
public sealed class SlurredSystem : SharedSlurredSystem
|
||||
{
|
||||
}
|
||||
Reference in New Issue
Block a user