feat: added health percentage to HUD

This commit is contained in:
Sara 2025-08-30 23:08:16 +02:00
parent a2c80df0b8
commit efe5607f60
3 changed files with 53 additions and 1 deletions

View file

@ -4,6 +4,8 @@ HeadsUpDisplay *HeadsUpDisplay::singleton_instance{ nullptr };
void HeadsUpDisplay::_bind_methods() {
ClassDB::bind_static_method("HeadsUpDisplay", D_METHOD("get_singleton"), &self_type::get_singleton);
ClassDB::bind_method(D_METHOD("set_tooltip", "tooltip"), &self_type::set_tooltip);
ClassDB::bind_method(D_METHOD("set_health_percentage", "percentage"), &self_type::set_health_percentage);
}
void HeadsUpDisplay::on_child_entered(Node *child) {
@ -11,6 +13,9 @@ void HeadsUpDisplay::on_child_entered(Node *child) {
if (child->is_unique_name_in_owner() && child->get_name() == "Tooltip") {
this->tooltip = cast_to<Label>(child);
}
if (child->is_unique_name_in_owner() && child->get_name() == "Healthbar") {
this->healthbar = cast_to<ProgressBar>(child);
}
}
void HeadsUpDisplay::enter_tree() {
@ -45,3 +50,7 @@ HeadsUpDisplay *HeadsUpDisplay::get_singleton() {
void HeadsUpDisplay::set_tooltip(String const &tooltip) {
this->tooltip->set_text(tooltip);
}
void HeadsUpDisplay::set_health_percentage(double health_percentage) {
this->healthbar->set_value(health_percentage);
}

View file

@ -3,6 +3,7 @@
#include "scene/gui/control.h"
#include "scene/gui/label.h"
#include "scene/gui/progress_bar.h"
class HeadsUpDisplay : public Control {
GDCLASS(HeadsUpDisplay, Control);
@ -18,9 +19,11 @@ protected:
public:
static HeadsUpDisplay *get_singleton();
void set_tooltip(String const &tooltip);
void set_health_percentage(double health_percentage);
private:
Label *tooltip{ nullptr };
ProgressBar *healthbar{ nullptr };
};
#endif // !HEADS_UP_DISPLAY_H

View file

@ -1,4 +1,28 @@
[gd_scene format=3 uid="uid://dqo1pfy3r00ai"]
[gd_scene load_steps=3 format=3 uid="uid://dqo1pfy3r00ai"]
[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_sbqmm"]
content_margin_left = 2.0
content_margin_top = 2.0
content_margin_right = 2.0
content_margin_bottom = 2.0
bg_color = Color(0.43, 0.43, 0.43, 1)
corner_radius_top_left = 6
corner_radius_top_right = 6
corner_radius_bottom_right = 6
corner_radius_bottom_left = 6
corner_detail = 6
[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_hsy36"]
content_margin_left = 2.0
content_margin_top = 2.0
content_margin_right = 2.0
content_margin_bottom = 2.0
bg_color = Color(0.45, 0.198, 0.198, 1)
corner_radius_top_left = 6
corner_radius_top_right = 6
corner_radius_bottom_right = 6
corner_radius_bottom_left = 6
corner_detail = 6
[node name="HeadsUpDisplay" type="HeadsUpDisplay"]
anchors_preset = 15
@ -32,3 +56,19 @@ offset_bottom = 44.0
grow_horizontal = 2
horizontal_alignment = 1
vertical_alignment = 2
[node name="Healthbar" type="ProgressBar" parent="."]
unique_name_in_owner = true
layout_mode = 1
anchors_preset = 2
anchor_top = 1.0
anchor_bottom = 1.0
offset_top = -37.0
offset_right = 280.0
grow_vertical = 0
theme_override_styles/background = SubResource("StyleBoxFlat_sbqmm")
theme_override_styles/fill = SubResource("StyleBoxFlat_hsy36")
max_value = 1.0
value = 1.0
rounded = true
show_percentage = false