Not sure why, but resetting is not as elegant as I had hoped

This is definitely the right approach, but why is everything becoming
red??
main
Elijah C. Voigt 2 years ago
parent dc981aafea
commit 666609e419

@ -58,6 +58,10 @@ impl DebugInfo {
self.0.get(key)
}
pub fn _clear(&mut self, key: String) {
self.0.remove(&key);
}
pub fn iter(&self) -> Iter<'_, String, String> {
self.0.iter()
}

@ -50,6 +50,7 @@ impl Plugin for Display3dPlugin {
.run_if(on_event::<AssetEvent<Tweaks>>()),
hydrate_camera.run_if(any_component_added::<Camera3d>),
set_piece_model.run_if(any_component_added::<Piece>),
set_piece_model.run_if(any_component_changed::<Piece>),
set_board_model.run_if(any_component_added::<game::BoardComponent>),
set_board_model.run_if(any_component_added::<TilesComponent>),
set_valid_move_model.run_if(any_component_added::<game::ValidMove>),
@ -91,6 +92,7 @@ impl Plugin for Display3dPlugin {
gizmo_system,
selected_gizmo,
moves_gizmo,
debug_selected.run_if(any_with_component::<game::Selected>())
)
.run_if(resource_exists::<debug::DebugEnabled>())
.run_if(in_state(GameState::Play))
@ -390,7 +392,7 @@ fn fix_skybox(
/// Set the model for each piece based on the game::Piece::* marker
fn set_piece_model(
mut events: Query<(&mut Handle<Scene>, &Piece), (Added<game::Piece>, With<Display3d>)>,
mut events: Query<(&mut Handle<Scene>, &Piece), (Or<(Added<game::Piece>, Changed<game::Piece>)>, With<Display3d>)>,
gltfs: Res<Assets<Gltf>>,
tweaks: Res<Assets<Tweaks>>,
tweaks_file: Res<tweak::GameTweaks>,
@ -1480,9 +1482,6 @@ fn capture_piece(
// If we have completed the animation
if *prog >= 1.0 {
// Remove the captured component for bookkeeping
commands.entity(entity).remove::<game::Captured>();
// Move to next state now that animation is done
*state = s.next();
}
@ -1521,3 +1520,12 @@ fn capture_piece(
}
}
}
fn debug_selected(
query: Query<(Entity, &BoardIndex, &Piece, &Side), With<game::Selected>>,
mut debug_info: ResMut<debug::DebugInfo>,
) {
query.iter().for_each(|(e, bi, p, s)| {
debug_info.set("Active".into(), format!("\n>>ID: {:?}\n>>Piece: {:?}\n>>Side: {:?}\n>>Index: {:?}", e, p, s, bi));
});
}

@ -21,7 +21,7 @@ impl Plugin for GamePlugin {
update_board
.run_if(on_event::<Move>())
.after(handle_selection),
set_side.run_if(on_event::<Move>()).after(update_board),
set_side.run_if(any_component_changed::<BoardIndex>),
cancel_place.run_if(|buttons: Res<Input<MouseButton>>| -> bool {
buttons.just_pressed(MouseButton::Right)
}),
@ -30,6 +30,8 @@ impl Plugin for GamePlugin {
hide_valid_moves.run_if(any_component_removed::<Selected>()),
manage_score.run_if(any_component_added::<Captured>),
check_endgame.run_if(resource_changed::<Board>()),
reset_game
.run_if(|keys: Res<Input<KeyCode>>| -> bool { keys.just_pressed(KeyCode::R) }),
),
)
.add_systems(OnEnter(GameState::Endgame), set_endgame)
@ -235,6 +237,55 @@ impl Board {
self.inner[y][x].as_ref()
}
fn new() -> Board {
use Piece::*;
Board {
moves: vec![],
inner: vec![
vec![
Some(Queen),
Some(Queen),
Some(Drone),
None,
None,
None,
None,
None,
],
vec![
Some(Queen),
Some(Drone),
Some(Pawn),
None,
None,
Some(Pawn),
Some(Pawn),
Some(Drone),
],
vec![
Some(Drone),
Some(Pawn),
Some(Pawn),
None,
None,
Some(Pawn),
Some(Drone),
Some(Queen),
],
vec![
None,
None,
None,
None,
None,
Some(Drone),
Some(Queen),
Some(Queen),
],
],
}
}
/// Show all pieces on one side of the board
/// OPTIMIZE: This is only used to tell if a side is empty, so it is more work than we need to do.
pub(crate) fn on(&self, side: Side) -> Vec<(&Piece, BoardIndex)> {
@ -479,52 +530,7 @@ pub(crate) struct Selectable;
pub(crate) struct Selection(pub BoardIndex);
fn setup_board(mut commands: Commands) {
use Piece::*;
commands.insert_resource(Board {
moves: vec![],
inner: vec![
vec![
Some(Queen),
Some(Queen),
Some(Drone),
None,
None,
None,
None,
None,
],
vec![
Some(Queen),
Some(Drone),
Some(Pawn),
None,
None,
Some(Pawn),
Some(Pawn),
Some(Drone),
],
vec![
Some(Drone),
Some(Pawn),
Some(Pawn),
None,
None,
Some(Pawn),
Some(Drone),
Some(Queen),
],
vec![
None,
None,
None,
None,
None,
Some(Drone),
Some(Queen),
Some(Queen),
],
],
});
commands.insert_resource(Board::new());
}
fn debug_board(board: Res<Board>, mut debug_info: ResMut<debug::DebugInfo>) {
@ -657,7 +663,7 @@ fn manage_score(
});
}
pub(crate) fn set_side(mut events: Query<(&mut Side, &BoardIndex), Changed<BoardIndex>>) {
pub(crate) fn set_side(mut events: Query<(&mut Side, &BoardIndex), Or<(Changed<BoardIndex>, Added<BoardIndex>)>>) {
events
.iter_mut()
.for_each(|(mut side, idx)| match Board::side(*idx) {
@ -809,3 +815,23 @@ fn hide_valid_moves(mut indicators: Query<&mut Visibility, With<ValidMove>>) {
*visibility = Visibility::Hidden;
});
}
// Resets the board to the "stock" setup
// Somewhat conflicts with board initialization!
fn reset_game(
pieces: Query<Entity, With<Piece>>,
mut board: ResMut<Board>,
mut score: ResMut<Score>,
mut commands: Commands,
) {
// Setup the board
*board = Board::new();
// Reset the score
*score = Score { ..default() };
// Move all pieces to their correct spots
pieces.iter().zip(board.pieces().iter()).for_each(|(e, (i, p))| {
commands.entity(e).insert((i.clone(), p.clone())).remove::<Captured>();
});
}
Loading…
Cancel
Save