Skip to main content

DamageOnCollisionSystem.ixx File

System that applies damage when solid collisions occur. More...

Included Headers

Namespaces Index

namespacehelios
namespaceengine

Main engine module aggregating core infrastructure and game systems. More...

namespacemechanics

High-level gameplay systems and components for game logic. More...

namespacedamage

Damage dealing system for game entities. More...

namespacesystems

Systems for damage processing. More...

Classes Index

classDamageOnCollisionSystem

Emits HealthChangedEvents when solid collisions involve damage dealers. More...

Macro Definitions Index

#defineHELIOS_LOG_SCOPE   "helios::engine::mechanics::damage::systems::DamageOnCollisionSystem"

Description

System that applies damage when solid collisions occur.

Macro Definitions

HELIOS_LOG_SCOPE

#define HELIOS_LOG_SCOPE   "helios::engine::mechanics::damage::systems::DamageOnCollisionSystem"

Definition at line 50 of file DamageOnCollisionSystem.ixx.

50#define HELIOS_LOG_SCOPE "helios::engine::mechanics::damage::systems::DamageOnCollisionSystem"

File Listing

The file content with the documentation metadata removed is:

1/**
2 * @file DamageOnCollisionSystem.ixx
3 * @brief System that applies damage when solid collisions occur.
4 */
5module;
6
7#include <format>
8
9#include <string>
10
11export module helios.engine.mechanics.damage.systems.DamageOnCollisionSystem;
12
13
14import helios.engine.ecs.GameObject;
15
16import helios.engine.runtime.world.UpdateContext;
17import helios.engine.runtime.world.GameWorld;
18
19import helios.engine.state.Bindings;
20import helios.engine.runtime.messaging.command.EngineCommandBuffer;
21
22import helios.engine.modules.physics.collision.events.SolidCollisionEvent;
23
24import helios.engine.mechanics.damage.components.DamageDealerComponent;
25import helios.engine.common.types;
26
27import helios.engine.mechanics.spawn.components.EmittedByComponent;
28
29import helios.engine.mechanics.damage.components.LastDamageComponent;
30import helios.engine.mechanics.damage.commands.ApplyDamageCommand;
31
32import helios.engine.mechanics.health.components.HealthComponent;
33import helios.engine.mechanics.health.types;
34import helios.engine.mechanics.health.events;
35
36import helios.math;
37import helios.util;
38
39import helios.engine.common.tags.SystemRole;
40
47
49
50#define HELIOS_LOG_SCOPE "helios::engine::mechanics::damage::systems::DamageOnCollisionSystem"
52
53
54 /**
55 * @brief Emits HealthChangedEvents when solid collisions involve damage dealers.
56 *
57 * Listens for SolidCollisionEvents and checks if the source entity has a
58 * DamageDealerComponent. If the target has a HealthComponent, emits an
59 * ApplyDamageCommand. Resolves the true attacker (instigator) via
60 * EmittedByComponent when applicable (e.g. projectiles).
61 */
63
64
67
68 public:
69
71
72
73 /**
74 * @brief Processes collision events and applies damage to targets.
75 *
76 * @param updateContext The current frame's update context.
77 */
79
80 auto eventPass = updateContext.readPass<
82
83 for (const auto& event : eventPass) {
84
85 auto go = updateContext.find(event.collisionContext().source);
86
87 if (!go) {
88 continue;
89 }
90
91 auto* ddc = go->get<DamageDealerComponent>();
92 if (!ddc) {
93 continue;
94 }
95
96 auto other = event.collisionContext().other;
97 if (!other.has_value()) {
98 continue;
99 }
100
101 auto target = updateContext.find(other.value());
102 if (!target) {
103 continue;
104 }
105 auto* hc = target->get<HealthComponent>();
106 if (!hc) {
107 continue;
108 }
109
110
111 const auto damageApplied = ddc->damage(event.collisionContext().otherCollisionLayerId);
112
113 logger_.info(std::format(
114 "Hitting entity {0} with {1} damage!", other.value().entityId, damageApplied
115 ));
116
117 // the go itself is the source
118 auto instigator= go->entityHandle();
119 auto causer = go->entityHandle();
120
121 auto* ebc = go->get<EmittedByComponent>();
122 if (ebc) {
123 // else the source is assigned to the go emitted
124 // by the go
125 instigator = ebc->source();
126 }
127
128 const auto interactionContext = InteractionContext{
129 .target = other.value(),
130 .instigator = instigator,
131 .causer = causer,
132 .contact = event.collisionContext().contact,
133 };
134
135 auto dc = DamageContext{
136 .interactionContext = interactionContext,
137 .damage = damageApplied
138 };
139
140 updateContext.queueCommand<ApplyDamageCommand>(dc);
141
142 }
143
144 }
145
146 };
147
148
149}

Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.