Cardboard Box and Stealth Components (#11569)

This commit is contained in:
keronshb
2022-10-09 18:17:53 -04:00
committed by GitHub
parent eadf01f323
commit 386c7f9223
16 changed files with 643 additions and 2 deletions

View File

@@ -0,0 +1,60 @@
using Content.Shared.CardboardBox;
using Content.Shared.CardboardBox.Components;
using Content.Shared.Examine;
using Content.Shared.Movement.Components;
using Robust.Client.GameObjects;
namespace Content.Client.CardboardBox;
public sealed class CardboardBoxSystem : SharedCardboardBoxSystem
{
[Dependency] private readonly EntityLookupSystem _entityLookup = default!;
public override void Initialize()
{
base.Initialize();
SubscribeNetworkEvent<PlayBoxEffectMessage>(OnBoxEffect);
}
private void OnBoxEffect(PlayBoxEffectMessage msg)
{
if (!TryComp<CardboardBoxComponent>(msg.Source, out var box))
return;
var xformQuery = GetEntityQuery<TransformComponent>();
if (!xformQuery.TryGetComponent(msg.Source, out var xform))
return;
var sourcePos = xform.MapPosition;
//Any mob that can move should be surprised?
//God mind rework needs to come faster so it can just check for mind
//TODO: Replace with Mind Query when mind rework is in.
var mobMoverEntities = new HashSet<EntityUid>();
//Filter out entities in range to see that they're a mob and add them to the mobMoverEntities hash for faster lookup
foreach (var moverComp in _entityLookup.GetComponentsInRange<MobMoverComponent>(xform.Coordinates, box.Distance))
{
if (moverComp.Owner == msg.Mover)
continue;
mobMoverEntities.Add(moverComp.Owner);
}
//Play the effect for the mobs as long as they can see the box and are in range.
foreach (var mob in mobMoverEntities)
{
if (!xformQuery.TryGetComponent(mob, out var moverTransform) || !ExamineSystemShared.InRangeUnOccluded(sourcePos, moverTransform.MapPosition, box.Distance, null))
continue;
var ent = Spawn(box.Effect, moverTransform.MapPosition);
if (!xformQuery.TryGetComponent(ent, out var entTransform) || !TryComp<SpriteComponent>(ent, out var sprite))
continue;
sprite.Offset = new Vector2(0, 1);
entTransform.AttachParent(mob);
}
}
}

View File

@@ -0,0 +1,74 @@
using Content.Client.Interactable.Components;
using Content.Shared.Stealth;
using Content.Shared.Stealth.Components;
using Robust.Client.GameObjects;
using Robust.Client.Graphics;
using Robust.Shared.Prototypes;
namespace Content.Client.Stealth;
public sealed class StealthSystem : SharedStealthSystem
{
[Dependency] private readonly IPrototypeManager _protoMan = default!;
private ShaderInstance _shader = default!;
public override void Initialize()
{
base.Initialize();
_shader = _protoMan.Index<ShaderPrototype>("Stealth").InstanceUnique();
SubscribeLocalEvent<StealthComponent, ComponentRemove>(OnRemove);
SubscribeLocalEvent<StealthComponent, BeforePostShaderRenderEvent>(OnShaderRender);
}
protected override void OnInit(EntityUid uid, StealthComponent component, ComponentInit args)
{
base.OnInit(uid, component, args);
if (!TryComp(uid, out SpriteComponent? sprite))
return;
sprite.PostShader = _shader;
sprite.GetScreenTexture = true;
sprite.RaiseShaderEvent = true;
if (TryComp(uid, out InteractionOutlineComponent? outline))
{
RemComp(uid, outline);
component.HadOutline = true;
}
}
private void OnRemove(EntityUid uid, StealthComponent component, ComponentRemove args)
{
if (!TryComp(uid, out SpriteComponent? sprite))
return;
sprite.PostShader = null;
sprite.GetScreenTexture = false;
sprite.RaiseShaderEvent = false;
sprite.Color = Color.White;
if (component.HadOutline)
AddComp<InteractionOutlineComponent>(uid);
}
private void OnShaderRender(EntityUid uid, StealthComponent component, BeforePostShaderRenderEvent args)
{
// Distortion effect uses screen coordinates. If a player moves, the entities appear to move on screen. this
// makes the distortion very noticeable.
// So we need to use relative screen coordinates. The reference frame we use is the parent's position on screen.
// this ensures that if the Stealth is not moving relative to the parent, its relative screen position remains
// unchanged.
var parentXform = Transform(Transform(uid).ParentUid);
var reference = args.Viewport.WorldToLocal(parentXform.WorldPosition);
var visibility = GetVisibility(uid, component);
_shader.SetParameter("reference", reference);
_shader.SetParameter("visibility", visibility);
visibility = MathF.Max(0, visibility);
args.Sprite.Color = new Color(visibility, visibility, 1, 1);
}
}

View File

@@ -0,0 +1,61 @@
using System.Linq;
using Content.Shared.CardboardBox.Components;
using Content.Server.Storage.Components;
using Content.Shared.CardboardBox;
using Content.Shared.Movement.Components;
using Content.Shared.Movement.Systems;
using Robust.Shared.Player;
using Robust.Shared.Timing;
namespace Content.Server.CardboardBox;
public sealed class CardboardBoxSystem : SharedCardboardBoxSystem
{
[Dependency] private readonly SharedAudioSystem _audio = default!;
[Dependency] private readonly SharedMoverController _mover = default!;
[Dependency] private readonly IGameTiming _timing = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<CardboardBoxComponent, StorageBeforeCloseEvent>(OnBeforeStorageClosed);
SubscribeLocalEvent<CardboardBoxComponent, StorageAfterOpenEvent>(AfterStorageOpen);
}
private void OnBeforeStorageClosed(EntityUid uid, CardboardBoxComponent component, StorageBeforeCloseEvent args)
{
var mobMover = args.Contents.Where(HasComp<MobMoverComponent>).ToList();
//Grab the first mob to set as the mover and to prevent other mobs from entering.
foreach (var mover in mobMover)
{
//Set the movement relay for the box as the first mob
if (component.Mover == null && args.Contents.Contains(mover))
{
var relay = EnsureComp<RelayInputMoverComponent>(mover);
_mover.SetRelay(mover, uid, relay);
component.Mover = mover;
}
if (mover != component.Mover)
args.Contents.Remove(mover);
}
}
private void AfterStorageOpen(EntityUid uid, CardboardBoxComponent component, StorageAfterOpenEvent args)
{
//Remove the mover after the box is opened and play the effect if it hasn't been played yet.
if (component.Mover != null)
{
RemComp<RelayInputMoverComponent>(component.Mover.Value);
if (_timing.CurTime > component.EffectCooldown)
{
RaiseNetworkEvent(new PlayBoxEffectMessage(component.Owner, component.Mover.Value), Filter.PvsExcept(component.Owner));
_audio.PlayPvs(component.EffectSound, component.Owner);
component.EffectCooldown = _timing.CurTime + CardboardBoxComponent.MaxEffectCooldown;
}
}
component.Mover = null;
}
}

View File

@@ -0,0 +1,8 @@
using Content.Shared.Stealth;
namespace Content.Server.Stealth;
public sealed class StealthSystem : SharedStealthSystem
{
}

View File

@@ -37,6 +37,15 @@ public sealed class EntityStorageComponent : Component, IGasMixtureHolder
[DataField("isCollidableWhenOpen")]
public bool IsCollidableWhenOpen;
/// <summary>
/// If true, it opens the storage when the entity inside of it moves
/// If false, it prevents the storage from opening when the entity inside of it moves.
/// This is for objects that you want the player to move while inside, like large cardboard boxes, without opening the storage.
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
[DataField("openOnMove")]
public bool OpenOnMove = true;
//The offset for where items are emptied/vacuumed for the EntityStorage.
[DataField("enteringOffset")]
public Vector2 EnteringOffset = new(0, 0);

View File

@@ -149,8 +149,11 @@ namespace Content.Server.Storage.EntitySystems
return;
component.LastInternalOpenAttempt = _gameTiming.CurTime;
if (component.OpenOnMove)
{
_entityStorage.TryOpenStorage(args.Entity, component.Owner);
}
}
private void AddToggleOpenVerb(EntityUid uid, EntityStorageComponent component, GetVerbsEvent<InteractionVerb> args)

View File

@@ -0,0 +1,68 @@
using Robust.Shared.Audio;
using Robust.Shared.GameStates;
using Robust.Shared.Serialization;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom;
namespace Content.Shared.CardboardBox.Components;
/// <summary>
/// Allows a user to control an EntityStorage entity while inside of it.
/// Used for big cardboard box entities.
/// </summary>
[RegisterComponent, NetworkedComponent]
public sealed class CardboardBoxComponent : Component
{
/// <summary>
/// The person in control of this box
/// </summary>
[ViewVariables]
[DataField("mover")]
public EntityUid? Mover;
/// <summary>
/// The entity used for the box opening effect
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
[DataField("effect")]
public string Effect = "Exclamation";
/// <summary>
/// Sound played upon effect creation
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
[DataField("effectSound")]
public SoundSpecifier? EffectSound;
/// <summary>
/// How far should the box opening effect go?
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
[DataField("distance")]
public float Distance = 6f;
/// <summary>
/// Current time + max effect cooldown to check to see if effect can play again
/// Prevents effect spam
/// </summary>
[DataField("effectCooldown", customTypeSerializer: typeof(TimeOffsetSerializer))]
public TimeSpan EffectCooldown = TimeSpan.FromSeconds(1f);
/// <summary>
/// How much time should pass + current time until the effect plays again
/// Prevents effect spam
/// </summary>
[DataField("maxEffectCooldown", customTypeSerializer: typeof(TimeOffsetSerializer))]
public static readonly TimeSpan MaxEffectCooldown = TimeSpan.FromSeconds(5f);
}
[Serializable, NetSerializable]
public sealed class PlayBoxEffectMessage : EntityEventArgs
{
public EntityUid Source;
public EntityUid Mover;
public PlayBoxEffectMessage(EntityUid source, EntityUid mover)
{
Source = source;
Mover = mover;
}
}

View File

@@ -0,0 +1,6 @@
namespace Content.Shared.CardboardBox;
public abstract class SharedCardboardBoxSystem : EntitySystem
{
}

View File

@@ -0,0 +1,60 @@
using Robust.Shared.GameStates;
using Robust.Shared.Serialization;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom;
namespace Content.Shared.Stealth.Components;
/// <summary>
/// Add this component to an entity that you want to be cloaked.
/// It overlays a shader on the entity to give them an invisibility cloaked effect
/// It also turns the entity invisible
/// </summary>
[RegisterComponent, NetworkedComponent]
[Access(typeof(SharedStealthSystem))]
public sealed class StealthComponent : Component
{
/// <summary>
/// Whether or not the entity previously had an interaction outline prior to cloaking.
/// </summary>
[DataField("hadOutline")]
public bool HadOutline;
/// <summary>
/// Last set level of visibility. Ranges from 1 (fully visible) and -1 (fully hidden). To get the actual current
/// visibility, use <see cref="SharedStealthSystem.GetVisibility(EntityUid, StealthComponent?)"/>
/// </summary>
[DataField("lastVisibility")]
[Access(typeof(SharedStealthSystem), Other = AccessPermissions.None)]
public float LastVisibility;
/// <summary>
/// Time at which <see cref="LastVisibility"/> was set. Null implies the entity is currently paused and not
/// accumulating any visibility change.
/// </summary>
[DataField("lastUpdate", customTypeSerializer:typeof(TimeOffsetSerializer))]
public TimeSpan? LastUpdated;
/// <summary>
/// Rate that effects how fast an entity's visibility passively changes.
/// </summary>
[DataField("passiveVisibilityRate")]
public readonly float PassiveVisibilityRate = -0.15f;
/// <summary>
/// Rate for movement induced visibility changes. Scales with distance moved.
/// </summary>
[DataField("movementVisibilityRate")]
public readonly float MovementVisibilityRate = 0.2f;
}
[Serializable, NetSerializable]
public sealed class StealthComponentState : ComponentState
{
public float Visibility;
public TimeSpan? LastUpdated;
public StealthComponentState(float stealthLevel, TimeSpan? lastUpdated)
{
Visibility = stealthLevel;
LastUpdated = lastUpdated;
}
}

View File

@@ -0,0 +1,122 @@
using Content.Shared.Stealth.Components;
using Robust.Shared.GameStates;
using Robust.Shared.Timing;
namespace Content.Shared.Stealth;
public abstract class SharedStealthSystem : EntitySystem
{
[Dependency] private readonly IGameTiming _timing = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<StealthComponent, ComponentGetState>(OnStealthGetState);
SubscribeLocalEvent<StealthComponent, ComponentHandleState>(OnStealthHandleState);
SubscribeLocalEvent<StealthComponent, MoveEvent>(OnMove);
SubscribeLocalEvent<StealthComponent, EntityPausedEvent>(OnPaused);
SubscribeLocalEvent<StealthComponent, ComponentInit>(OnInit);
}
private void OnPaused(EntityUid uid, StealthComponent component, EntityPausedEvent args)
{
if (args.Paused)
{
component.LastVisibility = GetVisibility(uid, component);
component.LastUpdated = null;
}
else
{
component.LastUpdated = _timing.CurTime;
}
Dirty(component);
}
protected virtual void OnInit(EntityUid uid, StealthComponent component, ComponentInit args)
{
if (component.LastUpdated != null || Paused(uid))
return;
component.LastUpdated = _timing.CurTime;
}
private void OnStealthGetState(EntityUid uid, StealthComponent component, ref ComponentGetState args)
{
args.State = new StealthComponentState(component.LastVisibility, component.LastUpdated);
}
private void OnStealthHandleState(EntityUid uid, StealthComponent component, ref ComponentHandleState args)
{
if (args.Current is not StealthComponentState cast)
return;
component.LastVisibility = cast.Visibility;
component.LastUpdated = cast.LastUpdated;
}
private void OnMove(EntityUid uid, StealthComponent component, ref MoveEvent args)
{
if (args.FromStateHandling)
return;
if (args.NewPosition.EntityId != args.OldPosition.EntityId)
return;
var delta = component.MovementVisibilityRate * (args.NewPosition.Position - args.OldPosition.Position).Length;
ModifyVisibility(uid, delta, component);
}
/// <summary>
/// Modifies the visibility based on the delta provided.
/// </summary>
/// <param name="delta">The delta to be used in visibility calculation.</param>
public void ModifyVisibility(EntityUid uid, float delta, StealthComponent? component = null)
{
if (delta == 0 || !Resolve(uid, ref component))
return;
if (component.LastUpdated != null)
{
component.LastVisibility = GetVisibility(uid, component);
component.LastUpdated = _timing.CurTime;
}
component.LastVisibility = Math.Clamp(component.LastVisibility + delta, -1f, 1f);
Dirty(component);
}
/// <summary>
/// Sets the visibility directly with no modifications
/// </summary>
/// <param name="value">The value to set the visibility to. -1 is fully invisible, 1 is fully visible</param>
public void SetVisibility(EntityUid uid, float value, StealthComponent? component = null)
{
if (!Resolve(uid, ref component))
return;
component.LastVisibility = value;
if (component.LastUpdated != null)
component.LastUpdated = _timing.CurTime;
Dirty(component);
}
/// <summary>
/// Gets the current visibility from the <see cref="StealthComponent"/>
/// Use this instead of getting LastVisibility from the component directly.
/// </summary>
/// <returns>Returns a calculation that accounts for any stealth change that happened since last update, otherwise returns based on if it can resolve the component.</returns>
public float GetVisibility(EntityUid uid, StealthComponent? component = null)
{
if (!Resolve(uid, ref component))
return 1;
if (component.LastUpdated == null)
return component.LastVisibility;
var deltaTime = _timing.CurTime - component.LastUpdated.Value;
return Math.Clamp(component.LastVisibility + (float) deltaTime.TotalSeconds * component.PassiveVisibilityRate, -1f, 1f);
}
}

Binary file not shown.

Binary file not shown.

View File

@@ -41,3 +41,15 @@ The following sounds are taken from TGstation github (licensed under CC by 3.0):
demon_dies.ogg: taken at https://github.com/tgstation/tgstation/commit/d4f678a1772007ff8d7eddd21cf7218c8e07bfc0
pop.ogg licensed under CC0 1.0 by mirrorcult
box_deploy.ogg and chime.ogg taken from Citadel Station at commit: https://github.com/Citadel-Station-13/Citadel-Station-13/commit/b604390f334343be80045d955705cf48ee056c61
- files: ["box_deploy.ogg"]
license: "CC-BY-NC-SA-3.0"
copyright: "box_deploy.ogg taken from Citadel Station."
source: "https://github.com/Citadel-Station-13/Citadel-Station-13/commit/b604390f334343be80045d955705cf48ee056c61"
- files: ["chime.ogg"]
license: "CC-BY-NC-SA-3.0"
copyright: "chime.ogg taken from Citadel Station."
source: "https://github.com/Citadel-Station-13/Citadel-Station-13/commit/b604390f334343be80045d955705cf48ee056c61"

View File

@@ -0,0 +1,115 @@
- type: entity
id: BaseBigBox
name: cardboard box
description: Huh? Just a box...
components:
- type: Transform
noRot: true
- type: Clickable
- type: Physics
bodyType: KinematicController
- type: Fixtures
fixtures:
- shape:
!type:PhysShapeAabb
bounds: "-0.4,-0.4,0.4,0.4"
mask:
- MobMask
layer:
- SlipLayer
hard: true
- type: Pullable
- type: CardboardBox
effectSound: /Audio/Effects/chime.ogg
- type: InputMover
- type: EntityStorage
isCollidableWhenOpen: false
openOnMove: false
airtight: false
capacity: 4 #4 Entities seems like a nice comfy fit for a cardboard box.
- type: ContainerContainer
containers:
entity_storage: !type:Container
- type: Sprite
noRot: true
netsync: false
sprite: Structures/Storage/closet.rsi
layers:
- state: cardboard
- state: cardboard_open
map: ["enum.StorageVisualLayers.Door"]
- type: Appearance
visuals:
- type: StorageVisualizer
state: cardboard
state_open: cardboard_open
- type: Tag
tags:
- DoorBumpOpener
- type: entity
id: StealthBox
parent: BaseBigBox
name: cardboard box #it's still just a box
description: Kept ya waiting, huh?
components:
- type: Sprite
noRot: true
netsync: false
sprite: Structures/Storage/closet.rsi
layers:
- state: agentbox
- state: cardboard_open
map: ["enum.StorageVisualLayers.Door"]
- type: Appearance
visuals:
- type: StorageVisualizer
state: agentbox
state_open: cardboard_open
- type: Stealth
passiveVisibilityRate: -0.24
movementVisibilityRate: 0.10
#For admin spawning only
- type: entity
id: GhostBox
parent: StealthBox
name: ghost box
description: Beware!
components:
- type: Fixtures
fixtures:
- shape:
!type:PhysShapeAabb
bounds: "-0.4,-0.4,0.4,0.4"
mask:
- MobMask
layer:
- SlipLayer
hard: false #It's a ghostly box, it can go through walls
- type: EntityStorage
isCollidableWhenOpen: false
openOnMove: false
airtight: true #whoever's inside should probably breath
#Exclamation effect for box opening
- type: entity
id: Exclamation
name: exclamation
noSpawn: true
save: false
components:
- type: Transform
noRot: true
- type: Sprite
sprite: Structures/Storage/closet.rsi
drawdepth: Effects
noRot: true
netsync: false
layers:
- state: "cardboard_special"
- type: TimedDespawn
lifetime: 1
- type: Tag
tags:
- HideContextMenu

View File

@@ -70,3 +70,9 @@
id: Dim
kind: source
path: "/Textures/Shaders/dim.swsl"
# cloaking distortion effect
- type: shader
id: Stealth
kind: source
path: "/Textures/Shaders/stealth.swsl"

View File

@@ -0,0 +1,37 @@
light_mode unshaded;
uniform sampler2D SCREEN_TEXTURE;
uniform highp float visibility; // number between -1 and 1
uniform mediump vec2 reference;
const mediump float time_scale = 0.25;
const mediump float distance_scale = 0.125;
void fragment() {
vec4 sprite = zTexture(UV);
if (sprite.a == 0.0) {
discard;
}
// get distortion magnitude. hand crafted from a random jumble of trig functions
vec2 coords = (FRAGCOORD.xy + reference) * distance_scale;
float w = sin(TIME + (coords.x + coords.y + 2.0*sin(TIME*time_scale) * sin(TIME*time_scale + coords.x - coords.y)) );
// visualize distortion via:
// COLOR = vec4(w,w,w,1);
w *= (3 + visibility * 2);
vec4 background = zTextureSpec(SCREEN_TEXTURE, ( FRAGCOORD.xy + vec2(w) ) * SCREEN_PIXEL_SIZE );
float alpha;
if (visibility>0)
alpha = sprite.a * visibility;
else
alpha = 0;
COLOR.xyz = mix(background.xyz, sprite.xyz, alpha);
COLOR.a = 1.0;
}