we can tell you if you won now

main
Jordan Orelli 1 year ago
parent 2535799505
commit 06980c06fe

@ -1,11 +1,13 @@
use crate::game::Throw;
use crate::game::{Outcome, Throw};
use bevy::prelude::*;
#[derive(Event, Default, Debug)]
pub(crate) struct Play;
#[derive(Event, Default, Debug)]
pub(crate) struct GameFinished;
#[derive(Event, Debug)]
pub(crate) struct GameFinished {
pub outcome: Outcome,
}
#[derive(Event)]
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::prelude::*;
use bevy::text::DEFAULT_FONT_HANDLE;
@ -39,11 +43,6 @@ impl Board {
pub struct Play {}
pub(crate) enum GameEvent {
RoundBegin,
RoundEnd,
}
#[derive(Component, Debug)]
struct Action {
throw: Throw,
@ -53,6 +52,7 @@ fn advance_game_state(
time: Res<Time>,
mut board: ResMut<Board>,
mut finished: EventWriter<events::GameFinished>,
commands: Commands,
) {
// add some stuff here
match &mut board.phase {
@ -70,14 +70,12 @@ fn advance_game_state(
board.phase = Phase::Reveal(Timer::new(Duration::from_secs(3), TimerMode::Once));
info!("Player threw: {throw:?}", throw = board.p1_throw);
info!("Computer threw: {throw:?}", throw = board.p2_throw);
info!(
"Outcome: {outcome:?}",
outcome = board
let outcome = board
.p1_throw
.as_ref()
.unwrap()
.against(board.p2_throw.as_ref().unwrap())
);
.against(board.p2_throw.as_ref().unwrap());
finished.send(events::GameFinished { outcome });
}
}
Phase::Reveal(timer) => {
@ -85,13 +83,26 @@ fn advance_game_state(
if timer.just_finished() {
info!("Reveal timer finished");
board.phase = Phase::AfterPlay;
finished.send_default();
// finished.send_default();
}
}
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(
actions: Query<(&Interaction, &Action), (Changed<Interaction>, With<Action>)>,
mut board: ResMut<Board>,
@ -187,10 +198,13 @@ fn handle_game_finished(
if events.is_empty() {
return;
}
events.clear();
for e in q.iter() {
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(

@ -1,8 +1,7 @@
use crate::{
events,
ui::{self, HoverColor},
ui::{self, HoverColor, Label},
};
use bevy::ecs::system::EntityCommand;
use bevy::prelude::*;
use bevy::text::DEFAULT_FONT_HANDLE;
use tracing::info;
@ -62,14 +61,16 @@ fn handle_play(
mut events: EventReader<events::Play>,
mut commands: Commands,
) {
for e in events.iter() {
if events.is_empty() {
return;
}
events.clear();
info!("Handling Play event");
for m in menu.iter() {
commands.entity(m).despawn_recursive();
}
}
}
fn click_quit(
mut actions: Query<&Interaction, (Changed<Interaction>, With<QuitButton>)>,
@ -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) {
if events.is_empty() {
return;
@ -198,9 +171,6 @@ impl Plugin for StartMenu {
fn build(&self, app: &mut App) {
app.add_plugins(ui::Plugin {});
app.add_systems(Startup, setup);
app.add_systems(
Update,
(click_quit, click_play, handle_play, handle_game_finished),
);
app.add_systems(Update, (click_quit, click_play, handle_play));
}
}

@ -1,4 +1,6 @@
use bevy::ecs::system::EntityCommand;
use bevy::prelude::*;
use bevy::text::DEFAULT_FONT_HANDLE;
#[derive(Component)]
pub(crate) struct HoverColor {
@ -58,3 +60,31 @@ pub(crate) fn column() -> NodeBundle {
..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