action buttons are clickable now

main
Jordan Orelli 1 year ago
parent a46ab65b3a
commit 9275445bfe

@ -1,8 +1,14 @@
use crate::game::Throw;
use bevy::prelude::*; use bevy::prelude::*;
#[derive(Event, Default)] #[derive(Event, Default)]
pub(crate) struct Play; pub(crate) struct Play;
#[derive(Event)]
pub(crate) struct PlayerMove {
throw: Throw,
}
pub(crate) struct Events {} pub(crate) struct Events {}
impl Plugin for Events { impl Plugin for Events {

@ -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 /// The top-level structure of our game, which is a bevy plugin
pub struct Game {} pub struct Game {}
#[derive(Debug)]
pub(crate) enum Throw {
Rock,
Paper,
Scissors,
}
impl Plugin for Game { impl Plugin for Game {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
info!("Building rps Plugin"); info!("Building rps Plugin");

@ -1,12 +1,26 @@
use crate::events; use crate::{events, game::Throw};
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;
pub struct Play {} pub struct Play {}
#[derive(Component, Debug)]
struct Action {
throw: Throw,
}
fn handle_actions(actions: Query<(&Interaction, &Action), (Changed<Interaction>, With<Action>)>) {
for (interaction, action) in actions.iter() {
if matches!(interaction, Interaction::Pressed) {
info!("Clicked {throw:?}", throw = action.throw);
}
}
}
struct ActionButton { struct ActionButton {
text: String, text: String,
throw: Throw,
} }
impl EntityCommand for ActionButton { impl EntityCommand for ActionButton {
@ -19,7 +33,8 @@ impl EntityCommand for ActionButton {
let button = world let button = world
.spawn(( .spawn((
Name::new("action button"), Name::new(format!("{action} button", action = &self.text)),
Action { throw: self.throw },
ButtonBundle { ButtonBundle {
style: Style { style: Style {
align_items: AlignItems::Center, align_items: AlignItems::Center,
@ -32,14 +47,13 @@ impl EntityCommand for ActionButton {
background_color: Color::GRAY.into(), background_color: Color::GRAY.into(),
..default() ..default()
}, },
// TextBundle::from_section("Cool", style),
)) ))
.set_parent(id) .set_parent(id)
.id(); .id();
world world
.spawn(( .spawn((
Name::new("fart"), Name::new("label"),
TextBundle::from_section(self.text, style), TextBundle::from_section(self.text, style),
)) ))
.set_parent(button); .set_parent(button);
@ -49,7 +63,7 @@ impl EntityCommand for ActionButton {
fn handle_play(mut events: EventReader<events::Play>, mut commands: Commands) { fn handle_play(mut events: EventReader<events::Play>, mut commands: Commands) {
for _ in events.iter() { for _ in events.iter() {
let mut bg = commands.spawn(( let mut bg = commands.spawn((
Name::new("UI Container"), Name::new("Play Container"),
NodeBundle { NodeBundle {
style: Style { style: Style {
display: Display::Flex, display: Display::Flex,
@ -121,12 +135,15 @@ fn handle_play(mut events: EventReader<events::Play>, mut commands: Commands) {
control_row.add(ActionButton { control_row.add(ActionButton {
text: "Rock".into(), text: "Rock".into(),
throw: Throw::Rock,
}); });
control_row.add(ActionButton { control_row.add(ActionButton {
text: "Paper".into(), text: "Paper".into(),
throw: Throw::Paper,
}); });
control_row.add(ActionButton { control_row.add(ActionButton {
text: "Scissors".into(), text: "Scissors".into(),
throw: Throw::Scissors,
}); });
}); });
} }
@ -134,6 +151,6 @@ fn handle_play(mut events: EventReader<events::Play>, mut commands: Commands) {
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); app.add_systems(Update, (handle_play, handle_actions));
} }
} }

Loading…
Cancel
Save