Add HUD component and update player state management for lives

This commit is contained in:
2025-04-27 02:02:03 +02:00
parent 0c1192536c
commit c3304408b9
10 changed files with 286 additions and 5 deletions

View File

@@ -1,9 +1,14 @@
class_name PlayerDeathComponent
extends Node
@onready var gm: GM = $"/root/GameManager"
func reset_scene() -> void:
get_tree().reload_current_scene()
func _on_health_component_on_death() -> void:
# todo: play audio on death
gm.remove_lives(1)
call_deferred("reset_scene")

View File

@@ -2,17 +2,38 @@ class_name GM
extends Node
var player_state = {
"coins": 0,
}
"coins": 0,
"lives": 3,
}
func add_coins(amount: int) -> void:
player_state["coins"] += amount
func set_coins(amount: int) -> void:
player_state["coins"] = amount
func get_coins() -> int:
return player_state["coins"]
func remove_coins(amount: int) -> void:
player_state["coins"] -= amount
player_state["coins"] -= amount
func add_lives(amount: int) -> void:
player_state["lives"] += amount
func remove_lives(amount: int) -> void:
player_state["lives"] -= amount
func set_lives(amount: int) -> void:
player_state["lives"] = amount
func get_lives() -> int:
return player_state["lives"]

51
scripts/hud.gd Normal file
View File

@@ -0,0 +1,51 @@
class_name Hud
extends Node
@export var player_health: HealthComponent
@export var coins_label: Label
@export var health_progressbar: ProgressBar
@export var lives_label: Label
@onready var game_manager: GM = $"/root/GameManager"
func _ready() -> void:
if not player_health:
var nodes := get_tree().get_nodes_in_group("player")
for node in nodes:
player_health = node.get_node_or_null("HealthComponent")
if player_health:
break
return
func _process(_delta: float) -> void:
if not game_manager:
return
set_health_progressbar()
set_lives_label()
set_coins_label()
func set_coins_label() -> void:
if not game_manager:
return
#todo: set internationalized text
coins_label.text = "Coins: " + str(game_manager.get_coins())
func set_lives_label() -> void:
if not game_manager:
return
lives_label.text = "Lives: " + str(game_manager.get_lives())
func set_health_progressbar() -> void:
if not player_health:
return
health_progressbar.value = player_health.health
health_progressbar.max_value = player_health.max_health