Game.EndangerSystem
Assembly:
Game
Namespace:
Game.Events
Type:
class
Base:
GameSystemBase
Summary:
EndangerSystem scans for Endanger event components on entities and consolidates them into InDanger components. It runs a Burst-compiled IJob (EndangerJob) over archetype chunks containing Endanger events, merges multiple Endanger entries per target entity using severity and timing rules, and writes/updates InDanger components via a ModificationBarrier4 command buffer. It also flags entities with EffectsUpdated when danger flags change or when an InDanger component is newly added. This system depends on the simulation frame index to prefer recently expiring states and treats certain building prefabs (schools, hospitals) specially for evacuation handling (converting Evacuate -> UseTransport when appropriate).
{{ This system is intended to be part of the game's event handling pipeline: producers create Endanger components (events) and this system converts and consolidates those into persistent InDanger state components used by other systems (e.g., agents, UI, effects). It is optimized for Burst, jobs, and ECS-style command buffering. }}
Fields
-
private SimulationSystem m_SimulationSystem
{{ Holds a reference to the SimulationSystem (used for frameIndex). This is obtained in OnCreate and used in the job to compare/adjust end-frame decisions relative to the current simulation frame. }} -
private ModificationBarrier4 m_ModificationBarrier
{{ A barrier system that provides an EntityCommandBuffer for safely adding/updating components from a job. The job writes InDanger components and EffectsUpdated via the command buffer produced by this barrier. }} -
private EntityQuery m_EndangerQuery
{{ An EntityQuery that matches entities with Endanger (and Event) components. The query is used to build an archetype chunk list that the Burst job iterates. The system requires this query for update. }} -
private TypeHandle __TypeHandle
{{ Internal struct that caches ComponentTypeHandle and ComponentLookup handles for Endanger, PrefabRef, School, Hospital, and InDanger. These handles are assigned in OnCreateForCompiler and are used to obtain the proper ComponentTypeHandle/ComponentLookup instances for the job. }} -
private struct EndangerJob
(nested)
{{ Burst-compiled job that does the main work: iterates Endanger components across chunks, builds a NativeParallelHashMap keyed by target Entity, merges multiple Endanger entries for a single target using EventUtils.IsWorse and frame heuristics, checks PrefabRef/School/Hospital to adjust DangerFlags (Evacuate -> UseTransport), then writes or updates InDanger components via the provided EntityCommandBuffer and marks EffectsUpdated when flags change or a component is added. The job uses temporary native containers allocated per Execute call. }} -
private struct TypeHandle
(nested)
{{ Helper type that stores component type/lookups for use in jobs and provides an __AssignHandles method to populate them from the SystemState. }}
Properties
- None.
{{ This system exposes no public properties. It operates entirely via ECS types, queries, and internal state. }}
Constructors
public EndangerSystem()
{{ Default constructor. The system relies on OnCreate to initialize internal references and queries. }}
Methods
-
protected override void OnCreate()
: System.Void
{{ Initializes the system: obtains a SimulationSystem, a ModificationBarrier4, prepares the entity query for Endanger events, and calls RequireForUpdate to ensure the system only updates when the query has matches. Also sets up any other required references. }} -
protected override void OnUpdate()
: System.Void
{{ Schedules the EndangerJob: - Converts the m_EndangerQuery into a NativeList
asynchronously. - Fills the EndangerJob with chunk list, simulation frame index, component handles/lookups (Endanger, PrefabRef, School, Hospital, InDanger), and an EntityCommandBuffer from the modification barrier.
- Schedules the job with dependency chaining and disposes the chunk list after scheduling.
-
Registers the job handle with the modification barrier and sets base.Dependency to the job handle. This method drives the conversion of Endanger events into persistent InDanger components on entities. }}
-
protected override void OnCreateForCompiler()
: System.Void
{{ Compiler helper that assigns queries and component handles on systems compiled for burst/jobs. It calls __AssignQueries and assigns handles in the cached TypeHandle. }} -
private void __AssignQueries(ref SystemState state)
: System.Void
{{ Internal method (compiler-generated pattern) used to initialize or validate queries at compile-time/run-time. In this compiled source it creates and disposes an EntityQueryBuilder. }} -
private struct TypeHandle.__AssignHandles(ref SystemState state)
: System.Void
{{ Assigns ComponentTypeHandle/ComponentLookup instances for the Endanger, PrefabRef, School, Hospital, and InDanger component types using the provided SystemState. Used to prepare job-safe handles. }}
Usage Example
// Example: Mark a building/entity as being in danger — the EndangerSystem will pick this up
// and convert it into (or update) an InDanger component for that entity on the next update.
var entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;
Entity targetBuilding = /* some building entity */;
// Create an Endanger event component (fields depend on the Endanger struct definition)
// Example pseudo-fields: m_Target, m_Event, m_Flags, m_EndFrame
Endanger endangerEvent = new Endanger {
m_Target = targetBuilding,
m_Event = SomeEventType.SomeId,
m_Flags = DangerFlags.Evacuate,
m_EndFrame = (uint)(simulationFrame + 300)
};
// Add the Endanger component to the entity (or a separate event entity)
entityManager.AddComponentData(targetBuilding, endangerEvent);
// On the next EndangerSystem update, the EndangerJob will run, consolidate events,
// and add or update the InDanger component on targetBuilding, setting EffectsUpdated if flags changed.
{{ Notes and tips: - This system is optimized for Burst and jobified execution; avoid reading/writing component data on the main thread for entities covered by this system during the same frame. - The job uses temporary NativeParallelHashMap and NativeArray instances; these are allocated per-execution and intended to be short-lived. - The system considers building type (School, Hospital) and PrefabRef to adjust evacuation behavior (adds UseTransport when appropriate). - When multiple Endanger events target the same entity, the system prefers the "worse" set of flags (via EventUtils.IsWorse) or chooses updates when existing InDanger is about to expire (endFrame < currentFrame + 64). }}