73 lines
2.1 KiB
C++
73 lines
2.1 KiB
C++
#include "player_input.h"
|
|
|
|
#include "core/input/input_event.h"
|
|
#include "macros.h"
|
|
|
|
String const PlayerInput::signal_movement{ "movement" };
|
|
|
|
void PlayerInput::_bind_methods() {
|
|
BIND_PROPERTY(Variant::STRING, action_move_left);
|
|
BIND_PROPERTY(Variant::STRING, action_move_right);
|
|
BIND_PROPERTY(Variant::STRING, action_move_forward);
|
|
BIND_PROPERTY(Variant::STRING, action_move_back);
|
|
ADD_SIGNAL(MethodInfo(self_type::signal_movement, PropertyInfo(Variant::VECTOR2, "movement_directions")));
|
|
}
|
|
|
|
void PlayerInput::_notification(int what) {
|
|
if (Engine::get_singleton()->is_editor_hint()) {
|
|
return;
|
|
}
|
|
switch (what) {
|
|
default:
|
|
return;
|
|
case NOTIFICATION_READY:
|
|
this->set_process_unhandled_input(true);
|
|
return;
|
|
}
|
|
}
|
|
|
|
void PlayerInput::unhandled_input(Ref<InputEvent> const &event) {
|
|
Input *input{ Input::get_singleton() };
|
|
bool is_movement_action{ event->is_action(self_type::action_move_left) };
|
|
is_movement_action |= event->is_action(self_type::action_move_right);
|
|
is_movement_action |= event->is_action(self_type::action_move_forward);
|
|
is_movement_action |= event->is_action(self_type::action_move_back);
|
|
if (is_movement_action) {
|
|
float const h_axis{ input->get_axis(self_type::action_move_right, self_type::action_move_left) };
|
|
float const v_axis{ input->get_axis(self_type::action_move_back, self_type::action_move_forward) };
|
|
this->emit_signal(self_type::signal_movement, Vector2{ h_axis, v_axis });
|
|
}
|
|
}
|
|
|
|
void PlayerInput::set_action_move_left(String action) {
|
|
this->action_move_left = action;
|
|
}
|
|
|
|
String PlayerInput::get_action_move_left() const {
|
|
return this->action_move_left;
|
|
}
|
|
|
|
void PlayerInput::set_action_move_right(String action) {
|
|
this->action_move_right = action;
|
|
}
|
|
|
|
String PlayerInput::get_action_move_right() const {
|
|
return this->action_move_right;
|
|
}
|
|
|
|
void PlayerInput::set_action_move_forward(String action) {
|
|
this->action_move_forward = action;
|
|
}
|
|
|
|
String PlayerInput::get_action_move_forward() const {
|
|
return this->action_move_forward;
|
|
}
|
|
|
|
void PlayerInput::set_action_move_back(String action) {
|
|
this->action_move_back = action;
|
|
}
|
|
|
|
String PlayerInput::get_action_move_back() const {
|
|
return this->action_move_back;
|
|
}
|