From 2c59572e40e475819c0973173aaf7f4e72506d62 Mon Sep 17 00:00:00 2001 From: Sara Date: Mon, 21 Jul 2025 23:17:54 +0200 Subject: [PATCH] feat: implemented damage box --- modules/wave_survival/damage_box.cpp | 47 ++++++++++++++++++++++++ modules/wave_survival/damage_box.h | 27 ++++++++++++++ modules/wave_survival/register_types.cpp | 2 + 3 files changed, 76 insertions(+) create mode 100644 modules/wave_survival/damage_box.cpp create mode 100644 modules/wave_survival/damage_box.h diff --git a/modules/wave_survival/damage_box.cpp b/modules/wave_survival/damage_box.cpp new file mode 100644 index 00000000..d277ae6b --- /dev/null +++ b/modules/wave_survival/damage_box.cpp @@ -0,0 +1,47 @@ +#include "damage_box.h" +#include "health_status.h" +#include "hitbox.h" + +void DamageBox::_bind_methods() { + ClassDB::bind_method(D_METHOD("attack_motion_begin"), &self_type::attack_motion_begin); + ClassDB::bind_method(D_METHOD("attack_motion_end"), &self_type::attack_motion_end); +} + +void DamageBox::attack_motion_begin() { + set_monitoring(true); +} + +void DamageBox::attack_motion_end() { + this->already_hit.clear(); + set_monitoring(true); +} + +void DamageBox::on_body_entered(Node3D *node) { + if (Hitbox * box{ cast_to(node) }) { + if (!this->already_hit.has(box->get_health())) { + box->get_health()->damage(this->damage * box->get_damage_modifier()); + this->already_hit.insert(box->get_health()); + } + } +} + +void DamageBox::_notification(int what) { + if (Engine::get_singleton()->is_editor_hint()) { + return; + } + switch (what) { + default: + return; + case NOTIFICATION_ENTER_TREE: + connect("body_entered", callable_mp(this, &self_type::on_body_entered)); + return; + } +} + +void DamageBox::set_damage(int amount) { + this->damage = amount; +} + +int DamageBox::get_damage() const { + return this->damage; +} diff --git a/modules/wave_survival/damage_box.h b/modules/wave_survival/damage_box.h new file mode 100644 index 00000000..2038c997 --- /dev/null +++ b/modules/wave_survival/damage_box.h @@ -0,0 +1,27 @@ +#ifndef DAMAGE_BOX_H +#define DAMAGE_BOX_H + +#include "core/templates/hash_set.h" +#include "scene/3d/physics/area_3d.h" +class HealthStatus; + +class DamageBox : public Area3D { + GDCLASS(DamageBox, Area3D); + static void _bind_methods(); + void attack_motion_begin(); + void attack_motion_end(); + void on_body_entered(Node3D *body); + +protected: + void _notification(int what); + +public: + void set_damage(int amount); + int get_damage() const; + +private: + int damage{ 1 }; + HashSet already_hit{}; +}; + +#endif // !DAMAGE_BOX_H diff --git a/modules/wave_survival/register_types.cpp b/modules/wave_survival/register_types.cpp index 0a0f19e1..717037fd 100644 --- a/modules/wave_survival/register_types.cpp +++ b/modules/wave_survival/register_types.cpp @@ -1,6 +1,7 @@ #include "register_types.h" #include "core/object/class_db.h" +#include "wave_survival/damage_box.h" #include "wave_survival/enemies/enemy_wretched.h" #include "wave_survival/enemy_body.h" #include "wave_survival/health_status.h" @@ -41,6 +42,7 @@ void initialize_wave_survival_module(ModuleInitializationLevel p_level) { GDREGISTER_CLASS(WretchedAttackState); GDREGISTER_CLASS(PlayerDetector); GDREGISTER_CLASS(Hitbox); + GDREGISTER_CLASS(DamageBox); } void uninitialize_wave_survival_module(ModuleInitializationLevel p_level) {