feat: cleaning and minor single-line optimization

main
Sara 2024-02-15 12:41:49 +01:00
parent 046fd8bf9f
commit bcb3e0b92f
2 changed files with 19 additions and 8 deletions

View File

@ -19,6 +19,7 @@ void Player::_bind_methods() {
#endif #endif
GDFUNCTION_ARGS(on_horizontal, "event", "value"); GDFUNCTION_ARGS(on_horizontal, "event", "value");
GDFUNCTION_ARGS(on_vertical, "event", "value"); GDFUNCTION_ARGS(on_vertical, "event", "value");
GDFUNCTION(get_is_moving);
} }
void Player::_enter_tree() { void Player::_enter_tree() {
@ -31,7 +32,7 @@ void Player::_enter_tree() {
void Player::_ready() { void Player::_ready() {
this->customization_init(); this->customization_init();
GDGAMEONLY(); GDGAMEONLY();
this->flat_view_init(); this->flat_view_update();
this->input->listen_to(PlayerInput::Listener("move_left", "move_right", this, "on_horizontal")); this->input->listen_to(PlayerInput::Listener("move_left", "move_right", this, "on_horizontal"));
this->input->listen_to(PlayerInput::Listener("move_forward", "move_back", this, "on_vertical")); this->input->listen_to(PlayerInput::Listener("move_forward", "move_back", this, "on_vertical"));
} }
@ -54,18 +55,19 @@ void Player::_physics_process(double deltaTime) {
this->move_and_slide(); this->move_and_slide();
} }
void Player::damage(Node *origin, int amount) {
}
void Player::set_customization_active(Node3D *node, bool active) { void Player::set_customization_active(Node3D *node, bool active) {
node->set_visible(active); node->set_visible(active);
node->set_process(active); node->set_process(active);
} }
void Player::on_horizontal(Ref<InputEvent> event, float value) { void Player::on_horizontal(Ref<InputEvent> event, float value) {
UtilityFunctions::print("horizontal ", value);
this->moveInput.x = value; this->moveInput.x = value;
} }
void Player::on_vertical(Ref<InputEvent> event, float value) { void Player::on_vertical(Ref<InputEvent> event, float value) {
UtilityFunctions::print("vertical ", value);
this->moveInput.y = value; this->moveInput.y = value;
} }
@ -138,7 +140,7 @@ void Player::rotate_to_movement() {
this->set_global_transform(trans); this->set_global_transform(trans);
} }
void Player::flat_view_init() { void Player::flat_view_update() {
Basis basis = this->get_viewport()->get_camera_3d()->get_global_transform().get_basis(); Basis basis = this->get_viewport()->get_camera_3d()->get_global_transform().get_basis();
Vector3 right = basis.get_column(0); Vector3 right = basis.get_column(0);
right = Vector3{-right.x, 0, -right.z}.normalized(); right = Vector3{-right.x, 0, -right.z}.normalized();
@ -190,4 +192,8 @@ void Player::customization_init() {
UtilityFunctions::print("removing ", key); UtilityFunctions::print("removing ", key);
} }
} }
bool Player::get_is_moving() const {
return this->is_on_floor() && !this->moveInput.is_zero_approx();
}
} // namespace godot } // namespace godot

View File

@ -2,15 +2,17 @@
#define PLAYER_HPP #define PLAYER_HPP
#include <map> #include <map>
#include "constants.hpp"
#include "godot_cpp/classes/animation_tree.hpp" #include "godot_cpp/classes/animation_tree.hpp"
#include "godot_cpp/classes/character_body3d.hpp" #include "godot_cpp/classes/character_body3d.hpp"
#include "godot_cpp/classes/input_event.hpp" #include "godot_cpp/classes/input_event.hpp"
#include "godot_cpp/classes/skeleton3d.hpp" #include "godot_cpp/classes/skeleton3d.hpp"
#include "hitable_interface.hpp"
#include "player_input.hpp" #include "player_input.hpp"
#include <optional> #include <optional>
namespace godot { namespace godot {
class Player : public CharacterBody3D { class Player : public CharacterBody3D, public HitableInterface {
GDCLASS(Player, CharacterBody3D) GDCLASS(Player, CharacterBody3D)
static void _bind_methods(); static void _bind_methods();
// the current customization state of a body part/attachment // the current customization state of a body part/attachment
@ -29,9 +31,9 @@ protected:
// the skeleton of the customizable character model // the skeleton of the customizable character model
Skeleton3D *customizationParent{nullptr}; Skeleton3D *customizationParent{nullptr};
PlayerInput *input{nullptr}; PlayerInput *input{nullptr};
Basis flatViewBasis{}; Basis flatViewBasis{identity::Basis};
float walkSpeed{4.f}; static float constexpr walkSpeed{4.f};
Vector2 moveInput{0.f, 0.f}; Vector2 moveInput{0.f, 0.f};
@ -41,6 +43,8 @@ public:
virtual void _process(double deltaTime) override; virtual void _process(double deltaTime) override;
virtual void _physics_process(double deltaTime) override; virtual void _physics_process(double deltaTime) override;
virtual void damage(Node *origin, int amount) override;
static void set_customization_active(Node3D *node, bool active); static void set_customization_active(Node3D *node, bool active);
void on_horizontal(Ref<InputEvent> event, float value); void on_horizontal(Ref<InputEvent> event, float value);
@ -61,8 +65,9 @@ public:
private: private:
void rotate_to_movement(); void rotate_to_movement();
void flat_view_init(); void flat_view_update();
void customization_init(); void customization_init();
bool get_is_moving() const;
}; };
} // namespace godot } // namespace godot