Files
RobustToolbox/Robust.Shared/GameObjects/Components/Map/MapComponent.cs
T
AcruidandGitHub 6b9b56ca83 Grid Components Allocate Grids (#2597)
* Deleting a map is now routed through MapComponent deletion.

* Remove map and grid deletions from map networking, just delete the entity if you want to remove the map.

* Moved the chunkSize property of created grids from the networked MapData to the MapGridComponent state.

* Remove unused IMapManager.DefaultMap property.

* Removed MapCreationTick field from MapManager.MapCollection.

* Removed _maps hashset field from MapManager.

* Removed CreatedMaps array from network MapData.

* MapGrid.ParentMapId is now derived from the bound TransformComponent, and isn't required in the MapGrid ctor.
Removed MapGrid.CreatedTick, it can be found on MapGridComponent.CreationTick.

* Remove a bunch of ApplyGameStatePre code duplication.

* Completely refactored CreateGrid.

* Adds AddComponentUninitialized to the ECS system. This allows you to access a component before it is initialized in a using block.

* Use AddComponentUninitialized to allocate a grid after the component is allocated.

* MapLoader now creates the grids after creating the map entities.

* Chunksize and TileSize properties are now actually read out of the map yaml.
TileSize has a public Setter.

* Minor cleanup.

* Moved grid allocation onto the MapGridComponent.

* Final Cleanup.

* Merge Fail.

* Fixed test, grid was getting deleted because it was empty.

* Remove DeletedChunkDatum from grid networking.

* ApplyMapGridState moved from map manager to the MapGridComponent.
2022-03-13 18:28:59 -07:00

109 lines
3.1 KiB
C#

using System;
using Robust.Shared.GameStates;
using Robust.Shared.IoC;
using Robust.Shared.Map;
using Robust.Shared.Serialization;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.ViewVariables;
namespace Robust.Shared.GameObjects
{
/// <summary>
/// Represents a world map inside the ECS system.
/// </summary>
public interface IMapComponent : IComponent
{
bool LightingEnabled { get; set; }
MapId WorldMap { get; }
bool MapPaused { get; internal set; }
bool MapPreInit { get; internal set; }
}
/// <inheritdoc cref="IMapComponent"/>
[ComponentReference(typeof(IMapComponent))]
[NetworkedComponent]
public sealed class MapComponent : Component, IMapComponent
{
[Dependency] private readonly IEntityManager _entMan = default!;
[ViewVariables(VVAccess.ReadOnly)]
[DataField("index")]
private MapId _mapIndex = MapId.Nullspace;
[ViewVariables(VVAccess.ReadWrite)]
[DataField(("lightingEnabled"))]
public bool LightingEnabled { get; set; } = true;
/// <inheritdoc />
public MapId WorldMap
{
get => _mapIndex;
internal set => _mapIndex = value;
}
internal bool MapPaused { get; set; } = false;
/// <inheritdoc />
bool IMapComponent.MapPaused
{
get => this.MapPaused;
set => this.MapPaused = value;
}
internal bool MapPreInit { get; set; } = false;
/// <inheritdoc />
bool IMapComponent.MapPreInit
{
get => this.MapPreInit;
set => this.MapPreInit = value;
}
/// <inheritdoc />
protected override void OnRemove()
{
base.OnRemove();
var mapMan = IoCManager.Resolve<IMapManagerInternal>();
mapMan.TrueDeleteMap(_mapIndex);
}
/// <inheritdoc />
public override ComponentState GetComponentState()
{
return new MapComponentState(_mapIndex, LightingEnabled);
}
/// <inheritdoc />
public override void HandleComponentState(ComponentState? curState, ComponentState? nextState)
{
base.HandleComponentState(curState, nextState);
if (curState is not MapComponentState state)
return;
_mapIndex = state.MapId;
LightingEnabled = state.LightingEnabled;
var xformQuery = _entMan.GetEntityQuery<TransformComponent>();
xformQuery.GetComponent(Owner).ChangeMapId(_mapIndex, xformQuery);
}
}
/// <summary>
/// Serialized state of a <see cref="MapGridComponentState"/>.
/// </summary>
[Serializable, NetSerializable]
internal sealed class MapComponentState : ComponentState
{
public MapId MapId { get; }
public bool LightingEnabled { get; }
public MapComponentState(MapId mapId, bool lightingEnabled)
{
MapId = mapId;
LightingEnabled = lightingEnabled;
}
}
}