Move all logic from SubFloorHideComponent to SubFloorHideSystem. (#3824)
This commit is contained in:
committed by
GitHub
parent
a03e9686ea
commit
0827a95bee
@@ -1,6 +1,6 @@
|
|||||||
#nullable enable
|
#nullable enable
|
||||||
|
using Content.Shared.GameObjects.EntitySystems;
|
||||||
using Robust.Shared.GameObjects;
|
using Robust.Shared.GameObjects;
|
||||||
using Robust.Shared.Log;
|
|
||||||
|
|
||||||
namespace Content.Shared.GameObjects.Components
|
namespace Content.Shared.GameObjects.Components
|
||||||
{
|
{
|
||||||
@@ -13,60 +13,7 @@ namespace Content.Shared.GameObjects.Components
|
|||||||
[RegisterComponent]
|
[RegisterComponent]
|
||||||
public sealed class SubFloorHideComponent : Component
|
public sealed class SubFloorHideComponent : Component
|
||||||
{
|
{
|
||||||
[ComponentDependency(nameof(OnAddSnapGrid))]
|
|
||||||
private SnapGridComponent? _snapGridComponent;
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override string Name => "SubFloorHide";
|
public override string Name => "SubFloorHide";
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
protected override void Startup()
|
|
||||||
{
|
|
||||||
base.Startup();
|
|
||||||
|
|
||||||
Owner.EntityManager.EventBus.RaiseEvent(EventSource.Local, new SubFloorHideDirtyEvent(Owner));
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
protected override void Shutdown()
|
|
||||||
{
|
|
||||||
base.Shutdown();
|
|
||||||
|
|
||||||
if (Owner.Transform.Running == false)
|
|
||||||
return;
|
|
||||||
|
|
||||||
if (_snapGridComponent != null)
|
|
||||||
{
|
|
||||||
_snapGridComponent.OnPositionChanged -= SnapGridOnPositionChanged;
|
|
||||||
}
|
|
||||||
|
|
||||||
Owner.EntityManager.EventBus.RaiseEvent(EventSource.Local, new SubFloorHideDirtyEvent(Owner));
|
|
||||||
}
|
|
||||||
|
|
||||||
private void OnAddSnapGrid()
|
|
||||||
{
|
|
||||||
if (_snapGridComponent == null)
|
|
||||||
{
|
|
||||||
// Shouldn't happen but allows us to use nullables. OnPositionChanged needs to be componentbus anyway.
|
|
||||||
Logger.Error("Snapgrid was null for subfloor {Owner}");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
_snapGridComponent.OnPositionChanged += SnapGridOnPositionChanged;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void SnapGridOnPositionChanged()
|
|
||||||
{
|
|
||||||
Owner.EntityManager.EventBus.RaiseEvent(EventSource.Local, new SubFloorHideDirtyEvent(Owner));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
internal sealed class SubFloorHideDirtyEvent : EntityEventArgs
|
|
||||||
{
|
|
||||||
public IEntity Sender { get; }
|
|
||||||
|
|
||||||
public SubFloorHideDirtyEvent(IEntity sender)
|
|
||||||
{
|
|
||||||
Sender = sender;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
#nullable enable
|
#nullable enable
|
||||||
using Content.Shared.GameObjects.Components;
|
using Content.Shared.GameObjects.Components;
|
||||||
using Content.Shared.Maps;
|
using Content.Shared.Maps;
|
||||||
|
using JetBrains.Annotations;
|
||||||
using Robust.Shared.GameObjects;
|
using Robust.Shared.GameObjects;
|
||||||
using Robust.Shared.IoC;
|
using Robust.Shared.IoC;
|
||||||
using Robust.Shared.Map;
|
using Robust.Shared.Map;
|
||||||
@@ -12,6 +13,7 @@ namespace Content.Shared.GameObjects.EntitySystems
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Entity system backing <see cref="SubFloorHideComponent"/>.
|
/// Entity system backing <see cref="SubFloorHideComponent"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
[UsedImplicitly]
|
||||||
public class SubFloorHideSystem : EntitySystem
|
public class SubFloorHideSystem : EntitySystem
|
||||||
{
|
{
|
||||||
[Dependency] private readonly IMapManager _mapManager = default!;
|
[Dependency] private readonly IMapManager _mapManager = default!;
|
||||||
@@ -52,18 +54,44 @@ namespace Content.Shared.GameObjects.EntitySystems
|
|||||||
_mapManager.GridChanged += MapManagerOnGridChanged;
|
_mapManager.GridChanged += MapManagerOnGridChanged;
|
||||||
_mapManager.TileChanged += MapManagerOnTileChanged;
|
_mapManager.TileChanged += MapManagerOnTileChanged;
|
||||||
|
|
||||||
SubscribeLocalEvent<SubFloorHideDirtyEvent>(HandleDirtyEvent);
|
// TODO: Make this sane when EntityStarted becomes a directed event.
|
||||||
|
EntityManager.EntityStarted += OnEntityStarted;
|
||||||
|
|
||||||
|
SubscribeLocalEvent<SubFloorHideComponent, EntityTerminatingEvent>(OnSubFloorTerminating);
|
||||||
|
SubscribeLocalEvent<SubFloorHideComponent, SnapGridPositionChangedEvent>(OnSnapGridPositionChanged);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void HandleDirtyEvent(SubFloorHideDirtyEvent ev)
|
public override void Shutdown()
|
||||||
{
|
{
|
||||||
if (!_mapManager.TryGetGrid(ev.Sender.Transform.GridID, out var grid))
|
base.Shutdown();
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var indices = grid.WorldToTile(ev.Sender.Transform.WorldPosition);
|
_mapManager.GridChanged -= MapManagerOnGridChanged;
|
||||||
UpdateTile(grid, indices);
|
_mapManager.TileChanged -= MapManagerOnTileChanged;
|
||||||
|
|
||||||
|
EntityManager.EntityStarted -= OnEntityStarted;
|
||||||
|
|
||||||
|
UnsubscribeLocalEvent<SubFloorHideComponent, EntityTerminatingEvent>(OnSubFloorTerminating);
|
||||||
|
UnsubscribeLocalEvent<SubFloorHideComponent, SnapGridPositionChangedEvent>(OnSnapGridPositionChanged);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnEntityStarted(object? sender, EntityUid uid)
|
||||||
|
{
|
||||||
|
if (ComponentManager.HasComponent<SubFloorHideComponent>(uid))
|
||||||
|
{
|
||||||
|
UpdateEntity(uid);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void MapManagerOnTileChanged(object? sender, TileChangedEventArgs e)
|
private void MapManagerOnTileChanged(object? sender, TileChangedEventArgs e)
|
||||||
@@ -79,6 +107,14 @@ namespace Content.Shared.GameObjects.EntitySystems
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
|
||||||
private void UpdateTile(IMapGrid grid, Vector2i position)
|
private void UpdateTile(IMapGrid grid, Vector2i position)
|
||||||
{
|
{
|
||||||
var tile = grid.GetTileRef(position);
|
var tile = grid.GetTileRef(position);
|
||||||
|
|||||||
Reference in New Issue
Block a user