Move pipe visualizers to systems. (#6565)

This commit is contained in:
Leon Friedrich
2022-02-14 16:20:35 +13:00
committed by GitHub
parent 60524f890f
commit ac23899a39
23 changed files with 340 additions and 568 deletions

View File

@@ -0,0 +1,83 @@
using Content.Client.SubFloor;
using Content.Shared.Atmos;
using Content.Shared.Atmos.Components;
using Content.Shared.Atmos.Piping;
using JetBrains.Annotations;
using Robust.Client.GameObjects;
using Robust.Client.ResourceManagement;
namespace Content.Client.Atmos.EntitySystems;
[UsedImplicitly]
public sealed class AtmosPipeAppearanceSystem : EntitySystem
{
[Dependency] private readonly IResourceCache _resCache = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<PipeAppearanceComponent, ComponentInit>(OnInit);
SubscribeLocalEvent<PipeAppearanceComponent, AppearanceChangeEvent>(OnAppearanceChanged, after: new[] { typeof(SubFloorHideSystem) });
}
private void OnInit(EntityUid uid, PipeAppearanceComponent component, ComponentInit args)
{
if (!TryComp(uid, out SpriteComponent? sprite))
return;
if (!_resCache.TryGetResource(SharedSpriteComponent.TextureRoot / component.RsiPath, out RSIResource? rsi))
{
Logger.Error($"{nameof(AtmosPipeAppearanceSystem)} could not load to load RSI {component.RsiPath}.");
return;
}
foreach (PipeConnectionLayer layerKey in Enum.GetValues(typeof(PipeConnectionLayer)))
{
sprite.LayerMapReserveBlank(layerKey);
var layer = sprite.LayerMapGet(layerKey);
sprite.LayerSetRSI(layer, rsi.RSI);
var layerState = component.BaseState + (PipeDirection) layerKey;
sprite.LayerSetState(layer, layerState);
}
}
private void OnAppearanceChanged(EntityUid uid, PipeAppearanceComponent component, ref AppearanceChangeEvent args)
{
if (!TryComp(uid, out SpriteComponent? sprite))
return;
if (!args.Component.TryGetData(PipeColorVisuals.Color, out Color color))
color = Color.White;
if (!args.Component.TryGetData(PipeVisuals.VisualState, out PipeDirection connectedDirections))
return;
var rotation = Transform(uid).LocalRotation;
foreach (PipeConnectionLayer layerKey in Enum.GetValues(typeof(PipeConnectionLayer)))
{
if (!sprite.LayerMapTryGet(layerKey, out var key))
continue;
var layer = sprite[key];
var dir = (PipeDirection) layerKey;
var visible = connectedDirections.HasDirection(dir);
layer.Visible &= visible;
if (!visible) continue;
layer.Rotation = -rotation;
layer.Color = color;
}
}
private enum PipeConnectionLayer : byte
{
NorthConnection = PipeDirection.North,
SouthConnection = PipeDirection.South,
EastConnection = PipeDirection.East,
WestConnection = PipeDirection.West,
}
}

View File

@@ -1,14 +1,11 @@
using Content.Shared.Atmos.Piping;
using JetBrains.Annotations;
using Robust.Client.GameObjects;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Maths;
namespace Content.Client.Atmos.Visualizers
{
[UsedImplicitly]
public class PipeColorVisualizer : AppearanceVisualizer
public sealed class PipeColorVisualizer : AppearanceVisualizer
{
public override void OnChangeData(AppearanceComponent component)
{
@@ -19,7 +16,9 @@ namespace Content.Client.Atmos.Visualizers
if (component.TryGetData(PipeColorVisuals.Color, out Color color))
{
sprite.LayerSetColor(Layers.Pipe, color);
// T-ray scanner / sub floor runs after this visualizer. Lets not bulldoze transparency.
var layer = sprite[Layers.Pipe];
layer.Color = color.WithAlpha(layer.Color.A);
}
}

View File

@@ -1,105 +0,0 @@
using System;
using Content.Shared.Atmos;
using Content.Shared.Atmos.Piping;
using Content.Shared.SubFloor;
using JetBrains.Annotations;
using Robust.Client.GameObjects;
using Robust.Client.Graphics;
using Robust.Client.ResourceManagement;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Log;
using Robust.Shared.Maths;
using Robust.Shared.Serialization;
using Robust.Shared.Serialization.Manager.Attributes;
namespace Content.Client.Atmos.Visualizers
{
[UsedImplicitly]
public class PipeConnectorVisualizer : AppearanceVisualizer, ISerializationHooks
{
[DataField("rsi")]
private string _rsi = "Structures/Piping/Atmospherics/pipe.rsi";
[DataField("baseState")]
private string _baseState = "pipeConnector";
private RSI? _connectorRsi;
void ISerializationHooks.AfterDeserialization()
{
var rsiString = SharedSpriteComponent.TextureRoot / _rsi;
var resourceCache = IoCManager.Resolve<IResourceCache>();
if (resourceCache.TryGetResource(rsiString, out RSIResource? rsi))
_connectorRsi = rsi.RSI;
else
Logger.Error($"{nameof(PipeConnectorVisualizer)} could not load to load RSI {rsiString}.");
}
public override void InitializeEntity(EntityUid entity)
{
base.InitializeEntity(entity);
var entities = IoCManager.Resolve<IEntityManager>();
if (!entities.TryGetComponent<ISpriteComponent?>(entity, out var sprite))
return;
if (_connectorRsi == null)
return;
foreach (Layer layerKey in Enum.GetValues(typeof(Layer)))
{
sprite.LayerMapReserveBlank(layerKey);
var layer = sprite.LayerMapGet(layerKey);
sprite.LayerSetRSI(layer, _connectorRsi);
var layerState = _baseState + ((PipeDirection) layerKey);
sprite.LayerSetState(layer, layerState);
}
}
public override void OnChangeData(AppearanceComponent component)
{
base.OnChangeData(component);
var entities = IoCManager.Resolve<IEntityManager>();
if (!entities.TryGetComponent<TransformComponent>(component.Owner, out var xform))
return;
if (!entities.TryGetComponent<ISpriteComponent>(component.Owner, out var sprite))
return;
if (!component.TryGetData(PipeColorVisuals.Color, out Color color))
color = Color.White;
if (!component.TryGetData(PipeVisuals.VisualState, out PipeDirection connectedDirections))
return;
if(!component.TryGetData(SubFloorVisuals.SubFloor, out bool subfloor))
subfloor = true;
var rotation = xform.LocalRotation;
foreach (Layer layerKey in Enum.GetValues(typeof(Layer)))
{
var layer = sprite.LayerMapGet(layerKey);
var dir = (PipeDirection) layerKey;
var visible = subfloor && connectedDirections.HasDirection(dir);
sprite.LayerSetVisible(layer, visible);
if (!visible) continue;
sprite.LayerSetRotation(layer, -rotation);
sprite.LayerSetColor(layer, color);
}
}
private enum Layer : byte
{
NorthConnection = PipeDirection.North,
SouthConnection = PipeDirection.South,
EastConnection = PipeDirection.East,
WestConnection = PipeDirection.West,
}
}
}

View File

@@ -1,11 +1,9 @@
using System;
using Content.Client.Markers;
using Content.Client.Popups;
using Content.Client.SubFloor;
using Content.Shared.SubFloor;
using Robust.Client.GameObjects;
using Robust.Shared.Console;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using DrawDepth = Content.Shared.DrawDepth.DrawDepth;
namespace Content.Client.Commands
@@ -33,8 +31,7 @@ namespace Content.Client.Commands
public void Execute(IConsoleShell shell, string argStr, string[] args)
{
EntitySystem.Get<SubFloorHideSystem>()
.ShowAll ^= true;
EntitySystem.Get<SubFloorHideSystem>().ShowAll ^= true;
}
}
@@ -47,8 +44,7 @@ namespace Content.Client.Commands
public void Execute(IConsoleShell shell, string argStr, string[] args)
{
EntitySystem.Get<SubFloorHideSystem>()
.ShowAll = true;
EntitySystem.Get<SubFloorHideSystem>().ShowAll = true;
var entMan = IoCManager.Resolve<IEntityManager>();
var components = entMan.EntityQuery<SubFloorHideComponent>(true);

View File

@@ -2,6 +2,7 @@ using System;
using Content.Client.Decals.UI;
using Content.Client.HUD;
using Content.Client.Markers;
using Content.Client.SubFloor;
using Content.Shared.Input;
using Content.Shared.Sandbox;
using Content.Shared.SubFloor;

View File

@@ -0,0 +1,80 @@
using Content.Shared.SubFloor;
using Robust.Client.GameObjects;
namespace Content.Client.SubFloor;
public sealed class SubFloorHideSystem : SharedSubFloorHideSystem
{
[Dependency] private readonly AppearanceSystem _appearanceSystem = default!;
private bool _showAll;
[ViewVariables(VVAccess.ReadWrite)]
public bool ShowAll
{
get => _showAll;
set
{
if (_showAll == value) return;
_showAll = value;
UpdateAll();
}
}
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<SubFloorHideComponent, AppearanceChangeEvent>(OnAppearanceChanged);
}
private void OnAppearanceChanged(EntityUid uid, SubFloorHideComponent component, ref AppearanceChangeEvent args)
{
if (!TryComp(uid, out SpriteComponent? sprite))
return;
args.Component.TryGetData(SubFloorVisuals.Covered, out bool covered);
args.Component.TryGetData(SubFloorVisuals.ScannerRevealed, out bool scannerRevealed);
scannerRevealed &= !ShowAll; // no transparency for show-subfloor mode.
var revealed = !covered || ShowAll || scannerRevealed;
var transparency = scannerRevealed ? component.ScannerTransparency : 1f;
// set visibility & color of each layer
foreach (var layer in sprite.AllLayers)
{
// pipe connection visuals are updated AFTER this, and may re-hide some layers
layer.Visible = revealed;
if (layer.Visible)
layer.Color = layer.Color.WithAlpha(transparency);
}
// Is there some layer that is always visible?
if (sprite.LayerMapTryGet(SubfloorLayers.FirstLayer, out var firstLayer))
{
var layer = sprite[firstLayer];
layer.Visible = true;
layer.Color = layer.Color.WithAlpha(1f);
sprite.Visible = true;
return;
}
sprite.Visible = revealed;
}
private void UpdateAll()
{
foreach (var (_, appearance) in EntityManager.EntityQuery<SubFloorHideComponent, AppearanceComponent>(true))
{
_appearanceSystem.MarkDirty(appearance);
}
}
}
public enum SubfloorLayers : byte
{
FirstLayer, // always visible. E.g. vent part of a vent..
}

View File

@@ -1,44 +0,0 @@
using Content.Shared.SubFloor;
using JetBrains.Annotations;
using Robust.Client.GameObjects;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
namespace Content.Client.SubFloor
{
[UsedImplicitly]
public class SubFloorShowLayerVisualizer : AppearanceVisualizer
{
public override void OnChangeData(AppearanceComponent component)
{
base.OnChangeData(component);
var entities = IoCManager.Resolve<IEntityManager>();
if (!entities.TryGetComponent(component.Owner, out SpriteComponent? sprite))
return;
if (!component.TryGetData(SubFloorVisuals.SubFloor, out bool subfloor))
return;
foreach (var layer in sprite.AllLayers)
{
layer.Visible = subfloor;
}
if (!sprite.LayerMapTryGet(Layers.FirstLayer, out var firstLayer))
{
sprite.Visible = subfloor;
return;
}
// show the top part of the sprite. E.g. the grille-part of a vent, but not the connecting pipes.
sprite.LayerSetVisible(firstLayer, true);
sprite.Visible = true;
}
public enum Layers : byte
{
FirstLayer,
}
}
}

View File

@@ -1,45 +0,0 @@
using Robust.Client.GameObjects;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Maths;
namespace Content.Client.SubFloor;
public class TrayScannerSubFloorVisualizer : AppearanceVisualizer
{
[Dependency] IEntityManager _entityManager = default!;
public override void InitializeEntity(EntityUid uid)
{
base.InitializeEntity(uid);
IoCManager.InjectDependencies(this);
}
public override void OnChangeData(AppearanceComponent component)
{
base.OnChangeData(component);
if (!_entityManager.TryGetComponent(component.Owner, out SpriteComponent? sprite))
return;
if (!component.TryGetData(TrayScannerTransparency.Key, out bool transparent))
return;
foreach (var layer in sprite.AllLayers)
{
var transparency = transparent == true ? 0.8f : 1f;
layer.Color = layer.Color.WithAlpha(transparency);
}
if (sprite.LayerMapTryGet(SubFloorShowLayerVisualizer.Layers.FirstLayer, out var firstLayer))
{
sprite.LayerSetColor(firstLayer, Color.White);
}
}
}
public enum TrayScannerTransparency
{
Key,
}

View File

@@ -1,210 +0,0 @@
using System.Linq;
using Content.Shared.SubFloor;
using Robust.Shared.Containers;
using Robust.Shared.Map;
using Robust.Shared.Utility;
namespace Content.Client.SubFloor;
public sealed class TrayScannerSystem : SharedTrayScannerSystem
{
[Dependency] private IMapManager _mapManager = default!;
[Dependency] private SubFloorHideSystem _subfloorSystem = default!;
[Dependency] private SharedContainerSystem _containerSystem = default!;
public override void Initialize()
{
base.Initialize();
UpdatesOutsidePrediction = true;
SubscribeLocalEvent<TrayScannerComponent, ComponentShutdown>(OnComponentShutdown);
}
public void OnComponentShutdown(EntityUid uid, TrayScannerComponent scanner, ComponentShutdown args)
{
_subfloorSystem.SetEntitiesRevealed(scanner.RevealedSubfloors, uid, false, _visualizerKeys);
_invalidScanners.Add(uid);
}
public override void ToggleTrayScanner(EntityUid uid, bool toggle, TrayScannerComponent? scanner = null)
{
if (!Resolve(uid, ref scanner))
return;
scanner.Toggled = toggle;
UpdateTrayScanner(uid, scanner);
if (toggle) _activeScanners.Add(uid);
}
private HashSet<EntityUid> _activeScanners = new();
private RemQueue<EntityUid> _invalidScanners = new();
public override void Update(float frameTime)
{
if (!_activeScanners.Any()) return;
foreach (var scanner in _activeScanners)
{
if (_invalidScanners.List != null
&& _invalidScanners.List.Contains(scanner))
continue;
if (!UpdateTrayScanner(scanner))
_invalidScanners.Add(scanner);
}
foreach (var invalidScanner in _invalidScanners)
_activeScanners.Remove(invalidScanner);
if (_invalidScanners.List != null) _invalidScanners.List.Clear();
}
/// <summary>
/// When a subfloor entity gets anchored (which includes spawning & coming into PVS range), Check for nearby scanners.
/// </summary>
public override void OnSubfloorAnchored(EntityUid uid, SubFloorHideComponent? hideComp = null, TransformComponent? xform = null)
{
if (!Resolve(uid, ref hideComp, ref xform))
return;
var pos = xform.MapPosition;
foreach (var entity in _activeScanners)
{
if (!TryComp(entity, out TrayScannerComponent? scanner))
continue;
if (!Transform(entity).MapPosition.InRange(pos, scanner.Range))
continue;
hideComp.RevealedBy.Add(entity);
scanner.RevealedSubfloors.Add(uid);
}
}
/// <summary>
/// Updates a T-Ray scanner. Should be called on immediate
/// state change (turned on/off), or during the update
/// loop.
/// </summary>
/// <returns>true if the update was successful, false otherwise</returns>
private bool UpdateTrayScanner(EntityUid uid, TrayScannerComponent? scanner = null, TransformComponent? transform = null)
{
// whoops?
if (!Resolve(uid, ref scanner, ref transform))
{
return false;
}
// if the scanner was toggled off recently,
// set all the known subfloor to invisible,
// and return false so it's removed from
// the active scanner list
if (!scanner.Toggled || transform.MapID == MapId.Nullspace)
{
_subfloorSystem.SetEntitiesRevealed(scanner.RevealedSubfloors, uid, false, _visualizerKeys);
scanner.LastLocation = Vector2.Zero;
scanner.RevealedSubfloors.Clear();
return false;
}
// get the rounded position so that small movements don't cause this to
// update every time
Vector2 flooredPos;
// zero vector implies container
//
// this means we should get the entity transform's parent
if (transform.LocalPosition == Vector2.Zero
&& transform.Parent != null
&& _containerSystem.ContainsEntity(transform.ParentUid, uid))
{
flooredPos = transform.Parent.LocalPosition.Rounded();
// if this is also zero, we can check one more time
//
// could recurse through fully but i think that's useless,
// just attempt to check through the gp's transform and if
// that doesn't work, just don't bother any further
if (flooredPos == Vector2.Zero)
{
var gpTransform = transform.Parent.Parent;
if (gpTransform != null
&& _containerSystem.ContainsEntity(gpTransform.Owner, transform.ParentUid))
{
flooredPos = gpTransform.LocalPosition.Rounded();
}
}
}
else
{
flooredPos = transform.LocalPosition.Rounded();
}
// is the position still logically zero? just clear,
// but we need to keep it as 'true' since this t-ray
// is still technically on
if (flooredPos == Vector2.Zero)
{
_subfloorSystem.SetEntitiesRevealed(scanner.RevealedSubfloors, uid, false, _visualizerKeys);
scanner.RevealedSubfloors.Clear();
return true;
}
// MAYBE redo this. Currently different players can see different entities
//
// Here we avoid the entity lookup & return early if the scanner's position hasn't appreciably changed. However,
// if a new player enters PVS-range, they will update the in-range entities on their end and use that to set
// LastLocation. This means that different players can technically see different entities being revealed by the
// same scanner. The correct fix for this is probably just to network the revealed entity set.... But I CBF
// doing that right now....
if (flooredPos == scanner.LastLocation
|| (float.IsNaN(flooredPos.X) && float.IsNaN(flooredPos.Y)))
return true;
scanner.LastLocation = flooredPos;
// Update entities in Range
HashSet<EntityUid> nearby = new();
var coords = transform.MapPosition;
var worldBox = Box2.CenteredAround(coords.Position, (scanner.Range * 2, scanner.Range * 2));
// For now, limiting to the scanner's own grid. We could do a grid-lookup, but then what do we do if one grid
// flies away, while the scanner's local-position remains unchanged?
if (_mapManager.TryGetGrid(transform.GridID, out var grid))
{
foreach (var entity in grid.GetAnchoredEntities(worldBox))
{
if (!Transform(entity).MapPosition.InRange(coords, scanner.Range))
continue;
if (!TryComp(entity, out SubFloorHideComponent? hideComp))
continue; // Not a hide-able entity.
nearby.Add(entity);
if (scanner.RevealedSubfloors.Add(entity))
_subfloorSystem.SetEntityRevealed(entity, uid, true, hideComp, _visualizerKeys);
}
}
// get all the old elements that are no longer detected
HashSet<EntityUid> missing = new(scanner.RevealedSubfloors.Except(nearby));
// remove those from the list
scanner.RevealedSubfloors.ExceptWith(missing);
// and hide them
_subfloorSystem.SetEntitiesRevealed(missing, uid, false, _visualizerKeys);
return true;
}
private static IEnumerable<object> _visualizerKeys = new List<object>
{
SubFloorVisuals.SubFloor,
TrayScannerTransparency.Key
};
}