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);
}
}