2021-03-13 14:46:22 +11:00
|
|
|
#nullable enable
|
|
|
|
|
using Content.Shared.GameObjects.Components;
|
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-03-13 14:46:22 +11:00
|
|
|
namespace Content.Shared.GameObjects.EntitySystems
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void UpdateAll()
|
|
|
|
|
{
|
2021-04-28 10:49:37 -07:00
|
|
|
foreach (var comp in ComponentManager.EntityQuery<SubFloorHideComponent>(true))
|
2020-04-20 09:44:21 +02:00
|
|
|
{
|
2021-04-28 10:49:37 -07:00
|
|
|
var transform = comp.Owner.Transform;
|
|
|
|
|
if (!_mapManager.TryGetGrid(transform.GridID, out var grid)) return;
|
|
|
|
|
UpdateTile(grid, grid.TileIndicesFor(transform.Coordinates));
|
2020-04-20 09:44:21 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-04 15:09:06 +02:00
|
|
|
public override void Initialize()
|
|
|
|
|
{
|
|
|
|
|
base.Initialize();
|
|
|
|
|
|
|
|
|
|
IoCManager.InjectDependencies(this);
|
|
|
|
|
|
|
|
|
|
_mapManager.GridChanged += MapManagerOnGridChanged;
|
|
|
|
|
_mapManager.TileChanged += MapManagerOnTileChanged;
|
|
|
|
|
|
2021-04-09 16:46:45 +02:00
|
|
|
// TODO: Make this sane when EntityStarted becomes a directed event.
|
|
|
|
|
EntityManager.EntityStarted += OnEntityStarted;
|
|
|
|
|
|
|
|
|
|
SubscribeLocalEvent<SubFloorHideComponent, EntityTerminatingEvent>(OnSubFloorTerminating);
|
|
|
|
|
SubscribeLocalEvent<SubFloorHideComponent, SnapGridPositionChangedEvent>(OnSnapGridPositionChanged);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void Shutdown()
|
|
|
|
|
{
|
|
|
|
|
base.Shutdown();
|
|
|
|
|
|
|
|
|
|
_mapManager.GridChanged -= MapManagerOnGridChanged;
|
|
|
|
|
_mapManager.TileChanged -= MapManagerOnTileChanged;
|
|
|
|
|
|
|
|
|
|
EntityManager.EntityStarted -= OnEntityStarted;
|
|
|
|
|
|
|
|
|
|
UnsubscribeLocalEvent<SubFloorHideComponent, EntityTerminatingEvent>(OnSubFloorTerminating);
|
|
|
|
|
UnsubscribeLocalEvent<SubFloorHideComponent, SnapGridPositionChangedEvent>(OnSnapGridPositionChanged);
|
2019-04-04 15:09:06 +02:00
|
|
|
}
|
|
|
|
|
|
2021-04-09 16:46:45 +02:00
|
|
|
private void OnEntityStarted(object? sender, EntityUid uid)
|
2019-04-04 15:09:06 +02:00
|
|
|
{
|
2021-04-09 16:46:45 +02:00
|
|
|
if (ComponentManager.HasComponent<SubFloorHideComponent>(uid))
|
2020-06-22 21:02:50 -04:00
|
|
|
{
|
2021-04-09 16:46:45 +02:00
|
|
|
UpdateEntity(uid);
|
2020-06-22 21:02:50 -04:00
|
|
|
}
|
2021-04-09 16:46:45 +02:00
|
|
|
}
|
2020-06-22 21:02:50 -04:00
|
|
|
|
2021-04-09 16:46:45 +02:00
|
|
|
private void OnSubFloorTerminating(EntityUid uid, SubFloorHideComponent component, EntityTerminatingEvent args)
|
|
|
|
|
{
|
|
|
|
|
UpdateEntity(uid);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnSnapGridPositionChanged(EntityUid uid, SubFloorHideComponent component, SnapGridPositionChangedEvent ev)
|
|
|
|
|
{
|
|
|
|
|
// We do this directly instead of calling UpdateEntity.
|
|
|
|
|
if(_mapManager.TryGetGrid(ev.NewGrid, out var grid))
|
|
|
|
|
UpdateTile(grid, ev.Position);
|
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-04-09 16:46:45 +02:00
|
|
|
private void UpdateEntity(EntityUid uid)
|
|
|
|
|
{
|
|
|
|
|
if (!ComponentManager.TryGetComponent(uid, out ITransformComponent? transform) ||
|
|
|
|
|
!_mapManager.TryGetGrid(transform.GridID, out var grid)) return;
|
|
|
|
|
|
|
|
|
|
UpdateTile(grid, grid.WorldToTile(transform.WorldPosition));
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-11 15:21:21 +02:00
|
|
|
private void UpdateTile(IMapGrid grid, Vector2i position)
|
2019-04-04 15:09:06 +02:00
|
|
|
{
|
2019-04-28 22:08:27 -07:00
|
|
|
var tile = grid.GetTileRef(position);
|
2019-04-20 16:20:18 -07:00
|
|
|
var tileDef = (ContentTileDefinition) _tileDefinitionManager[tile.Tile.TypeId];
|
2021-04-28 10:49:37 -07:00
|
|
|
foreach (var anchored in grid.GetAnchoredEntities(position))
|
2019-04-04 15:09:06 +02:00
|
|
|
{
|
2021-04-28 10:49:37 -07:00
|
|
|
if (!ComponentManager.TryGetComponent(anchored, out SubFloorHideComponent? subFloorComponent))
|
2019-04-04 15:09:06 +02:00
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-13 14:46:22 +11:00
|
|
|
// Show sprite
|
2021-04-28 10:49:37 -07:00
|
|
|
if (ComponentManager.TryGetComponent(anchored, out SharedSpriteComponent ? spriteComponent))
|
2021-03-08 04:09:59 +11:00
|
|
|
{
|
2021-03-13 14:46:22 +11:00
|
|
|
spriteComponent.Visible = ShowAll || !subFloorComponent.Running || tileDef.IsSubFloor;
|
2021-03-08 04:09:59 +11:00
|
|
|
}
|
|
|
|
|
|
2021-03-13 14:46:22 +11:00
|
|
|
// So for collision all we care about is that the component is running.
|
2021-04-28 10:49:37 -07:00
|
|
|
if (ComponentManager.TryGetComponent(anchored, out PhysicsComponent ? physicsComponent))
|
2021-03-08 04:09:59 +11:00
|
|
|
{
|
2021-03-13 14:46:22 +11:00
|
|
|
physicsComponent.CanCollide = !subFloorComponent.Running;
|
2021-03-08 04:09:59 +11:00
|
|
|
}
|
2019-04-04 15:09:06 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|