Added scoring, but having trouble placing defunct pieces

main
Elijah C. Voigt 2 years ago
parent 63dac42250
commit 050dd1d3d1

@ -493,15 +493,15 @@ fn capture_translation(side: &Side, num: usize) -> Vec3 {
info!("Side: {:?} Num: {:?}", side, num); info!("Side: {:?} Num: {:?}", side, num);
match side { match side {
Side::B => { Side::B => {
let x = -((num % 4) as f32 * 1.3 + 4.0); // mod(num, 4) let x = -((num % 4) as f32 * 1.3) - 5.2; // mod(num, 4)
let z = (num / 4) as f32 * 1.3 + 4.0; // floor(div(num, 4)) let z = ((num / 4) as f32 * 1.3) - 5.2; // floor(div(num, 4))
let y = -1.3; let y = -1.3;
info!("Vec3({}, {}, {})", x, y, z); info!("Vec3({}, {}, {})", x, y, z);
Vec3::new(x, y, z) Vec3::new(x, y, z)
}, },
Side::A => { Side::A => {
let x = (num % 4) as f32 * 1.3 + 4.0; // mod(num, 4) let x = ((num % 4) as f32 * 1.3) + 5.2; // mod(num, 4)
let z = -((num / 4) as f32 * 1.3 + 4.0); // floor(div(num, 4)) let z = -((num / 4) as f32 * 1.3) + 5.2; // floor(div(num, 4))
let y = -1.3; let y = -1.3;
info!("Vec3({}, {}, {})", x, y, z); info!("Vec3({}, {}, {})", x, y, z);
Vec3::new(x, y, z) Vec3::new(x, y, z)
@ -1258,6 +1258,7 @@ fn capture_piece(
children: Query<&Children>, children: Query<&Children>,
mut commands: Commands, mut commands: Commands,
time: Res<Time>, time: Res<Time>,
score: Res<game::Score>,
) { ) {
let duration: f32 = 3.0; let duration: f32 = 3.0;
@ -1305,8 +1306,7 @@ fn capture_piece(
// Hide piece now that animation is done // Hide piece now that animation is done
*v = Visibility::Hidden; *v = Visibility::Hidden;
// TODO: Dynamic number based on side's score t.translation = capture_translation(side, score.get(*side));
t.translation = capture_translation(side, 1);
*state = s.next(); *state = s.next();
}, },

@ -10,6 +10,7 @@ impl Plugin for GamePlugin {
app.add_event::<Move>() app.add_event::<Move>()
.add_event::<Selection>() .add_event::<Selection>()
.add_state::<TurnState>() .add_state::<TurnState>()
.insert_resource(Score { ..default() })
.add_systems(Startup, setup_board) .add_systems(Startup, setup_board)
.add_systems( .add_systems(
Update, Update,
@ -28,6 +29,7 @@ impl Plugin for GamePlugin {
}), }),
show_valid_moves.run_if(any_component_added::<Selected>), show_valid_moves.run_if(any_component_added::<Selected>),
hide_valid_moves.run_if(any_component_removed::<Selected>()), hide_valid_moves.run_if(any_component_removed::<Selected>()),
manage_score.run_if(any_component_added::<Captured>),
), ),
) )
.add_systems( .add_systems(
@ -105,6 +107,29 @@ pub(crate) enum GameError {
InvalidMove, InvalidMove,
} }
/// Tracks the score of each side of the game
#[derive(Debug, Resource, Default)]
pub(crate) struct Score {
a: usize,
b: usize,
}
impl Score {
fn increment(&mut self, side: Side) {
match side {
Side::A => self.a += 1,
Side::B => self.b += 1,
}
}
pub(crate) fn get(&self, side: Side) -> usize {
match side {
Side::A => self.a,
Side::B => self.b,
}
}
}
/// The board is setup like this: /// The board is setup like this:
/// ```text /// ```text
/// 0 1 2 3 4 5 6 7 /// 0 1 2 3 4 5 6 7
@ -166,6 +191,17 @@ pub(crate) enum Side {
B, B,
} }
impl std::ops::Not for Side {
type Output = Self;
fn not(self) -> Self::Output {
match self {
Side::A => Side::B,
Side::B => Side::A,
}
}
}
impl Board { impl Board {
/// Returns the piece at the given location /// Returns the piece at the given location
pub(crate) fn at(&self, BoardIndex { x, y }: BoardIndex) -> Option<&Piece> { pub(crate) fn at(&self, BoardIndex { x, y }: BoardIndex) -> Option<&Piece> {
@ -445,13 +481,13 @@ fn debug_board(board: Res<Board>, mut debug_info: ResMut<debug::DebugInfo>) {
pub(crate) fn update_board( pub(crate) fn update_board(
mut audio_events: EventWriter<AudioEvent>, mut audio_events: EventWriter<AudioEvent>,
mut events: EventReader<Move>, mut events: EventReader<Move>,
mut pieces: Query<(Entity, &mut BoardIndex), With<Piece>>, mut pieces: Query<(Entity, &mut BoardIndex, &Side), With<Piece>>,
selected: Query<Entity, With<Selected>>, selected: Query<Entity, With<Selected>>,
mut commands: Commands, mut commands: Commands,
mut played: Local<bool>, mut played: Local<bool>,
) { ) {
events.read().for_each(|Move { from, to, .. }| { events.read().for_each(|Move { from, to, .. }| {
pieces.iter_mut().for_each(|(entity, mut index)| { pieces.iter_mut().for_each(|(entity, mut index, side)| {
if *index == *from { if *index == *from {
match to { match to {
Some(to_idx) => { Some(to_idx) => {
@ -481,6 +517,18 @@ pub(crate) fn update_board(
*played = false; *played = false;
} }
// We only track 3D (hack, for now) to prevent duplicates
fn manage_score(
events: Query<&Side, (Added<Captured>, With<display3d::Display3d>)>,
mut debug_info: ResMut<debug::DebugInfo>,
mut score: ResMut<Score>,
) {
events.iter().for_each(|side| {
score.increment(!*side);
debug_info.set("score".into(), format!("A:{}|B:{}", score.a, score.b));
});
}
pub(crate) fn set_side(mut events: Query<(&mut Side, &BoardIndex), Changed<BoardIndex>>) { pub(crate) fn set_side(mut events: Query<(&mut Side, &BoardIndex), Changed<BoardIndex>>) {
events events
.iter_mut() .iter_mut()

Loading…
Cancel
Save