From 9275445bfe0a5fef1993d558372c714c9eadb237 Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Wed, 27 Sep 2023 10:33:20 -0500 Subject: [PATCH] action buttons are clickable now --- src/events.rs | 6 ++++++ src/game.rs | 7 +++++++ src/play.rs | 29 +++++++++++++++++++++++------ 3 files changed, 36 insertions(+), 6 deletions(-) diff --git a/src/events.rs b/src/events.rs index 1898bc1..0e413ec 100644 --- a/src/events.rs +++ b/src/events.rs @@ -1,8 +1,14 @@ +use crate::game::Throw; use bevy::prelude::*; #[derive(Event, Default)] pub(crate) struct Play; +#[derive(Event)] +pub(crate) struct PlayerMove { + throw: Throw, +} + pub(crate) struct Events {} impl Plugin for Events { diff --git a/src/game.rs b/src/game.rs index d20fd25..5cc9241 100644 --- a/src/game.rs +++ b/src/game.rs @@ -6,6 +6,13 @@ use crate::{events::Events, play::Play, start_menu::StartMenu}; /// The top-level structure of our game, which is a bevy plugin pub struct Game {} +#[derive(Debug)] +pub(crate) enum Throw { + Rock, + Paper, + Scissors, +} + impl Plugin for Game { fn build(&self, app: &mut App) { info!("Building rps Plugin"); diff --git a/src/play.rs b/src/play.rs index 5733a18..cb4538a 100644 --- a/src/play.rs +++ b/src/play.rs @@ -1,12 +1,26 @@ -use crate::events; +use crate::{events, game::Throw}; use bevy::ecs::system::EntityCommand; use bevy::prelude::*; use bevy::text::DEFAULT_FONT_HANDLE; pub struct Play {} +#[derive(Component, Debug)] +struct Action { + throw: Throw, +} + +fn handle_actions(actions: Query<(&Interaction, &Action), (Changed, With)>) { + for (interaction, action) in actions.iter() { + if matches!(interaction, Interaction::Pressed) { + info!("Clicked {throw:?}", throw = action.throw); + } + } +} + struct ActionButton { text: String, + throw: Throw, } impl EntityCommand for ActionButton { @@ -19,7 +33,8 @@ impl EntityCommand for ActionButton { let button = world .spawn(( - Name::new("action button"), + Name::new(format!("{action} button", action = &self.text)), + Action { throw: self.throw }, ButtonBundle { style: Style { align_items: AlignItems::Center, @@ -32,14 +47,13 @@ impl EntityCommand for ActionButton { background_color: Color::GRAY.into(), ..default() }, - // TextBundle::from_section("Cool", style), )) .set_parent(id) .id(); world .spawn(( - Name::new("fart"), + Name::new("label"), TextBundle::from_section(self.text, style), )) .set_parent(button); @@ -49,7 +63,7 @@ impl EntityCommand for ActionButton { fn handle_play(mut events: EventReader, mut commands: Commands) { for _ in events.iter() { let mut bg = commands.spawn(( - Name::new("UI Container"), + Name::new("Play Container"), NodeBundle { style: Style { display: Display::Flex, @@ -121,12 +135,15 @@ fn handle_play(mut events: EventReader, mut commands: Commands) { control_row.add(ActionButton { text: "Rock".into(), + throw: Throw::Rock, }); control_row.add(ActionButton { text: "Paper".into(), + throw: Throw::Paper, }); control_row.add(ActionButton { text: "Scissors".into(), + throw: Throw::Scissors, }); }); } @@ -134,6 +151,6 @@ fn handle_play(mut events: EventReader, mut commands: Commands) { impl Plugin for Play { fn build(&self, app: &mut App) { - app.add_systems(Update, handle_play); + app.add_systems(Update, (handle_play, handle_actions)); } }