From 25357995056bbe6299e5c8fddf7170e449447c7f Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Sun, 1 Oct 2023 15:57:55 -0500 Subject: [PATCH] a full game loop appears --- Cargo.lock | 37 ++++++++++++++++++++++++++++ Cargo.toml | 1 + src/events.rs | 4 +++ src/game.rs | 34 ++++++++++++++++++++++++++ src/play.rs | 62 ++++++++++++++++++++++++++++++++++++++++++++--- src/start_menu.rs | 23 +++++++++++++++--- 6 files changed, 155 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f63b6dc..2a5a79d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2812,6 +2812,12 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "ppv-lite86" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" + [[package]] name = "pretty-type-name" version = "1.0.1" @@ -2858,6 +2864,36 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "17fd96390ed3feda12e1dfe2645ed587e0bea749e319333f104a33ff62f77a0b" +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom", +] + [[package]] name = "range-alloc" version = "0.1.3" @@ -2963,6 +2999,7 @@ version = "0.1.0" dependencies = [ "bevy", "bevy-inspector-egui", + "rand", "tracing", ] diff --git a/Cargo.toml b/Cargo.toml index 18fbedd..46b1ca1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,4 +17,5 @@ edition = "2021" [dependencies] bevy = { version = "0.11.2", features = [ ] } bevy-inspector-egui = "0.19.0" +rand = "0.8.5" tracing = "0.1.37" diff --git a/src/events.rs b/src/events.rs index 9a8bec0..c511e15 100644 --- a/src/events.rs +++ b/src/events.rs @@ -4,6 +4,9 @@ use bevy::prelude::*; #[derive(Event, Default, Debug)] pub(crate) struct Play; +#[derive(Event, Default, Debug)] +pub(crate) struct GameFinished; + #[derive(Event)] pub(crate) struct PlayerMove { throw: Throw, @@ -14,5 +17,6 @@ pub(crate) struct Events {} impl Plugin for Events { fn build(&self, app: &mut App) { app.add_event::(); + app.add_event::(); } } diff --git a/src/game.rs b/src/game.rs index 0efcf5f..2f74f10 100644 --- a/src/game.rs +++ b/src/game.rs @@ -1,4 +1,5 @@ use bevy::prelude::*; +use rand::Rng; use tracing::info; use crate::{events::Events, play::Play, start_menu::StartMenu}; @@ -25,3 +26,36 @@ pub(crate) enum Throw { Paper, Scissors, } + +impl Throw { + pub fn random() -> Self { + let mut rng = rand::thread_rng(); + match rng.gen_range(0..3) { + 0 => Throw::Rock, + 1 => Throw::Paper, + 2 => Throw::Scissors, + _ => unreachable!(), + } + } + + pub fn against(&self, other: &Self) -> Outcome { + match (self, other) { + (Throw::Rock, Throw::Rock) + | (Throw::Paper, Throw::Paper) + | (Throw::Scissors, Throw::Scissors) => Outcome::Tie, + (Throw::Rock, Throw::Scissors) + | (Throw::Paper, Throw::Rock) + | (Throw::Scissors, Throw::Paper) => Outcome::Win, + (Throw::Rock, Throw::Paper) + | (Throw::Paper, Throw::Scissors) + | (Throw::Scissors, Throw::Rock) => Outcome::Loss, + } + } +} + +#[derive(Debug)] +pub(crate) enum Outcome { + Win, + Loss, + Tie, +} diff --git a/src/play.rs b/src/play.rs index 9a38a8f..f94296f 100644 --- a/src/play.rs +++ b/src/play.rs @@ -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