36 lines
963 B
GDScript3
Raw Normal View History

2024-02-07 23:04:19 +01:00
class_name AnimationComponent
2024-02-27 21:05:55 +01:00
extends CharacterComponent
2024-02-26 23:50:56 +01:00
var animations: AnimationPlayer
2024-02-15 13:45:17 +01:00
var state: StateComponent
2024-02-27 21:05:55 +01:00
var accumulated_delta: float
2024-02-28 12:15:58 +01:00
var delay: float
2024-02-07 23:04:19 +01:00
func _ready():
2024-02-27 21:05:55 +01:00
state = character.get_node("State")
2024-02-15 13:45:17 +01:00
state.transitioned.connect(on_transition)
2024-02-26 23:50:56 +01:00
animations = %AnimationPlayer
2024-02-27 23:21:44 +01:00
func _process(delta: float):
2024-02-27 21:05:55 +01:00
accumulated_delta += delta
2024-02-28 12:15:58 +01:00
if accumulated_delta >= delay:
2024-02-27 21:05:55 +01:00
animations.advance(accumulated_delta)
accumulated_delta = 0
2024-02-15 13:45:17 +01:00
func on_transition(_from: StateComponent.State, to: StateComponent.State):
match to:
StateComponent.State.Idle:
2024-02-26 23:50:56 +01:00
animations.play("human/idle")
2024-02-15 13:45:17 +01:00
StateComponent.State.Run:
2024-02-26 23:50:56 +01:00
animations.play("human/run-fast")
2024-02-15 13:45:17 +01:00
StateComponent.State.Jump:
2024-02-26 23:50:56 +01:00
animations.play("human/jump")
2024-02-15 13:45:17 +01:00
StateComponent.State.Fall:
2024-02-26 23:50:56 +01:00
animations.play("human/fall")
2024-02-15 13:45:17 +01:00
StateComponent.State.None:
2024-02-26 23:50:56 +01:00
animations.play("human/RESET")
2024-02-15 13:45:17 +01:00
func play(action_name: StringName, speed: float = 1.0):
2024-02-26 23:50:56 +01:00
animations.speed_scale = speed
animations.play(action_name)