feat: implemented damage box

This commit is contained in:
Sara 2025-07-21 23:17:54 +02:00
parent a734eb8b20
commit 2c59572e40
3 changed files with 76 additions and 0 deletions

View file

@ -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<Hitbox>(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;
}

View file

@ -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<HealthStatus *> already_hit{};
};
#endif // !DAMAGE_BOX_H

View file

@ -1,6 +1,7 @@
#include "register_types.h" #include "register_types.h"
#include "core/object/class_db.h" #include "core/object/class_db.h"
#include "wave_survival/damage_box.h"
#include "wave_survival/enemies/enemy_wretched.h" #include "wave_survival/enemies/enemy_wretched.h"
#include "wave_survival/enemy_body.h" #include "wave_survival/enemy_body.h"
#include "wave_survival/health_status.h" #include "wave_survival/health_status.h"
@ -41,6 +42,7 @@ void initialize_wave_survival_module(ModuleInitializationLevel p_level) {
GDREGISTER_CLASS(WretchedAttackState); GDREGISTER_CLASS(WretchedAttackState);
GDREGISTER_CLASS(PlayerDetector); GDREGISTER_CLASS(PlayerDetector);
GDREGISTER_CLASS(Hitbox); GDREGISTER_CLASS(Hitbox);
GDREGISTER_CLASS(DamageBox);
} }
void uninitialize_wave_survival_module(ModuleInitializationLevel p_level) { void uninitialize_wave_survival_module(ModuleInitializationLevel p_level) {