|
| 1 | +class_name TurnBasedGame extends Node |
| 2 | + |
| 3 | + |
| 4 | +## Emitted when a turn changes. |
| 5 | +signal turn_changed(manager: TurnBasedGame) |
| 6 | +## Emitted when the turn based game starts. |
| 7 | +signal turn_game_started() |
| 8 | +## Emitted when the turn based game stops. |
| 9 | +signal turn_game_stopped() |
| 10 | +## Emitted when a subscriber is added. |
| 11 | +signal subscriber_added(subscriber: TurnSubscriber) |
| 12 | +## Emitted when a subscriber is removed. |
| 13 | +signal subscriber_removed(subscriber: TurnSubscriber) |
| 14 | + |
| 15 | + |
| 16 | +## The current turn. |
| 17 | +var current_turn: int = 0 |
| 18 | +## The current turn subscriber. |
| 19 | +var current_turn_subscriber: TurnSubscriber: |
| 20 | + get: |
| 21 | + if subscribers.size() == 0: |
| 22 | + return null |
| 23 | + |
| 24 | + return subscribers[current_turn] |
| 25 | +## The turn subscribers. |
| 26 | +var subscribers: Array[TurnSubscriber] = [] |
| 27 | + |
| 28 | + |
| 29 | +func _ready() -> void: |
| 30 | + add_to_group("ggs.turnbased") |
| 31 | + |
| 32 | + |
| 33 | +func _sort_subscribers() -> void: |
| 34 | + subscribers.sort_custom(func (a, b): |
| 35 | + return a.priority < b.priority |
| 36 | + ) |
| 37 | + |
| 38 | + |
| 39 | +## Called when a [TurnSubscriber] is subscribed. |
| 40 | +## [br]This is a virtual method |
| 41 | +func _subscriber_added(sub: TurnSubscriber) -> void: |
| 42 | + pass |
| 43 | + |
| 44 | + |
| 45 | +## Called when a [TurnSubscriber] is unsubscribed. |
| 46 | +## [br]This is a virtual method |
| 47 | +func _subscriber_removed(sub: TurnSubscriber) -> void: |
| 48 | + pass |
| 49 | + |
| 50 | + |
| 51 | +## Adds a [TurnSubscriber] |
| 52 | +func add_subscriber(sub: TurnSubscriber) -> bool: |
| 53 | + if subscribers.has(sub): |
| 54 | + return false |
| 55 | + |
| 56 | + subscribers.append(sub) |
| 57 | + subscriber_added.emit(sub) |
| 58 | + |
| 59 | + _sort_subscribers() |
| 60 | + |
| 61 | + return true |
| 62 | + |
| 63 | + |
| 64 | +## Ends the current turn |
| 65 | +func end_turn_sequence() -> void: |
| 66 | + if subscribers.size() == 0: |
| 67 | + return |
| 68 | + |
| 69 | + subscribers[current_turn].end_turn() |
| 70 | + |
| 71 | + current_turn += 1 |
| 72 | + |
| 73 | + if current_turn >= subscribers.size(): |
| 74 | + current_turn = 0 |
| 75 | + |
| 76 | + subscribers[current_turn].turn_started.emit() |
| 77 | + subscribers[current_turn]._turn_started() |
| 78 | + |
| 79 | + turn_game_stopped.emit() |
| 80 | + |
| 81 | + |
| 82 | +## Calls next turn |
| 83 | +func next_turn() -> void: |
| 84 | + var subscribers_count := subscribers.size() |
| 85 | + |
| 86 | + if current_turn < subscribers_count: |
| 87 | + subscribers[current_turn].turn_ended.emit() |
| 88 | + subscribers[current_turn]._turn_ended() |
| 89 | + |
| 90 | + current_turn += 1 |
| 91 | + |
| 92 | + if current_turn >= subscribers_count: |
| 93 | + current_turn = 0 |
| 94 | + |
| 95 | + subscribers[current_turn].turn_started.emit() |
| 96 | + subscribers[current_turn]._turn_started() |
| 97 | + |
| 98 | + |
| 99 | +## Removes a [TurnSubscriber] |
| 100 | +func remove_subscriber(sub: TurnSubscriber) -> bool: |
| 101 | + if not subscribers.has(sub): |
| 102 | + return false |
| 103 | + |
| 104 | + var sub_turn_index = subscribers.find(func (x): return x == sub) |
| 105 | + |
| 106 | + subscribers.remove_at(sub_turn_index) |
| 107 | + subscriber_removed.emit(sub) |
| 108 | + |
| 109 | + _sort_subscribers() |
| 110 | + |
| 111 | + return true |
| 112 | + |
| 113 | + |
| 114 | +## Starts the turn based game. |
| 115 | +func start_turn_sequence() -> void: |
| 116 | + if subscribers.size() == 0: |
| 117 | + return |
| 118 | + |
| 119 | + current_turn = 0 |
| 120 | + |
| 121 | + subscribers[current_turn]._turn_started() |
| 122 | + subscribers[current_turn].turn_started.emit() |
| 123 | + |
| 124 | + turn_game_started.emit() |
0 commit comments