2024-11-26 23:41:39 +00:00
|
|
|
#include "player.hpp"
|
|
|
|
#include "utils/godot_macros.hpp"
|
2024-11-27 15:32:58 +00:00
|
|
|
#include <godot_cpp/variant/utility_functions.hpp>
|
|
|
|
#include <godot_cpp/classes/input_event_joypad_motion.hpp>
|
2024-11-26 23:41:39 +00:00
|
|
|
|
2024-11-27 15:32:58 +00:00
|
|
|
void Player::_bind_methods() {}
|
2024-11-26 23:41:39 +00:00
|
|
|
|
|
|
|
void Player::_ready() {
|
|
|
|
this->anim_tree = this->get_node<gd::AnimationTree>("%AnimationTree");
|
2024-11-27 15:32:58 +00:00
|
|
|
this->input = this->get_node<utils::PlayerInput>("%PlayerInput");
|
|
|
|
this->input->listen_to(utils::PlayerInput::Listener("dir_left", "dir_right", callable_mp(this, &Player::_on_dir_horizontal)));
|
|
|
|
this->input->listen_to(utils::PlayerInput::Listener("dir_backward", "dir_forward", callable_mp(this, &Player::_on_dir_vertical)));
|
|
|
|
this->input->listen_to(utils::PlayerInput::Listener("look_left", "look_right", callable_mp(this, &Player::_on_look_horizontal)));
|
|
|
|
this->input->listen_to(utils::PlayerInput::Listener("look_down", "look_up", callable_mp(this, &Player::_on_look_vertical)));
|
|
|
|
this->input->listen_to(utils::PlayerInput::Listener("_mouse_left", "_mouse_right", callable_mp(this, &Player::_on_look_horizontal)));
|
|
|
|
this->input->listen_to(utils::PlayerInput::Listener("_mouse_down", "_mouse_up", callable_mp(this, &Player::_on_look_vertical)));
|
2024-11-26 23:41:39 +00:00
|
|
|
this->model_node = this->get_node<gd::Node3D>("%CharacterModel");
|
|
|
|
}
|
|
|
|
|
2024-11-27 15:32:58 +00:00
|
|
|
void Player::_on_dir_horizontal(gd::Ref<gd::InputEvent>, float value) {
|
|
|
|
this->input_directions.x = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Player::_on_dir_vertical(gd::Ref<gd::InputEvent>, float value) {
|
|
|
|
this->input_directions.y = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Player::_on_look_horizontal(gd::Ref<gd::InputEvent>, float value) {
|
|
|
|
this->input_look.x = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Player::_on_look_vertical(gd::Ref<gd::InputEvent>, float value) {
|
|
|
|
this->input_look.y = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Player::_process(double delta [[maybe_unused]]) {
|
|
|
|
this->anim_tree->set("parameters/Movement/blend_position", this->input_directions);
|
|
|
|
}
|
|
|
|
|
2024-11-26 23:41:39 +00:00
|
|
|
void Player::_physics_process(double delta [[maybe_unused]]) {
|
|
|
|
gd::Basis const &model_basis{this->model_node->get_global_basis()};
|
|
|
|
gd::Vector3 const local_motion{this->anim_tree->get_root_motion_position()};
|
|
|
|
gd::Vector3 const motion {
|
|
|
|
local_motion.x * model_basis.get_column(0) +
|
|
|
|
local_motion.y * model_basis.get_column(1) +
|
|
|
|
local_motion.z * model_basis.get_column(2)
|
|
|
|
};
|
|
|
|
this->set_velocity(motion / delta);
|
|
|
|
this->move_and_slide();
|
|
|
|
}
|