Skip to main content

DamageDealerComponent.ixx File

Component for defining layer-based damage values. More...

Included Headers

#include <algorithm> #include <array> #include <bit> #include <cassert> #include <helios/helios_config.h> #include <helios.engine.modules.physics.collision.types.CollisionLayer>

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...

namespacecomponents

Damage-related ECS components. More...

Classes Index

classDamageDealerComponent

Component that stores damage values per collision layer. More...

Description

Component for defining layer-based damage values.

File Listing

The file content with the documentation metadata removed is:

1/**
2 * @file DamageDealerComponent.ixx
3 * @brief Component for defining layer-based damage values.
4 */
5module;
6
7#include <algorithm>
8#include <array>
9#include <bit>
10#include <cassert>
11#include <helios/helios_config.h>
12
13export module helios.engine.mechanics.damage.components.DamageDealerComponent;
14
15import helios.engine.modules.physics.collision.types.CollisionLayer;
16
17
18
19
20
22
23 /**
24 * @brief Component that stores damage values per collision layer.
25 *
26 * Allows an entity to deal different amounts of damage depending on
27 * which collision layer the target belongs to. Uses a fixed-size array
28 * indexed by layer ID for O(1) damage lookup.
29 */
31
32 private:
33
34 /**
35 * @brief Damage values indexed by collision layer.
36 */
37 std::array<float, helios::engine::modules::physics::collision::types::MAX_COLLISION_LAYERS> damageRegistry_{};
38
39 /**
40 * @brief Whether this component is enabled.
41 */
42 bool isEnabled_ = true;
43
44 public:
45
46 /**
47 * @brief Checks whether this component is enabled.
48 *
49 * @return True if enabled, false otherwise.
50 */
51 [[nodiscard]] bool isEnabled() const noexcept {
52 return isEnabled_;
53 }
54
55 /**
56 * @brief Enables this component.
57 */
58 void enable() noexcept {
59 isEnabled_ = true;
60 }
61
62 /**
63 * @brief Disables this component.
64 */
65 void disable() noexcept {
66 isEnabled_ = false;
67 }
68
70
71 /**
72 * @brief Copy constructor.
73 *
74 * @param other The component to copy from.
75 */
76 DamageDealerComponent(const DamageDealerComponent& other) : damageRegistry_(other.damageRegistry_) {}
77
80 DamageDealerComponent& operator=(DamageDealerComponent&&) noexcept = default;
81
82 /**
83 * @brief Sets the damage dealt to entities on a specific layer.
84 *
85 * @param damage The damage value to deal.
86 * @param otherLayerId The target collision layer ID (must be a power of 2).
87 *
88 * @return Reference to this component for method chaining.
89 */
91 const float damage,
92 const uint32_t otherLayerId
93 ) noexcept {
94 assert(otherLayerId != 0 && "Unexpected layer id, must not be 0");
95
96 const auto idx = std::countr_zero(otherLayerId);
97
98 assert(idx < damageRegistry_.size() && "Unexpected otherLayerId, index larger than size");
99
100 damageRegistry_[idx] = damage;
101 return *this;
102 }
103
104 /**
105 * @brief Returns the damage dealt to entities on a specific layer.
106 *
107 * @param otherLayerId The target collision layer ID (must be a power of 2).
108 *
109 * @return The damage value for the specified layer.
110 */
111 [[nodiscard]] float damage(const uint32_t otherLayerId) const noexcept {
112
113 assert(otherLayerId != 0 && "Unexpected layer id, must not be 0");
114
115 const auto idx = std::countr_zero(otherLayerId);
116 assert(idx < damageRegistry_.size() && "Unexpected layer id, index larger than size");
117
118 return damageRegistry_[idx];
119 }
120
121 };
122
123
124}

Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.