From 6727be5cd0b2128ae51b678f38c26b635efbb556 Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Tue, 13 Feb 2024 00:22:27 +0100 Subject: [PATCH] Added health bars --- client/camera/Camera.tscn | 2 - client/character/Attack.gd | 3 -- client/character/Character.gd | 2 - client/character/health/HealthComponent.gd | 15 ------- client/enemy/Enemy.gd | 2 +- client/enemy/slime/Slime.tscn | 11 +++-- client/player/Player.gd | 10 ++--- client/player/Player.tscn | 20 ++++++--- .../animation/AnimationComponent.gd | 0 .../animation/AnimationComponent.tscn | 2 +- client/player/health/DamageInstance.gd | 7 +++ client/player/health/HealthComponent.gd | 18 ++++++++ .../health/HealthComponent.tscn | 2 +- client/player/hud/HUDComponent.gd | 14 ++++++ client/player/hud/HUDComponent.tscn | 44 +++++++++++++++++++ client/player/hud/HealthBar.gd | 21 +++++++++ .../movement/MovementComponent.gd | 0 .../movement/MovementComponent.tscn | 2 +- .../rotation/RotationComponent.gd | 0 .../rotation/RotationComponent.tscn | 2 +- client/player/skills/SkillsComponent.gd | 11 +++++ client/player/skills/SkillsComponent.tscn | 6 +++ client/project.godot | 1 + 23 files changed, 154 insertions(+), 41 deletions(-) delete mode 100644 client/character/Attack.gd delete mode 100644 client/character/Character.gd delete mode 100644 client/character/health/HealthComponent.gd rename client/{character => player}/animation/AnimationComponent.gd (100%) rename client/{character => player}/animation/AnimationComponent.tscn (70%) create mode 100644 client/player/health/DamageInstance.gd create mode 100644 client/player/health/HealthComponent.gd rename client/{character => player}/health/HealthComponent.tscn (57%) create mode 100644 client/player/hud/HUDComponent.gd create mode 100644 client/player/hud/HUDComponent.tscn create mode 100644 client/player/hud/HealthBar.gd rename client/{character => player}/movement/MovementComponent.gd (100%) rename client/{character => player}/movement/MovementComponent.tscn (57%) rename client/{character => player}/rotation/RotationComponent.gd (100%) rename client/{character => player}/rotation/RotationComponent.tscn (57%) create mode 100644 client/player/skills/SkillsComponent.gd create mode 100644 client/player/skills/SkillsComponent.tscn diff --git a/client/camera/Camera.tscn b/client/camera/Camera.tscn index f8a1f15..d681324 100644 --- a/client/camera/Camera.tscn +++ b/client/camera/Camera.tscn @@ -8,8 +8,6 @@ [node name="Follow" type="Marker3D"] script = ExtResource("1_48rtd") -interpolate = true -speed = 10.0 [node name="Pivot" type="Marker3D" parent="."] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.4, 0) diff --git a/client/character/Attack.gd b/client/character/Attack.gd deleted file mode 100644 index ff9f973..0000000 --- a/client/character/Attack.gd +++ /dev/null @@ -1,3 +0,0 @@ -class_name Attack - -var damage: float diff --git a/client/character/Character.gd b/client/character/Character.gd deleted file mode 100644 index e836503..0000000 --- a/client/character/Character.gd +++ /dev/null @@ -1,2 +0,0 @@ -class_name Character -extends CharacterBody3D \ No newline at end of file diff --git a/client/character/health/HealthComponent.gd b/client/character/health/HealthComponent.gd deleted file mode 100644 index 8c5c529..0000000 --- a/client/character/health/HealthComponent.gd +++ /dev/null @@ -1,15 +0,0 @@ -class_name HealthComponent -extends Node - -@export var max_health: float - -var health: float - -func _ready(): - health = max_health - -func take_damage(attack: Attack): - health -= attack.damage - - if health <= 0: - get_parent().queue_free() diff --git a/client/enemy/Enemy.gd b/client/enemy/Enemy.gd index 2f7fa61..5aacd3d 100644 --- a/client/enemy/Enemy.gd +++ b/client/enemy/Enemy.gd @@ -1,2 +1,2 @@ class_name Enemy -extends Character +extends CharacterBody3D diff --git a/client/enemy/slime/Slime.tscn b/client/enemy/slime/Slime.tscn index be48b71..4258fef 100644 --- a/client/enemy/slime/Slime.tscn +++ b/client/enemy/slime/Slime.tscn @@ -1,9 +1,10 @@ -[gd_scene load_steps=9 format=3 uid="uid://cb2t7bvvf3gwh"] +[gd_scene load_steps=10 format=3 uid="uid://cb2t7bvvf3gwh"] [ext_resource type="PackedScene" uid="uid://b358op5h1y83m" path="res://assets/slime/Slime.blend" id="1_1h1hj"] [ext_resource type="Script" path="res://enemy/Enemy.gd" id="1_r5888"] -[ext_resource type="PackedScene" uid="uid://2bbycjulf00g" path="res://character/health/HealthComponent.tscn" id="2_fsqxc"] -[ext_resource type="PackedScene" uid="uid://x102pryt2s5a" path="res://character/movement/MovementComponent.tscn" id="3_2phqx"] +[ext_resource type="PackedScene" uid="uid://2bbycjulf00g" path="res://player/health/HealthComponent.tscn" id="2_fsqxc"] +[ext_resource type="PackedScene" uid="uid://x102pryt2s5a" path="res://player/movement/MovementComponent.tscn" id="3_2phqx"] +[ext_resource type="PackedScene" uid="uid://6jpnl6c4fdvo" path="res://player/hud/HUDComponent.tscn" id="3_4jtio"] [sub_resource type="CylinderShape3D" id="CylinderShape3D_ub3kk"] height = 0.5 @@ -48,6 +49,10 @@ script = ExtResource("1_r5888") transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0.0452515, 0.3, 0) shape = SubResource("CylinderShape3D_ub3kk") +[node name="HUD" parent="." node_paths=PackedStringArray("health") instance=ExtResource("3_4jtio")] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.986405, 0) +health = NodePath("../Health") + [node name="Animation" type="AnimationPlayer" parent="."] root_node = NodePath("../Model") libraries = { diff --git a/client/player/Player.gd b/client/player/Player.gd index 906609c..92a5b95 100644 --- a/client/player/Player.gd +++ b/client/player/Player.gd @@ -4,8 +4,10 @@ extends CharacterBody3D signal attacked signal dashed signal jumped +signal name_changed(new_name: String) signal direction_changed +var id: String var controller: Controller func attack(): @@ -20,10 +22,6 @@ func jump(): func set_direction(direction: Vector3): direction_changed.emit(direction) -# TODO: Remove this: - -var id: String - -func set_character_name(new_name: String): +func set_player_name(new_name: String): name = new_name - $Label.text = name + name_changed.emit(new_name) diff --git a/client/player/Player.tscn b/client/player/Player.tscn index 3ba9e62..70df7ba 100644 --- a/client/player/Player.tscn +++ b/client/player/Player.tscn @@ -1,16 +1,18 @@ -[gd_scene load_steps=13 format=3 uid="uid://2lcnu3dy54lx"] +[gd_scene load_steps=15 format=3 uid="uid://2lcnu3dy54lx"] [ext_resource type="Script" path="res://player/Player.gd" id="1_8gebs"] [ext_resource type="PackedScene" uid="uid://c8j7t4yg7anb0" path="res://assets/female/Female.blend" id="2_8nah6"] -[ext_resource type="PackedScene" uid="uid://2bbycjulf00g" path="res://character/health/HealthComponent.tscn" id="2_np5ag"] +[ext_resource type="PackedScene" uid="uid://2bbycjulf00g" path="res://player/health/HealthComponent.tscn" id="2_np5ag"] [ext_resource type="PackedScene" uid="uid://cgqbkj8wbcatv" path="res://assets/hair/PonyTail.blend" id="3_umw6q"] [ext_resource type="FontFile" uid="uid://b7mov13kwi8u8" path="res://assets/font/ubuntu_nf_regular.ttf" id="4_76ehj"] [ext_resource type="Skin" uid="uid://bbqyiue1vj37f" path="res://assets/hoodie/Hoodie_Skin.tres" id="4_b1tg1"] [ext_resource type="ArrayMesh" uid="uid://dbmluwi2atit" path="res://assets/hoodie/Hoodie_Mesh.res" id="5_mkrgn"] -[ext_resource type="PackedScene" uid="uid://x102pryt2s5a" path="res://character/movement/MovementComponent.tscn" id="8_25qd0"] -[ext_resource type="PackedScene" uid="uid://d0onbq0ad1ap4" path="res://character/rotation/RotationComponent.tscn" id="9_agxqu"] -[ext_resource type="PackedScene" uid="uid://bivxnxwi863o0" path="res://character/animation/AnimationComponent.tscn" id="10_bcaeg"] +[ext_resource type="PackedScene" uid="uid://6jpnl6c4fdvo" path="res://player/hud/HUDComponent.tscn" id="7_fwgtd"] +[ext_resource type="PackedScene" uid="uid://x102pryt2s5a" path="res://player/movement/MovementComponent.tscn" id="8_25qd0"] +[ext_resource type="PackedScene" uid="uid://d0onbq0ad1ap4" path="res://player/rotation/RotationComponent.tscn" id="9_agxqu"] +[ext_resource type="PackedScene" uid="uid://bivxnxwi863o0" path="res://player/animation/AnimationComponent.tscn" id="10_bcaeg"] [ext_resource type="AnimationLibrary" uid="uid://d4n0puibh4hyt" path="res://assets/animations/human.blend" id="11_d0e6r"] +[ext_resource type="PackedScene" uid="uid://b8xf4ltqyorjv" path="res://player/skills/SkillsComponent.tscn" id="14_6idcf"] [sub_resource type="CapsuleShape3D" id="CapsuleShape3D_2f50n"] radius = 0.3 @@ -52,6 +54,7 @@ shape = SubResource("CapsuleShape3D_2f50n") [node name="Label" type="Label3D" parent="."] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 2, 0) +visible = false pixel_size = 0.0005 billboard = 1 double_sided = false @@ -62,12 +65,17 @@ font = ExtResource("4_76ehj") font_size = 256 outline_size = 32 +[node name="HUD" parent="." node_paths=PackedStringArray("health") instance=ExtResource("7_fwgtd")] +health = NodePath("../Health") + [node name="Attractor" type="GPUParticlesAttractorSphere3D" parent="."] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0) strength = 15.0 radius = 2.0 [node name="Health" parent="." instance=ExtResource("2_np5ag")] +unique_name_in_owner = true +max_value = 100.0 [node name="Movement" parent="." instance=ExtResource("8_25qd0")] @@ -82,5 +90,7 @@ libraries = { "human": ExtResource("11_d0e6r") } +[node name="Skills" parent="." instance=ExtResource("14_6idcf")] + [editable path="Model/Female"] [editable path="Animation"] diff --git a/client/character/animation/AnimationComponent.gd b/client/player/animation/AnimationComponent.gd similarity index 100% rename from client/character/animation/AnimationComponent.gd rename to client/player/animation/AnimationComponent.gd diff --git a/client/character/animation/AnimationComponent.tscn b/client/player/animation/AnimationComponent.tscn similarity index 70% rename from client/character/animation/AnimationComponent.tscn rename to client/player/animation/AnimationComponent.tscn index b5e57c7..dcd6468 100644 --- a/client/character/animation/AnimationComponent.tscn +++ b/client/player/animation/AnimationComponent.tscn @@ -1,6 +1,6 @@ [gd_scene load_steps=2 format=3 uid="uid://bivxnxwi863o0"] -[ext_resource type="Script" path="res://character/animation/AnimationComponent.gd" id="1_lenw3"] +[ext_resource type="Script" path="res://player/animation/AnimationComponent.gd" id="1_lenw3"] [node name="Animation" type="Node"] script = ExtResource("1_lenw3") diff --git a/client/player/health/DamageInstance.gd b/client/player/health/DamageInstance.gd new file mode 100644 index 0000000..258606c --- /dev/null +++ b/client/player/health/DamageInstance.gd @@ -0,0 +1,7 @@ +class_name DamageInstance + +var damage: float + +func _init(_damage: float): + damage = _damage + diff --git a/client/player/health/HealthComponent.gd b/client/player/health/HealthComponent.gd new file mode 100644 index 0000000..7feb051 --- /dev/null +++ b/client/player/health/HealthComponent.gd @@ -0,0 +1,18 @@ +class_name HealthComponent +extends Node + +signal value_changed + +@export var max_value: float +var value: float + +func _ready(): + value = max_value + value_changed.emit() + +func take_damage(attack: DamageInstance): + value -= attack.damage + value_changed.emit() + + if value <= 0: + owner.queue_free() diff --git a/client/character/health/HealthComponent.tscn b/client/player/health/HealthComponent.tscn similarity index 57% rename from client/character/health/HealthComponent.tscn rename to client/player/health/HealthComponent.tscn index 6aab3ba..dc7783c 100644 --- a/client/character/health/HealthComponent.tscn +++ b/client/player/health/HealthComponent.tscn @@ -1,6 +1,6 @@ [gd_scene load_steps=2 format=3 uid="uid://2bbycjulf00g"] -[ext_resource type="Script" path="res://character/health/HealthComponent.gd" id="1_403dm"] +[ext_resource type="Script" path="res://player/health/HealthComponent.gd" id="1_403dm"] [node name="Health" type="Node"] script = ExtResource("1_403dm") diff --git a/client/player/hud/HUDComponent.gd b/client/player/hud/HUDComponent.gd new file mode 100644 index 0000000..ee35a5b --- /dev/null +++ b/client/player/hud/HUDComponent.gd @@ -0,0 +1,14 @@ +class_name HUDComponent +extends Sprite3D + +@export var health: HealthComponent + +func _ready(): + texture = $SubViewport.get_texture() + update_name(owner.name) + + if owner.has_signal("name_changed"): + owner.name_changed.connect(update_name) + +func update_name(new_name: String): + %Label.text = new_name diff --git a/client/player/hud/HUDComponent.tscn b/client/player/hud/HUDComponent.tscn new file mode 100644 index 0000000..9b94187 --- /dev/null +++ b/client/player/hud/HUDComponent.tscn @@ -0,0 +1,44 @@ +[gd_scene load_steps=7 format=3 uid="uid://6jpnl6c4fdvo"] + +[ext_resource type="Script" path="res://player/hud/HUDComponent.gd" id="1_vynft"] +[ext_resource type="Texture2D" uid="uid://d2rig4p2dfy30" path="res://assets/ui/bar-green.png" id="2_ad7qf"] +[ext_resource type="Script" path="res://player/hud/HealthBar.gd" id="3_61bgt"] +[ext_resource type="Texture2D" uid="uid://deopmcycal0aq" path="res://assets/ui/bar-yellow.png" id="4_5dl20"] +[ext_resource type="Texture2D" uid="uid://cjg6cgv6hvnd" path="res://assets/ui/bar-red.png" id="5_3x8xp"] + +[sub_resource type="LabelSettings" id="LabelSettings_y8qrs"] +font_size = 32 +shadow_color = Color(0, 0, 0, 1) + +[node name="HUD" type="Sprite3D"] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.83994, 0) +pixel_size = 0.003 +billboard = 1 +region_rect = Rect2(0, 0, 84, 26) +script = ExtResource("1_vynft") + +[node name="SubViewport" type="SubViewport" parent="."] +disable_3d = true +transparent_bg = true +size = Vector2i(200, 70) + +[node name="VBoxContainer" type="VBoxContainer" parent="SubViewport"] +offset_right = 40.0 +offset_bottom = 40.0 + +[node name="Label" type="Label" parent="SubViewport/VBoxContainer"] +unique_name_in_owner = true +layout_mode = 2 +label_settings = SubResource("LabelSettings_y8qrs") +uppercase = true + +[node name="HealthBar" type="TextureProgressBar" parent="SubViewport/VBoxContainer"] +layout_mode = 2 +max_value = 1.0 +step = 0.0 +value = 1.0 +texture_progress = ExtResource("2_ad7qf") +script = ExtResource("3_61bgt") +high = ExtResource("2_ad7qf") +medium = ExtResource("4_5dl20") +low = ExtResource("5_3x8xp") diff --git a/client/player/hud/HealthBar.gd b/client/player/hud/HealthBar.gd new file mode 100644 index 0000000..c9ef7b8 --- /dev/null +++ b/client/player/hud/HealthBar.gd @@ -0,0 +1,21 @@ +extends TextureProgressBar + +@export var high: Texture2D +@export var medium: Texture2D +@export var low: Texture2D + +var health: HealthComponent + +func _ready(): + health = owner.health + health.value_changed.connect(on_health_changed) + +func on_health_changed(): + value = health.value / health.max_value + + if value < 0.45: + texture_progress = low + elif value < 0.75: + texture_progress = medium + else: + texture_progress = high \ No newline at end of file diff --git a/client/character/movement/MovementComponent.gd b/client/player/movement/MovementComponent.gd similarity index 100% rename from client/character/movement/MovementComponent.gd rename to client/player/movement/MovementComponent.gd diff --git a/client/character/movement/MovementComponent.tscn b/client/player/movement/MovementComponent.tscn similarity index 57% rename from client/character/movement/MovementComponent.tscn rename to client/player/movement/MovementComponent.tscn index d6657a7..34e7c3c 100644 --- a/client/character/movement/MovementComponent.tscn +++ b/client/player/movement/MovementComponent.tscn @@ -1,6 +1,6 @@ [gd_scene load_steps=2 format=3 uid="uid://x102pryt2s5a"] -[ext_resource type="Script" path="res://character/movement/MovementComponent.gd" id="1_2gnmd"] +[ext_resource type="Script" path="res://player/movement/MovementComponent.gd" id="1_2gnmd"] [node name="Movement" type="Node"] script = ExtResource("1_2gnmd") diff --git a/client/character/rotation/RotationComponent.gd b/client/player/rotation/RotationComponent.gd similarity index 100% rename from client/character/rotation/RotationComponent.gd rename to client/player/rotation/RotationComponent.gd diff --git a/client/character/rotation/RotationComponent.tscn b/client/player/rotation/RotationComponent.tscn similarity index 57% rename from client/character/rotation/RotationComponent.tscn rename to client/player/rotation/RotationComponent.tscn index 8520798..21a8506 100644 --- a/client/character/rotation/RotationComponent.tscn +++ b/client/player/rotation/RotationComponent.tscn @@ -1,6 +1,6 @@ [gd_scene load_steps=2 format=3 uid="uid://d0onbq0ad1ap4"] -[ext_resource type="Script" path="res://character/rotation/RotationComponent.gd" id="1_j1874"] +[ext_resource type="Script" path="res://player/rotation/RotationComponent.gd" id="1_j1874"] [node name="Rotation" type="Node"] script = ExtResource("1_j1874") diff --git a/client/player/skills/SkillsComponent.gd b/client/player/skills/SkillsComponent.gd new file mode 100644 index 0000000..69f032f --- /dev/null +++ b/client/player/skills/SkillsComponent.gd @@ -0,0 +1,11 @@ +class_name SkillsComponent +extends Node + +var player: Player + +func _ready(): + player = owner + player.attacked.connect(attack) + +func attack(): + %Health.take_damage(DamageInstance.new(10)) diff --git a/client/player/skills/SkillsComponent.tscn b/client/player/skills/SkillsComponent.tscn new file mode 100644 index 0000000..541a051 --- /dev/null +++ b/client/player/skills/SkillsComponent.tscn @@ -0,0 +1,6 @@ +[gd_scene load_steps=2 format=3 uid="uid://b8xf4ltqyorjv"] + +[ext_resource type="Script" path="res://player/skills/SkillsComponent.gd" id="1_016bx"] + +[node name="Skills" type="Node"] +script = ExtResource("1_016bx") diff --git a/client/project.godot b/client/project.godot index 49506c1..3626706 100644 --- a/client/project.godot +++ b/client/project.godot @@ -83,6 +83,7 @@ attack={ "deadzone": 0.5, "events": [Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"button_mask":0,"position":Vector2(0, 0),"global_position":Vector2(0, 0),"factor":1.0,"button_index":1,"canceled":false,"pressed":false,"double_click":false,"script":null) , Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"button_index":0,"pressure":0.0,"pressed":false,"script":null) +, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":81,"key_label":0,"unicode":113,"echo":false,"script":null) ] } aim={