break-utopia/modules/break_utopia/cop_bot.cpp
2026-01-21 17:06:18 +01:00

59 lines
1.5 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);
BIND_HPROPERTY(Variant::OBJECT, anim, PROPERTY_HINT_NODE_TYPE, "AnimationPlayer");
}
void CopBot::ready() {
if (this->anim) {
this->anim->play("move");
}
}
void CopBot::process() {
Vector3 player_position{ PlayerBody::get_singleton()->get_global_position() };
Vector3 current_position{ get_global_position() };
Vector3 difference{ player_position - current_position };
if (difference.length() < 1.5 && this->anim->get_current_animation() != "attack") {
this->anim->play("attack");
this->anim->queue("move");
}
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() {
Vector3 velocity{ get_velocity() };
if (!is_on_floor()) {
velocity += get_gravity() * get_physics_process_delta_time();
} else {
velocity.y = 0;
}
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;
}
}