using Robust.Shared.Utility;
namespace Robust.Shared.GameObjects;
public partial class EntityManager
{
private static readonly ComponentAdd CompAddInstance = new();
private static readonly ComponentInit CompInitInstance = new();
private static readonly ComponentStartup CompStartupInstance = new();
private static readonly ComponentShutdown CompShutdownInstance = new();
private static readonly ComponentRemove CompRemoveInstance = new();
///
/// Increases the life stage from to ,
/// after raising a event.
///
internal void LifeAddToEntity(EntityUid uid, T component, CompIdx idx) where T : IComponent
{
DebugTools.Assert(!_deleteSet.Contains(component));
DebugTools.Assert(component.LifeStage == ComponentLifeStage.PreAdd);
#pragma warning disable CS0618 // Type or member is obsolete
component.LifeStage = ComponentLifeStage.Adding;
component.CreationTick = CurrentTick;
// networked components are assumed to be dirty when added to entities. See also: ClearTicks()
component.LastModifiedTick = CurrentTick;
EventBusInternal.RaiseComponentEvent(uid, component, idx, CompAddInstance);
component.LifeStage = ComponentLifeStage.Added;
#pragma warning restore CS0618 // Type or member is obsolete
}
///
/// Increases the life stage from to ,
/// calling .
///
internal void LifeInitialize(EntityUid uid, T component, CompIdx idx) where T : IComponent
{
DebugTools.Assert(!_deleteSet.Contains(component));
DebugTools.Assert(component.LifeStage == ComponentLifeStage.Added);
component.LifeStage = ComponentLifeStage.Initializing;
EventBusInternal.RaiseComponentEvent(uid, component, idx, CompInitInstance);
component.LifeStage = ComponentLifeStage.Initialized;
}
///
/// Increases the life stage from to
/// , calling .
///
internal void LifeStartup(EntityUid uid, T component, CompIdx idx) where T : IComponent
{
DebugTools.Assert(!_deleteSet.Contains(component));
DebugTools.Assert(component.LifeStage == ComponentLifeStage.Initialized);
component.LifeStage = ComponentLifeStage.Starting;
EventBusInternal.RaiseComponentEvent(uid, component, idx, CompStartupInstance);
component.LifeStage = ComponentLifeStage.Running;
}
///
/// Increases the life stage from to ,
/// calling .
///
///
/// Components are allowed to remove themselves in their own Startup function.
///
internal void LifeShutdown(EntityUid uid, T component, CompIdx idx) where T : IComponent
{
DebugTools.Assert(component.LifeStage is >= ComponentLifeStage.Initializing and < ComponentLifeStage.Stopping);
if (component.LifeStage <= ComponentLifeStage.Initialized)
{
// Component was never started, no shutdown logic necessary. Simply mark it as stopped.
component.LifeStage = ComponentLifeStage.Stopped;
return;
}
component.LifeStage = ComponentLifeStage.Stopping;
EventBusInternal.RaiseComponentEvent(uid, component, idx, CompShutdownInstance);
component.LifeStage = ComponentLifeStage.Stopped;
}
///
/// Increases the life stage from to ,
/// calling .
///
internal void LifeRemoveFromEntity(EntityUid uid, T component, CompIdx idx) where T : IComponent
{
// can be called at any time after PreAdd, including inside other life stage events.
DebugTools.Assert(component.LifeStage != ComponentLifeStage.PreAdd);
component.LifeStage = ComponentLifeStage.Removing;
EventBusInternal.RaiseComponentEvent(uid, component, idx, CompRemoveInstance);
component.LifeStage = ComponentLifeStage.Deleted;
}
internal virtual void SetLifeStage(MetaDataComponent meta, EntityLifeStage stage)
{
meta.EntityLifeStage = stage;
}
}