From 44008a5b890a83862f9fac5b5641a1b4188b42b4 Mon Sep 17 00:00:00 2001 From: Elijah Voigt Date: Sun, 29 Dec 2024 22:31:23 -0800 Subject: [PATCH] removing broken new game button for now --- src/menu.rs | 16 +++++++- src/play.rs | 104 ++++++++++++++++++++++++++------------------------- src/setup.rs | 2 +- todo.txt | 3 ++ 4 files changed, 72 insertions(+), 53 deletions(-) diff --git a/src/menu.rs b/src/menu.rs index 2db75c2..a082d6b 100644 --- a/src/menu.rs +++ b/src/menu.rs @@ -8,7 +8,7 @@ use bevy::{ use crate::{ audio, boot, deck::{Card, Deck, ItemColor, ItemNumber, ItemPattern, ItemShape}, - play::{check_for_sets, check_set, DeckOrder, PlayLocation, SetNumber}, + play::{check_for_sets, check_set, DeckOrder, DelayedAnimation, PlayLocation, SetNumber}, view::{button_set_state, ViewState}, }; @@ -90,6 +90,7 @@ fn setup(mut commands: Commands, server: Res) { .observe(button_hover_on) .observe(button_hover_off) .observe(button_set_state(ViewState::Play)); + /* parent .spawn(button_builder(Node::default())) .with_children(|parent| { @@ -99,6 +100,7 @@ fn setup(mut commands: Commands, server: Res) { .observe(button_hover_off) .observe(reset_game) .observe(button_set_state(ViewState::Play)); + */ parent .spawn(button_builder(Node::default())) .with_children(|parent| { @@ -537,12 +539,22 @@ fn reset_game( custom_size: Some(Vec2::new(80.0, 128.0)), ..this }; + let this_transform = + Transform::default().with_translation(Vec3::new(-400.0, -200.0, 0.0)); let order = DeckOrder(i as u8); commands .entity(entity) .remove::() .remove::() - .insert((this_sprite, this_card, order, Visibility::Hidden)); + .remove::() + .remove::() + .insert(( + this_sprite, + this_card, + this_transform, + order, + Visibility::Hidden, + )); }, ); } diff --git a/src/play.rs b/src/play.rs index 40803e7..fe557d8 100644 --- a/src/play.rs +++ b/src/play.rs @@ -12,7 +12,7 @@ pub struct PlayPlugin; impl Plugin for PlayPlugin { fn build(&self, app: &mut App) { app.add_event::() - .add_observer(serve_cards) + .add_systems(Update, serve_cards) .add_systems(OnEnter(ViewState::Play), deal_cards) .add_systems(Update, deal_cards.run_if(set_added)) .add_systems(Update, delayed_animation); @@ -28,7 +28,7 @@ pub(crate) struct Selected; pub(crate) struct DeckOrder(pub u8); /// Where on the board/table/play-space a card is -#[derive(Component)] +#[derive(Component, Debug)] pub(crate) struct PlayLocation { pub x: u8, pub y: u8, @@ -54,7 +54,7 @@ pub(crate) fn toggle_selected( } pub(crate) fn play_selected_animation( - trigger: Trigger, + trigger: Trigger, mut query: Query<&mut AnimationPlayer>, store: Res, mut commands: Commands, @@ -196,66 +196,67 @@ struct ServeCards; /// When requested, fill in empty spots on the board with cards fn serve_cards( - _trigger: Trigger, + mut events: EventReader, in_deck: Query<(Entity, &Card, &DeckOrder)>, spots: Query<&PlayLocation>, mut top_card: Single<&mut Visibility, With>, mut commands: Commands, ) { - info!( - "Serving cards from deck ({} cards left)", - in_deck.iter().len() - ); - if in_deck.is_empty() { - commands.trigger(UiMessage("Deck's empty!".into())); - top_card.toggle_visible_hidden(); - top_card.toggle_inherited_hidden(); - return; - } + events.read().for_each(|_| { + info!( + "Serving cards from deck ({} cards left)", + in_deck.iter().len() + ); + if in_deck.is_empty() { + commands.trigger(UiMessage("Deck's empty!".into())); + **top_card = Visibility::Hidden; + return; + } else { + **top_card = Visibility::Inherited; + } - let mut n = in_deck.iter().len().saturating_sub(1); - if n == 0 { - top_card.toggle_visible_hidden(); - top_card.toggle_inherited_hidden(); - } + let mut n = in_deck.iter().len().saturating_sub(1); + if n == 0 { + **top_card = Visibility::Hidden; + } - // Iterate over every x, y play location - for this_x in 0..=3 { - for this_y in 0..=3 { - // If this spot does not have a card - if spots - .iter() - .find(|PlayLocation { x, y }| *x == (this_x as u8) && *y == (this_y as u8)) - .is_none() - { - // If we have more than 9 cards on the board - let candidate = in_deck + // Iterate over every x, y play location + for this_x in 0..=3 { + for this_y in 0..=3 { + // If this spot does not have a card + if spots .iter() - .find(|(_entity, _deck_card, order)| order.0 == n as u8); - n = n.saturating_sub(1); - if n == 0 { - top_card.toggle_visible_hidden(); - top_card.toggle_inherited_hidden(); - } + .find(|PlayLocation { x, y }| *x == (this_x as u8) && *y == (this_y as u8)) + .is_none() + { + // If we have more than 9 cards on the board + let candidate = in_deck + .iter() + .find(|(_entity, _deck_card, order)| order.0 == n as u8); + n = n.saturating_sub(1); + if n == 0 { + **top_card = Visibility::Hidden; + } - // If that search was fruitful, pull the card from the deck and play it - if let Some((e, _, _)) = candidate { - commands - .entity(e) - .remove::() - .insert(PlayLocation { - x: this_x, - y: this_y, - }); + // If that search was fruitful, pull the card from the deck and play it + if let Some((e, _, _)) = candidate { + commands + .entity(e) + .remove::() + .insert(PlayLocation { + x: this_x, + y: this_y, + }); + } } } } - } + }); } /// Trigger dealing cards when the game starts -fn deal_cards(mut commands: Commands) { - commands.trigger(ServeCards); +fn deal_cards(mut events: EventWriter) { + events.send(ServeCards); } fn set_added(query: Query>) -> bool { @@ -263,7 +264,7 @@ fn set_added(query: Query>) -> bool { } #[derive(Component)] -struct DelayedAnimation { +pub(crate) struct DelayedAnimation { graph: AnimationGraphHandle, animation_index: AnimationNodeIndex, delay: Timer, @@ -274,7 +275,7 @@ pub(crate) struct AnimationComplete; /// When a card is added to the board, place it on the screen pub(crate) fn place_card( - trigger: Trigger, + trigger: Trigger, mut query: Query<(Entity, &PlayLocation, &mut Visibility)>, animation_store: Res, mut commands: Commands, @@ -287,6 +288,8 @@ pub(crate) fn place_card( .get(&format!("deck->{:?}", (play_location.x, play_location.y))) .unwrap(); + info!("Placing card {:?} at {:?}", entity, play_location); + let delay = ((random_state.hash_one(entity) % 9) as f32 / 10.0) + 0.1; commands.entity(entity).insert(DelayedAnimation { animation_index: animation_index.clone(), @@ -308,6 +311,7 @@ fn delayed_animation( .for_each(|(entity, mut delayed_animation, mut animation_player)| { delayed_animation.delay.tick(time.delta()); if delayed_animation.delay.just_finished() { + animation_player.rewind_all(); animation_player.play(delayed_animation.animation_index); commands .entity(entity) diff --git a/src/setup.rs b/src/setup.rs index 32a6a34..b9a8f62 100644 --- a/src/setup.rs +++ b/src/setup.rs @@ -54,7 +54,7 @@ pub(crate) fn setup_cards( // Top of card pile is a "face down" card { let top_card_transform = Transform { - translation: Vec3::new(-400.0, -200.0, 1.0), + translation: Vec3::new(-400.0, -200.0, 99.0), ..default() }; let top_card_sprite = Sprite { diff --git a/todo.txt b/todo.txt index 2cd4f24..df269c1 100644 --- a/todo.txt +++ b/todo.txt @@ -1,4 +1,7 @@ TODO: +* for testing, hotkeys h and enter for help and submit respectively +* there is a "new game" bug where I think SetNumber is not cleaned up properly +* new game should work * A deck with face-down card(s) * Make "set" button visually interesting when 3 cards selected * Better shuffling