3d board drawing and gizmos enabled with debug mode

selection-refactor
Elijah Voigt 2 years ago
parent 4dbf524e3f
commit f904945a05

1
.gitattributes vendored

@ -16,3 +16,4 @@ assets/ filter=lfs diff=lfs merge=lfs -text
*.png filter=lfs diff=lfs merge=lfs -text
*.dll filter=lfs diff=lfs merge=lfs -text
*.lib filter=lfs diff=lfs merge=lfs -text
*.gltf filter=lfs diff=lfs merge=lfs -text

BIN
assets/models/Martian Chess.glb (Stored with Git LFS)

Binary file not shown.

@ -197,6 +197,10 @@ fn initialize_board(sprite_sheet: Option<Res<SpriteSheet>>, mut commands: Comman
}
}
/// TODO: YOU DUMB IDIOT
/// JUST RE-DRAW ANY ENTITY WITH A CHANGED BoardIndex!!
/// INITIALIZE THE STARTING BOARD, THEN JUST UPDATE CHANGED BOARDINDEX!!!
/// GOD YOU ARE SO DUMB.
fn draw_board(
board: Option<Res<Board>>,
sprite_sheet: Option<Res<SpriteSheet>>,

@ -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,7 +111,8 @@ fn initialize_board(
));
info!("Intializeing 3D Board!");
parent.spawn((
parent
.spawn((
Board3d,
SceneBundle {
scene: gltf
@ -104,22 +122,46 @@ fn initialize_board(
.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) {
for y in 0..4 {
for x in 0..8 {
gizmos.cuboid(
Transform::from_translation(Vec3::Y * 0.5).with_scale(Vec3::splat(1.)),
Color::WHITE,
);
Transform::from_translation(board_translation(&BoardIndex { x, y }))
.with_scale(Vec3::splat(1.25)),
Color::PURPLE,
)
}
}
}

@ -65,6 +65,7 @@ fn init_menu_ui(mut commands: Commands) {
..default()
},
background_color: Color::NONE.into(),
visibility: Visibility::Hidden,
..default()
},
))
@ -156,7 +157,7 @@ fn handle_menu_start(
events
.iter()
.filter(|&interaction| *interaction == Interaction::Pressed)
.for_each(|_| next_state.set(GameState::Display3d))
.for_each(|_| next_state.set(GameState::Display2d))
}
fn handle_menu_quit(

Loading…
Cancel
Save