Tutorial 18.3 Player Stats with HP Label

This commit is contained in:
Alan Youngblood 2024-11-24 12:41:42 -05:00
parent 24bc047855
commit 7713127e27
4 changed files with 38 additions and 1 deletions

View File

@ -4,8 +4,10 @@ export(int) var max_health = 1
onready var health = max_health setget set_health onready var health = max_health setget set_health
signal no_health signal no_health
signal health_changed(value)
func set_health(value): func set_health(value):
health = value health = value
emit_signal("health_changed", health)
if health <= 0: if health <= 0:
emit_signal("no_health") emit_signal("no_health")

View File

@ -0,0 +1,19 @@
extends Control
var hearts = 4 setget set_hearts
var max_hearts = 4 setget set_max_hearts
onready var label = $Label
func set_hearts(value):
hearts = clamp(value, 0, max_hearts)
if label != null:
label.text = "HP = " + str(hearts)
func set_max_hearts(value):
max_hearts = max(value, 1)
func _ready():
self.max_hearts = PlayerStats.max_health
self.hearts = PlayerStats.health
PlayerStats.connect("health_changed", self, "set_hearts")

View File

@ -0,0 +1,13 @@
[gd_scene load_steps=2 format=2]
[ext_resource path="res://UI/HealthUI.gd" type="Script" id=1]
[node name="HealthUI" type="Control"]
margin_right = 122.0
margin_bottom = 25.0
script = ExtResource( 1 )
[node name="Label" type="Label" parent="."]
margin_right = 42.0
margin_bottom = 14.0
text = "HP = 4"

View File

@ -1,4 +1,4 @@
[gd_scene load_steps=62 format=2] [gd_scene load_steps=63 format=2]
[ext_resource path="res://World/Bush.tscn" type="PackedScene" id=1] [ext_resource path="res://World/Bush.tscn" type="PackedScene" id=1]
[ext_resource path="res://Player/Player.tscn" type="PackedScene" id=2] [ext_resource path="res://Player/Player.tscn" type="PackedScene" id=2]
@ -8,6 +8,7 @@
[ext_resource path="res://World/Grass.tscn" type="PackedScene" id=6] [ext_resource path="res://World/Grass.tscn" type="PackedScene" id=6]
[ext_resource path="res://touch_controls/TouchControls.tscn" type="PackedScene" id=7] [ext_resource path="res://touch_controls/TouchControls.tscn" type="PackedScene" id=7]
[ext_resource path="res://Enemies/Bat.tscn" type="PackedScene" id=8] [ext_resource path="res://Enemies/Bat.tscn" type="PackedScene" id=8]
[ext_resource path="res://UI/HealthUI.tscn" type="PackedScene" id=9]
[sub_resource type="TileSet" id=1] [sub_resource type="TileSet" id=1]
0/name = "DirtTileset.png 0" 0/name = "DirtTileset.png 0"
@ -608,3 +609,5 @@ margin_right = 40.0
margin_bottom = 40.0 margin_bottom = 40.0
[node name="TouchControls" parent="Control" instance=ExtResource( 7 )] [node name="TouchControls" parent="Control" instance=ExtResource( 7 )]
[node name="HealthUI" parent="." instance=ExtResource( 9 )]