Made hp bar aware of max hp

This commit is contained in:
Florian 2025-09-17 14:39:22 +02:00
parent fc55aa4c09
commit 5658b57749
4 changed files with 59 additions and 11 deletions

View file

@ -1,11 +1,13 @@
extends HBoxContainer
@export var initial_health : int = 5
@export var packedHeart : PackedScene;
var current_max_hp = 0
var current_health = 0
func _ready() -> void:
for i in range(initial_health):
add_child(packedHeart.instantiate())
#for i in range(initial_health):
#add_child(packedHeart.instantiate())
pass
func set_health(health : int):
var i = 0
@ -15,7 +17,27 @@ func set_health(health : int):
heart.restore()
else:
heart.fade()
current_health = health
func set_maxhealth(new_max_hp : int):
if new_max_hp > current_max_hp:
for i in range(new_max_hp - current_max_hp):
add_child(packedHeart.instantiate())
elif new_max_hp < current_max_hp:
var children = get_children()
for i in range(children.size()):
if new_max_hp < i+1:
children[i].queue_free()
current_health = min(current_health, current_max_hp)
else: pass
current_max_hp = new_max_hp
set_health(current_max_hp)
func _on_player_health_changed(new_health: int) -> void:
set_health(new_health)
func _on_player_max_hp_changed(new_max_hp: int) -> void:
set_maxhealth(new_max_hp)

View file

@ -1,4 +1,6 @@
[gd_scene load_steps=2 format=3 uid="uid://73g8y37skebh"]
[gd_scene load_steps=3 format=3 uid="uid://73g8y37skebh"]
[ext_resource type="Texture2D" uid="uid://cy70quh6k3s1j" path="res://icon.svg" id="1_lfuq2"]
[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_8oq0s"]
draw_center = false
@ -13,11 +15,23 @@ corner_radius_top_right = 50
corner_radius_bottom_right = 50
corner_radius_bottom_left = 50
[node name="ItemUI" type="PanelContainer"]
[node name="ItemUI" type="MarginContainer"]
anchors_preset = 1
anchor_left = 1.0
anchor_right = 1.0
offset_left = -100.0
offset_bottom = 100.0
offset_left = -150.0
offset_bottom = 150.0
grow_horizontal = 0
theme_override_constants/margin_left = 25
theme_override_constants/margin_top = 25
theme_override_constants/margin_right = 25
theme_override_constants/margin_bottom = 25
[node name="PanelContainer" type="PanelContainer" parent="."]
layout_mode = 2
theme_override_styles/panel = SubResource("StyleBoxFlat_8oq0s")
[node name="TextureRect" type="TextureRect" parent="PanelContainer"]
layout_mode = 2
texture = ExtResource("1_lfuq2")
expand_mode = 1

View file

@ -1,13 +1,14 @@
[gd_scene load_steps=10 format=3 uid="uid://cxo6bq26huau7"]
[gd_scene load_steps=11 format=3 uid="uid://cxo6bq26huau7"]
[ext_resource type="PackedScene" uid="uid://cmaovvr15b3qk" path="res://player/player.tscn" id="2_1bvp3"]
[ext_resource type="Script" uid="uid://vgxh2xdevat7" path="res://world/earth.gd" id="2_lquwl"]
[ext_resource type="PackedScene" uid="uid://jjoyj1ldafkf" path="res://world/earth.tscn" id="3_lquwl"]
[ext_resource type="Script" uid="uid://colvx6wq0e8n7" path="res://world/building_generator.gd" id="4_1bvp3"]
[ext_resource type="PackedScene" uid="uid://73g8y37skebh" path="res://item_ui/item_ui.tscn" id="6_4c57u"]
[ext_resource type="PackedScene" uid="uid://cjsrtswk4vgf2" path="res://healthbar/healthbar.tscn" id="6_7mycd"]
[ext_resource type="PackedScene" uid="uid://dpdn2php3ydsv" path="res://death_screen/death_screen.tscn" id="7_5vw27"]
[ext_resource type="PackedScene" path="res://utils/platform.tscn" id="7_272bh"]
[ext_resource type="PackedScene" path="res://items/healthup.tscn" id="9_4c57u"]
[ext_resource type="PackedScene" uid="uid://4l3elvxpghw8" path="res://utils/platform.tscn" id="7_272bh"]
[ext_resource type="PackedScene" uid="uid://gwctb2xqsbj" path="res://items/healthup.tscn" id="9_4c57u"]
[ext_resource type="PackedScene" uid="uid://chu67ci7sl488" path="res://enemies/ghost.tscn" id="9_kek77"]
[node name="main" type="Node2D"]
@ -42,6 +43,8 @@ autostart = true
offset_right = 128.0
offset_bottom = 128.0
[node name="ItemUI" parent="CanvasLayer" instance=ExtResource("6_4c57u")]
[node name="DeathScreen" parent="CanvasLayer" instance=ExtResource("7_5vw27")]
visible = false
@ -56,6 +59,7 @@ position = Vector2(0, -3000)
position = Vector2(0, -3100)
[connection signal="health_changed" from="Player" to="CanvasLayer/Healthbar" method="_on_player_health_changed"]
[connection signal="max_hp_changed" from="Player" to="CanvasLayer/Healthbar" method="_on_player_max_hp_changed"]
[connection signal="player_died" from="Player" to="CanvasLayer/DeathScreen" method="_on_player_player_died"]
[connection signal="timeout" from="Building Generator/Timer" to="Building Generator" method="_on_timer_timeout"]
[connection signal="visibility_changed" from="CanvasLayer/DeathScreen" to="Player" method="_on_death_screen_visibility_changed"]

View file

@ -22,13 +22,17 @@ var air_jumps_current = 1;
# HP and Iframes
signal health_changed(new_health : int)
signal max_hp_changed(new_max_hp : int)
signal player_died
var current_hp = 5:
set(new_hp):
current_hp = new_hp
health_changed.emit(current_hp)
var max_hp = 5;
var max_hp = 5:
set(new_max_hp):
max_hp = new_max_hp
max_hp_changed.emit(max_hp)
var hit_invulnerability = 0.5
var inv_time = 0;
var dead = false;
@ -46,6 +50,10 @@ var atk_timer = 0
var active_item = null
var item_cooldown = 0
func _ready() -> void:
max_hp_changed.emit(max_hp)
health_changed.emit(current_hp)
func _physics_process(delta: float) -> void:
manage_iframes(delta)
manage_movement_options()