Added state component
This commit is contained in:
@ -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()
|
||||
|
||||
|
@ -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"]
|
||||
|
@ -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)
|
@ -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)):
|
||||
|
48
client/player/state/StateComponent.gd
Normal file
48
client/player/state/StateComponent.gd
Normal 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)
|
6
client/player/state/StateComponent.tscn
Normal file
6
client/player/state/StateComponent.tscn
Normal 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")
|
Reference in New Issue
Block a user