87 lines
3 KiB
C++
87 lines
3 KiB
C++
#include "player_input.h"
|
|
#include "scene/main/scene_tree.h"
|
|
|
|
String PlayerInput::sig_movement_input{ "movement_input" };
|
|
String PlayerInput::sig_look_input{ "look_input" };
|
|
String PlayerInput::sig_primary_fire{ "primary_fire" };
|
|
String PlayerInput::sig_alt_mode{ "alt_mode" };
|
|
String PlayerInput::sig_switch_weapon{ "switch_weapon" };
|
|
String PlayerInput::sig_run{ "run" };
|
|
String PlayerInput::sig_jump{ "jump" };
|
|
String PlayerInput::sig_crouch{ "crouch" };
|
|
|
|
void PlayerInput::_bind_methods() {
|
|
ADD_SIGNAL(MethodInfo(sig_movement_input, PropertyInfo(Variant::VECTOR2, "axes")));
|
|
ADD_SIGNAL(MethodInfo(sig_look_input, PropertyInfo(Variant::VECTOR2, "axes")));
|
|
ADD_SIGNAL(MethodInfo(sig_primary_fire, PropertyInfo(Variant::BOOL, "is_pressed")));
|
|
ADD_SIGNAL(MethodInfo(sig_alt_mode, PropertyInfo(Variant::BOOL, "is_active")));
|
|
ADD_SIGNAL(MethodInfo(sig_switch_weapon));
|
|
ADD_SIGNAL(MethodInfo(sig_run, PropertyInfo(Variant::BOOL, "is_running")));
|
|
ADD_SIGNAL(MethodInfo(sig_jump));
|
|
ADD_SIGNAL(MethodInfo(sig_crouch, PropertyInfo(Variant::BOOL, "is_crouching")));
|
|
}
|
|
|
|
void PlayerInput::normalize_input() {
|
|
emit_signal(sig_movement_input, Vector2());
|
|
emit_signal(sig_look_input, Vector2());
|
|
emit_signal(sig_primary_fire, false);
|
|
emit_signal(sig_alt_mode, false);
|
|
emit_signal(sig_run, false);
|
|
emit_signal(sig_crouch, false);
|
|
}
|
|
|
|
void PlayerInput::_notification(int what) {
|
|
if (Engine::get_singleton()->is_editor_hint()) {
|
|
return;
|
|
}
|
|
switch (what) {
|
|
case NOTIFICATION_READY:
|
|
set_process(true);
|
|
set_process_unhandled_input(true);
|
|
Input::get_singleton()->set_mouse_mode(Input::MouseMode::MOUSE_MODE_CAPTURED);
|
|
return;
|
|
case NOTIFICATION_PROCESS: {
|
|
bool const is_paused{ get_tree()->is_paused() };
|
|
if (is_paused != this->was_paused) {
|
|
this->was_paused = is_paused;
|
|
callable_mp(this, &self_type::normalize_input).call_deferred();
|
|
}
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
void PlayerInput::unhandled_input(Ref<InputEvent> const &event) {
|
|
Input *input{ Input::get_singleton() };
|
|
if (event->is_action("move_left") || event->is_action("move_right") || event->is_action("move_forward") || event->is_action("move_back")) {
|
|
Vector2 state{
|
|
input->get_axis("move_right", "move_left"),
|
|
input->get_axis("move_back", "move_forward")
|
|
};
|
|
emit_signal(sig_movement_input, state);
|
|
}
|
|
Ref<InputEventMouseMotion> mouse_motion{ event };
|
|
if (mouse_motion.is_valid()) {
|
|
Vector2 state{ -mouse_motion->get_relative() * 0.001f };
|
|
emit_signal(sig_look_input, state);
|
|
}
|
|
if (event->is_action("primary_fire")) {
|
|
bool state{ event->is_pressed() };
|
|
emit_signal(sig_primary_fire, state);
|
|
}
|
|
if (event->is_action("alt_mode")) {
|
|
bool state{ event->is_pressed() };
|
|
emit_signal(sig_alt_mode, state);
|
|
}
|
|
if (event->is_action("run")) {
|
|
bool state{ event->is_pressed() };
|
|
emit_signal(sig_run, state);
|
|
}
|
|
if (event->is_pressed() && event->is_action("jump") && input->is_action_just_pressed("jump")) {
|
|
emit_signal(sig_jump);
|
|
}
|
|
if (event->is_action("switch_weapon") && event->is_pressed()) {
|
|
emit_signal(sig_switch_weapon);
|
|
}
|
|
}
|