Files
RobustToolbox/Robust.Shared/GameObjects/Components/Map/MapGridComponent.cs
Acruid dadd7b4cc3 Remove Static Component NetIds (#1842)
* ComponentNames are not sent over the network when components are created.

* Removed ComponentStates array from EntityState, now the state is stored directly inside the CompChange struct.

* Remove the unnecessary NetID property from ComponentState.

* Remove Component.NetworkSynchronizeExistence.

* Change GetNetComponents to return both the component and the component NetId.

* Remove public usages of the Component.NetID property.

* Adds the NetIDAttribute that can be applied to components.

* Removed Component.NetID.

* Revert changes to GetComponentState and how prediction works.

* Adds component netID automatic generation.

* Modifies ClientConsoleHost so that commands can be called before Initialize().

* Completely remove static NetIds.

* Renamed NetIDAttribute to NetworkedComponentAttribute.

* Fixing unit tests.
2021-07-12 10:23:13 +02:00

152 lines
4.6 KiB
C#

using System;
using Robust.Shared.GameStates;
using Robust.Shared.IoC;
using Robust.Shared.Map;
using Robust.Shared.Maths;
using Robust.Shared.Physics;
using Robust.Shared.Players;
using Robust.Shared.Serialization;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.ViewVariables;
namespace Robust.Shared.GameObjects
{
/// <summary>
/// Represents a map grid inside the ECS system.
/// </summary>
public interface IMapGridComponent : IComponent
{
GridId GridIndex { get; }
IMapGrid Grid { get; }
void ClearGridId();
bool AnchorEntity(ITransformComponent transform);
void UnanchorEntity(ITransformComponent transform);
}
/// <inheritdoc cref="IMapGridComponent"/>
[ComponentReference(typeof(IMapGridComponent))]
[NetworkedComponent()]
internal class MapGridComponent : Component, IMapGridComponent
{
[Dependency] private readonly IMapManager _mapManager = default!;
[ViewVariables(VVAccess.ReadOnly)]
[DataField("index")]
private GridId _gridIndex = GridId.Invalid;
/// <inheritdoc />
public override string Name => "MapGrid";
/// <inheritdoc />
public GridId GridIndex
{
get => _gridIndex;
internal set => _gridIndex = value;
}
/// <inheritdoc />
[ViewVariables]
public IMapGrid Grid => _mapManager.GetGrid(_gridIndex);
/// <inheritdoc />
public void ClearGridId()
{
_gridIndex = GridId.Invalid;
}
protected override void Initialize()
{
base.Initialize();
var mapId = Owner.Transform.MapID;
if (_mapManager.HasMapEntity(mapId))
{
Owner.Transform.AttachParent(_mapManager.GetMapEntity(mapId));
}
}
/// <inheritdoc />
public bool AnchorEntity(ITransformComponent transform)
{
var xform = (TransformComponent) transform;
var tileIndices = Grid.TileIndicesFor(transform.Coordinates);
var result = Grid.AddToSnapGridCell(tileIndices, transform.Owner.Uid);
if (result)
{
xform.Parent = Owner.Transform;
// anchor snapping
xform.LocalPosition = Grid.GridTileToLocal(Grid.TileIndicesFor(xform.LocalPosition)).Position;
xform.SetAnchored(result);
if (xform.Owner.TryGetComponent<PhysicsComponent>(out var physicsComponent))
{
physicsComponent.BodyType = BodyType.Static;
}
}
return result;
}
/// <inheritdoc />
public void UnanchorEntity(ITransformComponent transform)
{
//HACK: Client grid pivot causes this.
//TODO: make grid components the actual grid
if(GridIndex == GridId.Invalid)
return;
var xform = (TransformComponent)transform;
var tileIndices = Grid.TileIndicesFor(transform.Coordinates);
Grid.RemoveFromSnapGridCell(tileIndices, transform.Owner.Uid);
xform.SetAnchored(false);
if (xform.Owner.TryGetComponent<PhysicsComponent>(out var physicsComponent))
{
physicsComponent.BodyType = BodyType.Dynamic;
}
}
/// <param name="player"></param>
/// <inheritdoc />
public override ComponentState GetComponentState(ICommonSession player)
{
return new MapGridComponentState(_gridIndex);
}
/// <inheritdoc />
public override void HandleComponentState(ComponentState? curState, ComponentState? nextState)
{
base.HandleComponentState(curState, nextState);
if (curState is not MapGridComponentState state)
return;
_gridIndex = state.GridIndex;
}
}
/// <summary>
/// Serialized state of a <see cref="MapGridComponentState"/>.
/// </summary>
[Serializable, NetSerializable]
internal class MapGridComponentState : ComponentState
{
/// <summary>
/// Index of the grid this component is linked to.
/// </summary>
public GridId GridIndex { get; }
/// <summary>
/// Constructs a new instance of <see cref="MapGridComponentState"/>.
/// </summary>
/// <param name="gridIndex">Index of the grid this component is linked to.</param>
public MapGridComponentState(GridId gridIndex)
{
GridIndex = gridIndex;
}
}
}