From f32d462df6012b620defe99261c2310a2258415d Mon Sep 17 00:00:00 2001 From: Elijah Voigt Date: Sat, 28 Oct 2023 11:29:53 -0700 Subject: [PATCH] Added hitboxes --- src/display2d.rs | 11 ++-------- src/display3d.rs | 55 +++++++++++++++++++++++++++++++++++++----------- src/game.rs | 13 ++++++++++++ 3 files changed, 58 insertions(+), 21 deletions(-) diff --git a/src/display2d.rs b/src/display2d.rs index 67e1365..13493ae 100644 --- a/src/display2d.rs +++ b/src/display2d.rs @@ -156,16 +156,9 @@ fn initialize_board(board: Option>, 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)| { diff --git a/src/display3d.rs b/src/display3d.rs index 00a4f4b..aae1a63 100644 --- a/src/display3d.rs +++ b/src/display3d.rs @@ -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::), set_board_model.run_if(any_component_added::), + set_tile_hitbox.run_if(any_component_added::), set_piece_position.run_if(any_component_changed::), set_piece_texture.run_if(any_component_changed::), select_3d @@ -66,15 +67,32 @@ pub(crate) struct Display3d; struct AssetsMap { models: Handle, skybox: Handle, + hitbox_shape: Handle, + hitbox_material: Handle, } /// 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, mut commands: Commands) { +fn load_assets( + server: Res, + mut commands: Commands, + mut meshes: ResMut>, + mut materials: ResMut>, +) { + 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, assets: Res, Added)>, +) { + events.iter_mut().for_each(|(mut transform, index)| { + *transform = Transform::from_translation(board_translation(index)); + }); +} diff --git a/src/game.rs b/src/game.rs index da1d8b0..8afa5c6 100644 --- a/src/game.rs +++ b/src/game.rs @@ -39,6 +39,19 @@ pub(crate) enum Tile { Light, } +pub(crate) fn tiles() -> impl Iterator { + (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;