diff --git a/Content.Client/Drugs/DrugOverlaySystem.cs b/Content.Client/Drugs/DrugOverlaySystem.cs new file mode 100644 index 0000000000..7be63b4c50 --- /dev/null +++ b/Content.Client/Drugs/DrugOverlaySystem.cs @@ -0,0 +1,58 @@ +using Content.Shared.Drugs; +using Robust.Client.GameObjects; +using Robust.Client.Graphics; +using Robust.Client.Player; + +namespace Content.Client.Drugs; + +/// +/// System to handle drug related overlays. +/// +public sealed class DrugOverlaySystem : EntitySystem +{ + [Dependency] private readonly IPlayerManager _player = default!; + [Dependency] private readonly IOverlayManager _overlayMan = default!; + + private RainbowOverlay _overlay = default!; + + public static string RainbowKey = "SeeingRainbows"; + + public override void Initialize() + { + base.Initialize(); + + SubscribeLocalEvent(OnInit); + SubscribeLocalEvent(OnShutdown); + + SubscribeLocalEvent(OnPlayerAttached); + SubscribeLocalEvent(OnPlayerDetached); + + _overlay = new(); + } + + private void OnPlayerAttached(EntityUid uid, SeeingRainbowsComponent component, PlayerAttachedEvent args) + { + _overlayMan.AddOverlay(_overlay); + } + + private void OnPlayerDetached(EntityUid uid, SeeingRainbowsComponent component, PlayerDetachedEvent args) + { + _overlay.Intoxication = 0; + _overlayMan.RemoveOverlay(_overlay); + } + + private void OnInit(EntityUid uid, SeeingRainbowsComponent component, ComponentInit args) + { + if (_player.LocalPlayer?.ControlledEntity == uid) + _overlayMan.AddOverlay(_overlay); + } + + private void OnShutdown(EntityUid uid, SeeingRainbowsComponent component, ComponentShutdown args) + { + if (_player.LocalPlayer?.ControlledEntity == uid) + { + _overlay.Intoxication = 0; + _overlayMan.RemoveOverlay(_overlay); + } + } +} diff --git a/Content.Client/Drugs/RainbowOverlay.cs b/Content.Client/Drugs/RainbowOverlay.cs new file mode 100644 index 0000000000..2111ceba13 --- /dev/null +++ b/Content.Client/Drugs/RainbowOverlay.cs @@ -0,0 +1,70 @@ +using Content.Shared.Drugs; +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.Drugs; + +public sealed class RainbowOverlay : 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 _rainbowShader; + + public float Intoxication = 0.0f; + + private const float VisualThreshold = 10.0f; + private const float PowerDivisor = 250.0f; + + private float EffectScale => Math.Clamp((Intoxication - VisualThreshold) / PowerDivisor, 0.0f, 1.0f); + + public RainbowOverlay() + { + IoCManager.InjectDependencies(this); + _rainbowShader = _prototypeManager.Index("Rainbow").InstanceUnique(); + } + + protected override void FrameUpdate(FrameEventArgs args) + { + var playerEntity = _playerManager.LocalPlayer?.ControlledEntity; + + if (playerEntity == null) + return; + + if (!_entityManager.HasComponent(playerEntity) + || !_entityManager.TryGetComponent(playerEntity, out var status)) + return; + + var statusSys = _sysMan.GetEntitySystem(); + if (!statusSys.TryGetTime(playerEntity.Value, DrugOverlaySystem.RainbowKey, out var time, status)) + return; + + var timeLeft = (float) (time.Value.Item2 - time.Value.Item1).TotalSeconds; + Intoxication += (timeLeft - Intoxication) * args.DeltaSeconds / 16f; + } + + protected override bool BeforeDraw(in OverlayDrawArgs args) + { + return EffectScale > 0; + } + + protected override void Draw(in OverlayDrawArgs args) + { + if (ScreenTexture == null) + return; + + var handle = args.WorldHandle; + _rainbowShader.SetParameter("SCREEN_TEXTURE", ScreenTexture); + _rainbowShader.SetParameter("effectScale", EffectScale); + handle.UseShader(_rainbowShader); + handle.DrawRect(args.WorldBounds, Color.White); + } +} diff --git a/Content.Shared/Drugs/SeeingRainbowsComponent.cs b/Content.Shared/Drugs/SeeingRainbowsComponent.cs new file mode 100644 index 0000000000..50fc25494f --- /dev/null +++ b/Content.Shared/Drugs/SeeingRainbowsComponent.cs @@ -0,0 +1,9 @@ +using Robust.Shared.GameStates; + +namespace Content.Shared.Drugs; + +/// +/// Exists for use as a status effect. Adds a shader to the client that scales with the effect duration. +/// +[RegisterComponent, NetworkedComponent] +public sealed class SeeingRainbowsComponent : Component { } diff --git a/Resources/Prototypes/Entities/Mobs/Species/human.yml b/Resources/Prototypes/Entities/Mobs/Species/human.yml index 7496fb0dbe..020bcf35cc 100644 --- a/Resources/Prototypes/Entities/Mobs/Species/human.yml +++ b/Resources/Prototypes/Entities/Mobs/Species/human.yml @@ -80,6 +80,7 @@ - KnockedDown - SlowedDown - Stutter + - SeeingRainbows - Electrocution - PressureImmunity - Muted diff --git a/Resources/Prototypes/Reagents/narcotics.yml b/Resources/Prototypes/Reagents/narcotics.yml index 36eebd2141..17099952dd 100644 --- a/Resources/Prototypes/Reagents/narcotics.yml +++ b/Resources/Prototypes/Reagents/narcotics.yml @@ -89,6 +89,15 @@ amount: -5 - !type:PlantAdjustHealth amount: -1 + metabolisms: + Narcotic: + effects: + - !type:GenericStatusEffect + key: SeeingRainbows + component: SeeingRainbows + type: Add + time: 5 + refresh: false - type: reagent id: THCOil @@ -97,6 +106,15 @@ desc: reagent-desc-thc-oil physicalDesc: reagent-physical-desc-skunky color: "#DAA520" + metabolisms: + Narcotic: + effects: + - !type:GenericStatusEffect + key: SeeingRainbows + component: SeeingRainbows + type: Add + time: 5 + refresh: false - type: reagent id: Nicotine @@ -124,6 +142,15 @@ desc: reagent-desc-space-drugs physicalDesc: reagent-physical-desc-syrupy color: "#63806e" + metabolisms: + Narcotic: + effects: + - !type:GenericStatusEffect + key: SeeingRainbows + component: SeeingRainbows + type: Add + time: 5 + refresh: false # Probably replace this one with sleeping chem when putting someone in a comatose state is easier - type: reagent diff --git a/Resources/Prototypes/Shaders/shaders.yml b/Resources/Prototypes/Shaders/shaders.yml index c399a071f3..923a00ca2d 100644 --- a/Resources/Prototypes/Shaders/shaders.yml +++ b/Resources/Prototypes/Shaders/shaders.yml @@ -31,6 +31,11 @@ positionInput: 0,0 life: 0 +- type: shader + id: Rainbow + kind: source + path: "/Textures/Shaders/rainbow.swsl" + - type: shader id: CameraStatic kind: source diff --git a/Resources/Prototypes/status_effects.yml b/Resources/Prototypes/status_effects.yml index 9e36c423da..bee9c9dc0a 100644 --- a/Resources/Prototypes/status_effects.yml +++ b/Resources/Prototypes/status_effects.yml @@ -22,6 +22,9 @@ - type: statusEffect id: AllCaps +- type: statusEffect + id: SeeingRainbows + - type: statusEffect id: Electrocution diff --git a/Resources/Textures/Shaders/rainbow.swsl b/Resources/Textures/Shaders/rainbow.swsl new file mode 100644 index 0000000000..67b2b78686 --- /dev/null +++ b/Resources/Textures/Shaders/rainbow.swsl @@ -0,0 +1,63 @@ +uniform sampler2D SCREEN_TEXTURE; +uniform highp float effectScale; + +const highp float TimeScale = 0.15; +const highp float DistortionScale = 0.02; // how strongly to warp the screen +const highp float NoiseScale = 4.0; // scale of the random noise +const highp float MaxColorMix = 0.05; // rainbow effect strength. at 1.0, you wont be able to see the screen anymore + +// hash(), noise(), and hsv2rgb_smooth() were converted from shadertoy examples, which were released under the MIT License: +// The MIT License +// Copyright © 2013 Inigo Quilez +// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// https://www.youtube.com/c/InigoQuilez +// https://iquilezles.org +highp vec2 hash( highp vec2 p ) { + p = vec2( dot(p,vec2(127.1,311.7)), dot(p,vec2(269.5,183.3)) ); + return -1.0 + 2.0*fract(sin(p)*43758.5453123); +} + +highp float noise( highp vec2 p ) { + const highp float K1 = 0.366025404; + const highp float K2 = 0.211324865; + + highp vec2 i = floor( p + (p.x+p.y)*K1 ); + highp vec2 a = p - i + (i.x+i.y)*K2; + highp float m = step(a.y,a.x); + highp vec2 o = vec2(m,1.0-m); + highp vec2 b = a - o + K2; + highp vec2 c = a - 1.0 + 2.0*K2; + highp vec3 h = max( 0.5-vec3(dot(a,a), dot(b,b), dot(c,c) ), 0.0 ); + highp vec3 n = h*h*h*h*vec3( dot(a,hash(i+0.0)), dot(b,hash(i+o)), dot(c,hash(i+1.0))); + return dot( n, vec3(70.0) ); +} + +highp vec3 hsv2rgb_smooth( highp vec3 c ) { + highp vec3 rgb = clamp( abs(mod(c.x*6.0+vec3(0.0,4.0,2.0),6.0)-3.0)-1.0, 0.0, 1.0 ); + rgb = rgb*rgb*(3.0-2.0*rgb); + return c.z * mix( vec3(1.0), rgb, c.y); +} + + +highp float mixNoise(highp vec2 point, highp float phase) { + highp float time = TIME * TimeScale + phase; + highp float a = noise( NoiseScale * point - time); + highp float b = noise( NoiseScale * point + time ); + return mix(a,b,0.5); +} + +void fragment() { + highp vec2 coord = FRAGCOORD.xy * SCREEN_PIXEL_SIZE.xy; + + // warp the screen. + highp vec2 offset = vec2(mixNoise(coord, 0.), mixNoise(coord, 5.)); + COLOR = zTextureSpec(SCREEN_TEXTURE, coord + effectScale * DistortionScale * offset); + + // apply rainbow effect. + highp float hue = 1. + mixNoise(coord, 10.); + highp vec3 color = hsv2rgb_smooth(vec3(hue,1.0,1.0)); + COLOR.xyz = mix(COLOR.xyz, color, MaxColorMix * effectScale * effectScale); +} + + +