2023-02-25 01:04:08 +01:00
|
|
|
extends Node2D
|
|
|
|
|
|
|
|
onready var me = $AnimatedSprite
|
|
|
|
|
|
|
|
onready var timer
|
2023-02-25 14:02:57 +01:00
|
|
|
var limit_random
|
|
|
|
var rng = RandomNumberGenerator.new()
|
2023-02-25 18:46:19 +01:00
|
|
|
export(Array, String) var animations = ["green", "orange", "red"]
|
2023-02-25 01:04:08 +01:00
|
|
|
|
2023-02-25 17:29:51 +01:00
|
|
|
# True if animation should change
|
|
|
|
var next = false
|
|
|
|
|
2023-02-25 01:04:08 +01:00
|
|
|
|
|
|
|
# Called when the node enters the scene tree for the first time.
|
2023-02-25 19:31:26 +01:00
|
|
|
func _ready() -> void:
|
2023-02-26 17:24:17 +01:00
|
|
|
rng.randomize()
|
2023-02-25 18:46:19 +01:00
|
|
|
|
2023-02-26 17:24:17 +01:00
|
|
|
reset_timer()
|
|
|
|
me.play(animations[0])
|
2023-02-25 18:46:19 +01:00
|
|
|
|
|
|
|
|
2023-02-25 19:31:26 +01:00
|
|
|
func reset_timer() -> void:
|
2023-02-26 17:24:17 +01:00
|
|
|
timer = 0
|
|
|
|
limit_random = rng.randi_range(2, 10)
|
2023-02-25 01:04:08 +01:00
|
|
|
|
|
|
|
|
|
|
|
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
2023-02-25 19:31:26 +01:00
|
|
|
func _process(delta) -> void:
|
2023-02-26 17:24:17 +01:00
|
|
|
timer += delta
|
2023-02-25 17:29:51 +01:00
|
|
|
|
2023-02-26 17:24:17 +01:00
|
|
|
var max_frame = me.frames.get_frame_count(me.animation) - 1
|
|
|
|
var current_anim_idx = animations.find(me.animation)
|
|
|
|
if current_anim_idx >= animations.size() - 1:
|
|
|
|
# Last animation reached
|
|
|
|
check_next_animframe(max_frame, animations[0])
|
|
|
|
else:
|
|
|
|
check_next_animframe(max_frame, animations[current_anim_idx + 1])
|
2023-02-25 17:29:51 +01:00
|
|
|
|
|
|
|
|
|
|
|
# Jump to next animation at the end of the current one
|
2023-02-25 19:31:26 +01:00
|
|
|
func check_next_animframe(max_frame, next_animation) -> void:
|
2023-02-26 17:24:17 +01:00
|
|
|
if me.frame == 0 and next:
|
|
|
|
me.play(next_animation)
|
|
|
|
next = false
|
|
|
|
if next_animation == animations[0]:
|
|
|
|
# Reset random for first animation
|
|
|
|
reset_timer()
|
|
|
|
return
|
|
|
|
if max_frame == me.frame:
|
|
|
|
# Last frame of animation reached
|
|
|
|
next = true
|
|
|
|
return
|
|
|
|
if max_frame == -1 and timer >= limit_random:
|
|
|
|
# No frame in animation, use time
|
|
|
|
next = true
|
|
|
|
return
|