@@ -2,18 +2,36 @@ extends CanvasLayer
22
33var _active_arena : CombatArena = null
44
5+ # Keep track of what music track was playing previously, and return to it once combat has finished.
6+ var _previous_music_track : AudioStream = null
7+
58@onready var _combat_containter : = $ CenterContainer as CenterContainer
69
710
811func _ready () -> void :
9- CombatEvents .combat_initiated .connect (_on_combat_initiated )
10- CombatEvents .combat_finished .connect (_on_combat_finished )
12+ FieldEvents .combat_triggered .connect (start )
13+
14+ # TODO: remove with _unhandled input once Battlers have been implemented.
15+ set_process_unhandled_input (false )
1116
1217
13- func _on_combat_initiated (arena : PackedScene ) -> void :
14- # Don't start a new combat if one is currently ongoing.
15- if _active_arena :
16- return
18+ # TODO: This is included to allow leaving the combat state.
19+ # In future releases, these signals will be emitted once battlers from one side have fallen.
20+ func _unhandled_input (event : InputEvent ) -> void :
21+ if event .is_action_released ("back" ):
22+ CombatEvents .did_player_win_last_combat = false
23+ finish ()
24+
25+ elif event .is_action_released ("interact" ):
26+ CombatEvents .did_player_win_last_combat = true
27+ finish ()
28+
29+
30+ func start (arena : PackedScene ) -> void :
31+ assert (not _active_arena , "Attempting to start a combat when one is ongoing!" )
32+
33+ # Cover the screen.
34+ await Transition .cover (0.2 )
1735
1836 # Try to setup the combat arena (which comes with AI battlers, etc.).
1937 var new_arena : = arena .instantiate ()
@@ -22,11 +40,39 @@ func _on_combat_initiated(arena: PackedScene) -> void:
2240
2341 _active_arena = new_arena
2442 _combat_containter .add_child (_active_arena )
43+
44+ _previous_music_track = Music .get_playing_track ()
45+ Music .play (_active_arena .music )
46+
47+ # Let other systems know that the combat state is setup and ready to begin.
48+ CombatEvents .combat_initiated .emit ()
49+
50+ # Before starting combat itself, reveal the screen again.
51+ # The Transition.clear() call is deferred since it follows on the heels of cover(), and needs a
52+ # frame to allow everything else to respond to Transition.finished.
53+ Transition .clear .call_deferred (0.2 )
54+ await Transition .finished
55+
56+ # TODO: remove with _unhandled input once Battlers have been implemented.
57+ set_process_unhandled_input (true )
2558
2659
27- func _on_combat_finished () -> void :
60+ func finish () -> void :
61+ # TODO: remove with _unhandled input once Battlers have been implemented.
62+ set_process_unhandled_input (false )
63+
2864 if not _active_arena :
2965 return
3066
67+ # Cover the screen again, transitioning away from the combat game state.
68+ await Transition .cover (0.2 )
69+
3170 _active_arena .queue_free ()
3271 _active_arena = null
72+
73+ # Signal that the combat has been finished and all combat objects have been dealt with
74+ # accordingly. The field game 'state' will now be in focus.
75+ # Note that whatever object started the combat will now be responsible for flow of the game. In
76+ # particular, the screen is still covered, so the combat-starting object will want to decide
77+ # what to do now that the outcome of the combat is known.
78+ CombatEvents .combat_finished .emit ()
0 commit comments