diff --git a/src/enemy.cpp b/src/enemy.cpp deleted file mode 100644 index 004e3e1..0000000 --- a/src/enemy.cpp +++ /dev/null @@ -1,79 +0,0 @@ -#include "enemy.hpp" -#include "godot_cpp/classes/time.hpp" -#include "health.hpp" -#include "character_actor.hpp" -#include "utils/godot_macros.h" -#include - -namespace godot { -void Enemy::_bind_methods() { -#define CLASSNAME Enemy - GDFUNCTION_ARGS(body_entered_vision_area, "body"); - GDFUNCTION_ARGS(on_death, "damage"); - GDFUNCTION_ARGS(on_damage, "damage", "remaining"); -} - -void Enemy::_ready() { GDGAMEONLY(); - this->health = this->get_node("Health"); - this->nav_agent = this->get_node("NavigationAgent3D"); - this->vision_area = this->get_node("VisionArea"); - this->vision_area->connect("body_entered", Callable(this, "body_entered_vision_area")); - this->update_navigation_target(); - this->health->connect("death", Callable(this, "on_death")); - this->health->connect("damage", Callable(this, "on_damage")); -} - -void Enemy::_process(double delta_time) { GDGAMEONLY(); - if(this->renav_time > Time::get_singleton()->get_ticks_msec() / 1000.f) { - this->update_navigation_target(); - } - this->process_navigation(delta_time); - this->move_and_slide(); -} - -void Enemy::process_navigation(double delta_time) { - if(this->nav_agent->is_navigation_finished()) { - return; - } - Vector3 const desired_direction = (this->nav_agent->get_next_path_position() - this->get_global_position()).normalized(); - this->set_velocity(desired_direction); - Transform3D trans = this->get_global_transform(); - Vector3 const forward = desired_direction; - trans.set_basis(Basis{desired_direction.cross(Vector3{0.f, 1.f, 0.f}), Vector3{0.f, 1.f, 0.f}, forward}); - this->set_global_transform(trans); -} - -void Enemy::body_entered_vision_area(Node3D *body) { - CharacterActor *player = Object::cast_to(body); - if(player == nullptr) - return; - // TODO: replace this with some condition deciding wether to attack the new character or the current target - this->target_player = player; - this->update_navigation_target(); -} - -void Enemy::update_navigation_target() { - this->renav_time = float(Time::get_singleton()->get_ticks_msec()) / 1000.f + Enemy::RENAV_INTERVAL; - if(this->target_player == nullptr) - this->nav_agent->set_target_position(this->get_global_position()); - else - this->nav_agent->set_target_position(this->target_player->get_global_position()); -} - -void Enemy::on_damage(int delta, int health_left) { -} - -void Enemy::on_death(int delta) { - this->queue_free(); -} - -Health *Enemy::get_health() { - return this->health; -} - -Health const *Enemy::get_health() const { - return this->health; -} - -float const Enemy::RENAV_INTERVAL{0.25f}; -} diff --git a/src/enemy.hpp b/src/enemy.hpp deleted file mode 100644 index 23aad27..0000000 --- a/src/enemy.hpp +++ /dev/null @@ -1,43 +0,0 @@ -#ifndef ENEMY_HPP -#define ENEMY_HPP - -#include "godot_cpp/classes/area3d.hpp" -#include "health.hpp" -#include - -namespace godot { -class CharacterActor; -class NavigationAgent3D; - -class Enemy : public CharacterBody3D, - public IHealthEntity { - GDCLASS(Enemy, CharacterBody3D); - static void _bind_methods(); -public: - virtual void _ready() override; - virtual void _process(double delta_time) override; - void process_navigation(double delta_time); - - void body_entered_vision_area(Node3D *body); - - void update_navigation_target(); - - void on_damage(int delta, int health_left); - void on_death(int damage); - - virtual Health *get_health() override; - virtual Health const *get_health() const override; - -private: - float renav_time{0.f}; - - CharacterActor *target_player{nullptr}; - Health *health{nullptr}; - NavigationAgent3D *nav_agent{nullptr}; - Area3D *vision_area{nullptr}; - - static float const RENAV_INTERVAL; -}; -} - -#endif // !ENEMY_HPP