From 1dd1d3b961f12a22296eb1bd43c6bb40e15cabec Mon Sep 17 00:00:00 2001 From: Sara Date: Wed, 20 Mar 2024 09:44:25 +0100 Subject: [PATCH] feat: cleanup to player input --- player_input.cpp | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/player_input.cpp b/player_input.cpp index a4ca823..10098c3 100644 --- a/player_input.cpp +++ b/player_input.cpp @@ -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 PlayerInput::Listener::evaluate_action(String const &action) { Input *input = Input::get_singleton(); @@ -34,9 +35,8 @@ std::optional 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 PlayerInput::Listener::evaluate_action(String const &action } bool PlayerInput::Listener::has_changed(Ref 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 const &event) { @@ -102,7 +100,8 @@ void PlayerInput::_unhandled_input(Ref 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) {