44 lines
1.5 KiB
C++
44 lines
1.5 KiB
C++
#include "player_character.h"
|
|
#include "core/input/input.h"
|
|
#include "scene/main/viewport.h"
|
|
|
|
void PlayerCharacter::_bind_methods() {}
|
|
|
|
void PlayerCharacter::process(double delta) {
|
|
Basis const basis{ get_viewport()->get_camera_3d()->get_global_basis() };
|
|
Vector2 backward{ basis.get_column(2).x, basis.get_column(2).z };
|
|
Vector2 right{ basis.get_column(0).x, basis.get_column(0).z };
|
|
set_movement({ backward.normalized() * this->last_movement_input.x + right.normalized() * this->last_movement_input });
|
|
}
|
|
|
|
void PlayerCharacter::_notification(int what) {
|
|
if (Engine::get_singleton()->is_editor_hint()) {
|
|
return;
|
|
}
|
|
switch (what) {
|
|
default:
|
|
return;
|
|
case NOTIFICATION_ENTER_TREE:
|
|
set_process(true);
|
|
set_process_unhandled_input(true);
|
|
return;
|
|
case NOTIFICATION_PROCESS:
|
|
process(get_process_delta_time());
|
|
}
|
|
}
|
|
|
|
void PlayerCharacter::unhandled_input(Ref<InputEvent> const &what) {
|
|
if (what->is_action(input_move_left) || what->is_action(input_move_forward) || what->is_action(input_move_right) || what->is_action(input_move_backward)) {
|
|
this->last_movement_input = {
|
|
Input::get_singleton()->get_axis(input_move_left, input_move_right),
|
|
Input::get_singleton()->get_axis(input_move_backward, input_move_forward)
|
|
};
|
|
get_viewport()->set_input_as_handled();
|
|
}
|
|
}
|
|
|
|
String const PlayerCharacter::input_move_left{ "move_left" };
|
|
String const PlayerCharacter::input_move_right{ "move_right" };
|
|
String const PlayerCharacter::input_move_forward{ "move_forward" };
|
|
String const PlayerCharacter::input_move_backward{ "move_backward" };
|