Added background image

main
Elijah Voigt 2 years ago
parent 7c737e5d72
commit 6dbffa536c

Binary file not shown.

After

Width:  |  Height:  |  Size: 85 KiB

@ -55,14 +55,12 @@ impl Deck {
let rs = RandomState::new(); let rs = RandomState::new();
let mut base = Self::cards(); let mut base = Self::cards();
let len = base.len(); let len = base.len();
(0..2).iter().for_each(|_| { (1..len).into_iter().for_each(|i| {
(1..len).into_iter().for_each(|i| { let a = rs.hash_one(base[i]) % (len as u64);
let a = rs.hash_one(base[i]) % (len as u64); let b = rs.hash_one(base[i - 1]) % (len as u64);
let b = rs.hash_one(base[i - 1]) % (len as u64); if a > b {
if a > b { base.swap(a as usize, b as usize);
base.swap(a as usize, b as usize); }
}
});
}); });
base base
} }

@ -1,8 +1,10 @@
mod boot; mod boot;
mod debug; mod debug;
mod deck; mod deck;
mod menu;
mod play; mod play;
mod setup; mod setup;
mod view;
use bevy::prelude::*; use bevy::prelude::*;
@ -10,10 +12,12 @@ fn main() {
App::new() App::new()
.add_plugins(DefaultPlugins.set(ImagePlugin::default_nearest())) .add_plugins(DefaultPlugins.set(ImagePlugin::default_nearest()))
.add_plugins(( .add_plugins((
view::ViewPlugin,
deck::DeckPlugin, deck::DeckPlugin,
boot::BootPlugin, boot::BootPlugin,
setup::SetupPlugin, setup::SetupPlugin,
play::PlayPlugin, play::PlayPlugin,
menu::MenuPlugin,
debug::DebugPlugin, debug::DebugPlugin,
)) ))
.init_state::<GameState>() .init_state::<GameState>()
@ -25,5 +29,5 @@ pub(crate) enum GameState {
#[default] #[default]
Boot, Boot,
Setup, Setup,
Play, Main,
} }

@ -0,0 +1,25 @@
use bevy::{color::palettes::css::TEAL, prelude::*};
use crate::view::{button_set_state, ViewState};
/// Game Menu
pub struct MenuPlugin;
impl Plugin for MenuPlugin {
fn build(&self, app: &mut App) {
app.add_systems(Startup, setup);
}
}
fn setup(mut commands: Commands) {
commands
.spawn((ViewState::Menu, Node::default()))
.with_children(|parent| {
parent
.spawn((Button, BackgroundColor(TEAL.into())))
.with_children(|parent| {
parent.spawn(Text("Play".to_string()));
})
.observe(button_set_state(ViewState::Play));
});
}

@ -7,7 +7,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_observer(serve_new_cards) app.add_observer(serve_new_cards)
.add_systems(OnEnter(GameState::Play), serve_cards) .add_systems(OnEnter(GameState::Main), serve_cards)
.add_systems(Update, rotate_cards); .add_systems(Update, rotate_cards);
} }
} }
@ -64,7 +64,7 @@ pub(crate) fn reset_rotation(
/// Check a set when the "Set" button is clicked /// Check a set when the "Set" button is clicked
pub(crate) fn check_set( pub(crate) fn check_set(
_trigger: Trigger<Pointer<Click>>, trigger: Trigger<Pointer<Click>>,
mut query: Query<(Entity, &Card, &mut Visibility, &mut Transform), With<Selected>>, mut query: Query<(Entity, &Card, &mut Visibility, &mut Transform), With<Selected>>,
mut commands: Commands, mut commands: Commands,
) { ) {

@ -1,5 +1,6 @@
use crate::{deck::*, *}; use crate::{deck::*, *};
use bevy::{color::palettes::css::TEAL, prelude::*}; use bevy::{color::palettes::css::TEAL, prelude::*};
use view::ViewState;
pub struct SetupPlugin; pub struct SetupPlugin;
@ -8,9 +9,10 @@ impl Plugin for SetupPlugin {
app.add_systems( app.add_systems(
OnEnter(GameState::Setup), OnEnter(GameState::Setup),
( (
setup_background,
setup_cards, setup_cards,
setup_camera,
setup_set_check_button, setup_set_check_button,
setup_camera,
start_play, start_play,
) )
.chain(), .chain(),
@ -20,26 +22,30 @@ impl Plugin for SetupPlugin {
/// Setup drawing our cards on the screen /// Setup drawing our cards on the screen
pub(crate) fn setup_cards(mut commands: Commands, deck: Res<Deck>) { pub(crate) fn setup_cards(mut commands: Commands, deck: Res<Deck>) {
Deck::iter_shuffled() commands
.enumerate() .spawn((Transform::default(), Visibility::default(), ViewState::Play))
.for_each(|(i, this_card)| { .with_children(|parent| {
let this = deck Deck::iter_shuffled()
.cards .enumerate()
.get(&this_card) .for_each(|(i, this_card)| {
.unwrap_or_else(|| panic!("fech card sprite {:?}", this_card)) let this = deck
.clone(); .cards
let this_sprite = Sprite { .get(&this_card)
custom_size: Some(Vec2::new(80.0, 128.0)), .unwrap_or_else(|| panic!("fech card sprite {:?}", this_card))
..this .clone();
}; let this_sprite = Sprite {
let order = play::DeckOrder(i as u8); custom_size: Some(Vec2::new(80.0, 128.0)),
commands ..this
.spawn((this_sprite, this_card, order, Visibility::Hidden)) };
.observe(play::place_card) let order = play::DeckOrder(i as u8);
.observe(debug::set_debug_card) parent
.observe(debug::hide_debug_card) .spawn((this_sprite, this_card, order, Visibility::Hidden))
.observe(play::reset_rotation) .observe(play::place_card)
.observe(play::toggle_selected); .observe(debug::set_debug_card)
.observe(debug::hide_debug_card)
.observe(play::reset_rotation)
.observe(play::toggle_selected);
});
}); });
} }
@ -54,8 +60,20 @@ pub(crate) fn setup_camera(mut commands: Commands) {
)); ));
} }
#[derive(Component)] pub(crate) fn setup_background(
pub(crate) struct CheckSet; mut commands: Commands,
server: Res<AssetServer>,
window: Query<&Window>,
) {
commands.spawn((
Transform::default().with_translation(Vec3::new(0.0, 0.0, -1.0)),
Sprite {
image: server.load("background.png"),
custom_size: Some(window.single().resolution.size() * 1.1),
..default()
},
));
}
pub(crate) fn setup_set_check_button(mut commands: Commands) { pub(crate) fn setup_set_check_button(mut commands: Commands) {
commands commands
@ -66,11 +84,12 @@ pub(crate) fn setup_set_check_button(mut commands: Commands) {
align_items: AlignItems::Center, align_items: AlignItems::Center,
..default() ..default()
}, },
ViewState::Play,
BackgroundColor(Color::BLACK.with_alpha(0.8).into()), BackgroundColor(Color::BLACK.with_alpha(0.8).into()),
)) ))
.with_children(|parent| { .with_children(|parent| {
parent parent
.spawn((Button, CheckSet, BackgroundColor(TEAL.into()))) .spawn((Button, BackgroundColor(TEAL.into())))
.with_children(|parent| { .with_children(|parent| {
parent.spawn(Text("Set!?".to_string())); parent.spawn(Text("Set!?".to_string()));
}) })
@ -79,6 +98,11 @@ pub(crate) fn setup_set_check_button(mut commands: Commands) {
} }
/// Finish the setup state by progressing to the play state /// Finish the setup state by progressing to the play state
pub(crate) fn start_play(mut game_state: ResMut<NextState<GameState>>) { pub(crate) fn start_play(
game_state.set(GameState::Play); mut game_state: ResMut<NextState<GameState>>,
mut view_state: ResMut<NextState<ViewState>>,
) {
info!("starting play");
game_state.set(GameState::Main);
view_state.set(ViewState::Menu);
} }

@ -0,0 +1,52 @@
use bevy::{prelude::*, state::state::FreelyMutableState};
/// Deck and Cards
pub struct ViewPlugin;
impl Plugin for ViewPlugin {
fn build(&self, app: &mut App) {
app.add_observer(state_monitor).init_state::<ViewState>();
}
}
///
/// State component for which view an entity is associated with
///
#[derive(States, Component, Default, Clone, Eq, PartialEq, Hash, Debug)]
pub(crate) enum ViewState {
#[default]
None,
Menu,
Play,
HowToPlay,
About,
Sets,
Deck,
}
pub(crate) fn button_set_state<S: FreelyMutableState + Clone>(
s: S,
) -> impl Fn(Trigger<Pointer<Click>>, ResMut<NextState<S>>) {
move |_trigger: Trigger<Pointer<Click>>, mut next: ResMut<NextState<S>>| next.set(s.clone())
}
fn state_monitor(
trigger: Trigger<StateTransitionEvent<ViewState>>,
mut entities: Query<(&ViewState, &mut Visibility)>,
) {
entities
.iter_mut()
.for_each(|(view_state, mut visibility)| {
info!(
"Transition from {:?} to {:?}",
trigger.event().exited,
trigger.event().entered
);
if Some(view_state) == trigger.event().exited.as_ref() {
*visibility = Visibility::Hidden;
}
if Some(view_state) == trigger.event().entered.as_ref() {
*visibility = Visibility::Inherited;
}
});
}

@ -7,3 +7,7 @@ TODO:
* Background! * Background!
* Print "no set"/"too many cards" messages * Print "no set"/"too many cards" messages
* Add "quit game" button * Add "quit game" button
* View all cards with some indication of in-set
* Menu
* About/Credits
* How To Play

Loading…
Cancel
Save