Skip to content

Commit 23e6c30

Browse files
committed
Remove game phases. Fixes #6
Units can now be clicked without clicking on the board the first time
1 parent 5e5525b commit 23e6c30

File tree

1 file changed

+30
-76
lines changed

1 file changed

+30
-76
lines changed

src/gamemanager.rs

Lines changed: 30 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
use bevy::prelude::*;
22
use bevy_mod_picking::prelude::*;
33

4+
use crate::{ai, movement, units::*};
45
use crate::ai::AICache;
56
use crate::cell::*;
67
use crate::movement::GameMove;
78
use crate::scene::{self, MainCube, SceneChild};
8-
use crate::{ai, movement, units::*};
99

1010
#[derive(Resource, Debug)]
1111
pub(crate) struct Game {
1212
pub(crate) board: Board,
1313
pub(crate) units: Units,
1414
pub(crate) selected_cell: Option<CellCoordinates>,
15-
pub(crate) phase: GamePhase,
16-
pub(crate) stored_units: Vec<Unit>,
1715
pub(crate) turn: Team,
1816
pub(crate) entities_to_move: Vec<(Entity, CellCoordinates)>,
1917
pub(crate) palette: Palette,
@@ -26,8 +24,6 @@ impl Game {
2624
board: Board::new(cube_side_length),
2725
units: Units::game_starting_configuration(cube_side_length),
2826
selected_cell: None,
29-
phase: GamePhase::PlaceUnits,
30-
stored_units: vec![],
3127
turn: Team::White,
3228
entities_to_move: Vec::new(),
3329
palette: Palette::Pinkish,
@@ -93,79 +89,39 @@ impl Team {
9389
}
9490
}
9591

96-
#[derive(PartialEq, Debug)]
97-
pub(crate) enum GamePhase {
98-
PlaceUnits,
99-
Play,
100-
}
101-
10292
pub(crate) fn on_cell_clicked(
10393
mut click_events: EventReader<Pointer<Click>>,
104-
mut query: Query<(Option<&MainCube>, &mut Transform)>,
94+
query: Query<(Option<&MainCube>, &mut Transform)>,
10595
mut game: ResMut<Game>,
10696
mut commands: Commands,
10797
) {
10898
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 {
136100
return;
101+
};
102+
let mut target = click_event.target;
103+
for click_event in click_events.read() {
104+
target = click_event.target;
137105
}
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)
148107
}
149108

150-
fn on_cell_clicked_play_phase(
109+
fn on_cell_clicked_internal(
151110
target: Entity,
152-
query: &mut Query<(Option<&MainCube>, &mut Transform)>,
111+
query: &Query<(Option<&MainCube>, &mut Transform)>,
153112
game: &mut Game,
154113
commands: &mut Commands,
155114
) {
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);
167122
return;
168123
}
124+
let clicked_coords = cell_clicked.0.unwrap().coords;
169125

170126
let old_selected_cell = game.selected_cell;
171127
game.selected_cell = Some(clicked_coords);
@@ -271,28 +227,26 @@ pub(crate) fn spawn_unit_entity(
271227

272228
pub(crate) fn on_unit_clicked(
273229
mut click_events: EventReader<Pointer<Click>>,
274-
mut query: Query<(Option<&MainCube>, &mut Transform)>,
230+
query: Query<(Option<&MainCube>, &mut Transform)>,
275231
scene_child_query: Query<&SceneChild>,
276232
mut game: ResMut<Game>,
277233
mut commands: Commands,
278234
) {
279235
let game = &mut *game;
280236
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);
293245
} else {
294-
warn!("Unit is None");
246+
warn!("Cell is None");
295247
}
248+
} else {
249+
warn!("Unit is None");
296250
}
297251
}
298252
}

0 commit comments

Comments
 (0)