|
|
|
@ -2,9 +2,11 @@ use crate::{events, game::Throw, ui};
|
|
|
|
|
use bevy::ecs::system::EntityCommand;
|
|
|
|
|
use bevy::prelude::*;
|
|
|
|
|
use bevy::text::DEFAULT_FONT_HANDLE;
|
|
|
|
|
use bevy::time::Stopwatch;
|
|
|
|
|
use std::time::Duration;
|
|
|
|
|
|
|
|
|
|
#[derive(Component)]
|
|
|
|
|
struct PlayContainer;
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
pub enum Phase {
|
|
|
|
|
BeforePlay,
|
|
|
|
@ -28,6 +30,11 @@ impl Board {
|
|
|
|
|
p2_throw: None,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn clear(&mut self) {
|
|
|
|
|
self.p1_throw = None;
|
|
|
|
|
self.p2_throw = None;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct Play {}
|
|
|
|
@ -42,7 +49,11 @@ struct Action {
|
|
|
|
|
throw: Throw,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn advance_game_state(time: Res<Time>, mut board: ResMut<Board>) {
|
|
|
|
|
fn advance_game_state(
|
|
|
|
|
time: Res<Time>,
|
|
|
|
|
mut board: ResMut<Board>,
|
|
|
|
|
mut finished: EventWriter<events::GameFinished>,
|
|
|
|
|
) {
|
|
|
|
|
// add some stuff here
|
|
|
|
|
match &mut board.phase {
|
|
|
|
|
Phase::BeforePlay => {}
|
|
|
|
@ -50,7 +61,23 @@ fn advance_game_state(time: Res<Time>, mut board: ResMut<Board>) {
|
|
|
|
|
timer.tick(time.delta());
|
|
|
|
|
if timer.just_finished() {
|
|
|
|
|
info!("Countdown timer finished");
|
|
|
|
|
if board.p1_throw.is_none() {
|
|
|
|
|
info!("Player panics and throws at random");
|
|
|
|
|
board.p1_throw = Some(Throw::random());
|
|
|
|
|
}
|
|
|
|
|
board.p2_throw = Some(Throw::random());
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
.p1_throw
|
|
|
|
|
.as_ref()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.against(board.p2_throw.as_ref().unwrap())
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Phase::Reveal(timer) => {
|
|
|
|
@ -58,6 +85,7 @@ fn advance_game_state(time: Res<Time>, mut board: ResMut<Board>) {
|
|
|
|
|
if timer.just_finished() {
|
|
|
|
|
info!("Reveal timer finished");
|
|
|
|
|
board.phase = Phase::AfterPlay;
|
|
|
|
|
finished.send_default();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Phase::AfterPlay => {}
|
|
|
|
@ -71,6 +99,10 @@ fn handle_actions(
|
|
|
|
|
for (interaction, action) in actions.iter() {
|
|
|
|
|
if matches!(interaction, Interaction::Pressed) {
|
|
|
|
|
info!("Clicked {throw:?}", throw = action.throw);
|
|
|
|
|
if !matches!(board.phase, Phase::Countdown(_)) {
|
|
|
|
|
info!("Ignoring player throw");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if board.p1_throw.is_some() {
|
|
|
|
|
info!("Replacing existing throw");
|
|
|
|
|
}
|
|
|
|
@ -147,6 +179,20 @@ fn header() -> impl Bundle {
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn handle_game_finished(
|
|
|
|
|
mut events: EventReader<events::GameFinished>,
|
|
|
|
|
mut commands: Commands,
|
|
|
|
|
q: Query<Entity, With<PlayContainer>>,
|
|
|
|
|
) {
|
|
|
|
|
if events.is_empty() {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
events.clear();
|
|
|
|
|
for e in q.iter() {
|
|
|
|
|
commands.entity(e).despawn_recursive();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn handle_play(
|
|
|
|
|
mut events: EventReader<events::Play>,
|
|
|
|
|
mut commands: Commands,
|
|
|
|
@ -160,10 +206,12 @@ fn handle_play(
|
|
|
|
|
info!("play event {e:?}");
|
|
|
|
|
}
|
|
|
|
|
board.phase = Phase::Countdown(Timer::new(Duration::from_millis(3000), TimerMode::Once));
|
|
|
|
|
board.clear();
|
|
|
|
|
info!("Starting countdown");
|
|
|
|
|
|
|
|
|
|
let mut bg = commands.spawn((
|
|
|
|
|
Name::new("Play Container"),
|
|
|
|
|
PlayContainer {},
|
|
|
|
|
NodeBundle {
|
|
|
|
|
style: Style {
|
|
|
|
|
display: Display::Flex,
|
|
|
|
@ -235,7 +283,15 @@ fn handle_play(
|
|
|
|
|
|
|
|
|
|
impl Plugin for Play {
|
|
|
|
|
fn build(&self, app: &mut App) {
|
|
|
|
|
app.add_systems(Update, (handle_play, handle_actions, advance_game_state));
|
|
|
|
|
app.add_systems(
|
|
|
|
|
Update,
|
|
|
|
|
(
|
|
|
|
|
handle_play,
|
|
|
|
|
handle_actions,
|
|
|
|
|
advance_game_state,
|
|
|
|
|
handle_game_finished,
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
app.insert_resource(Board::new());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|