TurnQueue.gd 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. extends Node
  2. export(NodePath) var combatants_list
  3. var queue = [] setget set_queue
  4. var active_combatant = null setget _set_active_combatant
  5. signal active_combatant_changed(active_combatant)
  6. func _ready():
  7. combatants_list = get_node(combatants_list)
  8. func initialize():
  9. set_queue(combatants_list.get_children())
  10. play_turn()
  11. func play_turn():
  12. yield(active_combatant, "turn_finished")
  13. get_next_in_queue()
  14. play_turn()
  15. func get_next_in_queue():
  16. var current_combatant = queue.pop_front()
  17. current_combatant.active = false
  18. queue.append(current_combatant)
  19. self.active_combatant = queue[0]
  20. return active_combatant
  21. func remove(combatant):
  22. var new_queue = []
  23. for n in queue:
  24. new_queue.append(n)
  25. new_queue.remove(new_queue.find(combatant))
  26. combatant.queue_free()
  27. self.queue = new_queue
  28. func set_queue(new_queue):
  29. queue.clear()
  30. for node in new_queue:
  31. if not node is Combatant:
  32. continue
  33. queue.append(node)
  34. node.active = false
  35. if queue.size() > 0:
  36. self.active_combatant = queue[0]
  37. func _set_active_combatant(new_combatant):
  38. active_combatant = new_combatant
  39. active_combatant.active = true
  40. emit_signal("active_combatant_changed", active_combatant)