Added health bar

This commit is contained in:
2025-11-15 03:52:16 +03:00
parent 67b629baad
commit 85a4fc7381
14 changed files with 258 additions and 10 deletions
@@ -0,0 +1,60 @@
class_name HealthBarPart
extends Node2D
@export var texture_value : Texture2D
@export var texture_shade : Texture2D
@onready var value_bar : TextureProgressBar = $ValueBar
@onready var shade_bar : TextureProgressBar = $ShadeBar
@onready var shade_reset_timer : Timer = $ShadeResetTimer
@onready var shade_tick_timer : Timer = $ShadeTickTimer
const TICK_COUNT = 5
var _tick_size : int = 0
var _target_value: int = 0
func _ready() -> void:
value_bar.texture_progress = texture_value
shade_bar.texture_progress = texture_shade
func set_value(value: int) -> void:
if value_bar.value == value: return
value_bar.value = value
if shade_bar.value < value_bar.value:
shade_bar.value = value_bar.value
else:
shade_reset_timer.start()
func set_max_value(max_value: int) -> void:
value_bar.max_value = max_value
shade_bar.max_value = max_value
if max_value == 0:
value_bar.hide()
shade_bar.hide()
else:
value_bar.show()
shade_bar.show()
func _on_shade_reset_timer_timeout() -> void:
var value_delta := shade_bar.value - value_bar.value
_tick_size = ceil(value_delta / TICK_COUNT)
_target_value = value_bar.value
shade_tick_timer.start()
func _on_shade_tick_timer_timeout() -> void:
shade_bar.value -= _tick_size
if shade_bar.value <= _target_value:
shade_bar.value = _target_value
shade_tick_timer.stop()
@@ -0,0 +1 @@
uid://drwgq8fxaclvn
@@ -0,0 +1,48 @@
[gd_scene load_steps=4 format=3 uid="uid://xbfxsiumbgkp"]
[ext_resource type="Script" uid="uid://drwgq8fxaclvn" path="res://game/health_system/health_bar/health_bar_part.gd" id="1_nuv67"]
[ext_resource type="Texture2D" uid="uid://do586oblhwuc5" path="res://images/health.png" id="2_jlvn5"]
[sub_resource type="AtlasTexture" id="AtlasTexture_jlvn5"]
atlas = ExtResource("2_jlvn5")
region = Rect2(0, 48, 32, 16)
[node name="HealthBarPart" type="Node2D"]
script = ExtResource("1_nuv67")
[node name="ShadeBar" type="TextureProgressBar" parent="."]
anchors_preset = 8
anchor_left = 0.5
anchor_top = 0.5
anchor_right = 0.5
anchor_bottom = 0.5
offset_left = -16.0
offset_top = -8.0
offset_right = 16.0
offset_bottom = 8.0
grow_horizontal = 2
grow_vertical = 2
texture_under = SubResource("AtlasTexture_jlvn5")
[node name="ValueBar" type="TextureProgressBar" parent="."]
anchors_preset = 8
anchor_left = 0.5
anchor_top = 0.5
anchor_right = 0.5
anchor_bottom = 0.5
offset_left = -16.0
offset_top = -8.0
offset_right = 16.0
offset_bottom = 8.0
grow_horizontal = 2
grow_vertical = 2
[node name="ShadeResetTimer" type="Timer" parent="."]
wait_time = 0.5
one_shot = true
[node name="ShadeTickTimer" type="Timer" parent="."]
wait_time = 0.1
[connection signal="timeout" from="ShadeResetTimer" to="." method="_on_shade_reset_timer_timeout"]
[connection signal="timeout" from="ShadeTickTimer" to="." method="_on_shade_tick_timer_timeout"]
@@ -0,0 +1,26 @@
class_name HealthBar
extends Node2D
@export var health: Health
@onready var shield_part : HealthBarPart = $ShieldPart
@onready var armor_part : HealthBarPart = $ArmorPart
@onready var hull_part : HealthBarPart = $HullPart
func _ready() -> void:
if health:
shield_part.set_max_value(health.max_shield)
shield_part.set_value(health.shield)
armor_part.set_max_value(health.max_armor)
armor_part.set_value(health.armor)
hull_part.set_max_value(health.max_hull)
hull_part.set_value(health.hull)
health.shield_updated.connect(shield_part.set_value)
health.armor_updated.connect(armor_part.set_value)
health.hull_updated.connect(hull_part.set_value)
@@ -0,0 +1 @@
uid://be7k64p2kel8b
@@ -0,0 +1,44 @@
[gd_scene load_steps=10 format=3 uid="uid://d2snum2pxc2ui"]
[ext_resource type="Script" uid="uid://be7k64p2kel8b" path="res://game/health_system/health_bar/heath_bar.gd" id="1_bx561"]
[ext_resource type="PackedScene" uid="uid://xbfxsiumbgkp" path="res://game/health_system/health_bar/health_bar_part.tscn" id="2_wb6me"]
[ext_resource type="Texture2D" uid="uid://do586oblhwuc5" path="res://images/health.png" id="3_fogsl"]
[sub_resource type="AtlasTexture" id="AtlasTexture_wb6me"]
atlas = ExtResource("3_fogsl")
region = Rect2(0, 0, 32, 16)
[sub_resource type="AtlasTexture" id="AtlasTexture_fogsl"]
atlas = ExtResource("3_fogsl")
region = Rect2(32, 0, 32, 16)
[sub_resource type="AtlasTexture" id="AtlasTexture_b6cw0"]
atlas = ExtResource("3_fogsl")
region = Rect2(0, 16, 32, 16)
[sub_resource type="AtlasTexture" id="AtlasTexture_w8ken"]
atlas = ExtResource("3_fogsl")
region = Rect2(32, 16, 32, 16)
[sub_resource type="AtlasTexture" id="AtlasTexture_lfh3j"]
atlas = ExtResource("3_fogsl")
region = Rect2(0, 32, 32, 16)
[sub_resource type="AtlasTexture" id="AtlasTexture_lc4fa"]
atlas = ExtResource("3_fogsl")
region = Rect2(32, 32, 32, 16)
[node name="HeathBar" type="Node2D"]
script = ExtResource("1_bx561")
[node name="HullPart" parent="." instance=ExtResource("2_wb6me")]
texture_value = SubResource("AtlasTexture_wb6me")
texture_shade = SubResource("AtlasTexture_fogsl")
[node name="ArmorPart" parent="." instance=ExtResource("2_wb6me")]
texture_value = SubResource("AtlasTexture_b6cw0")
texture_shade = SubResource("AtlasTexture_w8ken")
[node name="ShieldPart" parent="." instance=ExtResource("2_wb6me")]
texture_value = SubResource("AtlasTexture_lfh3j")
texture_shade = SubResource("AtlasTexture_lc4fa")