Zombies with PathFinding Run and Attack !

This commit is contained in:
Morph01
2024-08-16 19:17:51 +02:00
parent 6160561498
commit 1a14c78ede
4 changed files with 103 additions and 57 deletions

View File

@@ -1,14 +1,13 @@
#include "Zombie.h"
#include <godot_cpp/classes/engine.hpp>
#include <godot_cpp/variant/utility_functions.hpp>
using namespace godot;
namespace blitz {
static constexpr float SPEED = 2.0f;
static constexpr float ATTACK_RANGE = 2.0f;
void Zombie::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_m_PlayerPath", "m_PlayerPath"), &Zombie::set_m_PlayerPath);
@@ -32,7 +31,11 @@ void Zombie::_ready() {
DEV_ASSERT(m_Player);
m_NavigationAgent = Object::cast_to<NavigationAgent3D>(find_child("NavigationAgent3D"));
DEV_ASSERT(m_NavigationAgent);
m_Velocity = Vector3(0, 0, 0);
m_AnimationTree = Object::cast_to<AnimationTree>(find_child("AnimationTree"));
DEV_ASSERT(m_AnimationTree);
m_StateMachine = Object::cast_to<AnimationNodeStateMachinePlayback>(m_AnimationTree->get("parameters/playback"));
DEV_ASSERT(m_StateMachine);
this->set_velocity(Vector3(0, 0, 0));
}
void Zombie::_process(float a_Delta) {
@@ -41,15 +44,26 @@ void Zombie::_process(float a_Delta) {
return;
}
#endif
this->set_velocity(m_Velocity);
m_NavigationAgent->set_target_position(m_Player->get_global_position());
Vector3 next_nav_point = m_NavigationAgent->get_next_path_position();
m_Velocity = (next_nav_point - get_global_position()).normalized() * SPEED;
this->set_velocity(m_Velocity);
look_at(Vector3(m_Player->get_global_position().x, get_global_position().y, m_Player->get_global_position().z), Vector3(0, 1, 0));
if (m_StateMachine->get_current_node().match("Run")) {
m_NavigationAgent->set_target_position(m_Player->get_global_position());
m_Velocity = (m_NavigationAgent->get_next_path_position() - this->get_global_position()).normalized() * SPEED;
this->set_velocity(m_Velocity);
look_at(Vector3(this->get_global_position().x + this->get_velocity().x, this->get_global_position().y,
this->get_global_position().z + this->get_velocity().z));
} else if (m_StateMachine->get_current_node().match("Attack")) {
this->set_velocity(Vector3(0, 0, 0));
look_at(Vector3(m_Player->get_global_position().x, m_Player->get_global_position().y / 2, m_Player->get_global_position().z));
}
m_AnimationTree->set("parameters/conditions/run", !_target_in_range());
m_AnimationTree->set("parameters/conditions/attack", _target_in_range());
move_and_slide();
}
bool Zombie::_target_in_range() {
return this->get_global_position().distance_to(m_Player->get_global_position()) < ATTACK_RANGE;
}
void Zombie::set_m_PlayerPath(const NodePath& path) {
m_PlayerPath = path;
}