turn_queue.gd 1.2 KB

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