Added state component

This commit is contained in:
2024-02-15 13:45:17 +01:00
parent 17a507d07c
commit 37ed8890ec
19 changed files with 172 additions and 86 deletions

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)