Skip to content

Commit 983b76f

Browse files
committed
feat(player): extract HealthBar to its own scene
1 parent e4f0363 commit 983b76f

File tree

4 files changed

+47
-39
lines changed

4 files changed

+47
-39
lines changed

objects/actors/player/Player.gd

+8-10
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@ class_name Player
22
extends Actor
33

44
@export var chestModifierSpeed: float = 0.7
5-
@export var maxHealth = 100
6-
var lifePoint = maxHealth
5+
@export var life = 100
76

87
@onready var attackTimer = $AttackTimer
9-
@onready var healthbar = $HealthBar
8+
@onready var healthbar: HealthBar = $HealthBar
109
@onready var animation_player: AnimationPlayer = $AnimationPlayer
1110

1211
@export var weapon: Weapon
@@ -15,8 +14,7 @@ var hasChest = false
1514
func _ready() -> void:
1615
if weapon == null:
1716
weapon = StoreManager.player_weapon
18-
healthbar.max_value = maxHealth
19-
healthbar.value = maxHealth
17+
healthbar.init(life)
2018
attackTimer.wait_time = weapon.attack_speed
2119

2220
func _input(event):
@@ -28,14 +26,14 @@ func _input(event):
2826
attack()
2927

3028
func apply_attack(force: int) -> void:
31-
if lifePoint == 0:
29+
if life <= 0:
3230
return
3331

34-
lifePoint -= force
35-
animation_player.play('Hit')
36-
healthbar.value = lifePoint
32+
life -= force
33+
animation_player.play("Hit")
34+
healthbar.value = life
3735

38-
if (lifePoint == 0):
36+
if (life <= 0):
3937
animation_player.play("Death")
4038

4139
func get_speed():

objects/actors/player/Player.tscn

+18-29
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
[ext_resource type="Resource" uid="uid://r3i80a6v2w0y" path="res://resources/weapons/gun/Gun.tres" id="3_irbyu"]
77
[ext_resource type="Texture2D" uid="uid://dw5vuo3agm77m" path="res://objects/actors/player/template.png" id="3_q5uec"]
88
[ext_resource type="PackedScene" uid="uid://cxlkjx4h6gv5h" path="res://components/collector/Collector.tscn" id="5_q1uwo"]
9-
[ext_resource type="Script" path="res://objects/actors/player/HealthBar.gd" id="7_ot68h"]
9+
[ext_resource type="PackedScene" uid="uid://c5w6dv2r65qsd" path="res://objects/hud/HealthBar.tscn" id="7_svy1a"]
1010

1111
[sub_resource type="SceneReplicationConfig" id="SceneReplicationConfig_p4sxr"]
1212
properties/0/path = NodePath(".:position")
@@ -544,21 +544,6 @@ tracks/1/keys = {
544544
}]
545545
}
546546

547-
[sub_resource type="Animation" id="Animation_fx0oj"]
548-
resource_name = "Hit"
549-
length = 0.4
550-
tracks/0/type = "bezier"
551-
tracks/0/imported = false
552-
tracks/0/enabled = true
553-
tracks/0/path = NodePath("AnimatedSprite:material:shader_parameter/flash_value")
554-
tracks/0/interp = 1
555-
tracks/0/loop_wrap = true
556-
tracks/0/keys = {
557-
"handle_modes": PackedInt32Array(0, 0, 0),
558-
"points": PackedFloat32Array(0, -0.2, 0, 0.2, 0, 1, -0.2, 0, 0.2, 0, 0, -0.2, 0, 0.2, 0),
559-
"times": PackedFloat32Array(0, 0.2, 0.4)
560-
}
561-
562547
[sub_resource type="Animation" id="Animation_l1t87"]
563548
resource_name = "fade_away"
564549
tracks/0/type = "value"
@@ -588,6 +573,21 @@ tracks/1/keys = {
588573
}]
589574
}
590575

576+
[sub_resource type="Animation" id="Animation_fx0oj"]
577+
resource_name = "Hit"
578+
length = 0.4
579+
tracks/0/type = "bezier"
580+
tracks/0/imported = false
581+
tracks/0/enabled = true
582+
tracks/0/path = NodePath("AnimatedSprite:material:shader_parameter/flash_value")
583+
tracks/0/interp = 1
584+
tracks/0/loop_wrap = true
585+
tracks/0/keys = {
586+
"handle_modes": PackedInt32Array(0, 0, 0),
587+
"points": PackedFloat32Array(0, -0.2, 0, 0.2, 0, 1, -0.2, 0, 0.2, 0, 0, -0.2, 0, 0.2, 0),
588+
"times": PackedFloat32Array(0, 0.2, 0.4)
589+
}
590+
591591
[sub_resource type="Animation" id="Animation_tc2ku"]
592592
length = 0.001
593593
tracks/0/type = "value"
@@ -626,7 +626,7 @@ _data = {
626626
collision_mask = 7
627627
script = ExtResource("2_bhjnv")
628628
chestModifierSpeed = 0.7
629-
maxHealth = 100
629+
life = 100
630630
weapon = ExtResource("3_irbyu")
631631

632632
[node name="MultiplayerSynchronizer" type="MultiplayerSynchronizer" parent="." index="0"]
@@ -656,17 +656,6 @@ libraries = {
656656
"": SubResource("AnimationLibrary_mt86f")
657657
}
658658

659-
[node name="HealthBar" type="ProgressBar" parent="." index="6"]
660-
modulate = Color(0.227451, 0.768627, 0.156863, 1)
661-
z_index = 10
662-
offset_left = -68.0
663-
offset_top = -60.0
664-
offset_right = 206.0
665-
offset_bottom = -33.0
666-
scale = Vector2(0.5, 0.5)
667-
mouse_filter = 2
668-
step = 1.0
669-
show_percentage = false
670-
script = ExtResource("7_ot68h")
659+
[node name="HealthBar" parent="." index="6" instance=ExtResource("7_svy1a")]
671660

672661
[connection signal="on_collecting" from="Collector" to="." method="_on_collecting"]

objects/actors/player/HealthBar.gd objects/hud/HealthBar.gd

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
class_name HealthBar
12
extends ProgressBar
23

34
var RED_COLOR = "e7001f"
@@ -11,4 +12,8 @@ func _process(delta):
1112
modulate = ORANGE_COLOR
1213
else:
1314
modulate = GREEN_COLOR
15+
16+
func init(maxHealth: int) -> void:
17+
max_value = maxHealth
18+
value = maxHealth
1419

objects/hud/HealthBar.tscn

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[gd_scene load_steps=2 format=3 uid="uid://c5w6dv2r65qsd"]
2+
3+
[ext_resource type="Script" path="res://objects/hud/HealthBar.gd" id="1_kbje0"]
4+
5+
[node name="HealthBar" type="ProgressBar"]
6+
modulate = Color(0.227451, 0.768627, 0.156863, 1)
7+
z_index = 10
8+
offset_left = -68.0
9+
offset_top = -60.0
10+
offset_right = 206.0
11+
offset_bottom = -33.0
12+
scale = Vector2(0.5, 0.5)
13+
mouse_filter = 2
14+
step = 1.0
15+
show_percentage = false
16+
script = ExtResource("1_kbje0")

0 commit comments

Comments
 (0)