feat: implemented damage to enemies
This commit is contained in:
parent
70696db134
commit
2cd1cba04f
9 changed files with 163 additions and 21 deletions
|
|
@ -37,10 +37,12 @@ int HealthStatus::get_health() const {
|
|||
}
|
||||
|
||||
void HealthStatus::damage(int amount) {
|
||||
amount = Math::abs(amount);
|
||||
this->health -= amount;
|
||||
emit_signal(sig_health_changed, this->health, -amount);
|
||||
if (this->health <= 0) {
|
||||
emit_signal(sig_death);
|
||||
if (this->health > 0) {
|
||||
amount = Math::abs(amount);
|
||||
this->health -= amount;
|
||||
emit_signal(sig_health_changed, this->health, -amount);
|
||||
if (this->health <= 0) {
|
||||
emit_signal(sig_death);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
24
modules/wave_survival/hitbox.cpp
Normal file
24
modules/wave_survival/hitbox.cpp
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
#include "hitbox.h"
|
||||
#include "health_status.h"
|
||||
#include "macros.h"
|
||||
|
||||
void Hitbox::_bind_methods() {
|
||||
BIND_HPROPERTY(Variant::OBJECT, health, PROPERTY_HINT_NODE_TYPE, "HealthStatus");
|
||||
BIND_PROPERTY(Variant::FLOAT, damage_modifier);
|
||||
}
|
||||
|
||||
void Hitbox::set_health(HealthStatus *value) {
|
||||
this->health = value;
|
||||
}
|
||||
|
||||
HealthStatus *Hitbox::get_health() const {
|
||||
return this->health;
|
||||
}
|
||||
|
||||
void Hitbox::set_damage_modifier(float value) {
|
||||
this->damage_modifier = value;
|
||||
}
|
||||
|
||||
float Hitbox::get_damage_modifier() const {
|
||||
return this->damage_modifier;
|
||||
}
|
||||
22
modules/wave_survival/hitbox.h
Normal file
22
modules/wave_survival/hitbox.h
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
#ifndef HITBOX_H
|
||||
#define HITBOX_H
|
||||
|
||||
#include "scene/3d/physics/area_3d.h"
|
||||
class HealthStatus;
|
||||
|
||||
class Hitbox : public Area3D {
|
||||
GDCLASS(Hitbox, Area3D);
|
||||
static void _bind_methods();
|
||||
|
||||
public:
|
||||
void set_health(HealthStatus *value);
|
||||
HealthStatus *get_health() const;
|
||||
void set_damage_modifier(float value);
|
||||
float get_damage_modifier() const;
|
||||
|
||||
private:
|
||||
HealthStatus *health{ nullptr };
|
||||
float damage_modifier{ 1.f };
|
||||
};
|
||||
|
||||
#endif // !HITBOX_H
|
||||
|
|
@ -1,4 +1,6 @@
|
|||
#include "hitscan_muzzle.h"
|
||||
#include "health_status.h"
|
||||
#include "hitbox.h"
|
||||
#include "macros.h"
|
||||
#include "scene/resources/packed_scene.h"
|
||||
|
||||
|
|
@ -8,12 +10,35 @@ void HitscanMuzzle::_bind_methods() {
|
|||
BIND_PROPERTY(Variant::INT, ray_count);
|
||||
}
|
||||
|
||||
void HitscanMuzzle::instantiate_impact_effect() {
|
||||
if (get_collider() == nullptr) {
|
||||
return;
|
||||
}
|
||||
Node *effect_as_node{ this->impact_effect->instantiate() };
|
||||
if (Node3D * effect{ cast_to<Node3D>(effect_as_node) }) {
|
||||
get_tree()->get_current_scene()->add_child(effect);
|
||||
Vector3 const point{ get_collision_point() };
|
||||
Vector3 const normal{ get_collision_normal() };
|
||||
Vector3 const position{ get_global_position() };
|
||||
effect->look_at_from_position(point, point + normal, (point - position).normalized());
|
||||
} else if (effect_as_node) {
|
||||
effect_as_node->queue_free();
|
||||
}
|
||||
}
|
||||
|
||||
void HitscanMuzzle::try_deal_damage() {
|
||||
if (Hitbox * hitbox{ cast_to<Hitbox>(get_collider()) }) {
|
||||
hitbox->get_health()->damage(this->damage * hitbox->get_damage_modifier());
|
||||
}
|
||||
}
|
||||
|
||||
void HitscanMuzzle::ready() {
|
||||
this->home_transform = get_transform();
|
||||
this->impact_effect = ResourceLoader::load("res://objects/effects/bullet_impact.tscn");
|
||||
if (!this->impact_effect.is_valid()) {
|
||||
print_error("HitscanMuzzle::ready: impact effect is invalid");
|
||||
}
|
||||
set_enabled(false);
|
||||
}
|
||||
|
||||
void HitscanMuzzle::_notification(int what) {
|
||||
|
|
@ -30,19 +55,13 @@ void HitscanMuzzle::_notification(int what) {
|
|||
}
|
||||
|
||||
void HitscanMuzzle::shoot() {
|
||||
set_transform(this->home_transform);
|
||||
rotate_object_local(Vector3(0.f, 1.f, 0.f), Math::random(-Math::PI, Math::PI));
|
||||
rotate_object_local(Vector3(1.f, 0.f, 0.f), Math::random(0.f, this->spread));
|
||||
force_raycast_update();
|
||||
Node *effect_as_node{ this->impact_effect->instantiate() };
|
||||
if (Node3D * effect{ cast_to<Node3D>(effect_as_node) }) {
|
||||
get_tree()->get_current_scene()->add_child(effect);
|
||||
Vector3 const point{ get_collision_point() };
|
||||
Vector3 const normal{ get_collision_normal() };
|
||||
Vector3 const position{ get_global_position() };
|
||||
effect->look_at_from_position(point, point + normal, (point - position).normalized());
|
||||
} else if (effect_as_node) {
|
||||
effect_as_node->queue_free();
|
||||
for (int i{ this->ray_count }; i > 0; --i) {
|
||||
set_transform(this->home_transform);
|
||||
rotate_object_local(Vector3(0.f, 1.f, 0.f), Math::random(-Math::PI, Math::PI));
|
||||
rotate_object_local(Vector3(1.f, 0.f, 0.f), Math::random(0.f, this->spread));
|
||||
force_raycast_update();
|
||||
instantiate_impact_effect();
|
||||
try_deal_damage();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -6,6 +6,8 @@
|
|||
class HitscanMuzzle : public RayCast3D {
|
||||
GDCLASS(HitscanMuzzle, RayCast3D);
|
||||
static void _bind_methods();
|
||||
void instantiate_impact_effect();
|
||||
void try_deal_damage();
|
||||
void ready();
|
||||
|
||||
public:
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@
|
|||
#include "wave_survival/enemies/enemy_wretched.h"
|
||||
#include "wave_survival/enemy_body.h"
|
||||
#include "wave_survival/health_status.h"
|
||||
#include "wave_survival/hitbox.h"
|
||||
#include "wave_survival/hitscan_muzzle.h"
|
||||
#include "wave_survival/npc_unit.h"
|
||||
#include "wave_survival/patrol_path.h"
|
||||
|
|
@ -39,6 +40,7 @@ void initialize_wave_survival_module(ModuleInitializationLevel p_level) {
|
|||
GDREGISTER_CLASS(WretchedChaseState);
|
||||
GDREGISTER_CLASS(WretchedAttackState);
|
||||
GDREGISTER_CLASS(PlayerDetector);
|
||||
GDREGISTER_CLASS(Hitbox);
|
||||
}
|
||||
|
||||
void uninitialize_wave_survival_module(ModuleInitializationLevel p_level) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue