fuzzyworld

godot-shooter / commit

Replaced enemy and player health label with color progress bars.

8adb41dc7efaa9754c66cc03350482281e3a9f49 · Pavel Safronov · 2023-11-19 00:01:39 -0800 · parents 5320761c6a20

Replaced enemy and player health label with color progress bars.
 actors/Enemy.gd     | 22 +++++++++++++++++-----
 actors/Enemy.tscn   | 52 ++++++++++++++++++++++++++++++++++------------------
 actors/Player.gd    | 14 +++++++++++++-
 actors/Player.tscn  | 20 +++++++++++++++++++-
 effects/Trails.tscn |  2 +-
 5 files changed, 84 insertions(+), 26 deletions(-)

diff --git a/actors/Enemy.gd b/actors/Enemy.gd
index 404f10c..c0794c5 100644
--- a/actors/Enemy.gd
+++ b/actors/Enemy.gd
@@ -18,6 +18,8 @@ signal enemy_killed(position)
 
 var velocity = Vector2.ZERO
 var dead = false
+var health_ratio
+var health_percentage
 
 func _ready() -> void:
 	$AnimationPlayer.play("fade_in")
@@ -30,11 +32,20 @@ func _ready() -> void:
 			hurt()
 
 func _process(_delta: float) -> void:
-	$Graphics/Alive/Container/HealthLabel.text = "%s / %s" % [health, max_health]
+	var pb: ProgressBar = $Graphics/Alive/Container/ProgressBar
+	health_ratio = float(health) / float(max_health)
+	health_percentage = health_ratio * 100
+	pb.value = health_percentage
+	if health_percentage < 20:
+		pb.modulate = Color.red
+	elif health_percentage < 60:
+		pb.modulate = Color.yellow
+	else:
+		pb.modulate = Color.blue
 	$Graphics/Alive/Container.global_rotation = 0
-	$Graphics/Alive/Target.visible = is_targeted	
-	$Graphics/Alive/Target.global_rotation = 0
-	$Graphics/Alive/Trails.add_position(global_position)
+	$Graphics/Alive/Sprite/Target.visible = is_targeted	
+	$Graphics/Alive/Sprite/Target.global_rotation = 0
+	$Graphics/Alive/Sprite/Trails.add_position(global_position)
 
 func _physics_process(_delta: float) -> void:
 	if dead:
@@ -69,7 +80,8 @@ func hurt(damage: int = 1):
 	var smoke = Smoke.instance()
 	var smoke_pos = get_damage_point_on_body()
 	smoke.position = smoke_pos
-	add_child(smoke)
+	$DamageObjects.add_child(smoke)
+#	add_child(smoke)
 	if health <= 0:
 		kill()
 
diff --git a/actors/Enemy.tscn b/actors/Enemy.tscn
index 05b8f7a..c9d1778 100644
--- a/actors/Enemy.tscn
+++ b/actors/Enemy.tscn
@@ -1,6 +1,5 @@
-[gd_scene load_steps=10 format=2]
+[gd_scene load_steps=11 format=2]
 
-[ext_resource path="res://themes/CharacterTheme.tres" type="Theme" id=1]
 [ext_resource path="res://effects/Trails.tscn" type="PackedScene" id=2]
 [ext_resource path="res://actors/Enemy.gd" type="Script" id=3]
 [ext_resource path="res://assets/kenney_simple-space/PNG/Default/enemy_A.png" type="Texture" id=4]
@@ -9,6 +8,12 @@
 [sub_resource type="Gradient" id=4]
 colors = PoolColorArray( 0, 0, 0, 0, 1, 0, 0, 1 )
 
+[sub_resource type="StyleBoxFlat" id=5]
+bg_color = Color( 1, 1, 1, 1 )
+
+[sub_resource type="StyleBoxFlat" id=6]
+bg_color = Color( 0.211765, 0.211765, 0.211765, 1 )
+
 [sub_resource type="Animation" id=1]
 length = 3.0
 
@@ -70,7 +75,7 @@ tracks/1/keys = {
 } ]
 }
 tracks/2/type = "value"
-tracks/2/path = NodePath("Graphics/Alive/Trails:alpha")
+tracks/2/path = NodePath("Graphics/Alive/Sprite/Trails:alpha")
 tracks/2/interp = 1
 tracks/2/loop_wrap = true
 tracks/2/imported = false
@@ -83,7 +88,6 @@ tracks/2/keys = {
 }
 
 [node name="Enemy" type="KinematicBody2D"]
-modulate = Color( 1, 1, 1, 0 )
 z_index = 10
 collision_layer = 2
 collision_mask = 9
@@ -91,33 +95,41 @@ script = ExtResource( 3 )
 
 [node name="Graphics" type="Node2D" parent="."]
 
-[node name="Alive" type="Sprite" parent="Graphics"]
-modulate = Color( 1, 0.537255, 0, 1 )
-texture = ExtResource( 4 )
-
-[node name="Container" type="Node2D" parent="Graphics/Alive"]
+[node name="Alive" type="Node2D" parent="Graphics"]
 
-[node name="HealthLabel" type="Label" parent="Graphics/Alive/Container"]
-margin_left = -48.0
-margin_top = 37.0
-margin_right = 49.0
-margin_bottom = 72.0
-theme = ExtResource( 1 )
-text = "HEALTH"
+[node name="Sprite" type="Sprite" parent="Graphics/Alive"]
+modulate = Color( 1, 0.470588, 0, 1 )
+texture = ExtResource( 4 )
 
-[node name="Target" type="Sprite" parent="Graphics/Alive"]
+[node name="Target" type="Sprite" parent="Graphics/Alive/Sprite"]
+visible = false
 modulate = Color( 1, 0, 0, 1 )
 texture = ExtResource( 5 )
 
-[node name="Trails" parent="Graphics/Alive" instance=ExtResource( 2 )]
+[node name="Trails" parent="Graphics/Alive/Sprite" instance=ExtResource( 2 )]
+visible = false
 gradient = SubResource( 4 )
 
+[node name="Container" type="Node2D" parent="Graphics/Alive"]
+
+[node name="ProgressBar" type="ProgressBar" parent="Graphics/Alive/Container"]
+margin_left = -50.0
+margin_top = 45.0
+margin_right = 50.0
+margin_bottom = 55.0
+rect_min_size = Vector2( 100, 10 )
+custom_styles/fg = SubResource( 5 )
+custom_styles/bg = SubResource( 6 )
+value = 30.0
+percent_visible = false
+
 [node name="AnimationPlayer" type="AnimationPlayer" parent="."]
 anims/RESET = SubResource( 1 )
 anims/fade_in = SubResource( 3 )
 anims/fade_out = SubResource( 2 )
 
 [node name="DamagePoints" type="Node2D" parent="."]
+visible = false
 
 [node name="Position2D" type="Position2D" parent="DamagePoints"]
 position = Vector2( -16, 16 )
@@ -129,8 +141,12 @@ position = Vector2( 0, -13 )
 position = Vector2( 16, 16 )
 
 [node name="RayCast2D" type="RayCast2D" parent="."]
+visible = false
 enabled = true
 cast_to = Vector2( 0, -50 )
 
 [node name="CollisionPolygon2D" type="CollisionPolygon2D" parent="."]
+visible = false
 polygon = PoolVector2Array( 0, -23, -7, 5, -12, 9, -19, 9, -23, 14, -23, 19, -19, 23, -14, 23, -9, 19, -9, 14, -10, 12, -6, 9, 0, 15, 6, 9, 10, 12, 9, 14, 9, 19, 13, 23, 18, 23, 23, 18, 23, 14, 19, 10, 13, 10, 7, 5 )
+
+[node name="DamageObjects" type="Node2D" parent="."]
diff --git a/actors/Player.gd b/actors/Player.gd
index 9762e21..6bfe9a4 100644
--- a/actors/Player.gd
+++ b/actors/Player.gd
@@ -60,7 +60,19 @@ func _ready() -> void:
 func _process(_delta: float) -> void:
 	if dead:
 		return
-	health_label.text = "%s / %s" % [health, max_health]
+
+#	health_label.text = "%s / %s" % [health, max_health]
+	var pb: ProgressBar = $Graphics/Alive/StaticControls/ProgressBar
+	var health_ratio = float(health) / float(max_health)
+	var health_percentage = health_ratio * 100
+	pb.value = health_percentage
+	if health_percentage < 20:
+		pb.modulate = Color.red
+	elif health_percentage < 60:
+		pb.modulate = Color.yellow
+	else:
+		pb.modulate = Color.blue
+		
 	static_controls.global_rotation = 0
 	trails.add_position(global_position)
 	hud.set_current_weapon(weapon)
diff --git a/actors/Player.tscn b/actors/Player.tscn
index f296983..82df0f3 100644
--- a/actors/Player.tscn
+++ b/actors/Player.tscn
@@ -1,4 +1,4 @@
-[gd_scene load_steps=16 format=2]
+[gd_scene load_steps=18 format=2]
 
 [ext_resource path="res://assets/kenney_simple-space/PNG/Default/ship_sidesA.png" type="Texture" id=1]
 [ext_resource path="res://effects/Explosion.tscn" type="PackedScene" id=2]
@@ -13,6 +13,12 @@
 [sub_resource type="Gradient" id=2]
 colors = PoolColorArray( 1, 0, 0, 1, 1, 0, 0, 0 )
 
+[sub_resource type="StyleBoxFlat" id=13]
+bg_color = Color( 1, 1, 1, 1 )
+
+[sub_resource type="StyleBoxFlat" id=14]
+bg_color = Color( 0.211765, 0.211765, 0.211765, 1 )
+
 [sub_resource type="Gradient" id=1]
 colors = PoolColorArray( 0, 0, 0, 0, 0, 1, 0, 1 )
 
@@ -62,6 +68,7 @@ texture = ExtResource( 1 )
 [node name="StaticControls" type="Node2D" parent="Graphics/Alive"]
 
 [node name="HealthLabel" type="Label" parent="Graphics/Alive/StaticControls"]
+visible = false
 margin_left = -48.0051
 margin_top = 30.992
 margin_right = 48.9949
@@ -78,6 +85,17 @@ offset = Vector2( 0, -300 )
 
 [node name="Radar" type="Node2D" parent="Graphics/Alive/StaticControls"]
 
+[node name="ProgressBar" type="ProgressBar" parent="Graphics/Alive/StaticControls"]
+margin_left = -50.0
+margin_top = 45.0
+margin_right = 50.0
+margin_bottom = 55.0
+rect_min_size = Vector2( 100, 10 )
+custom_styles/fg = SubResource( 13 )
+custom_styles/bg = SubResource( 14 )
+value = 30.0
+percent_visible = false
+
 [node name="Trails" parent="Graphics/Alive" instance=ExtResource( 6 )]
 modulate = Color( 1, 0, 0, 1 )
 gradient = SubResource( 1 )
diff --git a/effects/Trails.tscn b/effects/Trails.tscn
index 2513790..d8a4613 100644
--- a/effects/Trails.tscn
+++ b/effects/Trails.tscn
@@ -15,7 +15,7 @@ follow_viewport_enable = true
 [node name="Trails" type="Line2D" parent="CanvasLayer"]
 modulate = Color( 1, 1, 1, 0.670588 )
 rotation = -0.000165944
-points = PoolVector2Array( 13.8699, 784.002, 261.935, 390.043, 480.997, 20.0798 )
+points = PoolVector2Array( 0, 0, 10, 10, 20, 20 )
 width_curve = SubResource( 1 )
 default_color = Color( 0.4, 0.501961, 1, 1 )
 joint_mode = 2