Add tile entity occlusion (#14626)

This commit is contained in:
metalgearsloth
2023-08-30 12:32:35 +10:00
committed by GitHub
parent 1416942bea
commit cae574cbda
9 changed files with 168 additions and 3 deletions

View File

@@ -0,0 +1,55 @@
using Content.Shared.Movement.Components;
using Content.Shared.Movement.Systems;
using Robust.Client.GameObjects;
using Robust.Client.Graphics;
using Robust.Shared.Prototypes;
namespace Content.Client.Movement.Systems;
public sealed class FloorOcclusionSystem : SharedFloorOcclusionSystem
{
[Dependency] private readonly IPrototypeManager _proto = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<FloorOcclusionComponent, ComponentStartup>(OnOcclusionStartup);
SubscribeLocalEvent<FloorOcclusionComponent, AfterAutoHandleStateEvent>(OnOcclusionAuto);
}
private void OnOcclusionAuto(EntityUid uid, FloorOcclusionComponent component, ref AfterAutoHandleStateEvent args)
{
SetEnabled(uid, component, component.Enabled);
}
private void OnOcclusionStartup(EntityUid uid, FloorOcclusionComponent component, ComponentStartup args)
{
if (component.Enabled && TryComp<SpriteComponent>(uid, out var sprite))
SetShader(sprite, true);
}
protected override void SetEnabled(EntityUid uid, FloorOcclusionComponent component, bool enabled)
{
if (component.Enabled == enabled)
return;
base.SetEnabled(uid, component, enabled);
if (!TryComp<SpriteComponent>(uid, out var sprite))
return;
SetShader(sprite, enabled);
}
private void SetShader(SpriteComponent sprite, bool enabled)
{
if (enabled)
{
sprite.PostShader = _proto.Index<ShaderPrototype>("HorizontalCut").Instance();
}
else
{
sprite.PostShader = null;
}
}
}

View File

@@ -0,0 +1,8 @@
using Content.Shared.Movement.Systems;
namespace Content.Server.Movement.Systems;
public sealed class FloorOcclusionSystem : SharedFloorOcclusionSystem
{
}

View File

@@ -0,0 +1,12 @@
using Robust.Shared.GameStates;
namespace Content.Shared.Movement.Components;
/// <summary>
/// Applies floor occlusion to any <see cref="FloorOcclusionComponent"/> that intersect us.
/// </summary>
[RegisterComponent, NetworkedComponent]
public sealed partial class FloorOccluderComponent : Component
{
}

View File

@@ -0,0 +1,19 @@
using Robust.Shared.GameStates;
namespace Content.Shared.Movement.Components;
/// <summary>
/// Applies an occlusion shader to this entity if it's colliding with a <see cref="FloorOccluderComponent"/>
/// </summary>
[RegisterComponent, NetworkedComponent, AutoGenerateComponentState(true)]
public sealed partial class FloorOcclusionComponent : Component
{
/// <summary>
/// Is the shader currently enabled.
/// </summary>
[ViewVariables(VVAccess.ReadWrite), DataField("enabled"), AutoNetworkedField]
public bool Enabled;
[DataField("colliding")]
public List<EntityUid> Colliding = new();
}

View File

@@ -0,0 +1,53 @@
using Content.Shared.Movement.Components;
using Robust.Shared.Physics.Events;
namespace Content.Shared.Movement.Systems;
/// <summary>
/// Applies an occlusion shader for any relevant entities.
/// </summary>
public abstract class SharedFloorOcclusionSystem : EntitySystem
{
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<FloorOccluderComponent, StartCollideEvent>(OnStartCollide);
SubscribeLocalEvent<FloorOccluderComponent, EndCollideEvent>(OnEndCollide);
}
private void OnStartCollide(EntityUid uid, FloorOccluderComponent component, ref StartCollideEvent args)
{
var other = args.OtherEntity;
if (!TryComp<FloorOcclusionComponent>(other, out var occlusion) ||
occlusion.Colliding.Contains(uid))
{
return;
}
SetEnabled(other, occlusion, true);
occlusion.Colliding.Add(uid);
}
private void OnEndCollide(EntityUid uid, FloorOccluderComponent component, ref EndCollideEvent args)
{
var other = args.OtherEntity;
if (!TryComp<FloorOcclusionComponent>(other, out var occlusion))
return;
occlusion.Colliding.Remove(uid);
if (occlusion.Colliding.Count == 0)
SetEnabled(other, occlusion, false);
}
protected virtual void SetEnabled(EntityUid uid, FloorOcclusionComponent component, bool enabled)
{
if (component.Enabled == enabled)
return;
component.Enabled = enabled;
Dirty(uid, component);
}
}

View File

@@ -5,6 +5,7 @@
id: BaseMobOrganic
noSpawn: true
components:
- type: FloorOcclusion
- type: LagCompensation
- type: RangedDamageSound
soundGroups:

View File

@@ -7,9 +7,7 @@
snap:
- Wall
components:
- type: StepTrigger
requiredTriggeredSpeed: 0
intersectRatio: 0.1
- type: FloorOccluder
- type: Transform
anchored: true
- type: SyncSprite

View File

@@ -76,3 +76,9 @@
id: PaperStamp
kind: source
path: "/Textures/Shaders/paperstamp.swsl"
# Simple horizontal cut
- type: shader
id: HorizontalCut
kind: source
path: "/Textures/Shaders/hcut.swsl"

View File

@@ -0,0 +1,13 @@
light_mode unshaded;
const highp float c = 0.3;
const highp float alphaModifier = 0.2;
uniform bool below = true;
void fragment()
{
highp vec4 tex = zTexture(UV);
highp float modifier = (UV.y > c ^^ !below) ? 1 : alphaModifier;
COLOR = vec4(tex.x, tex.y, tex.z, tex.w * modifier);
}