#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; } 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; } }