Added hitboxes

selection-refactor
Elijah Voigt 2 years ago
parent 8a5375e445
commit f32d462df6

@ -156,16 +156,9 @@ fn initialize_board(board: Option<Res<Board>>, mut commands: Commands) {
))
.with_children(|parent| {
// Spawn tiles
for i in 0..32 {
let x = i % 8;
let y = i / 8;
let index = BoardIndex { x, y };
let s = (x % 2) ^ (y % 2);
let tile = if s == 0 { Tile::Dark } else { Tile::Light };
game::tiles().for_each(|(index, tile)| {
parent.spawn((tile, index, Display2d, SpriteSheetBundle { ..default() }));
}
});
// Spawn pieces
board.pieces().iter().for_each(|(index, piece)| {

@ -28,6 +28,7 @@ impl Plugin for Display3dPlugin {
menu::exit_to_menu.run_if(in_state(GameState::Display3d)),
set_piece_model.run_if(any_component_added::<Piece>),
set_board_model.run_if(any_component_added::<game::BoardComponent>),
set_tile_hitbox.run_if(any_component_added::<game::Tile>),
set_piece_position.run_if(any_component_changed::<BoardIndex>),
set_piece_texture.run_if(any_component_changed::<Side>),
select_3d
@ -66,15 +67,32 @@ pub(crate) struct Display3d;
struct AssetsMap {
models: Handle<Gltf>,
skybox: Handle<Image>,
hitbox_shape: Handle<Mesh>,
hitbox_material: Handle<StandardMaterial>,
}
/// Load 3d models
/// This is kind of pulling double duty.
/// Both loads the GLTF file _and_ populates the ModelMap once that is loaded.
fn load_assets(server: Res<AssetServer>, mut commands: Commands) {
fn load_assets(
server: Res<AssetServer>,
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
let hitbox_shape = meshes.add(shape::Box::new(1.0, 0.1, 1.0).into());
let hitbox_material = materials.add(StandardMaterial {
base_color: Color::NONE,
perceptual_roughness: 0.0,
reflectance: 0.0,
alpha_mode: AlphaMode::Blend,
..default()
});
commands.insert_resource(AssetsMap {
models: server.load("models/Martian Chess.glb"),
skybox: server.load("images/skybox.png"),
hitbox_shape,
hitbox_material,
});
}
@ -129,6 +147,22 @@ fn initialize(mut commands: Commands, board: Res<game::Board>, assets: Res<Asset
parent
.spawn((Display3d, game::BoardComponent, SceneBundle { ..default() }))
.with_children(|parent| {
// Hitboxes
game::tiles().for_each(|(index, tile)| {
parent.spawn((
Display3d,
index,
tile,
PbrBundle {
mesh: assets.hitbox_shape.clone(),
material: assets.hitbox_material.clone(),
visibility: Visibility::Hidden,
..default()
},
));
});
// Pieces
board.pieces().iter().for_each(|(index, piece)| {
let side = Board::side(index).expect("Spawn valid side");
@ -228,7 +262,7 @@ fn gizmo_system(mut gizmos: Gizmos) {
for x in 0..8 {
gizmos.cuboid(
Transform::from_translation(board_translation(&BoardIndex { x, y }))
.with_scale(Vec3::splat(1.25)),
.with_scale(Vec3::new(1.0, 0.1, 1.0)),
Color::PURPLE,
)
}
@ -513,13 +547,10 @@ fn put_down(
})
}
// Animations
// * QueenPickup
// * QueenPutDown
// * QueenIdle
// * DronePickup
// * DronePutDown
// * DroneIdle
// * PawnPickup
// * PawnPutDown
// * PawnIdle
fn set_tile_hitbox(
mut events: Query<(&mut Transform, &BoardIndex), (With<Display3d>, Added<game::Tile>)>,
) {
events.iter_mut().for_each(|(mut transform, index)| {
*transform = Transform::from_translation(board_translation(index));
});
}

@ -39,6 +39,19 @@ pub(crate) enum Tile {
Light,
}
pub(crate) fn tiles() -> impl Iterator<Item = (BoardIndex, Tile)> {
(0..32).map(|i| {
let x = i % 8;
let y = i / 8;
let s = (x % 2) ^ (y % 2);
let index = BoardIndex { x, y };
let tile = if s == 0 { Tile::Dark } else { Tile::Light };
(index, tile)
})
}
#[derive(Debug, Component)]
pub(crate) struct BoardComponent;

Loading…
Cancel
Save