2019-04-04 15:09:06 +02:00
|
|
|
using Content.Shared.Maps;
|
2021-04-09 16:46:45 +02:00
|
|
|
using JetBrains.Annotations;
|
2021-02-11 01:13:03 -08:00
|
|
|
using Robust.Shared.GameObjects;
|
2019-04-15 21:11:38 -06:00
|
|
|
using Robust.Shared.IoC;
|
|
|
|
|
using Robust.Shared.Map;
|
2020-10-11 15:21:21 +02:00
|
|
|
using Robust.Shared.Maths;
|
2020-04-20 09:44:21 +02:00
|
|
|
using Robust.Shared.ViewVariables;
|
2019-04-04 15:09:06 +02:00
|
|
|
|
2021-06-09 22:19:39 +02:00
|
|
|
namespace Content.Shared.SubFloor
|
2019-04-04 15:09:06 +02:00
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Entity system backing <see cref="SubFloorHideComponent"/>.
|
|
|
|
|
/// </summary>
|
2021-04-09 16:46:45 +02:00
|
|
|
[UsedImplicitly]
|
2021-03-13 14:46:22 +11:00
|
|
|
public class SubFloorHideSystem : EntitySystem
|
2019-04-04 15:09:06 +02:00
|
|
|
{
|
2020-08-24 14:10:28 +02:00
|
|
|
[Dependency] private readonly IMapManager _mapManager = default!;
|
|
|
|
|
[Dependency] private readonly ITileDefinitionManager _tileDefinitionManager = default!;
|
2020-04-20 09:44:21 +02:00
|
|
|
|
2021-03-13 14:46:22 +11:00
|
|
|
private bool _showAll;
|
2019-04-04 15:09:06 +02:00
|
|
|
|
2020-04-20 09:44:21 +02:00
|
|
|
[ViewVariables(VVAccess.ReadWrite)]
|
2021-03-13 14:46:22 +11:00
|
|
|
public bool ShowAll
|
2020-04-20 09:44:21 +02:00
|
|
|
{
|
2021-03-13 14:46:22 +11:00
|
|
|
get => _showAll;
|
2020-04-20 09:44:21 +02:00
|
|
|
set
|
|
|
|
|
{
|
2021-03-13 14:46:22 +11:00
|
|
|
if (_showAll == value) return;
|
|
|
|
|
_showAll = value;
|
2020-04-20 09:44:21 +02:00
|
|
|
|
|
|
|
|
UpdateAll();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-04 15:09:06 +02:00
|
|
|
public override void Initialize()
|
|
|
|
|
{
|
|
|
|
|
base.Initialize();
|
|
|
|
|
|
|
|
|
|
_mapManager.GridChanged += MapManagerOnGridChanged;
|
|
|
|
|
_mapManager.TileChanged += MapManagerOnTileChanged;
|
|
|
|
|
|
2021-05-27 11:11:56 +02:00
|
|
|
SubscribeLocalEvent<SubFloorHideComponent, ComponentStartup>(OnSubFloorStarted);
|
|
|
|
|
SubscribeLocalEvent<SubFloorHideComponent, ComponentShutdown>(OnSubFloorTerminating);
|
2021-06-19 19:41:26 -07:00
|
|
|
SubscribeLocalEvent<SubFloorHideComponent, AnchorStateChangedEvent>(HandleAnchorChanged);
|
2021-04-09 16:46:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void Shutdown()
|
|
|
|
|
{
|
|
|
|
|
base.Shutdown();
|
|
|
|
|
|
|
|
|
|
_mapManager.GridChanged -= MapManagerOnGridChanged;
|
|
|
|
|
_mapManager.TileChanged -= MapManagerOnTileChanged;
|
2019-04-04 15:09:06 +02:00
|
|
|
}
|
|
|
|
|
|
2021-05-27 11:11:56 +02:00
|
|
|
private void OnSubFloorStarted(EntityUid uid, SubFloorHideComponent component, ComponentStartup _)
|
2019-04-04 15:09:06 +02:00
|
|
|
{
|
2021-05-27 11:11:56 +02:00
|
|
|
UpdateEntity(uid);
|
2021-04-09 16:46:45 +02:00
|
|
|
}
|
2020-06-22 21:02:50 -04:00
|
|
|
|
2021-05-27 11:11:56 +02:00
|
|
|
private void OnSubFloorTerminating(EntityUid uid, SubFloorHideComponent component, ComponentShutdown _)
|
2021-04-09 16:46:45 +02:00
|
|
|
{
|
2021-07-10 11:04:16 +02:00
|
|
|
// Regardless of whether we're on a subfloor or not, unhide.
|
|
|
|
|
UpdateEntity(uid, true);
|
2021-04-09 16:46:45 +02:00
|
|
|
}
|
|
|
|
|
|
2021-06-19 19:41:26 -07:00
|
|
|
private void HandleAnchorChanged(EntityUid uid, SubFloorHideComponent component, AnchorStateChangedEvent args)
|
2021-04-09 16:46:45 +02:00
|
|
|
{
|
2021-06-19 19:41:26 -07:00
|
|
|
var transform = ComponentManager.GetComponent<ITransformComponent>(uid);
|
|
|
|
|
|
2021-04-09 16:46:45 +02:00
|
|
|
// We do this directly instead of calling UpdateEntity.
|
2021-07-30 12:25:58 +02:00
|
|
|
UpdateEntity(uid);
|
2019-04-04 15:09:06 +02:00
|
|
|
}
|
|
|
|
|
|
2021-03-10 14:48:29 +01:00
|
|
|
private void MapManagerOnTileChanged(object? sender, TileChangedEventArgs e)
|
2019-04-04 15:09:06 +02:00
|
|
|
{
|
2019-04-20 16:20:18 -07:00
|
|
|
UpdateTile(_mapManager.GetGrid(e.NewTile.GridIndex), e.NewTile.GridIndices);
|
2019-04-04 15:09:06 +02:00
|
|
|
}
|
|
|
|
|
|
2021-03-10 14:48:29 +01:00
|
|
|
private void MapManagerOnGridChanged(object? sender, GridChangedEventArgs e)
|
2019-04-04 15:09:06 +02:00
|
|
|
{
|
|
|
|
|
foreach (var modified in e.Modified)
|
|
|
|
|
{
|
|
|
|
|
UpdateTile(e.Grid, modified.position);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-10 11:04:16 +02:00
|
|
|
private bool IsSubFloor(IMapGrid grid, Vector2i position)
|
2021-04-09 16:46:45 +02:00
|
|
|
{
|
2021-07-10 11:04:16 +02:00
|
|
|
var tileDef = (ContentTileDefinition) _tileDefinitionManager[grid.GetTileRef(position).Tile.TypeId];
|
|
|
|
|
return tileDef.IsSubFloor;
|
|
|
|
|
}
|
2021-04-09 16:46:45 +02:00
|
|
|
|
2021-07-10 11:04:16 +02:00
|
|
|
private void UpdateAll()
|
|
|
|
|
{
|
|
|
|
|
foreach (var comp in ComponentManager.EntityQuery<SubFloorHideComponent>(true))
|
|
|
|
|
{
|
|
|
|
|
UpdateEntity(comp.Owner.Uid);
|
|
|
|
|
}
|
2021-04-09 16:46:45 +02:00
|
|
|
}
|
|
|
|
|
|
2020-10-11 15:21:21 +02:00
|
|
|
private void UpdateTile(IMapGrid grid, Vector2i position)
|
2019-04-04 15:09:06 +02:00
|
|
|
{
|
2021-07-10 11:04:16 +02:00
|
|
|
var isSubFloor = IsSubFloor(grid, position);
|
|
|
|
|
|
|
|
|
|
foreach (var uid in grid.GetAnchoredEntities(position))
|
|
|
|
|
{
|
|
|
|
|
if(ComponentManager.HasComponent<SubFloorHideComponent>(uid))
|
|
|
|
|
UpdateEntity(uid, isSubFloor);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void UpdateEntity(EntityUid uid)
|
|
|
|
|
{
|
|
|
|
|
var transform = ComponentManager.GetComponent<ITransformComponent>(uid);
|
2021-07-30 12:25:58 +02:00
|
|
|
|
2021-07-10 11:04:16 +02:00
|
|
|
if (!_mapManager.TryGetGrid(transform.GridID, out var grid))
|
2019-04-04 15:09:06 +02:00
|
|
|
{
|
2021-07-10 11:04:16 +02:00
|
|
|
// Not being on a grid counts as no subfloor, unhide this.
|
|
|
|
|
UpdateEntity(uid, true);
|
|
|
|
|
return;
|
2019-04-04 15:09:06 +02:00
|
|
|
}
|
2021-07-10 11:04:16 +02:00
|
|
|
|
|
|
|
|
// Update normally.
|
|
|
|
|
UpdateEntity(uid, IsSubFloor(grid, grid.TileIndicesFor(transform.Coordinates)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void UpdateEntity(EntityUid uid, bool subFloor)
|
|
|
|
|
{
|
|
|
|
|
// 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;
|
|
|
|
|
|
2021-07-30 12:25:58 +02:00
|
|
|
// This might look weird, but basically we only need to query the SubFloorHide and Transform components
|
|
|
|
|
// if we are gonna hide the entity and we require it to be anchored to be hidden. Because getting components
|
|
|
|
|
// is "expensive", we have a slow path where we query them, and a fast path where we don't.
|
|
|
|
|
if (!subFloor
|
|
|
|
|
&& ComponentManager.TryGetComponent(uid, out SubFloorHideComponent? subFloorHideComponent) &&
|
|
|
|
|
subFloorHideComponent.RequireAnchored
|
|
|
|
|
&& ComponentManager.TryGetComponent(uid, out ITransformComponent? transformComponent))
|
|
|
|
|
{
|
|
|
|
|
// If we require the entity to be anchored but it's not, this will set subfloor to true, unhiding it.
|
|
|
|
|
subFloor = !transformComponent.Anchored;
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-10 11:04:16 +02:00
|
|
|
// Show sprite
|
|
|
|
|
if (ComponentManager.TryGetComponent(uid, out SharedSpriteComponent? spriteComponent))
|
|
|
|
|
{
|
|
|
|
|
spriteComponent.Visible = ShowAll || subFloor;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// So for collision all we care about is that the component is running.
|
|
|
|
|
if (ComponentManager.TryGetComponent(uid, out PhysicsComponent? physicsComponent))
|
|
|
|
|
{
|
|
|
|
|
physicsComponent.CanCollide = subFloor;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class SubFloorHideEvent : HandledEntityEventArgs
|
|
|
|
|
{
|
|
|
|
|
public bool SubFloor { get; }
|
|
|
|
|
|
|
|
|
|
public SubFloorHideEvent(bool subFloor)
|
|
|
|
|
{
|
|
|
|
|
SubFloor = subFloor;
|
2019-04-04 15:09:06 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|