Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(player): implement player's health and healthbar #41

Merged
merged 6 commits into from
Mar 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion objects/actors/mob/Mob.gd
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
class_name Mob
extends Actor


var targeted_players: Array[Player]
23 changes: 17 additions & 6 deletions objects/actors/player/Player.gd
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@ class_name Player
extends Actor

@export var chestModifierSpeed: float = 0.7
@export var life = 100

@onready var attackTimer = $AttackTimer
@onready var healthbar: HealthBar = $HealthBar
@onready var animation_player: AnimationPlayer = $AnimationPlayer

@export var weapon: Weapon
var hasChest = false

func _ready() -> void:
if StoreManager.player_weapon != null:
weapon = StoreManager.player_weapon
healthbar.init(life)
attackTimer.wait_time = weapon.attack_speed

func _input(event):
Expand All @@ -22,7 +26,15 @@ func _input(event):
attack()

func apply_attack(force: int) -> void:
prints("Attacked with force", force)
if life <= 0:
return

life -= force
animation_player.play("Hit")
healthbar.value = life

if (life <= 0):
animation_player.play("Death")

func get_speed():
if hasChest: return _speed * chestModifierSpeed
Expand All @@ -38,7 +50,7 @@ func attack():
var direction = global_position.direction_to(mouseCoords)

var attack_scene = weapon.attackTo(direction)

if weapon.attack_type == Weapon.ATTACK_TYPES.projectile:
attack_scene.global_position = global_position
get_tree().current_scene.add_child(attack_scene)
Expand All @@ -53,7 +65,6 @@ func _on_collecting(element):
if element is Boat:
if hasChest:
element.can_enter = true
$AnimationPlayer.play("fade_away")

if element.can_enter: $AnimationPlayer.play("fade_away")

animation_player.play("FadeAway")

if element.can_enter: animation_player.play("FadeAway")
86 changes: 76 additions & 10 deletions objects/actors/player/Player.tscn
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
[gd_scene load_steps=77 format=3 uid="uid://bsfyypyhwfbs5"]
[gd_scene load_steps=80 format=3 uid="uid://bsfyypyhwfbs5"]

[ext_resource type="PackedScene" uid="uid://du0c1yciw8ul6" path="res://objects/actors/Actor.tscn" id="1_wrkhm"]
[ext_resource type="Script" path="res://objects/actors/player/Player.gd" id="2_bhjnv"]
[ext_resource type="Resource" uid="uid://r3i80a6v2w0y" path="res://resources/weapons/gun/Gun.tres" id="3_irbyu"]
[ext_resource type="Texture2D" uid="uid://dw5vuo3agm77m" path="res://objects/actors/player/template.png" id="3_q5uec"]
[ext_resource type="Resource" uid="uid://ca4y5d7kksi0h" path="res://resources/weapons/sword/Sword.tres" id="3_uqweb"]
[ext_resource type="PackedScene" uid="uid://cxlkjx4h6gv5h" path="res://components/collector/Collector.tscn" id="5_q1uwo"]
[ext_resource type="PackedScene" uid="uid://c5w6dv2r65qsd" path="res://objects/hud/HealthBar.tscn" id="7_svy1a"]

[sub_resource type="SceneReplicationConfig" id="SceneReplicationConfig_p4sxr"]
properties/0/path = NodePath(".:position")
Expand Down Expand Up @@ -508,19 +509,33 @@ radius = 32.0
[sub_resource type="CircleShape2D" id="CircleShape2D_xu8dn"]
radius = 80.0

[sub_resource type="Animation" id="Animation_tc2ku"]
length = 0.001
[sub_resource type="Animation" id="Animation_0v3ge"]
resource_name = "Death"
tracks/0/type = "value"
tracks/0/imported = false
tracks/0/enabled = true
tracks/0/path = NodePath(".:modulate")
tracks/0/interp = 1
tracks/0/loop_wrap = true
tracks/0/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"times": PackedFloat32Array(0, 0.75),
"transitions": PackedFloat32Array(1, 1),
"update": 0,
"values": [Color(1, 1, 1, 1)]
"values": [Color(1, 1, 1, 1), Color(1, 1, 1, 0)]
}
tracks/1/type = "method"
tracks/1/imported = false
tracks/1/enabled = true
tracks/1/path = NodePath(".")
tracks/1/interp = 1
tracks/1/loop_wrap = true
tracks/1/keys = {
"times": PackedFloat32Array(0.75),
"transitions": PackedFloat32Array(1),
"values": [{
"args": [],
"method": &"queue_free"
}]
}

[sub_resource type="Animation" id="Animation_l1t87"]
Expand Down Expand Up @@ -552,17 +567,62 @@ tracks/1/keys = {
}]
}

[sub_resource type="Animation" id="Animation_fx0oj"]
resource_name = "Hit"
length = 0.4
tracks/0/type = "value"
tracks/0/imported = false
tracks/0/enabled = true
tracks/0/path = NodePath("AnimatedSprite:modulate")
tracks/0/interp = 1
tracks/0/loop_wrap = true
tracks/0/keys = {
"times": PackedFloat32Array(0, 0.2, 0.4),
"transitions": PackedFloat32Array(1, 1, 1),
"update": 0,
"values": [Color(1, 1, 1, 1), Color(0.912963, 0.144798, 0.20362, 1), Color(1, 1, 1, 1)]
}

[sub_resource type="Animation" id="Animation_tc2ku"]
length = 0.001
tracks/0/type = "value"
tracks/0/imported = false
tracks/0/enabled = true
tracks/0/path = NodePath(".:modulate")
tracks/0/interp = 1
tracks/0/loop_wrap = true
tracks/0/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 0,
"values": [Color(1, 1, 1, 1)]
}
tracks/1/type = "bezier"
tracks/1/imported = false
tracks/1/enabled = true
tracks/1/path = NodePath(".:material:shader_parameter/flash_value")
tracks/1/interp = 1
tracks/1/loop_wrap = true
tracks/1/keys = {
"handle_modes": PackedInt32Array(0),
"points": PackedFloat32Array(0, -0.25, 0, 0.25, 0),
"times": PackedFloat32Array(0)
}

[sub_resource type="AnimationLibrary" id="AnimationLibrary_mt86f"]
_data = {
"RESET": SubResource("Animation_tc2ku"),
"fade_away": SubResource("Animation_l1t87")
"Death": SubResource("Animation_0v3ge"),
"FadeAway": SubResource("Animation_l1t87"),
"Hit": SubResource("Animation_fx0oj"),
"RESET": SubResource("Animation_tc2ku")
}

[node name="Player" instance=ExtResource("1_wrkhm")]
collision_mask = 7
script = ExtResource("2_bhjnv")
chestModifierSpeed = 0.7
weapon = ExtResource("3_uqweb")
life = 100
weapon = ExtResource("3_irbyu")

[node name="MultiplayerSynchronizer" type="MultiplayerSynchronizer" parent="." index="0"]
replication_config = SubResource("SceneReplicationConfig_p4sxr")
Expand Down Expand Up @@ -590,4 +650,10 @@ libraries = {
"": SubResource("AnimationLibrary_mt86f")
}

[node name="HealthBar" parent="." index="6" instance=ExtResource("7_svy1a")]
offset_left = -27.0
offset_top = -34.0
offset_right = 84.0
offset_bottom = -23.0

[connection signal="on_collecting" from="Collector" to="." method="_on_collecting"]
19 changes: 19 additions & 0 deletions objects/hud/HealthBar.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class_name HealthBar
extends ProgressBar

var RED_COLOR = "e7001f"
var ORANGE_COLOR = "edae14"
var GREEN_COLOR = "3ac428"

func _process(delta):
if value <= max_value / 10:
modulate = RED_COLOR
elif value <= max_value / 3:
modulate = ORANGE_COLOR
else:
modulate = GREEN_COLOR

func init(maxHealth: int) -> void:
max_value = maxHealth
value = maxHealth

16 changes: 16 additions & 0 deletions objects/hud/HealthBar.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[gd_scene load_steps=2 format=3 uid="uid://c5w6dv2r65qsd"]

[ext_resource type="Script" path="res://objects/hud/HealthBar.gd" id="1_kbje0"]

[node name="HealthBar" type="ProgressBar"]
modulate = Color(0.227451, 0.768627, 0.156863, 1)
z_index = 10
offset_left = -68.0
offset_top = -60.0
offset_right = 206.0
offset_bottom = -33.0
scale = Vector2(0.5, 0.5)
mouse_filter = 2
step = 1.0
show_percentage = false
script = ExtResource("1_kbje0")
2 changes: 1 addition & 1 deletion project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ config/icon="res://icon.svg"
ProgressionService="*res://services/save/ProgressionService.gd"
UiAudio="*res://components/uicontrols/UIAudio.gd"
SceneTransition="*res://services/transition/SceneTransition.tscn"
MultiplayerManager="*res://services/multiplayer/MultiplayerManager.gd"
StoreManager="*res://services/managers/store/StoreManager.gd"
MultiplayerManager="*res://services/multiplayer/MultiplayerManager.gd"

[debug]

Expand Down
2 changes: 2 additions & 0 deletions scenes/levels/debug/DebugLevel.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ script = ExtResource("1_ggoyj")

[node name="Player" parent="." instance=ExtResource("1_t7e46")]
weapon = ExtResource("2_sywcb")
chestModifierSpeed = 0.7
maxHealth = 100

[node name="Camera2D" type="Camera2D" parent="Player"]
position_smoothing_enabled = true
Expand Down