|
|
@ -2,9 +2,11 @@ use crate::{events, game::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;
|
|
|
|
use bevy::time::Stopwatch;
|
|
|
|
|
|
|
|
use std::time::Duration;
|
|
|
|
use std::time::Duration;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Component)]
|
|
|
|
|
|
|
|
struct PlayContainer;
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum Phase {
|
|
|
|
pub enum Phase {
|
|
|
|
BeforePlay,
|
|
|
|
BeforePlay,
|
|
|
@ -28,6 +30,11 @@ impl Board {
|
|
|
|
p2_throw: None,
|
|
|
|
p2_throw: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn clear(&mut self) {
|
|
|
|
|
|
|
|
self.p1_throw = None;
|
|
|
|
|
|
|
|
self.p2_throw = None;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub struct Play {}
|
|
|
|
pub struct Play {}
|
|
|
@ -42,7 +49,11 @@ struct Action {
|
|
|
|
throw: Throw,
|
|
|
|
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
|
|
|
|
// add some stuff here
|
|
|
|
match &mut board.phase {
|
|
|
|
match &mut board.phase {
|
|
|
|
Phase::BeforePlay => {}
|
|
|
|
Phase::BeforePlay => {}
|
|
|
@ -50,7 +61,23 @@ fn advance_game_state(time: Res<Time>, mut board: ResMut<Board>) {
|
|
|
|
timer.tick(time.delta());
|
|
|
|
timer.tick(time.delta());
|
|
|
|
if timer.just_finished() {
|
|
|
|
if timer.just_finished() {
|
|
|
|
info!("Countdown timer 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));
|
|
|
|
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) => {
|
|
|
|
Phase::Reveal(timer) => {
|
|
|
@ -58,6 +85,7 @@ fn advance_game_state(time: Res<Time>, mut board: ResMut<Board>) {
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Phase::AfterPlay => {}
|
|
|
|
Phase::AfterPlay => {}
|
|
|
@ -71,6 +99,10 @@ fn handle_actions(
|
|
|
|
for (interaction, action) in actions.iter() {
|
|
|
|
for (interaction, action) in actions.iter() {
|
|
|
|
if matches!(interaction, Interaction::Pressed) {
|
|
|
|
if matches!(interaction, Interaction::Pressed) {
|
|
|
|
info!("Clicked {throw:?}", throw = action.throw);
|
|
|
|
info!("Clicked {throw:?}", throw = action.throw);
|
|
|
|
|
|
|
|
if !matches!(board.phase, Phase::Countdown(_)) {
|
|
|
|
|
|
|
|
info!("Ignoring player throw");
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
if board.p1_throw.is_some() {
|
|
|
|
if board.p1_throw.is_some() {
|
|
|
|
info!("Replacing existing throw");
|
|
|
|
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(
|
|
|
|
fn handle_play(
|
|
|
|
mut events: EventReader<events::Play>,
|
|
|
|
mut events: EventReader<events::Play>,
|
|
|
|
mut commands: Commands,
|
|
|
|
mut commands: Commands,
|
|
|
@ -160,10 +206,12 @@ fn handle_play(
|
|
|
|
info!("play event {e:?}");
|
|
|
|
info!("play event {e:?}");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
board.phase = Phase::Countdown(Timer::new(Duration::from_millis(3000), TimerMode::Once));
|
|
|
|
board.phase = Phase::Countdown(Timer::new(Duration::from_millis(3000), TimerMode::Once));
|
|
|
|
|
|
|
|
board.clear();
|
|
|
|
info!("Starting countdown");
|
|
|
|
info!("Starting countdown");
|
|
|
|
|
|
|
|
|
|
|
|
let mut bg = commands.spawn((
|
|
|
|
let mut bg = commands.spawn((
|
|
|
|
Name::new("Play Container"),
|
|
|
|
Name::new("Play Container"),
|
|
|
|
|
|
|
|
PlayContainer {},
|
|
|
|
NodeBundle {
|
|
|
|
NodeBundle {
|
|
|
|
style: Style {
|
|
|
|
style: Style {
|
|
|
|
display: Display::Flex,
|
|
|
|
display: Display::Flex,
|
|
|
@ -235,7 +283,15 @@ fn handle_play(
|
|
|
|
|
|
|
|
|
|
|
|
impl Plugin for Play {
|
|
|
|
impl Plugin for Play {
|
|
|
|
fn build(&self, app: &mut App) {
|
|
|
|
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());
|
|
|
|
app.insert_resource(Board::new());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|