action buttons are clickable now

main
Jordan Orelli 12 months ago
parent a46ab65b3a
commit 9275445bfe

@ -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 {

@ -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");

@ -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<Interaction>, With<Action>)>) {
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<events::Play>, 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<events::Play>, 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<events::Play>, 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));
}
}

Loading…
Cancel
Save