1
1
use bevy:: prelude:: * ;
2
2
use bevy_mod_picking:: prelude:: * ;
3
3
4
+ use crate :: { ai, movement, units:: * } ;
4
5
use crate :: ai:: AICache ;
5
6
use crate :: cell:: * ;
6
7
use crate :: movement:: GameMove ;
7
8
use crate :: scene:: { self , MainCube , SceneChild } ;
8
- use crate :: { ai, movement, units:: * } ;
9
9
10
10
#[ derive( Resource , Debug ) ]
11
11
pub ( crate ) struct Game {
12
12
pub ( crate ) board : Board ,
13
13
pub ( crate ) units : Units ,
14
14
pub ( crate ) selected_cell : Option < CellCoordinates > ,
15
- pub ( crate ) phase : GamePhase ,
16
- pub ( crate ) stored_units : Vec < Unit > ,
17
15
pub ( crate ) turn : Team ,
18
16
pub ( crate ) entities_to_move : Vec < ( Entity , CellCoordinates ) > ,
19
17
pub ( crate ) palette : Palette ,
@@ -26,8 +24,6 @@ impl Game {
26
24
board : Board :: new ( cube_side_length) ,
27
25
units : Units :: game_starting_configuration ( cube_side_length) ,
28
26
selected_cell : None ,
29
- phase : GamePhase :: PlaceUnits ,
30
- stored_units : vec ! [ ] ,
31
27
turn : Team :: White ,
32
28
entities_to_move : Vec :: new ( ) ,
33
29
palette : Palette :: Pinkish ,
@@ -93,79 +89,39 @@ impl Team {
93
89
}
94
90
}
95
91
96
- #[ derive( PartialEq , Debug ) ]
97
- pub ( crate ) enum GamePhase {
98
- PlaceUnits ,
99
- Play ,
100
- }
101
-
102
92
pub ( crate ) fn on_cell_clicked (
103
93
mut click_events : EventReader < Pointer < Click > > ,
104
- mut query : Query < ( Option < & MainCube > , & mut Transform ) > ,
94
+ query : Query < ( Option < & MainCube > , & mut Transform ) > ,
105
95
mut game : ResMut < Game > ,
106
96
mut commands : Commands ,
107
97
) {
108
98
let game = & mut * game;
109
- for click in click_events. read ( ) {
110
- match game. phase {
111
- GamePhase :: Play => {
112
- on_cell_clicked_play_phase ( click. target , & mut query, game, & mut commands)
113
- }
114
- GamePhase :: PlaceUnits => {
115
- on_cell_clicked_place_units_phase ( click. target , & mut query, game)
116
- }
117
- }
118
- }
119
- }
120
-
121
- fn on_cell_clicked_place_units_phase (
122
- target : Entity ,
123
- query : & mut Query < ( Option < & MainCube > , & mut Transform ) > ,
124
- game : & mut Game ,
125
- ) {
126
- let game = & mut * game; // Convert game to normal rust reference for partial borrow
127
- let cell_clicked = query. get ( target) ;
128
- let coords;
129
- if let Ok ( cell_clicked) = cell_clicked {
130
- if cell_clicked. 0 . is_none ( ) {
131
- // Didn't click a part of the cube
132
- return ;
133
- }
134
- coords = cell_clicked. 0 . unwrap ( ) . coords ;
135
- } else {
99
+ let Some ( click_event) = click_events. read ( ) . next ( ) else {
136
100
return ;
101
+ } ;
102
+ let mut target = click_event. target ;
103
+ for click_event in click_events. read ( ) {
104
+ target = click_event. target ;
137
105
}
138
-
139
- if game. units . get_unit ( coords) . is_none ( ) {
140
- if let Some ( mut unit) = game. stored_units . pop ( ) {
141
- unit. coords = coords;
142
- game. units . add_unit ( unit) ;
143
- }
144
- }
145
- if game. stored_units . is_empty ( ) {
146
- game. phase = GamePhase :: Play ;
147
- }
106
+ on_cell_clicked_internal ( target, & query, game, & mut commands)
148
107
}
149
108
150
- fn on_cell_clicked_play_phase (
109
+ fn on_cell_clicked_internal (
151
110
target : Entity ,
152
- query : & mut Query < ( Option < & MainCube > , & mut Transform ) > ,
111
+ query : & Query < ( Option < & MainCube > , & mut Transform ) > ,
153
112
game : & mut Game ,
154
113
commands : & mut Commands ,
155
114
) {
156
- let cell_clicked = query. get ( target) ;
157
- let clicked_coords;
158
- if let Ok ( cell_clicked) = cell_clicked {
159
- if cell_clicked. 0 . is_none ( ) {
160
- // Didn't click a part of the cube
161
- game. selected_cell = None ;
162
- reset_cells_new_selection ( game) ;
163
- return ;
164
- }
165
- clicked_coords = cell_clicked. 0 . unwrap ( ) . coords ;
166
- } else {
115
+ let Ok ( cell_clicked) = query. get ( target) else {
116
+ return ;
117
+ } ;
118
+ if cell_clicked. 0 . is_none ( ) {
119
+ // Didn't click a part of the cube
120
+ game. selected_cell = None ;
121
+ reset_cells_new_selection ( game) ;
167
122
return ;
168
123
}
124
+ let clicked_coords = cell_clicked. 0 . unwrap ( ) . coords ;
169
125
170
126
let old_selected_cell = game. selected_cell ;
171
127
game. selected_cell = Some ( clicked_coords) ;
@@ -271,28 +227,26 @@ pub(crate) fn spawn_unit_entity(
271
227
272
228
pub ( crate ) fn on_unit_clicked (
273
229
mut click_events : EventReader < Pointer < Click > > ,
274
- mut query : Query < ( Option < & MainCube > , & mut Transform ) > ,
230
+ query : Query < ( Option < & MainCube > , & mut Transform ) > ,
275
231
scene_child_query : Query < & SceneChild > ,
276
232
mut game : ResMut < Game > ,
277
233
mut commands : Commands ,
278
234
) {
279
235
let game = & mut * game;
280
236
for click in click_events. read ( ) {
281
- if game. phase == GamePhase :: Play {
282
- let result = scene_child_query. get ( click. target ) ;
283
- let Ok ( scene_child) = result else {
284
- warn ! ( "Unit that was clicked on has disappeared" ) ;
285
- return ;
286
- } ;
287
- if let Some ( unit) = game. units . get_unit_from_entity ( scene_child. parent_entity ) {
288
- if let Some ( cell) = game. board . get_cell ( unit. coords ) {
289
- on_cell_clicked_play_phase ( cell. plane , & mut query, game, & mut commands) ;
290
- } else {
291
- warn ! ( "Cell is None" ) ;
292
- }
237
+ let result = scene_child_query. get ( click. target ) ;
238
+ let Ok ( scene_child) = result else {
239
+ warn ! ( "Unit that was clicked on has disappeared" ) ;
240
+ return ;
241
+ } ;
242
+ if let Some ( unit) = game. units . get_unit_from_entity ( scene_child. parent_entity ) {
243
+ if let Some ( cell) = game. board . get_cell ( unit. coords ) {
244
+ on_cell_clicked_internal ( cell. plane , & query, game, & mut commands) ;
293
245
} else {
294
- warn ! ( "Unit is None" ) ;
246
+ warn ! ( "Cell is None" ) ;
295
247
}
248
+ } else {
249
+ warn ! ( "Unit is None" ) ;
296
250
}
297
251
}
298
252
}
0 commit comments