From 7713127e27544e57d112c64811f0ef8c940c003c Mon Sep 17 00:00:00 2001 From: Alan Youngblood Date: Sun, 24 Nov 2024 12:41:42 -0500 Subject: [PATCH] Tutorial 18.3 Player Stats with HP Label --- ActionRPG-HeartBeast/Stats.gd | 2 ++ ActionRPG-HeartBeast/UI/HealthUI.gd | 19 +++++++++++++++++++ ActionRPG-HeartBeast/UI/HealthUI.tscn | 13 +++++++++++++ ActionRPG-HeartBeast/World.tscn | 5 ++++- 4 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 ActionRPG-HeartBeast/UI/HealthUI.gd create mode 100644 ActionRPG-HeartBeast/UI/HealthUI.tscn diff --git a/ActionRPG-HeartBeast/Stats.gd b/ActionRPG-HeartBeast/Stats.gd index 5dd8dc2..d7e6347 100644 --- a/ActionRPG-HeartBeast/Stats.gd +++ b/ActionRPG-HeartBeast/Stats.gd @@ -4,8 +4,10 @@ export(int) var max_health = 1 onready var health = max_health setget set_health signal no_health +signal health_changed(value) func set_health(value): health = value + emit_signal("health_changed", health) if health <= 0: emit_signal("no_health") diff --git a/ActionRPG-HeartBeast/UI/HealthUI.gd b/ActionRPG-HeartBeast/UI/HealthUI.gd new file mode 100644 index 0000000..64e341f --- /dev/null +++ b/ActionRPG-HeartBeast/UI/HealthUI.gd @@ -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") diff --git a/ActionRPG-HeartBeast/UI/HealthUI.tscn b/ActionRPG-HeartBeast/UI/HealthUI.tscn new file mode 100644 index 0000000..63772bb --- /dev/null +++ b/ActionRPG-HeartBeast/UI/HealthUI.tscn @@ -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" diff --git a/ActionRPG-HeartBeast/World.tscn b/ActionRPG-HeartBeast/World.tscn index 860f3ab..e372d25 100644 --- a/ActionRPG-HeartBeast/World.tscn +++ b/ActionRPG-HeartBeast/World.tscn @@ -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://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://touch_controls/TouchControls.tscn" type="PackedScene" id=7] [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] 0/name = "DirtTileset.png 0" @@ -608,3 +609,5 @@ margin_right = 40.0 margin_bottom = 40.0 [node name="TouchControls" parent="Control" instance=ExtResource( 7 )] + +[node name="HealthUI" parent="." instance=ExtResource( 9 )]