Adds T-ray Scanners (#5420)

This commit is contained in:
Flipp Syder
2021-12-13 23:46:47 -08:00
committed by GitHub
parent 26161e35cb
commit c0bfe43a05
14 changed files with 520 additions and 35 deletions

View File

@@ -0,0 +1,76 @@
using System;
using Content.Shared.Interaction;
using Robust.Shared.GameObjects;
using Robust.Shared.GameStates;
using Robust.Shared.IoC;
using Robust.Shared.Log;
using Robust.Shared.Serialization;
namespace Content.Shared.SubFloor;
public abstract class SharedTrayScannerSystem : EntitySystem
{
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<TrayScannerComponent, ComponentGetState>(OnTrayScannerGetState);
SubscribeLocalEvent<TrayScannerComponent, ComponentHandleState>(OnTrayScannerHandleState);
SubscribeLocalEvent<TrayScannerComponent, UseInHandEvent>(OnTrayScannerUsed);
SubscribeLocalEvent<TrayScannerComponent, ActivateInWorldEvent>(OnTrayScannerActivate);
}
private void OnTrayScannerUsed(EntityUid uid, TrayScannerComponent scanner, UseInHandEvent args)
{
ActivateTray(uid, scanner);
}
private void OnTrayScannerActivate(EntityUid uid, TrayScannerComponent scanner, ActivateInWorldEvent args)
{
ActivateTray(uid, scanner);
}
private void ActivateTray(EntityUid uid, TrayScannerComponent? scanner = null)
{
if (!Resolve(uid, ref scanner))
return;
ToggleTrayScanner(uid, !scanner.Toggled, scanner);
if (EntityManager.TryGetComponent<AppearanceComponent>(uid, out var appearance))
{
appearance.SetData(TrayScannerVisual.Visual, scanner.Toggled == true ? TrayScannerVisual.On : TrayScannerVisual.Off);
}
}
public virtual void ToggleTrayScanner(EntityUid uid, bool state, TrayScannerComponent? scanner = null)
{
if (!Resolve(uid, ref scanner))
return;
scanner.Toggled = state;
scanner.Dirty();
// RaiseLocalEvent(uid, new TrayScannerToggleEvent(scanner.Toggled));
}
private void OnTrayScannerGetState(EntityUid uid, TrayScannerComponent scanner, ref ComponentGetState args)
{
args.State = new TrayScannerState(scanner.Toggled);
}
private void OnTrayScannerHandleState(EntityUid uid, TrayScannerComponent scanner, ref ComponentHandleState args)
{
if (args.Current is not TrayScannerState state)
return;
ToggleTrayScanner(uid, state.Toggled, scanner);
}
}
[Serializable, NetSerializable]
public enum TrayScannerVisual : sbyte
{
Visual,
On,
Off
}

View File

@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using Robust.Shared.GameObjects;
using Robust.Shared.GameStates;
using Robust.Shared.Players;
@@ -39,6 +40,26 @@ namespace Content.Shared.SubFloor
{
return new SubFloorHideComponentState(Enabled, RequireAnchored);
}
/// <summary>
/// Whether or not this entity is supposed
/// to be visible.
/// </summary>
[ViewVariables]
public bool Visible { get; set; }
/// <summary>
/// The entities this subfloor is revealed by.
/// </summary>
[ViewVariables]
public HashSet<EntityUid> RevealedBy { get; set; } = new();
/// <summary>
/// Whether or not this entity was revealed with or without
/// an entity.
/// </summary>
[ViewVariables]
public bool RevealedWithoutEntity { get; set; }
}
[Serializable, NetSerializable]

View File

@@ -1,9 +1,12 @@
using System;
using System.Collections.Generic;
using Content.Shared.Interaction;
using Content.Shared.Maps;
using JetBrains.Annotations;
using Robust.Shared.GameObjects;
using Robust.Shared.GameStates;
using Robust.Shared.IoC;
using Robust.Shared.Log;
using Robust.Shared.Map;
using Robust.Shared.Maths;
using Robust.Shared.Serialization;
@@ -46,6 +49,7 @@ namespace Content.Shared.SubFloor
SubscribeLocalEvent<SubFloorHideComponent, ComponentShutdown>(OnSubFloorTerminating);
SubscribeLocalEvent<SubFloorHideComponent, AnchorStateChangedEvent>(HandleAnchorChanged);
SubscribeLocalEvent<SubFloorHideComponent, ComponentHandleState>(HandleComponentState);
SubscribeLocalEvent<SubFloorHideComponent, InteractUsingEvent>(OnInteractionAttempt);
}
public override void Shutdown()
@@ -70,6 +74,18 @@ namespace Content.Shared.SubFloor
UpdateEntity(subFloor.Owner);
}
private void OnInteractionAttempt(EntityUid uid, SubFloorHideComponent component, InteractUsingEvent args)
{
if (!EntityManager.TryGetComponent(uid, out TransformComponent? transform))
return;
if (_mapManager.TryGetGrid(transform.GridID, out var grid)
&& !IsSubFloor(grid, grid.TileIndicesFor(transform.Coordinates)))
{
args.Handled = true;
}
}
private void OnSubFloorStarted(EntityUid uid, SubFloorHideComponent component, ComponentStartup _)
{
UpdateEntity(uid);
@@ -153,39 +169,80 @@ namespace Content.Shared.SubFloor
}
// Update normally.
UpdateEntity(uid, IsSubFloor(grid, grid.TileIndicesFor(transform.Coordinates)));
bool isSubFloor = IsSubFloor(grid, grid.TileIndicesFor(transform.Coordinates));
UpdateEntity(uid, isSubFloor);
}
private void UpdateEntity(EntityUid uid, bool subFloor)
// Toggles an enumerable set of entities to display.
public void ToggleSubfloorEntities(IEnumerable<EntityUid> entities, bool visible, EntityUid? uid = null, IEnumerable<object>? appearanceKeys = null)
{
// We raise an event to allow other entity systems to handle this.
var subFloorHideEvent = new SubFloorHideEvent(subFloor);
RaiseLocalEvent(uid, subFloorHideEvent, false);
// Check if it has been handled by someone else.
if (subFloorHideEvent.Handled)
return;
// We only need to query the subfloor component to check if it's enabled or not when we're not on subfloor.
// Getting components is expensive, after all.
if (!subFloor && EntityManager.TryGetComponent(uid, out SubFloorHideComponent? subFloorHideComponent))
foreach (var entity in entities)
{
// If the component isn't enabled, then subfloor will always be true, and the entity will be shown.
if (!subFloorHideComponent.Enabled)
{
subFloor = true;
}
// We only need to query the TransformComp if the SubfloorHide is enabled and requires anchoring.
else if (subFloorHideComponent.RequireAnchored && EntityManager.TryGetComponent(uid, out TransformComponent? transformComponent))
{
// If we require the entity to be anchored but it's not, this will set subfloor to true, unhiding it.
subFloor = !transformComponent.Anchored;
}
if (!EntityManager.HasComponent<SubFloorHideComponent>(entity))
continue;
UpdateEntity(entity, visible, uid, appearanceKeys);
}
}
private void UpdateEntity(EntityUid uid, bool subFloor, EntityUid? revealedUid = null, IEnumerable<object>? appearanceKeys = null)
{
bool revealedWithoutEntity = false;
if (EntityManager.TryGetComponent(uid, out SubFloorHideComponent? subFloorHideComponent))
{
// We only need to query the subfloor component to check if it's enabled or not when we're not on subfloor.
// Getting components is expensive, after all.
if (!subFloor)
{
// If the component isn't enabled, then subfloor will always be true, and the entity will be shown.
if (!subFloorHideComponent.Enabled)
{
subFloor = true;
}
// We only need to query the TransformComp if the SubfloorHide is enabled and requires anchoring.
else if (subFloorHideComponent.RequireAnchored && EntityManager.TryGetComponent(uid, out TransformComponent? transformComponent))
{
// If we require the entity to be anchored but it's not, this will set subfloor to true, unhiding it.
subFloor = !transformComponent.Anchored;
}
}
// If this was revealed by anything, we need to add it into the
// component's set of entities that reveal it
//
if (revealedUid != null)
{
if (subFloor) subFloorHideComponent.RevealedBy.Add((EntityUid) revealedUid);
else subFloorHideComponent.RevealedBy.Remove((EntityUid) revealedUid);
}
else
{
subFloorHideComponent.RevealedWithoutEntity = subFloor;
}
subFloor = subFloorHideComponent.RevealedBy.Count != 0 || subFloorHideComponent.RevealedWithoutEntity;
revealedWithoutEntity = subFloorHideComponent.RevealedWithoutEntity;
}
// Whether to show this entity as visible, visually.
var subFloorVisible = ShowAll || subFloor;
// if there are no keys given,
// or if the subfloor is already revealed,
// set the keys to the default:
//
// the reason why it's set to default when the subfloor is
// revealed without an entity is because the appearance keys
// should only apply if the visualizer is underneath a subfloor
if (appearanceKeys == null || revealedWithoutEntity) appearanceKeys = _defaultVisualizerKeys;
ShowSubfloorSprite(uid, subFloorVisible, appearanceKeys);
}
private void ShowSubfloorSprite(EntityUid uid, bool subFloorVisible, IEnumerable<object> appearanceKeys)
{
// Show sprite
if (EntityManager.TryGetComponent(uid, out SharedSpriteComponent? spriteComponent))
{
@@ -195,19 +252,22 @@ namespace Content.Shared.SubFloor
// Set an appearance data value so visualizers can use this as needed.
if (EntityManager.TryGetComponent(uid, out AppearanceComponent? appearanceComponent))
{
appearanceComponent.SetData(SubFloorVisuals.SubFloor, subFloorVisible);
foreach (var key in appearanceKeys)
{
switch (key)
{
case Enum enumKey:
appearanceComponent.SetData(enumKey, subFloorVisible);
break;
case string stringKey:
appearanceComponent.SetData(stringKey, subFloorVisible);
break;
}
}
}
}
}
public class SubFloorHideEvent : HandledEntityEventArgs
{
public bool SubFloor { get; }
public SubFloorHideEvent(bool subFloor)
{
SubFloor = subFloor;
}
private static List<object> _defaultVisualizerKeys = new List<object>{ SubFloorVisuals.SubFloor };
}
[Serializable, NetSerializable]

View File

@@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
using Robust.Shared.GameObjects;
using Robust.Shared.GameStates;
using Robust.Shared.Maths;
using Robust.Shared.Serialization;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.ViewVariables;
namespace Content.Shared.SubFloor;
[RegisterComponent]
[NetworkedComponent]
public class TrayScannerComponent : Component
{
public override string Name { get; } = "TrayScanner";
[ViewVariables]
public bool Toggled { get; set; }
// this should always be rounded
[ViewVariables]
public Vector2 LastLocation { get; set; }
// range of the scanner itself
[DataField("range")]
public float Range { get; set; } = 0.5f;
// exclude entities that are not the set
// of entities in range & entities already revealed
[ViewVariables]
public HashSet<EntityUid> RevealedSubfloors = new();
}
[Serializable, NetSerializable]
public sealed class TrayScannerState : ComponentState
{
public bool Toggled { get; }
public TrayScannerState(bool toggle)
{
Toggled = toggle;
}
}