feat: implemented enemy rifleman combat

This commit is contained in:
Sara Gerretsen 2026-02-09 13:29:37 +01:00
parent f1c4fe75f2
commit 18c8154576
10 changed files with 8267 additions and 6 deletions

View file

@ -1,11 +1,15 @@
#include "enemy_rifleman.h"
#include "core/os/memory.h"
#include "scene/animation/animation_player.h"
#include "wave_survival/macros.h"
#include "wave_survival/npc_unit.h"
#include "wave_survival/player_body.h"
#include "wave_survival/player_detector.h"
#include "wave_survival/state_machine.h"
void EnemyRifleman::_bind_methods() {
BIND_PROPERTY(Variant::INT, damage_amount);
ClassDB::bind_method(D_METHOD("shoot"), &self_type::shoot);
}
void EnemyRifleman::ready() {
@ -27,6 +31,20 @@ void EnemyRifleman::_notification(int what) {
}
}
void EnemyRifleman::shoot() {
if (get_detector()->line_of_sight_exists()) {
PlayerBody::get_singleton()->get_health()->damage(this->damage_amount);
}
}
void EnemyRifleman::set_damage_amount(int amount) {
this->damage_amount = amount;
}
int EnemyRifleman::get_damage_amount() const {
return this->damage_amount;
}
EnemyRifleman *RiflemanState::get_target() const {
return cast_to<EnemyRifleman>(get_body());
}
@ -50,9 +68,16 @@ void RiflemanFireState::enter_state() {
get_target()->set_movement_direction(Vector2());
}
void RiflemanFireState::process(double delta) {
Vector3 const body_pos{ get_target()->get_global_position() };
Vector3 player_pos{ PlayerBody::get_singleton()->get_global_position() };
player_pos.y = body_pos.y;
get_target()->look_at(body_pos + player_pos - body_pos);
}
String RiflemanFireState::get_next_state() const {
if (get_anim()->get_current_animation().is_empty()) {
return RiflemanFireState::get_class_static();
return RiflemanChaseState::get_class_static();
}
return get_class();
}

View file

@ -14,6 +14,14 @@ class EnemyRifleman : public EnemyBody {
protected:
void _notification(int what);
public:
void shoot();
void set_damage_amount(int amount);
int get_damage_amount() const;
private:
int damage_amount{ 1 };
};
/* ============================== STATES ============================== */
@ -48,6 +56,7 @@ class RiflemanFireState : public RiflemanState {
public:
void enter_state() override;
void process(double delta) override;
String get_next_state() const override;
};