Move some Station methods into shared (#38976)
This commit is contained in:
88
Content.Shared/Station/SharedStationSystem.Tracker.cs
Normal file
88
Content.Shared/Station/SharedStationSystem.Tracker.cs
Normal file
@@ -0,0 +1,88 @@
|
||||
using Content.Shared.Station.Components;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Shared.Map;
|
||||
|
||||
namespace Content.Shared.Station;
|
||||
|
||||
public abstract partial class SharedStationSystem
|
||||
{
|
||||
private void InitializeTracker()
|
||||
{
|
||||
SubscribeLocalEvent<StationTrackerComponent, MapInitEvent>(OnTrackerMapInit);
|
||||
SubscribeLocalEvent<StationTrackerComponent, ComponentRemove>(OnTrackerRemove);
|
||||
SubscribeLocalEvent<StationTrackerComponent, GridUidChangedEvent>(OnTrackerGridChanged);
|
||||
SubscribeLocalEvent<StationTrackerComponent, MetaFlagRemoveAttemptEvent>(OnMetaFlagRemoveAttempt);
|
||||
}
|
||||
|
||||
private void OnTrackerMapInit(Entity<StationTrackerComponent> ent, ref MapInitEvent args)
|
||||
{
|
||||
_meta.AddFlag(ent, MetaDataFlags.ExtraTransformEvents);
|
||||
UpdateStationTracker(ent.AsNullable());
|
||||
}
|
||||
|
||||
private void OnTrackerRemove(Entity<StationTrackerComponent> ent, ref ComponentRemove args)
|
||||
{
|
||||
_meta.RemoveFlag(ent, MetaDataFlags.ExtraTransformEvents);
|
||||
}
|
||||
|
||||
private void OnTrackerGridChanged(Entity<StationTrackerComponent> ent, ref GridUidChangedEvent args)
|
||||
{
|
||||
UpdateStationTracker((ent, ent.Comp, args.Transform));
|
||||
}
|
||||
|
||||
private void OnMetaFlagRemoveAttempt(Entity<StationTrackerComponent> ent, ref MetaFlagRemoveAttemptEvent args)
|
||||
{
|
||||
if ((args.ToRemove & MetaDataFlags.ExtraTransformEvents) != 0 &&
|
||||
ent.Comp.LifeStage <= ComponentLifeStage.Running)
|
||||
{
|
||||
args.ToRemove &= ~MetaDataFlags.ExtraTransformEvents;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates the station tracker component based on entity's current location.
|
||||
/// </summary>
|
||||
[PublicAPI]
|
||||
public void UpdateStationTracker(Entity<StationTrackerComponent?, TransformComponent?> ent)
|
||||
{
|
||||
if (!Resolve(ent, ref ent.Comp1))
|
||||
return;
|
||||
|
||||
var xform = ent.Comp2;
|
||||
|
||||
if (!_xformQuery.Resolve(ent, ref xform))
|
||||
return;
|
||||
|
||||
// Entity is in nullspace or not on a grid
|
||||
if (xform.MapID == MapId.Nullspace || xform.GridUid == null)
|
||||
{
|
||||
SetStation(ent, null);
|
||||
return;
|
||||
}
|
||||
|
||||
// Check if the grid is part of a station
|
||||
if (!_stationMemberQuery.TryGetComponent(xform.GridUid.Value, out var stationMember))
|
||||
{
|
||||
SetStation(ent, null);
|
||||
return;
|
||||
}
|
||||
|
||||
SetStation(ent, stationMember.Station);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the station for a StationTrackerComponent.
|
||||
/// </summary>
|
||||
[PublicAPI]
|
||||
public void SetStation(Entity<StationTrackerComponent?> ent, EntityUid? station)
|
||||
{
|
||||
if (!Resolve(ent, ref ent.Comp))
|
||||
return;
|
||||
|
||||
if (ent.Comp.Station == station)
|
||||
return;
|
||||
|
||||
ent.Comp.Station = station;
|
||||
Dirty(ent);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user