-
Notifications
You must be signed in to change notification settings - Fork 0
/
enemy_turn_manager.gd
43 lines (34 loc) · 1.1 KB
/
enemy_turn_manager.gd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
extends RefCounted
class_name EnemyTurnManager
var fresh = false
var current_thread = Thread.new()
var enemy_turn: EnemyTurn
var map_manager: MapManager
var effects_node: Node
signal calculated(damage_taken: Array)
signal invalidated
func initialize(map_manager: MapManager, effects_node: Node):
self.map_manager = map_manager
self.effects_node = effects_node
func execute_moves(map: MapManager):
assert(fresh)
await enemy_turn.execute_moves(map, effects_node)
func update():
fresh = false
invalidated.emit()
if current_thread.is_alive():
enemy_turn.abort()
current_thread = Thread.new()
enemy_turn = EnemyTurn.new(map_manager)
current_thread.start(_async_enemy_turn.bind(current_thread))
func _async_enemy_turn(thread: Thread):
var result = enemy_turn.calculate()
_wait_enemy_turn_completed.bind(thread).call_deferred()
return result
func _wait_enemy_turn_completed(thread: Thread):
var thread_id = thread.get_id()
var current_thread_id = current_thread.get_id()
var result = thread.wait_to_finish()
if thread_id == current_thread_id:
fresh = result
calculated.emit(enemy_turn.damage_taken)