feat: cleanup to player input

stripped
Sara 2024-03-20 09:44:25 +01:00
parent e6c3d0988d
commit 1dd1d3b961
1 changed files with 10 additions and 11 deletions

View File

@ -1,4 +1,5 @@
#include "player_input.hpp"
#include "godot_cpp/variant/utility_functions.hpp"
#include "godot_macros.h"
#include "godot_cpp/classes/input.hpp"
#include "godot_cpp/classes/input_event.hpp"
@ -20,7 +21,7 @@ PlayerInput::Listener::Listener(String positive, String negative, Node *object,
, isMouseEvent{positive.begins_with("_mouse_") || negative.begins_with("_mouse_")} {}
PlayerInput::Listener::Listener(String action, Node *object, String method)
: PlayerInput::Listener::Listener(action, "", object, method) {}
: PlayerInput::Listener::Listener(action, String(), object, method) {}
std::optional<float> PlayerInput::Listener::evaluate_action(String const &action) {
Input *input = Input::get_singleton();
@ -34,9 +35,8 @@ std::optional<float> PlayerInput::Listener::evaluate_action(String const &action
return vector.x > 0.f ? vector.x : 0.f;
else if(action.ends_with("_left"))
return vector.x < 0.f ? -vector.x : 0.f;
else
return std::nullopt;
} else if(action.is_empty()) {
}
if(action.is_empty()) {
return 0.f;
} else {
return float(input->is_action_pressed(action));
@ -44,12 +44,10 @@ std::optional<float> PlayerInput::Listener::evaluate_action(String const &action
}
bool PlayerInput::Listener::has_changed(Ref<InputEvent> const &event) {
return (
(!event->is_class("InputEventMouseMotion") ||
this->isMouseEvent) ||
event->is_action(this->actionNegative) ||
event->is_action(this->actionPositive)
);
bool const mouse_changed{this->isMouseEvent && event->is_class("InputEventMouseMotion")};
bool const negative_changed{!this->actionNegative.is_empty() && event->is_action(this->actionNegative)};
bool const positive_changed{!this->actionPositive.is_empty() && event->is_action(this->actionPositive)};
return mouse_changed || negative_changed || positive_changed;
}
float PlayerInput::Listener::evaluate(Ref<InputEvent> const &event) {
@ -102,7 +100,8 @@ void PlayerInput::_unhandled_input(Ref<InputEvent> const &event) {
}
void PlayerInput::_process(double deltaTime) {
PlayerInput::lastMouseMotion = {0.f, 0.f};
if(this->isPrimary)
PlayerInput::lastMouseMotion = {0.f, 0.f};
}
void PlayerInput::listen_to(Listener const& listener) {