diff --git a/assets/models/Martian Chess.glb b/assets/models/Martian Chess.glb index 1c30f34..3b2fec4 100644 --- a/assets/models/Martian Chess.glb +++ b/assets/models/Martian Chess.glb @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:c8c01fbc805d5f9fa6250ac87c69c3864d05c872f6dff742fb1062f4dcea815d -size 30079836 +oid sha256:aca7bf25047e219de0284ccf750a94f21169a96481e022696cb45d8f32a0fb56 +size 30079816 diff --git a/src/display3d.rs b/src/display3d.rs index 2a0a221..41bb7f0 100644 --- a/src/display3d.rs +++ b/src/display3d.rs @@ -1,5 +1,5 @@ use crate::{ - game::{Board, BoardIndex, Piece, Side}, + game::{Board, BoardIndex, Piece, Side, BoardComponent}, prelude::*, }; use bevy::{ @@ -40,6 +40,9 @@ impl Plugin for Display3dPlugin { switch_sides .run_if(in_state(GameState::Play)) .run_if(state_changed::()), + create_valid_move_entity.run_if(any_component_added::), + remove_valid_move_entity.run_if(any_component_removed::()), + set_valid_move_model.run_if(any_component_added::), ), ) .add_systems( @@ -562,6 +565,59 @@ fn moves_gizmo( }); } +/// Spawn 3d "Valid move" indicators when a piece is selected +/// Another system registers these new entities and associates the correct models and plays animations. +fn create_valid_move_entity( + events: Query<&BoardIndex, (With, Added, With)>, + board: Res, + root: Query>, + mut commands: Commands, +) { + events.iter().for_each(|idx| { + if let Ok(board_entity) = root.get_single() { + board + .valid_moves(*idx) + .iter() + .map(|i| Transform::from_translation(board_translation(i))) + .for_each(|t| { + commands.entity(board_entity).with_children(|parent| { + parent.spawn((Display3d, game::ValidMove, SceneBundle { ..default() })).insert(t); + }); + }); + } + }); +} + +fn set_valid_move_model( + mut events: Query<&mut Handle, (With, Added)>, + gltfs: Res>, + assets_map: Res, +) { + if let Some(gltf) = gltfs.get(&assets_map.models) { + events.iter_mut().for_each(|mut handle| *handle = gltf.named_scenes.get("Valid Move Spot").unwrap().clone()) + } +} + +fn play_valid_move_animation( + players: Query<&AnimationPlayer>, +) { + todo!(); +} + +// TODO: Move this to game.rs +/// Remove "Valid Move" indicators when a piece is de-selected +fn remove_valid_move_entity( + mut events: RemovedComponents, + valid_moves: Query>, + mut commands: Commands, +) { + events.iter().for_each(|_| { + valid_moves.iter().for_each(|entity| { + commands.entity(entity).despawn_recursive(); + }); + }); +} + fn pick_up( mut events: Query< (Entity, &game::Piece), diff --git a/src/game.rs b/src/game.rs index da3d1da..e40a486 100644 --- a/src/game.rs +++ b/src/game.rs @@ -80,6 +80,9 @@ pub(crate) fn tiles() -> impl Iterator { #[derive(Debug, Component)] pub(crate) struct BoardComponent; +#[derive(Debug, Component)] +pub(crate) struct ValidMove; + #[derive(Debug, Component)] pub(crate) struct Captured;