Skip to content

Commit f1432e7

Browse files
Slocalyolivierperez
authored andcommitted
feat(player): add player healthbar
1 parent 59735be commit f1432e7

File tree

4 files changed

+38
-5
lines changed

4 files changed

+38
-5
lines changed

objects/actors/mob/Mob.gd

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
class_name Mob
22
extends Actor
33

4-
54
var targeted_players: Array[Player]

objects/actors/player/Player.gd

+17-4
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,20 @@ class_name Player
22
extends Actor
33

44
@export var chestModifierSpeed: float = 0.7
5+
@export var maxHealth = 100
6+
var lifePoint = maxHealth
57

68
@onready var attackTimer = $AttackTimer
9+
@onready var healthbar = $HealthBar
710

811
@export var weapon: Weapon
912
var hasChest = false
1013

1114
func _ready() -> void:
1215
if weapon == null:
1316
weapon = StoreManager.player_weapon
17+
healthbar.max_value = maxHealth
18+
healthbar.value = maxHealth
1419
attackTimer.wait_time = weapon.attack_speed
1520

1621
func _input(event):
@@ -22,7 +27,15 @@ func _input(event):
2227
attack()
2328

2429
func apply_attack(force: int) -> void:
25-
prints("Attacked with force", force)
30+
if lifePoint == 0:
31+
return
32+
33+
lifePoint -= force
34+
$AnimationPlayer.play('Hit')
35+
healthbar.value = lifePoint
36+
37+
if (lifePoint == 0):
38+
$AnimationPlayer.play("Death")
2639

2740
func get_speed():
2841
if hasChest: return _speed * chestModifierSpeed
@@ -38,7 +51,7 @@ func attack():
3851
var direction = global_position.direction_to(mouseCoords)
3952

4053
var attack_scene = weapon.attackTo(direction)
41-
54+
4255
if weapon.attack_type == Weapon.ATTACK_TYPES.projectile:
4356
attack_scene.global_position = global_position
4457
get_tree().current_scene.add_child(attack_scene)
@@ -54,6 +67,6 @@ func _on_collecting(element):
5467
if hasChest:
5568
element.can_enter = true
5669
$AnimationPlayer.play("fade_away")
57-
70+
5871
if element.can_enter: $AnimationPlayer.play("fade_away")
59-
72+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
shader_type canvas_item;
2+
3+
uniform vec4 flash_color : source_color;
4+
uniform float flash_value : hint_range(0.0, 1.0, 0.1);
5+
6+
void fragment() {
7+
vec4 texture_color = texture(TEXTURE, UV);
8+
COLOR = mix(texture_color, flash_color, flash_value);
9+
COLOR.a = texture_color.a;
10+
}

objects/actors/player/health_bar.gd

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
extends ProgressBar
2+
3+
# Called every frame. 'delta' is the elapsed time since the previous frame.
4+
func _process(delta):
5+
if value <= max_value / 10:
6+
modulate = 'e7001f'
7+
elif value <= max_value / 3:
8+
modulate = 'edae14'
9+
else:
10+
modulate = '3ac428'
11+

0 commit comments

Comments
 (0)