Skip to content

Instantly share code, notes, and snippets.

@PLyczkowski
Created November 11, 2018 11:01
Show Gist options
  • Save PLyczkowski/5ee7436dc66ec881ede0e857330d2f53 to your computer and use it in GitHub Desktop.
Save PLyczkowski/5ee7436dc66ec881ede0e857330d2f53 to your computer and use it in GitHub Desktop.
extends Node
onready var tools = load("res://lib/tools.gd").new(self)
var debug_prints = true
# Settings
export(float) var animation_speed = 1.0
# State
var active = false
var gui
var currently_playing
var history
var current_queue
var animation_timer
class AnimationQueueItem extends Node:
var target_weakref
var animation_name
var parameters
signal animation_finished() # Whole queue
func _ready():
# Self
set_name("AnimationQueue")
# Current Queue
current_queue = Node.new()
current_queue.set_name("CurrentQueue")
add_child(current_queue)
# History
history = Node.new()
history.set_name("History")
add_child(history)
# Timer
animation_timer = Timer.new()
animation_timer.one_shot = true
animation_timer.name = "AnimationTimer"
animation_timer.connect("timeout", self, "on_animation_finished")
add_child(animation_timer)
func setup(gui):
pass
self.gui = gui
func queue(target, animation_name, parameters = {}):
if debug_prints: prints("Queueing animation:", animation_name, "-", target.name)
# Queue animation as aq_item
var aq_item = AnimationQueueItem.new()
aq_item.target_weakref = weakref(target)
aq_item.animation_name = animation_name
aq_item.parameters = parameters
aq_item.set_name(target.name + " - " + animation_name)
if tools.get_names_of_all_children(current_queue).has(aq_item.name) or tools.get_names_of_all_children(history).has(aq_item.name):
tools.increment_name_until_unique(aq_item, current_queue.get_children() + history.get_children())
current_queue.add_child(aq_item)
# Start playing if not playing
if currently_playing == null and current_queue.get_child_count() > 0:
play_aq_item(current_queue.get_child(0))
func play_aq_item(aq_item):
# Check status
if currently_playing != null: breakpoint
# Set currently playing
currently_playing = aq_item
if debug_prints: prints("Playing animation:", aq_item.animation_name, "-", aq_item.target_weakref.get_ref().name)
# If self is not active, skip animation
# if !active:
# on_animation_finished(aq_item.animation_name)
# return
# Get target
var target = aq_item.target_weakref.get_ref()
# If no target, finish animation
if target == null:
# It does not - animation_finished
prints("Aq_item lost it's target:", aq_item.animation_name)
on_animation_finished(aq_item.animation_name)
return
# Play
# Call play animation, get lenght in return
var animation_lenght = target.call("play_animation", aq_item.animation_name, aq_item.parameters)
# Set timer, when ends call animation_finished
if animation_lenght != null:
# Wait the given lenght
animation_timer.wait_time = animation_lenght
animation_timer.start()
# Some targets use custom call >
target.connect("animation_finished", self, "on_animation_finished")
func on_animation_finished(animation_name = null):
if debug_prints: prints("Finished playing animation:", animation_name)
if animation_name != null and animation_name != currently_playing.animation_name:
print("Got ", animation_name, ", expected ", currently_playing.animation_name)
breakpoint
# Get target
var target = currently_playing.target_weakref.get_ref()
if target != null:
# Disconnect signal from target
target.disconnect("animation_finished", self, "on_animation_finished")
# Remove currently_playing and store in History
current_queue.remove_child(currently_playing)
if tools.get_names_of_all_children(history).has(currently_playing.name): tools.increment_name_until_unique(currently_playing, history.get_children())
history.add_child(currently_playing)
currently_playing = null
# Start playing if not playing, else emit animation_finished
if current_queue.get_child_count() > 0: play_aq_item(current_queue.get_child(0))
else: emit_signal("animation_finished")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment