Skip to content

Game.Events.ImpactSystem

Assembly: Assembly-CSharp
Namespace: Game.Events

Type: class

Base: GameSystemBase

Summary:
ImpactSystem is a simulation system that processes Impact components on entities and applies their effects to vehicles and creatures. It collects impacts each frame (in a Burst-compiled job), updates Moving/velocity state, transitions parked or stopped vehicles/ trailers/creatures to moving states, marks entities as OutOfControl or Stumbling, and records/queues InvolvedInAccident data (and associated target buffers) via a modification barrier command buffer. The heavy-lifting is done in a nested Burst IJob (AddImpactJob) to be safe and efficient for DOTS/ECS contexts. {{ This system is useful for modders who want to understand or extend how collisions/impacts trigger state transitions (parked->moving, stopped->moving), accident involvement tracking, and basic physical impulse application in Cities: Skylines 2. }}


Fields

  • private ModificationBarrier4 m_ModificationBarrier
    {{ Command-buffer producer used to enqueue structural changes (add/remove/set components and buffers) from the AddImpactJob. Obtained from the world in OnCreate and used to create an EntityCommandBuffer for the job. }}

  • private SimulationSystem m_SimulationSystem
    {{ Reference to the SimulationSystem used to read the current simulation frame index (m_SimulationSystem.frameIndex). This is used when creating InvolvedInAccident timestamps. }}

  • private EntityQuery m_ImpactQuery
    {{ Entity query that finds entities with Impact (and related Event) components. The system RequireForUpdate's on this query so the system only runs when there are Impact components. }}

  • private ComponentTypeSet m_ParkedToMovingCarRemoveTypes
    {{ A ComponentTypeSet describing components to remove when activating a parked car into a moving car. Used with command buffer RemoveComponent in bulk. }}

  • private ComponentTypeSet m_ParkedToMovingPersonalCarAddTypes
    {{ ComponentTypeSet describing components to add when activating a parked personal car into a moving state (e.g., Moving, TransformFrame, InterpolatedTransform, CarNavigation, CarCurrentLane, Swaying, Updated, etc.). }}

  • private ComponentTypeSet m_ParkedToMovingTaxiAddTypes
    {{ ComponentTypeSet for adding components when a parked taxi is activated into moving (similar to personal car but includes ServiceDispatch). }}

  • private ComponentTypeSet m_ParkedToMovingServiceCarAddTypes
    {{ ComponentTypeSet for service vehicles (includes PathInformation, ServiceDispatch, etc.). }}

  • private ComponentTypeSet m_ParkedToMovingTrailerAddTypes
    {{ ComponentTypeSet used when activating a parked trailer into a moving trailer (Moving, TransformFrame, InterpolatedTransform, CarTrailerLane, Swaying, Updated). }}

  • private TypeHandle __TypeHandle
    {{ Internal struct instance that stores ComponentTypeHandle/ComponentLookup/BufferLookup instances used by the job. __TypeHandle.__AssignHandles is called to populate these lookups from the system state. }}


Properties

  • (none)
    {{ The system exposes no public properties. All state is internal/private and accessed via the job and command buffer. }}

Constructors

  • public ImpactSystem()
    {{ Default constructor. The ECS framework (and [Preserve] attributed OnCreate) performs initialization in OnCreate; the constructor itself contains no custom initialization logic. }}

Methods

  • protected override void OnCreate()
    {{ Sets up the ModificationBarrier4 and SimulationSystem references, creates the m_ImpactQuery, initializes the various ComponentTypeSet instances for parked->moving transitions, and calls RequireForUpdate(m_ImpactQuery) so the system runs only when there are Impact components. This is where the system declares the structural changes it may perform. }}

  • protected override void OnUpdate()
    {{ Main scheduling method. Creates an asynchronous archetype-chunk list for entities matching m_ImpactQuery, constructs and schedules the Burst-compiled AddImpactJob with all necessary ComponentLookup/BufferLookup/ComponentTypeHandles and the command buffer from m_ModificationBarrier. Disposes the temporary chunk list and links the job handle to the modification barrier and the system dependency. This is the bridge between ECS main thread and the parallel job that applies impacts. }}

  • private void __AssignQueries(ref SystemState state)
    {{ Internal helper (compiler-generated) stub for assigning queries. Present for compiler/IL-generated patterns; it currently creates and disposes an EntityQueryBuilder. }}

  • protected override void OnCreateForCompiler()
    {{ Internal method called by the compiler-era scaffolding to ensure queries and __TypeHandle are assigned for compiled code paths. It forwards the SystemState and calls __TypeHandle.__AssignHandles. }}

  • (Nested) AddImpactJob (BurstCompile)
    {{ A Burst-compiled IJob that processes all Impact components collected this frame. It:

  • Iterates impact chunks and builds a NativeParallelHashMap combining impacts per target (keeping the highest severity per target).
  • Applies velocity/angVelocity deltas to Moving components (or creates a Moving local to be assigned).
  • Handles vehicle/creature type branching: parked cars/trailers, stopped cars/trailers, creatures — and transitions them using the command buffer (ActivateParkedCar, ActivateParkedCarTrailer, ActivateStoppedCar, ActivateStoppedTrailer, ActivateStoppedCreature).
  • Adds OutOfControl or Stumbling components depending on the target type.
  • Adds or updates InvolvedInAccident component on targets and adds the target to the relevant event target buffer (TargetElement) if appropriate.
  • Uses ComponentLookup and BufferLookup to access and mutate per-entity data safely within the job. }}

  • public void Execute()
    {{ Job entrypoint. Processes the Impact chunks, builds per-target InvolvedInAccident map, applies movement deltas, triggers activation/transition logic and collects/commits InvolvedInAccident components and TargetElement buffers via the command buffer. This method contains the core logic run in parallel (Burst). }}

  • private void DetachVehicle(Entity entity)
    {{ If a trailer or similar entity has a Controller value pointing to another entity, this removes the layout reference from the controller's LayoutElement buffer and clears the Controller reference. Used when impact should detach a trailer. }}

  • private void ActivateParkedCarTrailer(Entity entity, Moving moving, ParkedCar parkedCar)
    {{ Converts a parked trailer entity into a trailer moving state: sets parked lane/curve position if needed, removes parked components, adds trailer moving component set, sets CarTrailerLane from parkedCar and optionally marks the lane with PathfindUpdated. Uses the command buffer to apply changes. }}

  • private void ActivateParkedCar(Entity entity, Moving moving)
    {{ Converts a parked car to moving: removes parked components, adds the appropriate parked->moving set depending on personal/taxi/service, sets Moving and CarCurrentLane (with flags), marks parking/garage lanes for pathfind updates, and activates attached trailers (LayoutElement buffer). }}

  • private void ActivateStoppedCar(Entity entity, Moving moving)
    {{ Reactivates a stopped car: removes Stopped, adds Moving and required transform/interpolation components (TransformFrame buffer, InterpolatedTransform, Swaying, Updated), marks lane(s) PathfindUpdated, and activates stopped trailers present in the LayoutElement buffer. }}

  • private void ActivateStoppedTrailer(Entity entity, Moving moving)
    {{ Reactivates a stopped trailer: remove Stopped and add Moving/transform/interpolation/Swaying/Updated, and mark the trailer lane PathfindUpdated as needed. }}

  • private void ActivateStoppedCreature(Entity entity, Moving moving)
    {{ Reactivates a stopped creature: remove Stopped, add TransformFrame buffer, InterpolatedTransform, Moving, and Updated. Also adds Stumbling earlier in Execute for creatures impacted. }}

  • (Nested) TypeHandle struct and __AssignHandles(ref SystemState state)
    {{ Holds strongly-typed ComponentTypeHandle, ComponentLookup and BufferLookup instances used by AddImpactJob. __AssignHandles fills these using state.GetComponentTypeHandle / GetComponentLookup / GetBufferLookup with the correct read/write permissions. This is part of the standard pattern for caching type handles for jobs. }}


Usage Example

// Example: creating an Impact component on an entity so ImpactSystem will process it.
// Note: Impact struct definition is part of game code; assume it has fields like m_Target, m_Event, m_Severity, m_VelocityDelta, m_AngularVelocityDelta, m_CheckStoppedEvent.

void CreateImpactOnEntity(EntityManager entityManager, Entity sourceEntity, Entity targetEntity, Game.Common.Event eventType)
{
    var impact = new Impact
    {
        m_Target = targetEntity,
        m_Event = eventType,
        m_Severity = 1, // severity scale used by the game
        m_VelocityDelta = new float3(0, 0, 0),
        m_AngularVelocityDelta = new float3(0, 0, 0),
        m_CheckStoppedEvent = false
    };

    entityManager.AddComponentData(sourceEntity, impact);
}

// The ImpactSystem will detect the new Impact component in its query on the next simulation update,
// schedule the AddImpactJob and apply the transitions/accident bookkeeping as described above.

{{ Notes for modders: - AddImpactJob is Burst-compiled and uses ComponentLookup/BufferLookup — modifying those APIs requires following DOTS/ECS safety patterns. - Structural changes are enqueued through the ModificationBarrier4's command buffer; you should not attempt to perform structural changes directly from the job. - To trigger or test behavior, add an Impact component to an entity (as above) where m_Target is the entity you want to affect. - If you need to intercept or extend accident handling (for example to add custom effects when a vehicle becomes OutOfControl), consider subscribing to the same Event/TargetElement buffers or adding your own system that responds to InvolvedInAccident or OutOfControl components, using proper system ordering or barriers to ensure consistent state. }}