Game.DestroySystem
Assembly: Game (in-game assembly / Game.dll)
Namespace: Game.Objects
Type: class
Base: GameSystemBase
Summary:
DestroySystem processes Destroy event entities and applies destruction logic to target objects (entities). It:
- Marks objects as Destroyed and adds Updated so other systems react.
- Spawns collapse/area entities (collapsed surface / area prefabs) for physical prefabs that have lots and physical geometry, and initializes their nodes.
- Optionally adds InterpolatedTransform so destruction animations/transitions can be handled if the object has a transform and a collapse delay.
- Removes certain building-specific components from destroyed buildings and enqueues updates for road-edge connection graphs (electricity and water) when building road edges are affected.
- Recursively processes sub-objects of destroyed objects (except sub-objects that are themselves buildings).
- Uses a ModificationBarrier2 EntityCommandBuffer to defer structural changes safely from the job.
Fields
-
private ModificationBarrier2 m_ModificationBarrier
Used to create an EntityCommandBuffer for deferred structural modifications (add/remove components, create entities). -
private ElectricityRoadConnectionGraphSystem m_ElectricityRoadConnectionGraphSystem
Reference to the electricity connection graph system; provides a queue to enqueue road-edge updates for electricity consumers on destroyed buildings. -
private WaterPipeRoadConnectionGraphSystem m_WaterPipeRoadConnectionGraphSystem
Reference to the water connection graph system; provides a queue to enqueue road-edge updates for water consumers on destroyed buildings. -
private EntityQuery m_EventQuery
Query used to find Event + Destroy entities to process. -
private EntityQuery m_BuildingConfigurationQuery
Query for singleton BuildingConfigurationData used when spawning collapsed area prefabs. -
private ComponentTypeSet m_DestroyedBuildingComponents
Set of building-related component types (ElectricityConsumer, WaterConsumer, GarbageProducer, MailProducer) that are removed from destroyed buildings. -
private TypeHandle __TypeHandle
Internal cached handles (component type handles and buffer lookups) used to set up the job's ComponentTypeHandle/ComponentLookup/BufferLookup instances. -
private struct DestroyObjectsJob
(nested)
Burst-compiled IJobChunk that performs the core destruction processing for each chunk of Destroy events. It: - Reads Destroy components and associated components/lookups.
- Uses a NativeHashSet to avoid processing the same object multiple times.
- Creates area entities when needed, fills node buffers, marks area components, and flags Deleted for clipped/air-space areas.
- Adds Destroyed, InterpolatedTransform (when collapse time present), and Updated components to destroyed objects.
- Removes building-specific components and enqueues road-edge updates.
-
Recursively processes SubObject buffers.
-
private struct TypeHandle
(nested)
Helper struct that caches ComponentTypeHandle, ComponentLookup, and BufferLookup instances required by the job and assigns them in OnCreateForCompiler / before scheduling.
Properties
- (No public properties on this system)
Notes: - The system relies on a number of ComponentLookup/BufferLookup instances assigned through the internal TypeHandle for its job.
Constructors
public DestroySystem()
Default public constructor. System is created via the World/Systems bootstrap as usual for ECS systems. The real initialization happens in OnCreate.
Methods
protected override void OnCreate()
Sets up required systems and entity queries:- Gets/creates ModificationBarrier2, ElectricityRoadConnectionGraphSystem, WaterPipeRoadConnectionGraphSystem.
- Creates m_EventQuery to require Event + Destroy for updates.
- Creates m_BuildingConfigurationQuery to obtain BuildingConfigurationData singleton.
- Initializes m_DestroyedBuildingComponents with building-relevant component types.
-
Calls RequireForUpdate for the queries so the system only runs when relevant.
-
protected override void OnUpdate()
Builds and schedules the Burst-compiled DestroyObjectsJob: - Assigns component lookups/type handles from the cached TypeHandle.
- Creates a temporary NativeHashSet to track processed objects within the job.
- Uses the ModificationBarrier2 command buffer to apply structural changes deferred from the job.
- Gets edge-update queues from the connection graph systems and includes their dependencies.
- Schedules the job over the m_EventQuery and wires dependencies into base.Dependency.
-
Disposes the temporary NativeHashSet (scheduled with the job) and registers the job handle with the modification barrier and the connection graph systems as queue writers.
-
protected override void OnCreateForCompiler()
Compiler helper that assigns query and type handle state when building with job/codegen. Calls __AssignQueries and __TypeHandle.__AssignHandles. -
private void __AssignQueries(ref SystemState state)
Internal method used for compiler/codegen to ensure EntityQueryBuilder is assigned (no-op in runtime flow in this class; used during AOT/codegen). -
(Nested job)
DestroyObjectsJob.Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex, bool useEnabledMask, in v128 chunkEnabledMask)
Processes each Destroy component in the chunk. For each Destroy: - Performs checks for existing Destroyed, processed set, and PrefabRef presence.
- If object geometry indicates physical + has lot, computes collapse time, spawns a collapsed surface area entity (using area prefab selection), fills Node buffer positions across the sub-mesh bounds transformed into world space.
- Sets Deleted on clipped/air-space subareas where appropriate.
- Adds Destroyed / InterpolatedTransform (if needed) / Updated components to the destroyed entity.
- Removes building-specific components for buildings and enqueues road-edge updates.
-
Recurses into SubObject buffers for non-building sub-objects to destroy them as well.
-
(Nested job helper)
DestroyObjectsJob.DestroyObject(ref Random random, Entity entity, Destroy destroyEvent)
Internal method implementing object destruction logic described above. Called from Execute() and for recursion. -
(Nested job helper)
DestroyObjectsJob.IsAnyOnGround(DynamicBuffer<Node> nodes)
Checks Node buffer to determine whether any node is on ground (returns true if any node has elevation == float.MinValue which indicates ground in this code).
Usage Example
// Example: How to enqueue a Destroy event for an object (simplified).
// The DestroySystem processes entities that have both Event and Destroy components.
//
// 'entityManager' is a reference to Unity.Entities.EntityManager,
// and 'targetEntity' is the entity you want to destroy (e.g. a building or object).
// Create a new event entity and add Event + Destroy components.
var eventEntity = entityManager.CreateEntity(typeof(Event), typeof(Destroy));
// Populate the Destroy component.
// The Destroy struct in-game contains at least: Entity m_Object; int m_Event; (names used by the system)
// Replace the m_Event value with the appropriate event id for your mod/game logic.
entityManager.SetComponentData(eventEntity, new Destroy
{
m_Object = targetEntity,
m_Event = 0 // example event id; use the proper value for your use-case
});
// On the next simulation update, DestroySystem will process the event and apply destruction logic.
// Note: In actual mod code you must reference the correct Destroy struct and event ids from the game's assemblies.
Additional notes / tips: - DestroySystem uses a Burst IJobChunk. To trigger destruction reliably during gameplay, ensure you create the event entity with both Event and Destroy components (Event is used to mark that this is a transient event entity). - Because the system uses a ModificationBarrier2 command buffer, structural changes happen safely after the job finishes; if you need to read resulting components immediately, you must respect system/job dependencies. - If your mod needs to intercept or override destruction behavior, consider adding a small system that runs before DestroySystem (by controlling update order) to inspect/remove Destroy events or add custom components consumed by custom systems.