Shared subfloor system (#3590)
* Shared subfloor system Will also cull the broadphase for server a lot. * Nullable subfloor * Snapgrid nullable * Actually use ComponentDependency Co-authored-by: Metal Gear Sloth <metalgearsloth@gmail.com>
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using Content.Shared.GameObjects.Components;
|
||||
using Content.Shared.GameObjects.Components.Disposal;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Client.GameObjects;
|
||||
|
||||
@@ -1,76 +0,0 @@
|
||||
using System.Diagnostics;
|
||||
using Robust.Client.GameObjects;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Utility;
|
||||
|
||||
namespace Content.Client.GameObjects.Components
|
||||
{
|
||||
/// <summary>
|
||||
/// Simple component that automatically hides the sibling
|
||||
/// <see cref="ISpriteComponent" /> when the tile it's on is not a sub floor
|
||||
/// (plating).
|
||||
/// </summary>
|
||||
/// <seealso cref="P:Content.Shared.Maps.ContentTileDefinition.IsSubFloor" />
|
||||
[RegisterComponent]
|
||||
public sealed class SubFloorHideComponent : Component
|
||||
{
|
||||
[ComponentDependency(nameof(OnAddSnapGrid))]
|
||||
private SnapGridComponent? _snapGridComponent;
|
||||
|
||||
/// <inheritdoc />
|
||||
public override string Name => "SubFloorHide";
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
_snapGridComponent = Owner.GetComponent<SnapGridComponent>();
|
||||
}
|
||||
|
||||
/// <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()
|
||||
{
|
||||
DebugTools.AssertNotNull(_snapGridComponent);
|
||||
_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,107 +0,0 @@
|
||||
using Content.Client.GameObjects.Components;
|
||||
using Content.Shared.Maps;
|
||||
using Robust.Client.GameObjects;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Map;
|
||||
using Robust.Shared.Maths;
|
||||
using Robust.Shared.ViewVariables;
|
||||
|
||||
namespace Content.Client.GameObjects.EntitySystems
|
||||
{
|
||||
/// <summary>
|
||||
/// Entity system backing <see cref="SubFloorHideComponent"/>.
|
||||
/// </summary>
|
||||
internal sealed class SubFloorHideSystem : EntitySystem
|
||||
{
|
||||
[Dependency] private readonly IMapManager _mapManager = default!;
|
||||
[Dependency] private readonly ITileDefinitionManager _tileDefinitionManager = default!;
|
||||
|
||||
private bool _enableAll;
|
||||
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
public bool EnableAll
|
||||
{
|
||||
get => _enableAll;
|
||||
set
|
||||
{
|
||||
_enableAll = value;
|
||||
|
||||
UpdateAll();
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateAll()
|
||||
{
|
||||
foreach (var comp in EntityManager.ComponentManager.EntityQuery<SubFloorHideComponent>(true))
|
||||
{
|
||||
if (!_mapManager.TryGetGrid(comp.Owner.Transform.GridID, out var grid)) return;
|
||||
|
||||
var snapPos = comp.Owner.GetComponent<SnapGridComponent>();
|
||||
UpdateTile(grid, snapPos.Position);
|
||||
}
|
||||
}
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
IoCManager.InjectDependencies(this);
|
||||
|
||||
_mapManager.GridChanged += MapManagerOnGridChanged;
|
||||
_mapManager.TileChanged += MapManagerOnTileChanged;
|
||||
|
||||
SubscribeLocalEvent<SubFloorHideDirtyEvent>(HandleDirtyEvent);
|
||||
}
|
||||
|
||||
private void HandleDirtyEvent(SubFloorHideDirtyEvent ev)
|
||||
{
|
||||
if (!_mapManager.TryGetGrid(ev.Sender.Transform.GridID, out var grid))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var indices = grid.WorldToTile(ev.Sender.Transform.WorldPosition);
|
||||
UpdateTile(grid, indices);
|
||||
}
|
||||
|
||||
private void MapManagerOnTileChanged(object? sender, TileChangedEventArgs e)
|
||||
{
|
||||
UpdateTile(_mapManager.GetGrid(e.NewTile.GridIndex), e.NewTile.GridIndices);
|
||||
}
|
||||
|
||||
private void MapManagerOnGridChanged(object? sender, GridChangedEventArgs e)
|
||||
{
|
||||
foreach (var modified in e.Modified)
|
||||
{
|
||||
UpdateTile(e.Grid, modified.position);
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateTile(IMapGrid grid, Vector2i position)
|
||||
{
|
||||
var tile = grid.GetTileRef(position);
|
||||
var tileDef = (ContentTileDefinition) _tileDefinitionManager[tile.Tile.TypeId];
|
||||
foreach (var snapGridComponent in grid.GetSnapGridCell(position, SnapGridOffset.Center))
|
||||
{
|
||||
var entity = snapGridComponent.Owner;
|
||||
if (!entity.TryGetComponent(out SubFloorHideComponent? subFloorComponent))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var enabled = EnableAll || !subFloorComponent.Running || tileDef.IsSubFloor;
|
||||
|
||||
if (entity.TryGetComponent(out ISpriteComponent? spriteComponent))
|
||||
{
|
||||
spriteComponent.Visible = enabled;
|
||||
}
|
||||
|
||||
if (entity.TryGetComponent(out PhysicsComponent? physicsComponent))
|
||||
{
|
||||
physicsComponent.CanCollide = enabled;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user