using System;
using Robust.Shared.IoC;
using Robust.Shared.Log;
using Robust.Shared.Map;
using Robust.Shared.Prototypes;
using Robust.Shared.Players;
using Robust.Shared.Serialization;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.ViewVariables;
namespace Robust.Shared.GameObjects
{
///
/// Represents a map grid inside the ECS system.
///
public 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)]
[DataField("index")]
private GridId _gridIndex = GridId.Invalid;
///
public override string Name => "MapGrid";
///
public override uint? NetID => NetIDs.MAP_GRID;
///
public GridId GridIndex
{
get => _gridIndex;
internal set => _gridIndex = value;
}
///
[ViewVariables]
public IMapGrid Grid => _mapManager.GetGrid(_gridIndex);
public void ClearGridId()
{
_gridIndex = GridId.Invalid;
}
///
///
public override ComponentState GetComponentState(ICommonSession player)
{
return new MapGridComponentState(_gridIndex, Grid.HasGravity);
}
///
public override void HandleComponentState(ComponentState? curState, ComponentState? nextState)
{
base.HandleComponentState(curState, nextState);
if (!(curState is MapGridComponentState state))
return;
_gridIndex = state.GridIndex;
Grid.HasGravity = state.HasGravity;
}
}
///
/// Serialized state of a .
///
[Serializable, NetSerializable]
internal class MapGridComponentState : ComponentState
{
///
/// Index of the grid this component is linked to.
///
public GridId GridIndex { get; }
public bool HasGravity { get; }
///
/// Constructs a new instance of .
///
/// Index of the grid this component is linked to.
public MapGridComponentState(GridId gridIndex, bool hasGravity)
: base(NetIDs.MAP_GRID)
{
GridIndex = gridIndex;
HasGravity = hasGravity;
}
}
}