using System; using Robust.Shared.Interfaces.GameObjects; using Robust.Shared.Interfaces.Map; using Robust.Shared.IoC; using Robust.Shared.Log; using Robust.Shared.Map; using Robust.Shared.Serialization; using Robust.Shared.ViewVariables; namespace Robust.Shared.GameObjects.Components.Map { /// /// Represents a map grid inside the ECS system. /// internal interface IMapGridComponent : IComponent { GridId GridIndex { get; } IMapGrid Grid { get; } void ClearGridId(); } /// internal class MapGridComponent : Component, IMapGridComponent { [Dependency] private readonly IMapManager _mapManager = default!; [ViewVariables(VVAccess.ReadOnly)] private GridId _gridIndex; /// public override string Name => "MapGrid"; /// public override uint? NetID => NetIDs.MAP_GRID; /// public GridId GridIndex { get => _gridIndex; internal set => _gridIndex = value; } /// public IMapGrid Grid => _mapManager.GetGrid(_gridIndex); public void ClearGridId() { _gridIndex = GridId.Invalid; } public override void OnRemove() { if(GridIndex != GridId.Invalid) { if(_mapManager.GridExists(_gridIndex)) { Logger.DebugS("map", $"Entity {Owner.Uid} removed grid component, removing bound grid {_gridIndex}"); _mapManager.DeleteGrid(_gridIndex); } } base.OnRemove(); } /// public override ComponentState GetComponentState() { return new MapGridComponentState(_gridIndex); } /// public override void HandleComponentState(ComponentState? curState, ComponentState? nextState) { base.HandleComponentState(curState, nextState); if (!(curState is MapGridComponentState state)) return; _gridIndex = state.GridIndex; } /// public override void ExposeData(ObjectSerializer serializer) { base.ExposeData(serializer); serializer.DataField(ref _gridIndex, "index", GridId.Invalid); } } /// /// Serialized state of a . /// [Serializable, NetSerializable] internal class MapGridComponentState : ComponentState { /// /// Index of the grid this component is linked to. /// public GridId GridIndex { get; } public override uint NetID => NetIDs.MAP_GRID; /// /// Constructs a new instance of . /// /// Index of the grid this component is linked to. public MapGridComponentState(GridId gridIndex) { GridIndex = gridIndex; } } }