removing broken new game button for now

main
Elijah Voigt 1 year ago
parent fc6a9053b3
commit 44008a5b89

@ -8,7 +8,7 @@ use bevy::{
use crate::{ use crate::{
audio, boot, audio, boot,
deck::{Card, Deck, ItemColor, ItemNumber, ItemPattern, ItemShape}, 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}, view::{button_set_state, ViewState},
}; };
@ -90,6 +90,7 @@ fn setup(mut commands: Commands, server: Res<AssetServer>) {
.observe(button_hover_on) .observe(button_hover_on)
.observe(button_hover_off) .observe(button_hover_off)
.observe(button_set_state(ViewState::Play)); .observe(button_set_state(ViewState::Play));
/*
parent parent
.spawn(button_builder(Node::default())) .spawn(button_builder(Node::default()))
.with_children(|parent| { .with_children(|parent| {
@ -99,6 +100,7 @@ fn setup(mut commands: Commands, server: Res<AssetServer>) {
.observe(button_hover_off) .observe(button_hover_off)
.observe(reset_game) .observe(reset_game)
.observe(button_set_state(ViewState::Play)); .observe(button_set_state(ViewState::Play));
*/
parent parent
.spawn(button_builder(Node::default())) .spawn(button_builder(Node::default()))
.with_children(|parent| { .with_children(|parent| {
@ -537,12 +539,22 @@ fn reset_game(
custom_size: Some(Vec2::new(80.0, 128.0)), custom_size: Some(Vec2::new(80.0, 128.0)),
..this ..this
}; };
let this_transform =
Transform::default().with_translation(Vec3::new(-400.0, -200.0, 0.0));
let order = DeckOrder(i as u8); let order = DeckOrder(i as u8);
commands commands
.entity(entity) .entity(entity)
.remove::<SetNumber>() .remove::<SetNumber>()
.remove::<PlayLocation>() .remove::<PlayLocation>()
.insert((this_sprite, this_card, order, Visibility::Hidden)); .remove::<PickingBehavior>()
.remove::<DelayedAnimation>()
.insert((
this_sprite,
this_card,
this_transform,
order,
Visibility::Hidden,
));
}, },
); );
} }

@ -12,7 +12,7 @@ pub struct PlayPlugin;
impl Plugin for PlayPlugin { impl Plugin for PlayPlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.add_event::<ServeCards>() app.add_event::<ServeCards>()
.add_observer(serve_cards) .add_systems(Update, serve_cards)
.add_systems(OnEnter(ViewState::Play), deal_cards) .add_systems(OnEnter(ViewState::Play), deal_cards)
.add_systems(Update, deal_cards.run_if(set_added)) .add_systems(Update, deal_cards.run_if(set_added))
.add_systems(Update, delayed_animation); .add_systems(Update, delayed_animation);
@ -28,7 +28,7 @@ pub(crate) struct Selected;
pub(crate) struct DeckOrder(pub u8); pub(crate) struct DeckOrder(pub u8);
/// Where on the board/table/play-space a card is /// Where on the board/table/play-space a card is
#[derive(Component)] #[derive(Component, Debug)]
pub(crate) struct PlayLocation { pub(crate) struct PlayLocation {
pub x: u8, pub x: u8,
pub y: u8, pub y: u8,
@ -54,7 +54,7 @@ pub(crate) fn toggle_selected(
} }
pub(crate) fn play_selected_animation( pub(crate) fn play_selected_animation(
trigger: Trigger<OnAdd, Selected>, trigger: Trigger<OnInsert, Selected>,
mut query: Query<&mut AnimationPlayer>, mut query: Query<&mut AnimationPlayer>,
store: Res<AnimationStore>, store: Res<AnimationStore>,
mut commands: Commands, mut commands: Commands,
@ -196,66 +196,67 @@ struct ServeCards;
/// When requested, fill in empty spots on the board with cards /// When requested, fill in empty spots on the board with cards
fn serve_cards( fn serve_cards(
_trigger: Trigger<ServeCards>, mut events: EventReader<ServeCards>,
in_deck: Query<(Entity, &Card, &DeckOrder)>, in_deck: Query<(Entity, &Card, &DeckOrder)>,
spots: Query<&PlayLocation>, spots: Query<&PlayLocation>,
mut top_card: Single<&mut Visibility, With<TopCard>>, mut top_card: Single<&mut Visibility, With<TopCard>>,
mut commands: Commands, mut commands: Commands,
) { ) {
info!( events.read().for_each(|_| {
"Serving cards from deck ({} cards left)", info!(
in_deck.iter().len() "Serving cards from deck ({} cards left)",
); in_deck.iter().len()
if in_deck.is_empty() { );
commands.trigger(UiMessage("Deck's empty!".into())); if in_deck.is_empty() {
top_card.toggle_visible_hidden(); commands.trigger(UiMessage("Deck's empty!".into()));
top_card.toggle_inherited_hidden(); **top_card = Visibility::Hidden;
return; return;
} } else {
**top_card = Visibility::Inherited;
}
let mut n = in_deck.iter().len().saturating_sub(1); let mut n = in_deck.iter().len().saturating_sub(1);
if n == 0 { if n == 0 {
top_card.toggle_visible_hidden(); **top_card = Visibility::Hidden;
top_card.toggle_inherited_hidden(); }
}
// Iterate over every x, y play location // Iterate over every x, y play location
for this_x in 0..=3 { for this_x in 0..=3 {
for this_y in 0..=3 { for this_y in 0..=3 {
// If this spot does not have a card // If this spot does not have a card
if spots 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
.iter() .iter()
.find(|(_entity, _deck_card, order)| order.0 == n as u8); .find(|PlayLocation { x, y }| *x == (this_x as u8) && *y == (this_y as u8))
n = n.saturating_sub(1); .is_none()
if n == 0 { {
top_card.toggle_visible_hidden(); // If we have more than 9 cards on the board
top_card.toggle_inherited_hidden(); 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 that search was fruitful, pull the card from the deck and play it
if let Some((e, _, _)) = candidate { if let Some((e, _, _)) = candidate {
commands commands
.entity(e) .entity(e)
.remove::<DeckOrder>() .remove::<DeckOrder>()
.insert(PlayLocation { .insert(PlayLocation {
x: this_x, x: this_x,
y: this_y, y: this_y,
}); });
}
} }
} }
} }
} });
} }
/// Trigger dealing cards when the game starts /// Trigger dealing cards when the game starts
fn deal_cards(mut commands: Commands) { fn deal_cards(mut events: EventWriter<ServeCards>) {
commands.trigger(ServeCards); events.send(ServeCards);
} }
fn set_added(query: Query<Entity, Added<SetNumber>>) -> bool { fn set_added(query: Query<Entity, Added<SetNumber>>) -> bool {
@ -263,7 +264,7 @@ fn set_added(query: Query<Entity, Added<SetNumber>>) -> bool {
} }
#[derive(Component)] #[derive(Component)]
struct DelayedAnimation { pub(crate) struct DelayedAnimation {
graph: AnimationGraphHandle, graph: AnimationGraphHandle,
animation_index: AnimationNodeIndex, animation_index: AnimationNodeIndex,
delay: Timer, delay: Timer,
@ -274,7 +275,7 @@ pub(crate) struct AnimationComplete;
/// When a card is added to the board, place it on the screen /// When a card is added to the board, place it on the screen
pub(crate) fn place_card( pub(crate) fn place_card(
trigger: Trigger<OnAdd, PlayLocation>, trigger: Trigger<OnInsert, PlayLocation>,
mut query: Query<(Entity, &PlayLocation, &mut Visibility)>, mut query: Query<(Entity, &PlayLocation, &mut Visibility)>,
animation_store: Res<AnimationStore>, animation_store: Res<AnimationStore>,
mut commands: Commands, mut commands: Commands,
@ -287,6 +288,8 @@ pub(crate) fn place_card(
.get(&format!("deck->{:?}", (play_location.x, play_location.y))) .get(&format!("deck->{:?}", (play_location.x, play_location.y)))
.unwrap(); .unwrap();
info!("Placing card {:?} at {:?}", entity, play_location);
let delay = ((random_state.hash_one(entity) % 9) as f32 / 10.0) + 0.1; let delay = ((random_state.hash_one(entity) % 9) as f32 / 10.0) + 0.1;
commands.entity(entity).insert(DelayedAnimation { commands.entity(entity).insert(DelayedAnimation {
animation_index: animation_index.clone(), animation_index: animation_index.clone(),
@ -308,6 +311,7 @@ fn delayed_animation(
.for_each(|(entity, mut delayed_animation, mut animation_player)| { .for_each(|(entity, mut delayed_animation, mut animation_player)| {
delayed_animation.delay.tick(time.delta()); delayed_animation.delay.tick(time.delta());
if delayed_animation.delay.just_finished() { if delayed_animation.delay.just_finished() {
animation_player.rewind_all();
animation_player.play(delayed_animation.animation_index); animation_player.play(delayed_animation.animation_index);
commands commands
.entity(entity) .entity(entity)

@ -54,7 +54,7 @@ pub(crate) fn setup_cards(
// Top of card pile is a "face down" card // Top of card pile is a "face down" card
{ {
let top_card_transform = Transform { let top_card_transform = Transform {
translation: Vec3::new(-400.0, -200.0, 1.0), translation: Vec3::new(-400.0, -200.0, 99.0),
..default() ..default()
}; };
let top_card_sprite = Sprite { let top_card_sprite = Sprite {

@ -1,4 +1,7 @@
TODO: 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) * A deck with face-down card(s)
* Make "set" button visually interesting when 3 cards selected * Make "set" button visually interesting when 3 cards selected
* Better shuffling * Better shuffling

Loading…
Cancel
Save