Files
OldThink/Content.Shared/SubFloor/SharedSubFloorHideSystem.cs

187 lines
7.1 KiB
C#
Raw Normal View History

2022-03-12 16:20:31 -06:00
using Content.Shared.Audio;
2023-07-05 03:29:57 +03:00
using Content.Shared.Explosion;
using Content.Shared.Interaction.Events;
using Content.Shared.Maps;
using JetBrains.Annotations;
using Robust.Shared.Map;
using Robust.Shared.Map.Components;
using Robust.Shared.Serialization;
2021-06-09 22:19:39 +02:00
namespace Content.Shared.SubFloor
{
/// <summary>
/// Entity system backing <see cref="SubFloorHideComponent"/>.
/// </summary>
[UsedImplicitly]
public abstract class SharedSubFloorHideSystem : EntitySystem
{
[Dependency] protected readonly IMapManager MapManager = default!;
[Dependency] private readonly ITileDefinitionManager _tileDefinitionManager = default!;
2022-03-12 16:20:31 -06:00
[Dependency] private readonly SharedAmbientSoundSystem _ambientSoundSystem = default!;
[Dependency] protected readonly SharedAppearanceSystem Appearance = default!;
2020-04-20 09:44:21 +02:00
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<GridModifiedEvent>(OnGridChanged);
SubscribeLocalEvent<TileChangedEvent>(OnTileChanged);
SubscribeLocalEvent<SubFloorHideComponent, ComponentStartup>(OnSubFloorStarted);
SubscribeLocalEvent<SubFloorHideComponent, ComponentShutdown>(OnSubFloorTerminating);
// Like 80% sure this doesn't need to handle re-anchoring.
SubscribeLocalEvent<SubFloorHideComponent, AnchorStateChangedEvent>(HandleAnchorChanged);
SubscribeLocalEvent<SubFloorHideComponent, GettingInteractedWithAttemptEvent>(OnInteractionAttempt);
SubscribeLocalEvent<SubFloorHideComponent, GettingAttackedAttemptEvent>(OnAttackAttempt);
2023-07-05 03:29:57 +03:00
SubscribeLocalEvent<SubFloorHideComponent, GetExplosionResistanceEvent>(OnGetExplosionResistance);
}
private void OnGetExplosionResistance(EntityUid uid, SubFloorHideComponent component, GetExplosionResistanceEvent args)
{
if (component.BlockInteractions && component.IsUnderCover)
args.DamageCoefficient = 0;
}
private void OnAttackAttempt(EntityUid uid, SubFloorHideComponent component, ref GettingAttackedAttemptEvent args)
{
if (component.BlockInteractions && component.IsUnderCover)
args.Cancelled = true;
}
private void OnInteractionAttempt(EntityUid uid, SubFloorHideComponent component, GettingInteractedWithAttemptEvent args)
2021-12-13 23:46:47 -08:00
{
// No interactions with entities hidden under floor tiles.
if (component.BlockInteractions && component.IsUnderCover)
args.Cancel();
2021-12-13 23:46:47 -08:00
}
private void OnSubFloorStarted(EntityUid uid, SubFloorHideComponent component, ComponentStartup _)
{
UpdateFloorCover(uid, component);
UpdateAppearance(uid, component);
EntityManager.EnsureComponent<CollideOnAnchorComponent>(uid);
}
private void OnSubFloorTerminating(EntityUid uid, SubFloorHideComponent component, ComponentShutdown _)
{
// If component is being deleted don't need to worry about updating any component stuff because it won't matter very shortly.
2021-12-04 14:14:22 +01:00
if (EntityManager.GetComponent<MetaDataComponent>(uid).EntityLifeStage >= EntityLifeStage.Terminating)
return;
2021-07-10 11:04:16 +02:00
// Regardless of whether we're on a subfloor or not, unhide.
component.IsUnderCover = false;
UpdateAppearance(uid, component);
}
private void HandleAnchorChanged(EntityUid uid, SubFloorHideComponent component, ref AnchorStateChangedEvent args)
{
if (args.Anchored)
{
var xform = Transform(uid);
UpdateFloorCover(uid, component, xform);
}
else if (component.IsUnderCover)
{
component.IsUnderCover = false;
UpdateAppearance(uid, component);
}
}
private void OnTileChanged(ref TileChangedEvent args)
{
if (args.OldTile.IsEmpty)
return; // Nothing is anchored here anyways.
if (args.NewTile.Tile.IsEmpty)
return; // Anything that was here will be unanchored anyways.
UpdateTile(MapManager.GetGrid(args.NewTile.GridUid), args.NewTile.GridIndices);
}
private void OnGridChanged(GridModifiedEvent args)
{
foreach (var modified in args.Modified)
{
UpdateTile(args.Grid, modified.position);
}
}
/// <summary>
/// Update whether a given entity is currently covered by a floor tile.
/// </summary>
private void UpdateFloorCover(EntityUid uid, SubFloorHideComponent? component = null, TransformComponent? xform = null)
{
if (!Resolve(uid, ref component, ref xform))
return;
2022-06-20 12:14:35 +12:00
if (xform.Anchored && MapManager.TryGetGrid(xform.GridUid, out var grid))
component.IsUnderCover = HasFloorCover(grid, grid.TileIndicesFor(xform.Coordinates));
else
component.IsUnderCover = false;
UpdateAppearance(uid, component);
}
public bool HasFloorCover(MapGridComponent grid, Vector2i position)
{
// TODO Redo this function. Currently wires on an asteroid are always "below the floor"
2021-07-10 11:04:16 +02:00
var tileDef = (ContentTileDefinition) _tileDefinitionManager[grid.GetTileRef(position).Tile.TypeId];
return !tileDef.IsSubFloor;
2021-07-10 11:04:16 +02:00
}
private void UpdateTile(MapGridComponent grid, Vector2i position)
{
var covered = HasFloorCover(grid, position);
2021-07-10 11:04:16 +02:00
foreach (var uid in grid.GetAnchoredEntities(position))
{
if (!TryComp(uid, out SubFloorHideComponent? hideComp))
continue;
2021-07-10 11:04:16 +02:00
if (hideComp.IsUnderCover == covered)
continue;
hideComp.IsUnderCover = covered;
UpdateAppearance(uid, hideComp);
}
2021-07-10 11:04:16 +02:00
}
public void UpdateAppearance(
EntityUid uid,
SubFloorHideComponent? hideComp = null,
AppearanceComponent? appearance = null)
{
if (!Resolve(uid, ref hideComp, false))
return;
2021-12-13 23:46:47 -08:00
2022-03-12 16:20:31 -06:00
if (hideComp.BlockAmbience && hideComp.IsUnderCover)
_ambientSoundSystem.SetAmbience(uid, false);
else if (hideComp.BlockAmbience && !hideComp.IsUnderCover)
_ambientSoundSystem.SetAmbience(uid, true);
if (Resolve(uid, ref appearance, false))
{
Appearance.SetData(uid, SubFloorVisuals.Covered, hideComp.IsUnderCover, appearance);
}
2021-12-13 23:46:47 -08:00
}
}
[Serializable, NetSerializable]
public enum SubFloorVisuals : byte
{
2023-03-31 14:40:38 +11:00
/// <summary>
/// Is there a floor tile over this entity
/// </summary>
Covered,
/// <summary>
/// Is this entity revealed by a scanner or some other entity?
/// </summary>
ScannerRevealed,
}
2022-07-09 21:59:39 +12:00
public enum SubfloorLayers : byte
{
FirstLayer
}
}