Added health bars

This commit is contained in:
2024-02-13 00:22:27 +01:00
parent ba978049a5
commit 6727be5cd0
23 changed files with 154 additions and 41 deletions

View File

@ -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)

View File

@ -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"]

View File

@ -0,0 +1,55 @@
class_name AnimationComponent
extends Node
const RESET = "human/RESET"
var animation_player: AnimationPlayer
var movement: MovementComponent
var next_animation: StringName = RESET
var skip: int
func _ready():
var player := owner as Player
player.dashed.connect(dash)
player.attacked.connect(attack)
movement = player.find_child("Movement")
animation_player = $AnimationPlayer
func _process(_delta):
if skip == 0:
play_movement()
if animation_player.current_animation == next_animation:
return
animation_player.play(next_animation)
func play_movement():
if !movement:
play(RESET)
return
if movement.body.velocity.y > 0:
play("human/jump")
elif movement.body.velocity.y < 0:
play("human/fall")
elif movement.direction != Vector3.ZERO:
play("human/run-fast")
else:
play("human/idle")
func dash():
play_with_duration("human/roll", 1.0)
func attack():
play_with_duration("human/slash", 1.0)
func play(action_name: StringName):
next_animation = action_name
func play_with_duration(action_name: StringName, duration: float):
next_animation = action_name
skip += 1
await get_tree().create_timer(duration).timeout
skip -= 1

View File

@ -0,0 +1,9 @@
[gd_scene load_steps=2 format=3 uid="uid://bivxnxwi863o0"]
[ext_resource type="Script" path="res://player/animation/AnimationComponent.gd" id="1_lenw3"]
[node name="Animation" type="Node"]
script = ExtResource("1_lenw3")
[node name="AnimationPlayer" type="AnimationPlayer" parent="."]
playback_default_blend_time = 0.2

View File

@ -0,0 +1,7 @@
class_name DamageInstance
var damage: float
func _init(_damage: float):
damage = _damage

View File

@ -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()

View File

@ -0,0 +1,6 @@
[gd_scene load_steps=2 format=3 uid="uid://2bbycjulf00g"]
[ext_resource type="Script" path="res://player/health/HealthComponent.gd" id="1_403dm"]
[node name="Health" type="Node"]
script = ExtResource("1_403dm")

View File

@ -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

View File

@ -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")

View File

@ -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

View File

@ -0,0 +1,42 @@
class_name MovementComponent
extends Node
@export var move_speed := 4.5
@export var jump_velocity := 4.5
@export var deceleration := 0.75
var body: CharacterBody3D
var direction: Vector3
var gravity: float
func _ready():
if owner.has_signal("direction_changed"):
owner.direction_changed.connect(on_direction_changed)
if owner.has_signal("jumped"):
owner.jumped.connect(jump)
body = owner as CharacterBody3D
gravity = ProjectSettings.get_setting("physics/3d/default_gravity")
func _physics_process(delta):
if direction:
body.velocity.x = direction.x * move_speed
body.velocity.z = direction.z * move_speed
else:
body.velocity.x *= deceleration
body.velocity.z *= deceleration
if !body.is_on_floor():
body.velocity.y -= gravity * delta
body.move_and_slide()
func on_direction_changed(new_direction: Vector3):
direction = new_direction
func can_jump() -> bool:
return body.is_on_floor()
func jump():
body.velocity.y = jump_velocity

View File

@ -0,0 +1,6 @@
[gd_scene load_steps=2 format=3 uid="uid://x102pryt2s5a"]
[ext_resource type="Script" path="res://player/movement/MovementComponent.gd" id="1_2gnmd"]
[node name="Movement" type="Node"]
script = ExtResource("1_2gnmd")

View File

@ -0,0 +1,25 @@
class_name RotationComponent
extends Node
@export var root: Node3D
@export var rotation_speed: float = 20.0
var direction: Vector3
var angle: float
func _ready():
assert(root, "Rotation root needs to be set")
if owner.has_signal("direction_changed"):
owner.direction_changed.connect(on_direction_changed)
update_configuration_warnings()
func _process(delta):
if direction != Vector3.ZERO:
angle = atan2(direction.x, direction.z)
root.rotation.y = lerp_angle(root.rotation.y, angle, rotation_speed * delta)
func on_direction_changed(new_direction: Vector3):
direction = new_direction

View File

@ -0,0 +1,6 @@
[gd_scene load_steps=2 format=3 uid="uid://d0onbq0ad1ap4"]
[ext_resource type="Script" path="res://player/rotation/RotationComponent.gd" id="1_j1874"]
[node name="Rotation" type="Node"]
script = ExtResource("1_j1874")

View File

@ -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))

View File

@ -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")