From 37ed8890ec1c4e94be3b3ba152424c8157682403 Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Thu, 15 Feb 2024 13:45:17 +0100 Subject: [PATCH] Added state component --- client/player/Player.gd | 6 +- client/player/Player.tscn | 8 ++- client/player/animation/AnimationComponent.gd | 66 ++++++------------- client/player/controller/PlayerController.gd | 3 - client/player/state/StateComponent.gd | 48 ++++++++++++++ client/player/state/StateComponent.tscn | 6 ++ client/project.godot | 7 +- client/skill/AreaSkillInstance.gd | 17 +++++ client/skill/SkillInstance.gd | 23 +++---- client/skill/dash/dash.gd | 6 ++ client/skill/dash/dash.tres | 11 ++++ client/skill/dash/dash.tscn | 6 ++ client/skill/slash/slash.gd | 8 +-- client/skill/spin/spin.gd | 8 +-- client/skill/thrust/thrust.gd | 8 +-- client/ui/UI.tscn | 8 ++- client/ui/debug/DebugLabel.gd | 4 +- client/ui/debug/DebugLabel.tscn | 1 + client/ui/debug/StateLabel.gd | 14 ++++ 19 files changed, 172 insertions(+), 86 deletions(-) create mode 100644 client/player/state/StateComponent.gd create mode 100644 client/player/state/StateComponent.tscn create mode 100644 client/skill/AreaSkillInstance.gd create mode 100644 client/skill/dash/dash.gd create mode 100644 client/skill/dash/dash.tres create mode 100644 client/skill/dash/dash.tscn create mode 100644 client/ui/debug/StateLabel.gd diff --git a/client/player/Player.gd b/client/player/Player.gd index 6937787..d83a586 100644 --- a/client/player/Player.gd +++ b/client/player/Player.gd @@ -13,11 +13,11 @@ var id: String var controller: Controller func use_skill(slot: int): + if get_node("State").current == StateComponent.State.Skill: + return + skill_used.emit(skills[slot]) -func dash(): - dashed.emit() - func jump(): jumped.emit() diff --git a/client/player/Player.tscn b/client/player/Player.tscn index 2b6c407..57eb3cb 100644 --- a/client/player/Player.tscn +++ b/client/player/Player.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=29 format=3 uid="uid://2lcnu3dy54lx"] +[gd_scene load_steps=31 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"] @@ -10,6 +10,7 @@ [ext_resource type="Skin" uid="uid://bbqyiue1vj37f" path="res://assets/hoodie/Hoodie_Skin.tres" id="4_b1tg1"] [ext_resource type="Resource" uid="uid://1vmnijk8ap6b" path="res://skill/thrust/thrust.tres" id="4_s8cf2"] [ext_resource type="ArrayMesh" uid="uid://dbmluwi2atit" path="res://assets/hoodie/Hoodie_Mesh.res" id="5_mkrgn"] +[ext_resource type="Resource" uid="uid://cnusbw23jf672" path="res://skill/dash/dash.tres" id="5_pnues"] [ext_resource type="PackedScene" uid="uid://6jpnl6c4fdvo" path="res://player/hud/HUDComponent.tscn" id="7_fwgtd"] [ext_resource type="PackedScene" uid="uid://qecdmrg6mbws" path="res://assets/swords/heirloom/Heirloom.blend" id="7_u8433"] [ext_resource type="PackedScene" uid="uid://x102pryt2s5a" path="res://player/movement/MovementComponent.tscn" id="8_25qd0"] @@ -27,6 +28,7 @@ [ext_resource type="AudioStream" uid="uid://cdywep3dxm0y3" path="res://assets/footsteps/Footsteps-Human-Dirt-07.wav" id="19_vvmo0"] [ext_resource type="AudioStream" uid="uid://bp8pka7qhcetq" path="res://assets/footsteps/Footsteps-Human-Dirt-08.wav" id="20_srjtb"] [ext_resource type="AudioStream" uid="uid://bgdodasvt7ier" path="res://assets/footsteps/Footsteps-Human-Dirt-01.wav" id="21_a8ikg"] +[ext_resource type="PackedScene" uid="uid://sx4ein0bju6m" path="res://player/state/StateComponent.tscn" id="28_0i0of"] [sub_resource type="CapsuleShape3D" id="CapsuleShape3D_2f50n"] radius = 0.3 @@ -36,7 +38,7 @@ height = 1.6 collision_layer = 512 collision_mask = 769 script = ExtResource("1_8gebs") -skills = Array[Resource("res://skill/Skill.gd")]([ExtResource("2_x58e1"), ExtResource("3_l76ly"), ExtResource("4_s8cf2"), null]) +skills = Array[Resource("res://skill/Skill.gd")]([ExtResource("2_x58e1"), ExtResource("3_l76ly"), ExtResource("4_s8cf2"), ExtResource("5_pnues")]) [node name="Model" type="Node3D" parent="."] @@ -120,5 +122,7 @@ libraries = { [node name="Skills" parent="." instance=ExtResource("14_6idcf")] +[node name="State" parent="." instance=ExtResource("28_0i0of")] + [editable path="Model/Female"] [editable path="Animation"] diff --git a/client/player/animation/AnimationComponent.gd b/client/player/animation/AnimationComponent.gd index 1a70e40..f37f832 100644 --- a/client/player/animation/AnimationComponent.gd +++ b/client/player/animation/AnimationComponent.gd @@ -1,53 +1,27 @@ class_name AnimationComponent extends Node -const RESET = "human/RESET" - -var animation_player: AnimationPlayer -var movement: MovementComponent -var next_animation: StringName = RESET -var skip: int +var player: AnimationPlayer +var state: StateComponent func _ready(): - var player := owner as Player - player.dashed.connect(dash) - movement = player.find_child("Movement") - animation_player = $AnimationPlayer + state = owner.find_child("State") + state.transitioned.connect(on_transition) + player = $AnimationPlayer -func _process(_delta): - if skip == 0: - play_movement() +func on_transition(_from: StateComponent.State, to: StateComponent.State): + match to: + StateComponent.State.Idle: + player.play("human/idle") + StateComponent.State.Run: + player.play("human/run-fast") + StateComponent.State.Jump: + player.play("human/jump") + StateComponent.State.Fall: + player.play("human/fall") + StateComponent.State.None: + player.play("human/RESET") - 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 play(action_name: StringName): - next_animation = action_name - -func play_with_duration(action_name: StringName, duration: float, speed: float = 1.0): - next_animation = action_name - skip += 1 - animation_player.speed_scale = speed - await get_tree().create_timer(duration).timeout - animation_player.speed_scale = 1.0 - skip -= 1 - +func play(action_name: StringName, speed: float = 1.0): + player.speed_scale = speed + player.play(action_name) \ No newline at end of file diff --git a/client/player/controller/PlayerController.gd b/client/player/controller/PlayerController.gd index 46ea6ba..4b3383f 100644 --- a/client/player/controller/PlayerController.gd +++ b/client/player/controller/PlayerController.gd @@ -26,9 +26,6 @@ func _unhandled_input(event): if event.is_action_pressed("jump") && movement && movement.can_jump(): player.jump() - - if event.is_action_pressed("dash"): - player.dash() for i in range(4): if event.is_action_pressed("skill_%d" % (i + 1)): diff --git a/client/player/state/StateComponent.gd b/client/player/state/StateComponent.gd new file mode 100644 index 0000000..84efb26 --- /dev/null +++ b/client/player/state/StateComponent.gd @@ -0,0 +1,48 @@ +class_name StateComponent +extends Node + +enum State { + None, + Idle, + Run, + Jump, + Fall, + Skill, +} + +signal transitioned(from: State, to: State) + +var movement: MovementComponent +var _current: State + +func _ready(): + current = State.Idle + movement = owner.find_child("Movement") + +func _process(_delta): + if current == State.Skill: + return + + current = next_state() + +func next_state() -> State: + if movement.body.velocity.y > 0: + return State.Jump + elif movement.body.velocity.y < 0: + return State.Fall + elif movement.direction != Vector3.ZERO: + return State.Run + else: + return State.Idle + +## Current character state getter and setter. +var current: State: + get: + return _current + set(new_state): + if _current == new_state: + return + + var old_state := _current + _current = new_state + transitioned.emit(old_state, new_state) diff --git a/client/player/state/StateComponent.tscn b/client/player/state/StateComponent.tscn new file mode 100644 index 0000000..cbd0bae --- /dev/null +++ b/client/player/state/StateComponent.tscn @@ -0,0 +1,6 @@ +[gd_scene load_steps=2 format=3 uid="uid://sx4ein0bju6m"] + +[ext_resource type="Script" path="res://player/state/StateComponent.gd" id="1_n56nn"] + +[node name="State" type="Node"] +script = ExtResource("1_n56nn") diff --git a/client/project.godot b/client/project.godot index feebdf9..9c36d42 100644 --- a/client/project.godot +++ b/client/project.godot @@ -77,11 +77,6 @@ jump={ , Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"button_index":1,"pressure":0.0,"pressed":false,"script":null) ] } -dash={ -"deadzone": 0.5, -"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194325,"key_label":0,"unicode":0,"echo":false,"script":null) -] -} menu={ "deadzone": 0.5, "events": [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":4194305,"key_label":0,"unicode":0,"echo":false,"script":null) @@ -142,7 +137,7 @@ skill_3={ } skill_4={ "deadzone": 0.5, -"events": [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":84,"key_label":0,"unicode":116,"echo":false,"script":null) +"events": [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":4194325,"key_label":0,"unicode":0,"echo":false,"script":null) ] } diff --git a/client/skill/AreaSkillInstance.gd b/client/skill/AreaSkillInstance.gd new file mode 100644 index 0000000..195b456 --- /dev/null +++ b/client/skill/AreaSkillInstance.gd @@ -0,0 +1,17 @@ +class_name AreaSkillInstance +extends SkillInstance + +var area: Area3D + +func _enter_tree(): + super._enter_tree() + area = get_node("Area") + +func area_damage(before_time: float, active_time: float, after_time: float): + await get_tree().create_timer(before_time).timeout + area.monitoring = true + + await get_tree().create_timer(active_time).timeout + area.monitoring = false + + await get_tree().create_timer(after_time).timeout \ No newline at end of file diff --git a/client/skill/SkillInstance.gd b/client/skill/SkillInstance.gd index 7107e86..ec707c0 100644 --- a/client/skill/SkillInstance.gd +++ b/client/skill/SkillInstance.gd @@ -1,14 +1,15 @@ class_name SkillInstance -extends Node3D +extends Node -func play_animation(animation_name: String, duration: float, speed: float): - var character := get_parent() - var animation := character.get_node("Animation") as AnimationComponent - animation.play_with_duration(animation_name, duration, speed) +var state: StateComponent +var animation: AnimationComponent -func melee_damage(wait_time: float): - var area := get_node("Area") - await get_tree().create_timer(wait_time).timeout - area.monitoring = true - await get_tree().create_timer(0.1).timeout - area.monitoring = false \ No newline at end of file +func _enter_tree(): + state = get_parent().get_node("State") as StateComponent + animation = get_parent().get_node("Animation") as AnimationComponent + state.current = StateComponent.State.Skill + +func end(): + animation.player.speed_scale = 1.0 + state.current = state.next_state() + queue_free() \ No newline at end of file diff --git a/client/skill/dash/dash.gd b/client/skill/dash/dash.gd new file mode 100644 index 0000000..b6c7347 --- /dev/null +++ b/client/skill/dash/dash.gd @@ -0,0 +1,6 @@ +extends SkillInstance + +func _ready(): + animation.play("human/roll") + await get_tree().create_timer(1.0).timeout + end() diff --git a/client/skill/dash/dash.tres b/client/skill/dash/dash.tres new file mode 100644 index 0000000..726d342 --- /dev/null +++ b/client/skill/dash/dash.tres @@ -0,0 +1,11 @@ +[gd_resource type="Resource" script_class="Skill" load_steps=3 format=3 uid="uid://cnusbw23jf672"] + +[ext_resource type="PackedScene" uid="uid://1u5qbxotvgo4" path="res://skill/dash/dash.tscn" id="1_0n3i4"] +[ext_resource type="Script" path="res://skill/Skill.gd" id="2_2m415"] + +[resource] +script = ExtResource("2_2m415") +id = "dash" +name = "Dash" +cooldown = 0.0 +scene = ExtResource("1_0n3i4") diff --git a/client/skill/dash/dash.tscn b/client/skill/dash/dash.tscn new file mode 100644 index 0000000..838c995 --- /dev/null +++ b/client/skill/dash/dash.tscn @@ -0,0 +1,6 @@ +[gd_scene load_steps=2 format=3 uid="uid://1u5qbxotvgo4"] + +[ext_resource type="Script" path="res://skill/dash/dash.gd" id="1_rxa7a"] + +[node name="Dash" type="Node"] +script = ExtResource("1_rxa7a") diff --git a/client/skill/slash/slash.gd b/client/skill/slash/slash.gd index 065e371..b309f7e 100644 --- a/client/skill/slash/slash.gd +++ b/client/skill/slash/slash.gd @@ -1,6 +1,6 @@ -extends SkillInstance +extends AreaSkillInstance func _ready(): - play_animation("human/slash", 0.8, 1.7) - await melee_damage(0.5) - queue_free() \ No newline at end of file + animation.play("human/slash", 1.7) + await area_damage(0.5, 0.1, 0.2) + end() \ No newline at end of file diff --git a/client/skill/spin/spin.gd b/client/skill/spin/spin.gd index 3e06b9e..a1c2420 100644 --- a/client/skill/spin/spin.gd +++ b/client/skill/spin/spin.gd @@ -1,6 +1,6 @@ -extends SkillInstance +extends AreaSkillInstance func _ready(): - play_animation("human/spin", 1.0, 2.0) - await melee_damage(0.5) - queue_free() \ No newline at end of file + animation.play("human/spin", 2.0) + await area_damage(0.5, 0.1, 0.4) + end() \ No newline at end of file diff --git a/client/skill/thrust/thrust.gd b/client/skill/thrust/thrust.gd index 51ea417..cc8ec5a 100644 --- a/client/skill/thrust/thrust.gd +++ b/client/skill/thrust/thrust.gd @@ -1,6 +1,6 @@ -extends SkillInstance +extends AreaSkillInstance func _ready(): - play_animation("human/thrust", 1.0, 2.0) - await melee_damage(0.5) - queue_free() \ No newline at end of file + animation.play("human/thrust", 2.0) + await area_damage(0.5, 0.1, 0.4) + end() \ No newline at end of file diff --git a/client/ui/UI.tscn b/client/ui/UI.tscn index efcc381..063fad6 100644 --- a/client/ui/UI.tscn +++ b/client/ui/UI.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=15 format=3 uid="uid://dagn5bf7ou3sd"] +[gd_scene load_steps=16 format=3 uid="uid://dagn5bf7ou3sd"] [ext_resource type="PackedScene" uid="uid://cch67vqpsmtej" path="res://ui/debug/DebugLabel.tscn" id="1_7s8uu"] [ext_resource type="Script" path="res://ui/UI.gd" id="1_l5b6o"] @@ -13,6 +13,7 @@ [ext_resource type="Script" path="res://ui/debug/DownloadLabel.gd" id="8_ogt38"] [ext_resource type="Script" path="res://ui/connect/ConnectPanel.gd" id="11_cwl0t"] [ext_resource type="PackedScene" uid="uid://bqpbrju7mc7d5" path="res://ui/settings/Settings.tscn" id="11_rt7sl"] +[ext_resource type="Script" path="res://ui/debug/StateLabel.gd" id="12_rr0mv"] [ext_resource type="PackedScene" uid="uid://y6kdpmp5glv0" path="res://ui/inventory/Inventory.tscn" id="13_1fc2b"] [node name="UI" type="Control"] @@ -104,6 +105,11 @@ grow_horizontal = 0 [node name="VBoxContainer" type="VBoxContainer" parent="Canvas/TopRight"] layout_mode = 2 +[node name="State" parent="Canvas/TopRight/VBoxContainer" instance=ExtResource("1_7s8uu")] +layout_mode = 2 +alignment = 2 +script = ExtResource("12_rr0mv") + [node name="Position" parent="Canvas/TopRight/VBoxContainer" instance=ExtResource("1_7s8uu")] layout_mode = 2 alignment = 2 diff --git a/client/ui/debug/DebugLabel.gd b/client/ui/debug/DebugLabel.gd index 404c3d8..f128659 100644 --- a/client/ui/debug/DebugLabel.gd +++ b/client/ui/debug/DebugLabel.gd @@ -3,6 +3,6 @@ extends Control var text: String: get: - return get_child(1).text + return %Value.text set(value): - get_child(1).text = value + %Value.text = value \ No newline at end of file diff --git a/client/ui/debug/DebugLabel.tscn b/client/ui/debug/DebugLabel.tscn index ca4dadf..400dbd4 100644 --- a/client/ui/debug/DebugLabel.tscn +++ b/client/ui/debug/DebugLabel.tscn @@ -12,4 +12,5 @@ text = "Container:" script = ExtResource("2_l1apn") [node name="Value" type="Label" parent="."] +unique_name_in_owner = true layout_mode = 2 diff --git a/client/ui/debug/StateLabel.gd b/client/ui/debug/StateLabel.gd new file mode 100644 index 0000000..e6a0b82 --- /dev/null +++ b/client/ui/debug/StateLabel.gd @@ -0,0 +1,14 @@ +extends DebugLabel + +var keys := StateComponent.State.keys() +var state: StateComponent + +func _process(_delta): + if !Global.player: + return + + if !state: + state = Global.player.get_node("State") + return + + text = keys[state.current]