|
|
|
|
@ -1,4 +1,4 @@
|
|
|
|
|
use crate::prelude::*;
|
|
|
|
|
use crate::{game::BoardIndex, prelude::*};
|
|
|
|
|
|
|
|
|
|
pub(crate) struct Display3dPlugin;
|
|
|
|
|
|
|
|
|
|
@ -11,7 +11,16 @@ impl Plugin for Display3dPlugin {
|
|
|
|
|
Update,
|
|
|
|
|
menu::exit_to_menu.run_if(in_state(GameState::Display3d)),
|
|
|
|
|
)
|
|
|
|
|
.add_systems(Update, gizmo_system.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(
|
|
|
|
|
Update,
|
|
|
|
|
set_piece_position.run_if(in_state(GameState::Display3d)),
|
|
|
|
|
)
|
|
|
|
|
.add_systems(OnEnter(GameState::Display3d), activate)
|
|
|
|
|
.add_systems(OnExit(GameState::Display3d), deactivate);
|
|
|
|
|
}
|
|
|
|
|
@ -20,6 +29,9 @@ impl Plugin for Display3dPlugin {
|
|
|
|
|
#[derive(Debug, Component)]
|
|
|
|
|
struct Board3d;
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Component)]
|
|
|
|
|
struct Piece3d;
|
|
|
|
|
|
|
|
|
|
fn initialize_camera(mut commands: Commands) {
|
|
|
|
|
commands.spawn((
|
|
|
|
|
Camera3dBundle {
|
|
|
|
|
@ -28,7 +40,11 @@ fn initialize_camera(mut commands: Commands) {
|
|
|
|
|
hdr: true,
|
|
|
|
|
..default()
|
|
|
|
|
},
|
|
|
|
|
transform: Transform::from_xyz(0., 1.5, 6.).looking_at(Vec3::ZERO, Vec3::Y),
|
|
|
|
|
projection: Projection::Orthographic(OrthographicProjection {
|
|
|
|
|
scale: 0.02,
|
|
|
|
|
..default()
|
|
|
|
|
}),
|
|
|
|
|
transform: Transform::from_xyz(0.0, 20.0, 10.0).looking_at(Vec3::ZERO, Vec3::Y),
|
|
|
|
|
..default()
|
|
|
|
|
},
|
|
|
|
|
UiCameraConfig { show_ui: true },
|
|
|
|
|
@ -64,6 +80,7 @@ fn initialize_board(
|
|
|
|
|
mut commands: Commands,
|
|
|
|
|
model_file: Option<Res<ModelsFile>>,
|
|
|
|
|
gltfs: Res<Assets<Gltf>>,
|
|
|
|
|
board: Option<Res<game::Board>>,
|
|
|
|
|
) {
|
|
|
|
|
info!("Initializing board");
|
|
|
|
|
if let Some(mf) = model_file {
|
|
|
|
|
@ -94,32 +111,57 @@ fn initialize_board(
|
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
info!("Intializeing 3D Board!");
|
|
|
|
|
parent.spawn((
|
|
|
|
|
Board3d,
|
|
|
|
|
SceneBundle {
|
|
|
|
|
scene: gltf
|
|
|
|
|
.named_scenes
|
|
|
|
|
.get("Gameboard")
|
|
|
|
|
.expect("Game board model")
|
|
|
|
|
.clone(),
|
|
|
|
|
..default()
|
|
|
|
|
},
|
|
|
|
|
));
|
|
|
|
|
parent
|
|
|
|
|
.spawn((
|
|
|
|
|
Board3d,
|
|
|
|
|
SceneBundle {
|
|
|
|
|
scene: gltf
|
|
|
|
|
.named_scenes
|
|
|
|
|
.get("Gameboard")
|
|
|
|
|
.expect("Game board model")
|
|
|
|
|
.clone(),
|
|
|
|
|
..default()
|
|
|
|
|
},
|
|
|
|
|
))
|
|
|
|
|
.with_children(|parent| {
|
|
|
|
|
board
|
|
|
|
|
.unwrap()
|
|
|
|
|
.pieces()
|
|
|
|
|
.into_iter()
|
|
|
|
|
.for_each(|(index, piece)| {
|
|
|
|
|
let scene = match piece {
|
|
|
|
|
game::Piece::Pawn => gltf.named_scenes.get("Pawn"),
|
|
|
|
|
game::Piece::Drone => gltf.named_scenes.get("Drone"),
|
|
|
|
|
game::Piece::Queen => gltf.named_scenes.get("Queen"),
|
|
|
|
|
}
|
|
|
|
|
.expect("Game board model")
|
|
|
|
|
.clone();
|
|
|
|
|
|
|
|
|
|
parent.spawn((Piece3d, index, SceneBundle { scene, ..default() }));
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Sets a piece location given it's board index
|
|
|
|
|
fn set_piece_position(
|
|
|
|
|
mut events: Query<(&mut Transform, &BoardIndex), (With<Piece3d>, Changed<BoardIndex>)>,
|
|
|
|
|
) {
|
|
|
|
|
events.iter_mut().for_each(|(mut t, i)| {
|
|
|
|
|
t.translation = board_translation(i);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Make this the active state
|
|
|
|
|
fn activate(
|
|
|
|
|
mut cameras: Query<&mut Camera, With<Camera3d>>,
|
|
|
|
|
mut boards: Query<&mut Visibility, With<Board3d>>,
|
|
|
|
|
) {
|
|
|
|
|
cameras.iter_mut().for_each(|mut camera| {
|
|
|
|
|
info!("Activating 3d camera");
|
|
|
|
|
camera.is_active = true;
|
|
|
|
|
});
|
|
|
|
|
boards.iter_mut().for_each(|mut visibility| {
|
|
|
|
|
info!("Making entities visible");
|
|
|
|
|
*visibility = Visibility::Visible;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
@ -130,18 +172,39 @@ fn deactivate(
|
|
|
|
|
mut boards: Query<&mut Visibility, With<Board3d>>,
|
|
|
|
|
) {
|
|
|
|
|
cameras.iter_mut().for_each(|mut camera| {
|
|
|
|
|
info!("Deactivating 3d camera");
|
|
|
|
|
camera.is_active = false;
|
|
|
|
|
});
|
|
|
|
|
boards.iter_mut().for_each(|mut visibility| {
|
|
|
|
|
info!("Making entities visible");
|
|
|
|
|
*visibility = Visibility::Hidden;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Given a board index returns the Vec3 location in space
|
|
|
|
|
fn board_translation(&BoardIndex { x, y }: &BoardIndex) -> Vec3 {
|
|
|
|
|
// Scale x down by 4 to account for -4..4 scaling
|
|
|
|
|
let x = x as i8 - 4;
|
|
|
|
|
// Mirror y axis because our board index is inverted...
|
|
|
|
|
let y = -(y as i8) + 1;
|
|
|
|
|
|
|
|
|
|
let x = if x < 0 {
|
|
|
|
|
x as f32 * 1.3 + 0.325
|
|
|
|
|
} else {
|
|
|
|
|
x as f32 * 1.3 + 1.0
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let y = y as f32 * 1.3 + 0.65;
|
|
|
|
|
|
|
|
|
|
Vec3::new(x, 0.0, y)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn gizmo_system(mut gizmos: Gizmos) {
|
|
|
|
|
gizmos.cuboid(
|
|
|
|
|
Transform::from_translation(Vec3::Y * 0.5).with_scale(Vec3::splat(1.)),
|
|
|
|
|
Color::WHITE,
|
|
|
|
|
);
|
|
|
|
|
for y in 0..4 {
|
|
|
|
|
for x in 0..8 {
|
|
|
|
|
gizmos.cuboid(
|
|
|
|
|
Transform::from_translation(board_translation(&BoardIndex { x, y }))
|
|
|
|
|
.with_scale(Vec3::splat(1.25)),
|
|
|
|
|
Color::PURPLE,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|