authority/modules/authority/actor_body.cpp

82 lines
2 KiB
C++

#include "actor_body.h"
#include "core/config/engine.h"
#include "core/object/object.h"
#include "macros.h"
void ActorBody::_bind_methods() {
BIND_HPROPERTY(Variant::FLOAT, movement_speed, PROPERTY_HINT_RANGE, "0.0,20.0");
}
void ActorBody::physics_process(double delta) {
if (this->teleport_on_process) {
set_global_position(this->teleport_target);
this->teleport_on_process = false;
force_update_transform();
}
set_velocity(get_movement_direction() * this->movement_speed);
move_and_slide();
}
void ActorBody::_notification(int what) {
if (Engine::get_singleton()->is_editor_hint()) {
return;
}
switch (what) {
default:
return;
case NOTIFICATION_READY:
set_physics_process(true);
set_as_top_level(true);
case NOTIFICATION_PHYSICS_PROCESS:
physics_process(get_physics_process_delta_time());
return;
}
}
void ActorBody::set_movement_direction(Vector3 direction) {
this->mode = Direction;
this->movement_vector = Vector3(direction.x, 0.f, direction.z).normalized();
}
Vector3 ActorBody::get_movement_direction() const {
switch (this->mode) {
case Position: {
Vector3 const direction_3d{ get_global_position().direction_to(get_movement_target()) };
return Vector3{ direction_3d.x, 0.f, direction_3d.z };
} break;
case Direction:
return this->movement_vector;
}
}
void ActorBody::set_movement_target(Vector3 location) {
this->mode = Position;
this->movement_vector = { location.x, 0.f, location.z };
}
Vector3 ActorBody::get_movement_target() const {
switch (this->mode) {
case Position:
return this->movement_vector;
case Direction:
return get_global_position() + this->movement_vector;
}
}
void ActorBody::teleport(Vector3 target) {
this->teleport_target = target;
this->teleport_on_process = true;
}
void ActorBody::set_movement_speed(float speed) {
this->movement_speed = speed;
}
float ActorBody::get_movement_speed() const {
return this->movement_speed;
}
ActorBody::MovementMode ActorBody::get_movement_mode() const {
return this->mode;
}