feat: player health status updates HUD
This commit is contained in:
parent
7b231bd0f3
commit
e6af6557e0
|
@ -9,6 +9,7 @@ void HealthStatus::_bind_methods() {
|
|||
|
||||
ADD_SIGNAL(MethodInfo(sig_death));
|
||||
ADD_SIGNAL(MethodInfo(sig_health_changed, PropertyInfo(Variant::INT, "remaining"), PropertyInfo(Variant::INT, "delta")));
|
||||
ClassDB::bind_method(D_METHOD("get_max_health"), &self_type::get_max_health);
|
||||
}
|
||||
|
||||
void HealthStatus::ready() {
|
||||
|
@ -30,12 +31,20 @@ void HealthStatus::_notification(int what) {
|
|||
|
||||
void HealthStatus::set_health(int value) {
|
||||
this->health = value;
|
||||
if (!is_ready() || Engine::get_singleton()->is_editor_hint()) {
|
||||
// if setting health as serialized, set max_health as well
|
||||
this->max_health = value;
|
||||
}
|
||||
}
|
||||
|
||||
int HealthStatus::get_health() const {
|
||||
return this->health;
|
||||
}
|
||||
|
||||
int HealthStatus::get_max_health() const {
|
||||
return this->max_health;
|
||||
}
|
||||
|
||||
void HealthStatus::damage(int amount) {
|
||||
if (this->health > 0) {
|
||||
amount = Math::abs(amount);
|
||||
|
|
|
@ -14,6 +14,7 @@ protected:
|
|||
public:
|
||||
void set_health(int health);
|
||||
int get_health() const;
|
||||
int get_max_health() const;
|
||||
|
||||
void damage(int amount);
|
||||
void heal(int amount);
|
||||
|
|
|
@ -10,6 +10,12 @@ script/source = "extends HealthStatus
|
|||
|
||||
func _on_death() -> void:
|
||||
get_tree().change_scene_to_file.call_deferred(\"res://guis/main_menu.tscn\")
|
||||
|
||||
|
||||
func _on_health_changed(remaining: int, _delta: int) -> void:
|
||||
var hud := HeadsUpDisplay.get_singleton()
|
||||
if hud:
|
||||
hud.set_health_percentage(float(remaining) / float(self.get_max_health()))
|
||||
"
|
||||
|
||||
[sub_resource type="CapsuleShape3D" id="CapsuleShape3D_bxedw"]
|
||||
|
@ -61,3 +67,4 @@ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0)
|
|||
shape = SubResource("CapsuleShape3D_eqqp1")
|
||||
|
||||
[connection signal="death" from="HealthStatus" to="HealthStatus" method="_on_death"]
|
||||
[connection signal="health_changed" from="HealthStatus" to="HealthStatus" method="_on_health_changed"]
|
||||
|
|
Loading…
Reference in a new issue