feat: exposed health status getter on player

This commit is contained in:
Sara 2025-07-21 23:17:46 +02:00
parent 74aead946c
commit a734eb8b20
2 changed files with 23 additions and 17 deletions

View file

@ -1,4 +1,5 @@
#include "player_body.h" #include "player_body.h"
#include "health_status.h"
#include "macros.h" #include "macros.h"
#include "player_input.h" #include "player_input.h"
#include "weapon_base.h" #include "weapon_base.h"
@ -9,16 +10,19 @@ PlayerBody *PlayerBody::singleton_instance{ nullptr };
void PlayerBody::_bind_methods() { void PlayerBody::_bind_methods() {
} }
void PlayerBody::ready() { void PlayerBody::on_child_entered(Node *node) {
PlayerInput *input{ cast_to<PlayerInput>(get_node(NodePath("%PlayerInput"))) }; if (PlayerInput * input{ cast_to<PlayerInput>(node) }) {
input->connect(PlayerInput::sig_movement_input, callable_mp(this, &self_type::set_movement_input)); input->connect(PlayerInput::sig_movement_input, callable_mp(this, &self_type::set_movement_input));
input->connect(PlayerInput::sig_look_input, callable_mp(this, &self_type::on_look_input)); input->connect(PlayerInput::sig_look_input, callable_mp(this, &self_type::on_look_input));
input->connect(PlayerInput::sig_jump, callable_mp(this, &self_type::on_jump_input)); input->connect(PlayerInput::sig_jump, callable_mp(this, &self_type::on_jump_input));
input->connect(PlayerInput::sig_run, callable_mp(this, &self_type::on_run_input)); input->connect(PlayerInput::sig_run, callable_mp(this, &self_type::on_run_input));
this->weapons = cast_to<WeaponInventory>(get_node(NodePath("%WeaponInventory"))); }
} if (WeaponInventory * inventory{ cast_to<WeaponInventory>(node) }) {
this->weapons = inventory;
void PlayerBody::process(double delta) { }
if (HealthStatus * health{ cast_to<HealthStatus>(node) }) {
this->health = health;
}
} }
void PlayerBody::physics_process(double delta) { void PlayerBody::physics_process(double delta) {
@ -65,6 +69,7 @@ void PlayerBody::_notification(int what) {
return; return;
case NOTIFICATION_ENTER_TREE: case NOTIFICATION_ENTER_TREE:
singleton_instance = this; singleton_instance = this;
connect("child_entered_tree", callable_mp(this, &self_type::on_child_entered));
return; return;
case NOTIFICATION_EXIT_TREE: case NOTIFICATION_EXIT_TREE:
singleton_instance = nullptr; singleton_instance = nullptr;
@ -72,10 +77,6 @@ void PlayerBody::_notification(int what) {
case NOTIFICATION_READY: case NOTIFICATION_READY:
set_process(true); set_process(true);
set_physics_process(true); set_physics_process(true);
ready();
return;
case NOTIFICATION_PROCESS:
process(get_process_delta_time());
return; return;
case NOTIFICATION_PHYSICS_PROCESS: case NOTIFICATION_PHYSICS_PROCESS:
physics_process(get_physics_process_delta_time()); physics_process(get_physics_process_delta_time());
@ -94,3 +95,7 @@ bool PlayerBody::get_is_running() const {
bool PlayerBody::get_wants_to_run() const { bool PlayerBody::get_wants_to_run() const {
return this->try_running && this->movement_input.y > 0.f && this->is_on_floor(); return this->try_running && this->movement_input.y > 0.f && this->is_on_floor();
} }
HealthStatus *PlayerBody::get_health() const {
return this->health;
}

View file

@ -3,15 +3,14 @@
#include "scene/3d/physics/character_body_3d.h" #include "scene/3d/physics/character_body_3d.h"
class WeaponInventory; class WeaponInventory;
class HealthStatus;
class PlayerBody : public CharacterBody3D { class PlayerBody : public CharacterBody3D {
GDCLASS(PlayerBody, CharacterBody3D); GDCLASS(PlayerBody, CharacterBody3D);
static void _bind_methods(); static void _bind_methods();
static PlayerBody *singleton_instance; static PlayerBody *singleton_instance;
private: void on_child_entered(Node *node);
void ready();
void process(double delta);
void physics_process(double delta); void physics_process(double delta);
void set_movement_input(Vector2 state); void set_movement_input(Vector2 state);
@ -26,6 +25,7 @@ public:
static PlayerBody *get_singleton(); static PlayerBody *get_singleton();
bool get_is_running() const; bool get_is_running() const;
bool get_wants_to_run() const; bool get_wants_to_run() const;
HealthStatus *get_health() const;
private: private:
bool try_running{ false }; bool try_running{ false };
@ -36,6 +36,7 @@ private:
Vector2 movement_input{}; Vector2 movement_input{};
WeaponInventory *weapons; WeaponInventory *weapons;
HealthStatus *health{ nullptr };
}; };
#endif // !PLAYER_BODY_H #endif // !PLAYER_BODY_H