break-utopia/modules/break_utopia/cop_bot.cpp

58 lines
1.4 KiB
C++

#include "cop_bot.h"
#include "break_utopia/macros.h"
#include "break_utopia/player_body.h"
#include "core/config/engine.h"
void CopBot::_bind_methods() {
BIND_PROPERTY(Variant::FLOAT, speed);
}
void CopBot::ready() {
}
void CopBot::process() {
Vector3 player_position{ PlayerBody::get_singleton()->get_global_position() };
Vector3 current_position{ get_global_position() };
Vector3 difference{ player_position - current_position };
Vector3 velocity{ 0, 0, 0 };
constexpr float detection_distance{ 7. * 7. };
if (difference.length_squared() > detection_distance) {
this->aware_of_player = true;
}
if (this->aware_of_player) {
velocity = difference;
}
if (is_on_floor()) {
difference += get_gravity();
} else {
difference.y = 0;
}
set_velocity(difference.normalized() * this->speed);
Vector3 look_at_position{ (current_position - player_position) + current_position };
look_at({ look_at_position.x, current_position.y, look_at_position.z });
}
void CopBot::physics_process() {
move_and_slide();
}
void CopBot::_notification(int what) {
if (Engine::get_singleton()->is_editor_hint()) {
return;
}
switch (what) {
default:
return;
case NOTIFICATION_READY:
set_process(true);
set_physics_process(true);
ready();
return;
case NOTIFICATION_PROCESS:
process();
return;
case NOTIFICATION_PHYSICS_PROCESS:
physics_process();
return;
}
}