turn_queue.gd 1.1 KB

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