Added state component

This commit is contained in:
Eduard Urbach 2024-02-15 13:45:17 +01:00
parent 17a507d07c
commit 37ed8890ec
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
19 changed files with 172 additions and 86 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -0,0 +1,6 @@
extends SkillInstance
func _ready():
animation.play("human/roll")
await get_tree().create_timer(1.0).timeout
end()

View File

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

View File

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

View File

@ -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()
animation.play("human/slash", 1.7)
await area_damage(0.5, 0.1, 0.2)
end()

View File

@ -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()
animation.play("human/spin", 2.0)
await area_damage(0.5, 0.1, 0.4)
end()

View File

@ -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()
animation.play("human/thrust", 2.0)
await area_damage(0.5, 0.1, 0.4)
end()

View File

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

View File

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

View File

@ -12,4 +12,5 @@ text = "Container:"
script = ExtResource("2_l1apn")
[node name="Value" type="Label" parent="."]
unique_name_in_owner = true
layout_mode = 2

View File

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