36 lines
963 B
GDScript3
Raw Normal View History

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