41 lines
1.0 KiB
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
@export var skip_frames: int = 0
2024-02-07 22:04:19 +00:00
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
var skipped_frames: int
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
if skipped_frames >= skip_frames:
animations.advance(accumulated_delta)
accumulated_delta = 0
skipped_frames = 0
else:
skipped_frames += 1
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)