AtmosphereSystem no longer creates a component manually. (#3839)

- Maps get SpaceGridAtmosphereComponent added automatically
This commit is contained in:
Vera Aguilera Puerto
2021-04-13 13:17:10 +02:00
committed by GitHub
parent 30d5b58319
commit c17426dfa7
21 changed files with 97 additions and 257 deletions

View File

@@ -5,6 +5,7 @@ using System.Linq;
using Content.Server.Atmos;
using Content.Server.Atmos.Reactions;
using Content.Server.GameObjects.Components.Atmos;
using Content.Server.GameObjects.Components.NodeContainer.Nodes;
using Content.Shared;
using Content.Shared.Atmos;
using Content.Shared.GameObjects.EntitySystems.Atmos;
@@ -31,7 +32,6 @@ namespace Content.Server.GameObjects.EntitySystems
private GasReactionPrototype[] _gasReactions = Array.Empty<GasReactionPrototype>();
private SpaceGridAtmosphereComponent _spaceAtmos = default!;
private GridTileLookupSystem? _gridTileLookup = null;
/// <summary>
@@ -51,10 +51,7 @@ namespace Content.Server.GameObjects.EntitySystems
_gasReactions = _protoMan.EnumeratePrototypes<GasReactionPrototype>().ToArray();
Array.Sort(_gasReactions, (a, b) => b.Priority.CompareTo(a.Priority));
_spaceAtmos = new SpaceGridAtmosphereComponent();
_spaceAtmos.Initialize();
IoCManager.InjectDependencies(_spaceAtmos);
_mapManager.MapCreated += OnMapCreated;
_mapManager.TileChanged += OnTileChanged;
Array.Resize(ref _gasSpecificHeats, MathHelper.NextMultipleOf(Atmospherics.TotalNumberOfGases, 4));
@@ -116,6 +113,8 @@ namespace Content.Server.GameObjects.EntitySystems
{
base.Shutdown();
_mapManager.MapCreated -= OnMapCreated;
EntityManager.EventBus.UnsubscribeEvent<RotateEvent>(EventSource.Local, this);
}
@@ -127,18 +126,39 @@ namespace Content.Server.GameObjects.EntitySystems
}
}
public IGridAtmosphereComponent GetGridAtmosphere(GridId gridId)
public IGridAtmosphereComponent? GetGridAtmosphere(GridId gridId)
{
if (!gridId.IsValid())
return null;
if (!_mapManager.TryGetGrid(gridId, out var grid))
return null;
return ComponentManager.TryGetComponent(grid.GridEntityId, out IGridAtmosphereComponent? gridAtmosphere)
? gridAtmosphere : null;
}
public IGridAtmosphereComponent GetGridAtmosphere(EntityCoordinates coordinates)
{
return GetGridAtmosphere(coordinates.ToMap(EntityManager));
}
public IGridAtmosphereComponent GetGridAtmosphere(MapCoordinates coordinates)
{
if (coordinates.MapId == MapId.Nullspace)
{
return _spaceAtmos;
throw new ArgumentException($"Coordinates cannot be in nullspace!", nameof(coordinates));
}
var grid = _mapManager.GetGrid(gridId);
if (_mapManager.TryFindGridAt(coordinates, out var grid))
{
if (ComponentManager.TryGetComponent(grid.GridEntityId, out IGridAtmosphereComponent? atmos))
{
return atmos;
}
}
if (!EntityManager.TryGetEntity(grid.GridEntityId, out var gridEnt)) return _spaceAtmos;
return gridEnt.TryGetComponent(out IGridAtmosphereComponent? atmos) ? atmos : _spaceAtmos;
return _mapManager.GetMapEntity(coordinates.MapId).GetComponent<IGridAtmosphereComponent>();
}
public override void Update(float frameTime)
@@ -164,7 +184,18 @@ namespace Content.Server.GameObjects.EntitySystems
return;
}
GetGridAtmosphere(eventArgs.NewTile.GridIndex)?.Invalidate(eventArgs.NewTile.GridIndices);
GetGridAtmosphere(eventArgs.NewTile.GridPosition(_mapManager))?.Invalidate(eventArgs.NewTile.GridIndices);
}
private void OnMapCreated(object? sender, MapEventArgs e)
{
if (e.Map == MapId.Nullspace)
return;
var map = _mapManager.GetMapEntity(e.Map);
if (!map.HasComponent<IGridAtmosphereComponent>())
map.AddComponent<SpaceGridAtmosphereComponent>();
}
}
}