36 lines
963 B
GDScript

class_name AnimationComponent
extends CharacterComponent
var animations: AnimationPlayer
var state: StateComponent
var accumulated_delta: float
var delay: float
func _ready():
state = character.get_node("State")
state.transitioned.connect(on_transition)
animations = %AnimationPlayer
func _process(delta: float):
accumulated_delta += delta
if accumulated_delta >= delay:
animations.advance(accumulated_delta)
accumulated_delta = 0
func on_transition(_from: StateComponent.State, to: StateComponent.State):
match to:
StateComponent.State.Idle:
animations.play("human/idle")
StateComponent.State.Run:
animations.play("human/run-fast")
StateComponent.State.Jump:
animations.play("human/jump")
StateComponent.State.Fall:
animations.play("human/fall")
StateComponent.State.None:
animations.play("human/RESET")
func play(action_name: StringName, speed: float = 1.0):
animations.speed_scale = speed
animations.play(action_name)