we can tell you if you won now

main
Jordan Orelli 12 months ago
parent 2535799505
commit 06980c06fe

@ -1,11 +1,13 @@
use crate::game::Throw; use crate::game::{Outcome, Throw};
use bevy::prelude::*; use bevy::prelude::*;
#[derive(Event, Default, Debug)] #[derive(Event, Default, Debug)]
pub(crate) struct Play; pub(crate) struct Play;
#[derive(Event, Default, Debug)] #[derive(Event, Debug)]
pub(crate) struct GameFinished; pub(crate) struct GameFinished {
pub outcome: Outcome,
}
#[derive(Event)] #[derive(Event)]
pub(crate) struct PlayerMove { pub(crate) struct PlayerMove {

@ -1,4 +1,8 @@
use crate::{events, game::Throw, ui}; use crate::{
events,
game::{Outcome, Throw},
ui,
};
use bevy::ecs::system::EntityCommand; use bevy::ecs::system::EntityCommand;
use bevy::prelude::*; use bevy::prelude::*;
use bevy::text::DEFAULT_FONT_HANDLE; use bevy::text::DEFAULT_FONT_HANDLE;
@ -39,11 +43,6 @@ impl Board {
pub struct Play {} pub struct Play {}
pub(crate) enum GameEvent {
RoundBegin,
RoundEnd,
}
#[derive(Component, Debug)] #[derive(Component, Debug)]
struct Action { struct Action {
throw: Throw, throw: Throw,
@ -53,6 +52,7 @@ fn advance_game_state(
time: Res<Time>, time: Res<Time>,
mut board: ResMut<Board>, mut board: ResMut<Board>,
mut finished: EventWriter<events::GameFinished>, mut finished: EventWriter<events::GameFinished>,
commands: Commands,
) { ) {
// add some stuff here // add some stuff here
match &mut board.phase { match &mut board.phase {
@ -70,14 +70,12 @@ fn advance_game_state(
board.phase = Phase::Reveal(Timer::new(Duration::from_secs(3), TimerMode::Once)); board.phase = Phase::Reveal(Timer::new(Duration::from_secs(3), TimerMode::Once));
info!("Player threw: {throw:?}", throw = board.p1_throw); info!("Player threw: {throw:?}", throw = board.p1_throw);
info!("Computer threw: {throw:?}", throw = board.p2_throw); info!("Computer threw: {throw:?}", throw = board.p2_throw);
info!( let outcome = board
"Outcome: {outcome:?}", .p1_throw
outcome = board .as_ref()
.p1_throw .unwrap()
.as_ref() .against(board.p2_throw.as_ref().unwrap());
.unwrap() finished.send(events::GameFinished { outcome });
.against(board.p2_throw.as_ref().unwrap())
);
} }
} }
Phase::Reveal(timer) => { Phase::Reveal(timer) => {
@ -85,13 +83,26 @@ fn advance_game_state(
if timer.just_finished() { if timer.just_finished() {
info!("Reveal timer finished"); info!("Reveal timer finished");
board.phase = Phase::AfterPlay; board.phase = Phase::AfterPlay;
finished.send_default(); // finished.send_default();
} }
} }
Phase::AfterPlay => {} Phase::AfterPlay => {}
} }
} }
fn spawn_outcome_display(mut commands: Commands, outcome: &Outcome) {
let text = match outcome {
Outcome::Win => "You Win!",
Outcome::Tie => "Tie.",
Outcome::Loss => "You Lose...",
};
commands
.spawn((Name::new("Outcome"), crate::ui::column()))
.add(ui::Label {
text: String::from(text),
});
}
fn handle_actions( fn handle_actions(
actions: Query<(&Interaction, &Action), (Changed<Interaction>, With<Action>)>, actions: Query<(&Interaction, &Action), (Changed<Interaction>, With<Action>)>,
mut board: ResMut<Board>, mut board: ResMut<Board>,
@ -187,10 +198,13 @@ fn handle_game_finished(
if events.is_empty() { if events.is_empty() {
return; return;
} }
events.clear();
for e in q.iter() { for e in q.iter() {
commands.entity(e).despawn_recursive(); commands.entity(e).despawn_recursive();
} }
let finish = events.iter().next().unwrap().to_owned();
info!("Round finished with outcome: {finish:?}");
spawn_outcome_display(commands, &finish.outcome);
events.clear();
} }
fn handle_play( fn handle_play(

@ -1,8 +1,7 @@
use crate::{ use crate::{
events, events,
ui::{self, HoverColor}, ui::{self, HoverColor, Label},
}; };
use bevy::ecs::system::EntityCommand;
use bevy::prelude::*; use bevy::prelude::*;
use bevy::text::DEFAULT_FONT_HANDLE; use bevy::text::DEFAULT_FONT_HANDLE;
use tracing::info; use tracing::info;
@ -62,12 +61,14 @@ fn handle_play(
mut events: EventReader<events::Play>, mut events: EventReader<events::Play>,
mut commands: Commands, mut commands: Commands,
) { ) {
for e in events.iter() { if events.is_empty() {
info!("Handling Play event"); return;
}
events.clear();
info!("Handling Play event");
for m in menu.iter() { for m in menu.iter() {
commands.entity(m).despawn_recursive(); commands.entity(m).despawn_recursive();
}
} }
} }
@ -110,34 +111,6 @@ fn column() -> impl Bundle {
} }
} }
struct Label {
text: String,
}
impl EntityCommand for Label {
fn apply(self, id: Entity, world: &mut World) {
let style = TextStyle {
font: DEFAULT_FONT_HANDLE.typed(),
font_size: 24.0,
color: Color::WHITE,
};
world.spawn_empty().set_parent(id).insert(
TextBundle::from_section(self.text, style)
.with_text_alignment(TextAlignment::Center)
.with_style(Style {
display: Display::Flex,
margin: UiRect {
left: Val::Auto,
right: Val::Auto,
top: Val::Auto,
bottom: Val::Auto,
},
..Default::default()
}),
);
}
}
fn handle_game_finished(mut events: EventReader<events::GameFinished>, mut commands: Commands) { fn handle_game_finished(mut events: EventReader<events::GameFinished>, mut commands: Commands) {
if events.is_empty() { if events.is_empty() {
return; return;
@ -198,9 +171,6 @@ impl Plugin for StartMenu {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.add_plugins(ui::Plugin {}); app.add_plugins(ui::Plugin {});
app.add_systems(Startup, setup); app.add_systems(Startup, setup);
app.add_systems( app.add_systems(Update, (click_quit, click_play, handle_play));
Update,
(click_quit, click_play, handle_play, handle_game_finished),
);
} }
} }

@ -1,4 +1,6 @@
use bevy::ecs::system::EntityCommand;
use bevy::prelude::*; use bevy::prelude::*;
use bevy::text::DEFAULT_FONT_HANDLE;
#[derive(Component)] #[derive(Component)]
pub(crate) struct HoverColor { pub(crate) struct HoverColor {
@ -58,3 +60,31 @@ pub(crate) fn column() -> NodeBundle {
..default() ..default()
} }
} }
pub(crate) struct Label {
pub text: String,
}
impl EntityCommand for Label {
fn apply(self, id: Entity, world: &mut World) {
let style = TextStyle {
font: DEFAULT_FONT_HANDLE.typed(),
font_size: 24.0,
color: Color::WHITE,
};
world.spawn_empty().set_parent(id).insert(
TextBundle::from_section(self.text, style)
.with_text_alignment(TextAlignment::Center)
.with_style(Style {
display: Display::Flex,
margin: UiRect {
left: Val::Auto,
right: Val::Auto,
top: Val::Auto,
bottom: Val::Auto,
},
..Default::default()
}),
);
}
}

Loading…
Cancel
Save