basic hp for spawnables

This commit is contained in:
Victor Turgeon 2023-10-10 19:38:24 -04:00
parent 7dbd9f75f1
commit 448dad5331
5 changed files with 92 additions and 9 deletions

View File

@ -15,8 +15,12 @@ position = Vector2(497, 324)
[node name="BatSpawnable" parent="." instance=ExtResource("3_a1ucf")]
position = Vector2(248, 225)
collision_layer = 6
collision_mask = 6
motion_mode = 0
[node name="BatSpawnable2" parent="." instance=ExtResource("3_a1ucf")]
position = Vector2(644, 251)
collision_layer = 6
collision_mask = 6
motion_mode = 0

View File

@ -0,0 +1,10 @@
extends Control
@onready var healthBar : ProgressBar = $HealthBar
@onready var hpLabel : Label = $HealthBar/HP
func setHP(currentHp, maxHp):
healthBar.min_value = 0
healthBar.max_value = maxHp
healthBar.value = currentHp
hpLabel.text = str(currentHp) + "/" + str(maxHp)

View File

@ -0,0 +1,42 @@
[gd_scene load_steps=2 format=3 uid="uid://fojbwshce7ah"]
[ext_resource type="Script" path="res://Scenes/Spawnables/UI/SpawnableUI.gd" id="1_en1j6"]
[node name="SpawnableUI" type="Control"]
layout_mode = 3
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
script = ExtResource("1_en1j6")
[node name="HealthBar" type="ProgressBar" parent="."]
layout_mode = 1
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
min_value = 100.0
step = 1.0
value = 100.0
show_percentage = false
[node name="HP" type="Label" parent="HealthBar"]
layout_mode = 1
anchors_preset = 8
anchor_left = 0.5
anchor_top = 0.5
anchor_right = 0.5
anchor_bottom = 0.5
offset_left = -15.5
offset_top = -7.5
offset_right = 15.5
offset_bottom = 7.5
grow_horizontal = 2
grow_vertical = 2
theme_override_font_sizes/font_size = 8
text = "100/100"
horizontal_alignment = 1
vertical_alignment = 1

View File

@ -18,6 +18,8 @@ const ATTACK_COOLDOWN : float = 100
@onready var animatedSprite : AnimatedSprite2D = $AnimatedSprite2D
@onready var attackArea : Area2D = $AttackArea
@onready var damageCollision : CollisionShape2D = $DamageArea/DamageCollision
@onready var bodyCollision : CollisionShape2D = $BodyCollision
@onready var spawnableUI : Control = $UIContainer/SpawnableUI
var isAlive : bool = true
var isFacingLeft : bool = false
@ -30,6 +32,8 @@ var cooldown : float = 0
func _ready():
enemy = get_node("../Enemy")
currentHp = maxHp
spawnableUI.setHP(currentHp, maxHp)
spawnableUI.visible = true
animatedSprite.play("default")
func _process(delta):
@ -43,12 +47,14 @@ func _process(delta):
if !isFacingLeft && direction.x < 0:
isFacingLeft = true
scale.x = -1
scale.y = 1
attackArea.scale = Vector2(-1,1)
damageCollision.scale = Vector2(-1,1)
animatedSprite.flip_h = isFacingLeft
elif isFacingLeft && direction.x > 0:
isFacingLeft = false
scale.x = -1
scale.y = -1
attackArea.scale = Vector2(1,1)
damageCollision.scale = Vector2(1,1)
animatedSprite.flip_h = isFacingLeft
velocity = direction * speed * delta
move_and_slide()
@ -83,13 +89,16 @@ func receive_damage(dmg):
if currentHp - dmg <= 0:
currentHp = 0
die()
call_deferred("die")
else :
currentHp -= dmg
spawnableUI.setHP(currentHp, maxHp)
func die():
isAlive = false
bodyCollision.disabled = true
damageCollision.disabled = true
spawnableUI.visible = false
isAlive = false
animatedSprite.stop()
animatedSprite.play("death")
animatedSprite.disconnect("animation_finished", endAttack)

View File

@ -1,9 +1,10 @@
[gd_scene load_steps=37 format=3 uid="uid://kkfxguj0lr5a"]
[gd_scene load_steps=38 format=3 uid="uid://kkfxguj0lr5a"]
[ext_resource type="Script" path="res://Scenes/Spawnables/template_spawnable.gd" id="1_rkej7"]
[ext_resource type="Texture2D" uid="uid://f60ndepwmpj2" path="res://Assets/Bat/noBKG_BatAttack_strip.png" id="2_7ayi7"]
[ext_resource type="Texture2D" uid="uid://bd74k1s6qdx2r" path="res://Assets/Bat/noBKG_BatDeath_strip.png" id="3_jipj7"]
[ext_resource type="Texture2D" uid="uid://ddvr5ltbo8ome" path="res://Assets/Bat/noBKG_BatFlight_strip.png" id="4_u36ok"]
[ext_resource type="PackedScene" uid="uid://fojbwshce7ah" path="res://Scenes/Spawnables/UI/SpawnableUI.tscn" id="5_c2blu"]
[sub_resource type="AtlasTexture" id="AtlasTexture_y0lcs"]
atlas = ExtResource("2_7ayi7")
@ -231,11 +232,26 @@ height = 24.0
radius = 8.0
[node name="TemplateSpawnable" type="CharacterBody2D"]
collision_layer = 6
collision_mask = 6
collision_layer = 4
collision_mask = 4
motion_mode = 1
script = ExtResource("1_rkej7")
[node name="UIContainer" type="Control" parent="."]
layout_mode = 3
anchors_preset = 5
anchor_left = 0.5
anchor_right = 0.5
offset_left = -25.0
offset_top = 8.0
offset_right = 25.0
offset_bottom = 16.0
grow_horizontal = 2
[node name="SpawnableUI" parent="UIContainer" instance=ExtResource("5_c2blu")]
visible = false
layout_mode = 1
[node name="AnimatedSprite2D" type="AnimatedSprite2D" parent="."]
sprite_frames = SubResource("SpriteFrames_k6v1d")
animation = &"attack"
@ -265,4 +281,6 @@ debug_color = Color(0.639216, 0, 0.156863, 0.419608)
[node name="AI" type="Node" parent="."]
[node name="Node2D" type="Node2D" parent="."]
[connection signal="body_entered" from="DamageArea" to="." method="_on_damage_area_body_entered"]