|
|
|
|
@ -13,14 +13,20 @@ impl Plugin for Display3dPlugin {
|
|
|
|
|
)
|
|
|
|
|
.add_systems(
|
|
|
|
|
Update,
|
|
|
|
|
gizmo_system
|
|
|
|
|
.run_if(in_state(GameState::Display3d))
|
|
|
|
|
.run_if(resource_exists::<debug::DebugEnabled>()),
|
|
|
|
|
update_board
|
|
|
|
|
// .run_if(in_state(GameState::Display3d))
|
|
|
|
|
.run_if(resource_changed::<game::Board>()),
|
|
|
|
|
)
|
|
|
|
|
.add_systems(
|
|
|
|
|
Update,
|
|
|
|
|
set_piece_position.run_if(in_state(GameState::Display3d)),
|
|
|
|
|
)
|
|
|
|
|
.add_systems(
|
|
|
|
|
Update,
|
|
|
|
|
gizmo_system
|
|
|
|
|
.run_if(in_state(GameState::Display3d))
|
|
|
|
|
.run_if(resource_exists::<debug::DebugEnabled>()),
|
|
|
|
|
)
|
|
|
|
|
.add_systems(OnEnter(GameState::Display3d), activate)
|
|
|
|
|
.add_systems(OnExit(GameState::Display3d), deactivate);
|
|
|
|
|
}
|
|
|
|
|
@ -124,11 +130,13 @@ fn initialize_board(
|
|
|
|
|
},
|
|
|
|
|
))
|
|
|
|
|
.with_children(|parent| {
|
|
|
|
|
// TODO: Add this to a reactive system when board is added
|
|
|
|
|
board
|
|
|
|
|
.unwrap()
|
|
|
|
|
.pieces()
|
|
|
|
|
.into_iter()
|
|
|
|
|
.for_each(|(index, piece)| {
|
|
|
|
|
// TODO: Move this to a reactive system like index -> Transform
|
|
|
|
|
let scene = match piece {
|
|
|
|
|
game::Piece::Pawn => gltf.named_scenes.get("Pawn"),
|
|
|
|
|
game::Piece::Drone => gltf.named_scenes.get("Drone"),
|
|
|
|
|
@ -137,13 +145,98 @@ fn initialize_board(
|
|
|
|
|
.expect("Game board model")
|
|
|
|
|
.clone();
|
|
|
|
|
|
|
|
|
|
parent.spawn((Piece3d, index, SceneBundle { scene, ..default() }));
|
|
|
|
|
parent.spawn((
|
|
|
|
|
Piece3d,
|
|
|
|
|
piece,
|
|
|
|
|
index,
|
|
|
|
|
SceneBundle { scene, ..default() },
|
|
|
|
|
));
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Update the location of pieces on the 3d board
|
|
|
|
|
fn update_board(
|
|
|
|
|
board: Option<Res<game::Board>>,
|
|
|
|
|
mut pieces: Query<(Entity, &mut BoardIndex, &game::Piece), With<Piece3d>>,
|
|
|
|
|
mut commands: Commands,
|
|
|
|
|
) {
|
|
|
|
|
if let Some(board) = board {
|
|
|
|
|
let board_pieces = board.pieces();
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
// Get a list of all Queen piece entities
|
|
|
|
|
let mut queen_entities = pieces
|
|
|
|
|
.iter_mut()
|
|
|
|
|
.filter_map(|(entity, idx, p)| (*p == game::Piece::Queen).then_some((entity, idx)));
|
|
|
|
|
|
|
|
|
|
// Compare each board piece with an entity piece
|
|
|
|
|
// Set the board index for this particular queen
|
|
|
|
|
board_pieces
|
|
|
|
|
.iter()
|
|
|
|
|
.filter_map(|(index, p)| (*p == game::Piece::Queen).then_some(index))
|
|
|
|
|
.for_each(|index| {
|
|
|
|
|
if let Some((_, mut idx)) = queen_entities.next() {
|
|
|
|
|
*idx = index.clone();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
// If we have one left that means this queen has been "destroyed" so we remove the
|
|
|
|
|
// BoardIndex component from the entitiy, removing it from play.
|
|
|
|
|
if let Some((entity, _)) = queen_entities.next() {
|
|
|
|
|
commands.entity(entity).remove::<BoardIndex>();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
// Get a list of all drone piece entities
|
|
|
|
|
let mut drone_entities = pieces
|
|
|
|
|
.iter_mut()
|
|
|
|
|
.filter_map(|(entity, idx, p)| (*p == game::Piece::Drone).then_some((entity, idx)));
|
|
|
|
|
|
|
|
|
|
// Compare each board piece with an entity piece
|
|
|
|
|
// Set the board index for this particular drone
|
|
|
|
|
board_pieces
|
|
|
|
|
.iter()
|
|
|
|
|
.filter_map(|(index, p)| (*p == game::Piece::Drone).then_some(index))
|
|
|
|
|
.for_each(|index| {
|
|
|
|
|
if let Some((_, mut idx)) = drone_entities.next() {
|
|
|
|
|
*idx = index.clone();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
// If we have one left that means this drone has been "destroyed" so we remove the
|
|
|
|
|
// BoardIndex component from the entitiy, removing it from play.
|
|
|
|
|
if let Some((entity, _)) = drone_entities.next() {
|
|
|
|
|
commands.entity(entity).remove::<BoardIndex>();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
// Get a list of all pawn piece entities
|
|
|
|
|
let mut pawn_entities = pieces
|
|
|
|
|
.iter_mut()
|
|
|
|
|
.filter_map(|(entity, idx, p)| (*p == game::Piece::Pawn).then_some((entity, idx)));
|
|
|
|
|
|
|
|
|
|
// Compare each board piece with an entity piece
|
|
|
|
|
// Set the board index for this particular pawn
|
|
|
|
|
board_pieces
|
|
|
|
|
.iter()
|
|
|
|
|
.filter_map(|(index, p)| (*p == game::Piece::Pawn).then_some(index))
|
|
|
|
|
.for_each(|index| {
|
|
|
|
|
if let Some((_, mut idx)) = pawn_entities.next() {
|
|
|
|
|
*idx = index.clone();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
// If we have one left that means this pawn has been "destroyed" so we remove the
|
|
|
|
|
// BoardIndex component from the entitiy, removing it from play.
|
|
|
|
|
if let Some((entity, _)) = pawn_entities.next() {
|
|
|
|
|
commands.entity(entity).remove::<BoardIndex>();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Sets a piece location given it's board index
|
|
|
|
|
fn set_piece_position(
|
|
|
|
|
mut events: Query<(&mut Transform, &BoardIndex), (With<Piece3d>, Changed<BoardIndex>)>,
|
|
|
|
|
|