Refactor: valid move indicators no longer despawn

main
Elijah C. Voigt 2 years ago
parent 8689574e95
commit 63dac42250

@ -56,8 +56,6 @@ impl Plugin for Display3dPlugin {
switch_sides
.run_if(in_state(GameState::Play))
.run_if(state_changed::<game::TurnState>()),
create_valid_move_entity.run_if(any_component_added::<game::Selected>),
remove_valid_move_entity.run_if(any_component_removed::<game::Selected>()),
set_valid_move_model.run_if(any_component_added::<game::ValidMove>),
update_tweaks.run_if(on_event::<AssetEvent<Tweaks>>()),
scale_lighting.run_if(
@ -176,6 +174,20 @@ fn initialize(mut commands: Commands, board: Res<game::Board>, assets: Res<Asset
));
});
// Valid move indicators
game::tiles().for_each(|(index, _)| {
parent.spawn((
Display3d,
index,
SceneBundle {
visibility: Visibility::Hidden,
transform: Transform::from_translation(board_translation(&index)),
..default()
},
game::ValidMove,
));
});
// Pieces
board.pieces().iter().for_each(|(index, piece)| {
let side = Board::side(*index).expect("Spawn valid side");
@ -808,32 +820,6 @@ fn moves_gizmo(
});
}
/// Spawn 3d "Valid move" indicators when a piece is selected
/// Another system registers these new entities and associates the correct models and plays animations.
/// TODO: Do not create/delete entities at runtime
fn create_valid_move_entity(
events: Query<&BoardIndex, (With<game::Piece>, Added<game::Selected>, With<Display3d>)>,
board: Res<Board>,
root: Query<Entity, With<game::BoardComponent>>,
mut commands: Commands,
) {
events.iter().for_each(|idx| {
if let Ok(board_entity) = root.get_single() {
board
.valid_moves(*idx)
.iter()
.map(|i| Transform::from_translation(board_translation(i)))
.for_each(|t| {
commands.entity(board_entity).with_children(|parent| {
parent
.spawn((Display3d, game::ValidMove, SceneBundle { ..default() }))
.insert(t);
});
});
}
});
}
fn set_valid_move_model(
mut events: Query<&mut Handle<Scene>, (With<Display3d>, Added<game::ValidMove>)>,
gltfs: Res<Assets<Gltf>>,
@ -866,20 +852,6 @@ fn _play_valid_move_animation(_players: Query<&AnimationPlayer>) {
todo!();
}
// TODO: Do not create/delete entities at runtime
/// Remove "Valid Move" indicators when a piece is de-selected
fn remove_valid_move_entity(
mut events: RemovedComponents<game::Selected>,
valid_moves: Query<Entity, With<game::ValidMove>>,
mut commands: Commands,
) {
events.read().for_each(|_| {
valid_moves.iter().for_each(|entity| {
commands.entity(entity).despawn_recursive();
});
});
}
fn pick_up(
mut events: Query<
(Entity, &game::Piece),
@ -1378,7 +1350,7 @@ fn capture_piece(
commands.entity(entity).insert(orig.clone());
commands.entity(entity).remove::<Backup<Handle<StandardMaterial>>>();
} else {
warn!("Entity {} does not have original material")
warn!("Entity {:?} does not have original material", entity)
}
}
});

@ -26,6 +26,8 @@ impl Plugin for GamePlugin {
switch_sides.run_if(|input: Res<Input<KeyCode>>| -> bool {
input.just_pressed(KeyCode::N)
}),
show_valid_moves.run_if(any_component_added::<Selected>),
hide_valid_moves.run_if(any_component_removed::<Selected>()),
),
)
.add_systems(
@ -610,3 +612,36 @@ fn switch_sides(state: Res<State<TurnState>>, mut next: ResMut<NextState<TurnSta
TurnState::SideB => next.set(TurnState::SideA),
}
}
/// Spawn "Valid move" indicators when a piece is selected
/// Another system registers these new entities and associates the correct models and plays animations.
fn show_valid_moves(
events: Query<&BoardIndex, (With<Piece>, Added<Selected>)>,
board: Res<Board>,
mut valid_moves: Query<(&BoardIndex, &mut Visibility), With<ValidMove>>,
) {
// When a piece is selected
events.iter().for_each(|idx| {
// Iterate over all ValidMove entities
// For each one with a ValidMove index, make it visible
board.valid_moves(*idx).iter().for_each(|idx| {
valid_moves.iter_mut().for_each(|(i, mut vis)| {
if i == idx {
*vis = Visibility::Inherited;
}
});
})
});
}
/// Hide "Valid Move" indicators when a piece is de-selected
fn hide_valid_moves(
mut events: RemovedComponents<Selected>,
mut valid_moves: Query<&mut Visibility, With<ValidMove>>,
) {
events.read().for_each(|_| {
valid_moves.iter_mut().for_each(|mut visibility| {
*visibility = Visibility::Hidden;
});
});
}

Loading…
Cancel
Save