ECS grid methods and map partials (#3441)

This commit is contained in:
metalgearsloth
2022-11-13 15:37:01 +11:00
committed by GitHub
parent 45e58c1ed8
commit 72a952fbdd
7 changed files with 131 additions and 141 deletions

View File

@@ -4,7 +4,6 @@ using Robust.Shared.GameStates;
using Robust.Shared.IoC;
using Robust.Shared.Map;
using Robust.Shared.Maths;
using Robust.Shared.Physics;
using Robust.Shared.Serialization;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.Utility;
@@ -16,8 +15,8 @@ namespace Robust.Shared.GameObjects
[NetworkedComponent]
public sealed class MapGridComponent : Component
{
[Dependency] private readonly IMapManagerInternal _mapManager = default!;
[Dependency] private readonly IEntityManager _entMan = default!;
[Dependency] private readonly IMapManagerInternal _mapManager = default!;
// This field is used for deserialization internally in the map loader.
// If you want to remove this, you would have to restructure the map save file.
@@ -29,7 +28,7 @@ namespace Robust.Shared.GameObjects
private IMapGrid? _mapGrid;
[DataField("chunkSize")]
private ushort _chunkSize = 16;
internal ushort ChunkSize = 16;
[ViewVariables]
public IMapGrid Grid
@@ -38,43 +37,6 @@ namespace Robust.Shared.GameObjects
private set => _mapGrid = value;
}
protected override void Initialize()
{
base.Initialize();
var xform = _entMan.GetComponent<TransformComponent>(Owner);
var mapId = xform.MapID;
if (_mapManager.HasMapEntity(mapId))
{
xform.AttachParent(_mapManager.GetMapEntityIdOrThrow(mapId));
}
}
protected override void OnRemove()
{
if (_mapGrid != null)
_mapManager.TrueGridDelete((MapGrid)_mapGrid);
base.OnRemove();
}
/// <inheritdoc />
public override ComponentState GetComponentState()
{
return new MapGridComponentState(Owner, _chunkSize);
}
/// <inheritdoc />
public override void HandleComponentState(ComponentState? curState, ComponentState? nextState)
{
base.HandleComponentState(curState, nextState);
if (curState is not MapGridComponentState state)
return;
_chunkSize = state.ChunkSize;
}
internal MapGrid AllocMapGrid(ushort chunkSize, ushort tileSize)
{
DebugTools.Assert(LifeStage == ComponentLifeStage.Added);
@@ -142,15 +104,9 @@ namespace Robust.Shared.GameObjects
/// <summary>
/// Serialized state of a <see cref="MapGridComponentState"/>.
/// </summary>
#pragma warning disable CS0618
[Serializable, NetSerializable]
internal sealed class MapGridComponentState : ComponentState
{
/// <summary>
/// Index of the grid this component is linked to.
/// </summary>
public EntityUid GridIndex { get; }
/// <summary>
/// The size of the chunks in the map grid.
/// </summary>
@@ -159,13 +115,10 @@ namespace Robust.Shared.GameObjects
/// <summary>
/// Constructs a new instance of <see cref="MapGridComponentState"/>.
/// </summary>
/// <param name="gridIndex">Index of the grid this component is linked to.</param>
/// <param name="chunkSize">The size of the chunks in the map grid.</param>
public MapGridComponentState(EntityUid gridIndex, ushort chunkSize)
public MapGridComponentState(ushort chunkSize)
{
GridIndex = gridIndex;
ChunkSize = chunkSize;
}
}
#pragma warning restore CS0618
}

View File

@@ -0,0 +1,71 @@
using Robust.Shared.GameStates;
using Robust.Shared.Map;
namespace Robust.Shared.GameObjects;
public abstract partial class SharedMapSystem
{
private void InitializeGrid()
{
SubscribeLocalEvent<MapGridComponent, ComponentGetState>(OnGridGetState);
SubscribeLocalEvent<MapGridComponent, ComponentHandleState>(OnGridHandleState);
SubscribeLocalEvent<MapGridComponent, ComponentAdd>(OnGridAdd);
SubscribeLocalEvent<MapGridComponent, ComponentInit>(OnGridInit);
SubscribeLocalEvent<MapGridComponent, ComponentStartup>(OnGridStartup);
SubscribeLocalEvent<MapGridComponent, ComponentShutdown>(OnGridRemove);
}
private void OnGridHandleState(EntityUid uid, MapGridComponent component, ref ComponentHandleState args)
{
if (args.Current is not MapGridComponentState state)
return;
component.ChunkSize = state.ChunkSize;
}
private void OnGridGetState(EntityUid uid, MapGridComponent component, ref ComponentGetState args)
{
args.State = new MapGridComponentState(component.ChunkSize);
}
private void OnGridAdd(EntityUid uid, MapGridComponent component, ComponentAdd args)
{
// GridID is not set yet so we don't include it.
var msg = new GridAddEvent(uid);
RaiseLocalEvent(uid, msg, true);
}
private void OnGridInit(EntityUid uid, MapGridComponent component, ComponentInit args)
{
var xformQuery = GetEntityQuery<TransformComponent>();
var xform = xformQuery.GetComponent(uid);
var mapId = xform.MapID;
if (MapManager.HasMapEntity(mapId))
{
_transform.SetParent(xform, MapManager.GetMapEntityIdOrThrow(mapId), xformQuery);
}
var msg = new GridInitializeEvent(uid);
RaiseLocalEvent(uid, msg, true);
}
private void OnGridStartup(EntityUid uid, MapGridComponent component, ComponentStartup args)
{
var msg = new GridStartupEvent(uid);
RaiseLocalEvent(uid, msg, true);
}
private void OnGridRemove(EntityUid uid, MapGridComponent component, ComponentShutdown args)
{
RaiseLocalEvent(uid, new GridRemovalEvent(uid), true);
if (uid == EntityUid.Invalid)
return;
if (!MapManager.GridExists(uid))
return;
MapManager.DeleteGrid(uid);
}
}

View File

@@ -0,0 +1,51 @@
using Robust.Shared.GameStates;
using Robust.Shared.Map;
namespace Robust.Shared.GameObjects;
public abstract partial class SharedMapSystem
{
private void InitializeMap()
{
SubscribeLocalEvent<MapComponent, ComponentAdd>(OnMapAdd);
SubscribeLocalEvent<MapComponent, ComponentInit>(OnMapInit);
SubscribeLocalEvent<MapComponent, ComponentShutdown>(OnMapRemoved);
SubscribeLocalEvent<MapComponent, ComponentHandleState>(OnMapHandleState);
SubscribeLocalEvent<MapComponent, ComponentGetState>(OnMapGetState);
}
private void OnMapHandleState(EntityUid uid, MapComponent component, ref ComponentHandleState args)
{
if (args.Current is not MapComponentState state)
return;
component.WorldMap = state.MapId;
component.LightingEnabled = state.LightingEnabled;
var xformQuery = GetEntityQuery<TransformComponent>();
xformQuery.GetComponent(uid).ChangeMapId(state.MapId, xformQuery);
}
private void OnMapGetState(EntityUid uid, MapComponent component, ref ComponentGetState args)
{
args.State = new MapComponentState(component.WorldMap, component.LightingEnabled);
}
protected abstract void OnMapAdd(EntityUid uid, MapComponent component, ComponentAdd args);
private void OnMapInit(EntityUid uid, MapComponent component, ComponentInit args)
{
var msg = new MapChangedEvent(component.WorldMap, true);
RaiseLocalEvent(uid, msg, true);
}
private void OnMapRemoved(EntityUid uid, MapComponent component, ComponentShutdown args)
{
var iMap = (IMapManagerInternal)MapManager;
iMap.TrueDeleteMap(component.WorldMap);
var msg = new MapChangedEvent(component.WorldMap, false);
RaiseLocalEvent(uid, msg, true);
}
}

View File

@@ -12,92 +12,18 @@ namespace Robust.Shared.GameObjects
public abstract partial class SharedMapSystem : EntitySystem
{
[Dependency] protected readonly IMapManager MapManager = default!;
[Dependency] private readonly SharedTransformSystem _transform = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<MapComponent, ComponentAdd>(OnMapAdd);
SubscribeLocalEvent<MapComponent, ComponentInit>(OnMapInit);
SubscribeLocalEvent<MapComponent, ComponentShutdown>(OnMapRemoved);
SubscribeLocalEvent<MapComponent, ComponentHandleState>(OnMapHandleState);
SubscribeLocalEvent<MapComponent, ComponentGetState>(OnMapGetState);
SubscribeLocalEvent<MapGridComponent, ComponentAdd>(OnGridAdd);
SubscribeLocalEvent<MapGridComponent, ComponentInit>(OnGridInit);
SubscribeLocalEvent<MapGridComponent, ComponentStartup>(OnGridStartup);
SubscribeLocalEvent<MapGridComponent, ComponentShutdown>(OnGridRemove);
InitializeMap();
InitializeGrid();
SubscribeLocalEvent<MapLightComponent, ComponentGetState>(OnMapLightGetState);
SubscribeLocalEvent<MapLightComponent, ComponentHandleState>(OnMapLightHandleState);
}
private void OnMapHandleState(EntityUid uid, MapComponent component, ref ComponentHandleState args)
{
if (args.Current is not MapComponentState state)
return;
component.WorldMap = state.MapId;
component.LightingEnabled = state.LightingEnabled;
var xformQuery = GetEntityQuery<TransformComponent>();
xformQuery.GetComponent(uid).ChangeMapId(state.MapId, xformQuery);
}
private void OnMapGetState(EntityUid uid, MapComponent component, ref ComponentGetState args)
{
args.State = new MapComponentState(component.WorldMap, component.LightingEnabled);
}
protected abstract void OnMapAdd(EntityUid uid, MapComponent component, ComponentAdd args);
private void OnMapInit(EntityUid uid, MapComponent component, ComponentInit args)
{
var msg = new MapChangedEvent(component.WorldMap, true);
RaiseLocalEvent(uid, msg, true);
}
private void OnMapRemoved(EntityUid uid, MapComponent component, ComponentShutdown args)
{
var iMap = (IMapManagerInternal)MapManager;
iMap.TrueDeleteMap(component.WorldMap);
var msg = new MapChangedEvent(component.WorldMap, false);
RaiseLocalEvent(uid, msg, true);
}
private void OnGridAdd(EntityUid uid, MapGridComponent component, ComponentAdd args)
{
// GridID is not set yet so we don't include it.
var msg = new GridAddEvent(uid);
RaiseLocalEvent(uid, msg, true);
}
private void OnGridInit(EntityUid uid, MapGridComponent component, ComponentInit args)
{
var msg = new GridInitializeEvent(uid);
RaiseLocalEvent(uid, msg, true);
}
private void OnGridStartup(EntityUid uid, MapGridComponent component, ComponentStartup args)
{
var msg = new GridStartupEvent(uid);
RaiseLocalEvent(uid, msg, true);
}
private void OnGridRemove(EntityUid uid, MapGridComponent component, ComponentShutdown args)
{
RaiseLocalEvent(uid, new GridRemovalEvent(uid), true);
if (uid == EntityUid.Invalid)
return;
if (!MapManager.GridExists(uid))
return;
MapManager.DeleteGrid(uid);
}
}
/// <summary>

View File

@@ -44,7 +44,6 @@ namespace Robust.Shared.Map
/// <param name="oldTile">The old tile that got replaced.</param>
void RaiseOnTileChanged(TileRef tileRef, Tile oldTile);
void TrueGridDelete(MapGrid grid);
void TrueDeleteMap(MapId mapId);
void OnGridAllocated(MapGridComponent gridComponent, MapGrid mapGrid);
void OnGridBoundsChange(EntityUid uid, MapGrid grid);

View File

@@ -151,6 +151,8 @@ internal partial class MapManager
public virtual void DeleteGrid(EntityUid euid)
{
_grids.Remove(euid);
#if DEBUG
DebugTools.Assert(_dbgGuardRunning);
#endif
@@ -182,18 +184,6 @@ internal partial class MapManager
EntityManager.DeleteEntity(entityId);
}
public void TrueGridDelete(MapGrid grid)
{
grid.Deleting = true;
var xform = EntityManager.GetComponent<TransformComponent>(grid.GridEntityId);
var mapId = xform.MapID;
_grids.Remove(grid.GridEntityId);
Logger.DebugS("map", $"Deleted grid {grid.GridEntityId}");
}
/// <inheritdoc />
public event EventHandler<TileChangedEventArgs>? TileChanged;

View File

@@ -62,7 +62,7 @@ namespace Robust.UnitTesting.Shared.GameObjects
new EntityUid(512),
new []
{
new ComponentChange(0, new MapGridComponentState(new EntityUid(0), 16), default)
new ComponentChange(0, new MapGridComponentState(16), default)
}, default);
serializer.Serialize(stream, payload);