diff --git a/src/events.rs b/src/events.rs new file mode 100644 index 0000000..1898bc1 --- /dev/null +++ b/src/events.rs @@ -0,0 +1,12 @@ +use bevy::prelude::*; + +#[derive(Event, Default)] +pub(crate) struct Play; + +pub(crate) struct Events {} + +impl Plugin for Events { + fn build(&self, app: &mut App) { + app.add_event::(); + } +} diff --git a/src/game.rs b/src/game.rs index 87fbfa8..d20fd25 100644 --- a/src/game.rs +++ b/src/game.rs @@ -1,7 +1,7 @@ use bevy::prelude::*; use tracing::info; -use crate::start_menu::StartMenu; +use crate::{events::Events, play::Play, start_menu::StartMenu}; /// The top-level structure of our game, which is a bevy plugin pub struct Game {} @@ -9,7 +9,7 @@ pub struct Game {} impl Plugin for Game { fn build(&self, app: &mut App) { info!("Building rps Plugin"); - app.add_plugins(StartMenu {}); + app.add_plugins((Events {}, StartMenu {}, Play {})); } } diff --git a/src/main.rs b/src/main.rs index 1c920d1..9e9b50e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,6 @@ +mod events; mod game; +mod play; mod start_menu; use bevy::prelude::*; diff --git a/src/play.rs b/src/play.rs new file mode 100644 index 0000000..4262203 --- /dev/null +++ b/src/play.rs @@ -0,0 +1,126 @@ +use crate::events; +use bevy::prelude::*; + +pub struct Play {} + +fn action_button() -> ButtonBundle { + ButtonBundle { + style: Style { + align_items: AlignItems::Center, + width: Val::Percent(100.0), + height: Val::Percent(100.0), + ..default() + }, + background_color: Color::GRAY.into(), + ..default() + } +} + +fn handle_play(mut events: EventReader, mut commands: Commands) { + for _ in events.iter() { + let mut bg = commands.spawn(NodeBundle { + style: Style { + display: Display::Flex, + flex_direction: FlexDirection::Column, + height: Val::Percent(100.0), + width: Val::Percent(100.0), + ..default() + }, + background_color: Color::GRAY.into(), + ..default() + }); + + bg.with_children(|bg| { + // header + bg.spawn(NodeBundle { + style: Style { + display: Display::Flex, + height: Val::Percent(10.0), + width: Val::Percent(100.0), + border: UiRect { + bottom: Val::Px(2.0), + ..default() + }, + ..default() + }, + border_color: Color::BLACK.into(), + background_color: Color::GRAY.into(), + ..default() + }); + + // main play area + bg.spawn(NodeBundle { + style: Style { + display: Display::Flex, + height: Val::Percent(50.0), + width: Val::Percent(100.0), + border: UiRect { + bottom: Val::Px(2.0), + ..default() + }, + ..default() + }, + background_color: Color::GRAY.into(), + border_color: Color::BLACK.into(), + ..default() + }); + + // Action controls + let mut control_row = bg.spawn(NodeBundle { + style: Style { + flex_direction: FlexDirection::Row, + display: Display::Flex, + height: Val::Percent(40.0), + width: Val::Percent(100.0), + ..default() + }, + background_color: Color::GRAY.into(), + ..default() + }); + control_row.with_children(|controls| { + controls.spawn(NodeBundle { + style: Style { + display: Display::Flex, + height: Val::Percent(100.0), + width: Val::Percent(100.0), + border: UiRect::all(Val::Px(8.0)), + ..default() + }, + border_color: Color::RED.into(), + background_color: Color::GRAY.into(), + ..default() + }); + controls.spawn(NodeBundle { + style: Style { + display: Display::Flex, + height: Val::Percent(100.0), + width: Val::Percent(100.0), + border: UiRect::all(Val::Px(8.0)), + ..default() + }, + border_color: Color::RED.into(), + background_color: Color::GRAY.into(), + ..default() + }); + controls.spawn(NodeBundle { + style: Style { + display: Display::Flex, + height: Val::Percent(100.0), + width: Val::Percent(100.0), + border: UiRect::all(Val::Px(8.0)), + ..default() + }, + border_color: Color::RED.into(), + background_color: Color::GRAY.into(), + ..default() + }); + }); + }); + } +} + +impl Plugin for Play { + fn build(&self, app: &mut App) { + app.add_systems(Update, handle_play); + } +} diff --git a/src/start_menu.rs b/src/start_menu.rs index 15a9ddc..44669a9 100644 --- a/src/start_menu.rs +++ b/src/start_menu.rs @@ -1,3 +1,4 @@ +use crate::events; use bevy::ecs::system::EntityCommand; use bevy::prelude::*; use bevy::text::DEFAULT_FONT_HANDLE; @@ -9,10 +10,10 @@ pub struct StartMenu {} struct MainMenu; #[derive(Component)] -struct InactiveColor(Color); - -#[derive(Component)] -struct HoverColor(Color); +struct HoverColor { + on: Color, + off: Color, +} #[derive(Component)] struct Title; @@ -21,7 +22,7 @@ struct Title; struct QuitButton; #[derive(Component)] -struct PlayButton; +pub(crate) struct PlayButton; fn title_text() -> TextBundle { let style = TextStyle { @@ -49,23 +50,18 @@ fn thicc_button() -> ButtonBundle { fn hover_colors( mut actions: Query< - ( - &Interaction, - &mut BackgroundColor, - &InactiveColor, - &HoverColor, - ), - (Changed, With, With), + (&Interaction, &mut BackgroundColor, &HoverColor), + (Changed, With), >, ) { - for (act, mut bg, off, on) in &mut actions { + for (act, mut bg, colors) in &mut actions { match act { Interaction::Pressed => {} Interaction::Hovered => { - bg.0 = on.0; + bg.0 = colors.on; } Interaction::None => { - bg.0 = off.0; + bg.0 = colors.off; } } } @@ -73,15 +69,24 @@ fn hover_colors( fn click_play( mut actions: Query<&Interaction, (Changed, With)>, - menu: Query>, - mut commands: Commands, + mut play: EventWriter, ) { for interaction in &mut actions { if matches!(interaction, Interaction::Pressed) { - info!("Play button clicked"); - for m in menu.iter() { - commands.entity(m).despawn_recursive(); - } + play.send_default(); + } + } +} + +fn handle_play( + menu: Query>, + mut events: EventReader, + mut commands: Commands, +) { + for _ in events.iter() { + info!("Handling Play event"); + for m in menu.iter() { + commands.entity(m).despawn_recursive(); } } } @@ -163,19 +168,24 @@ fn setup(mut commands: Commands) { menu.spawn(( PlayButton, thicc_button(), - HoverColor(green), - InactiveColor(red), + HoverColor { + on: green, + off: red, + }, )) .add(AddChildLabel { text: String::from("Play"), }); menu.spawn(vertical_spacer(32.0)); + menu.spawn(( QuitButton, thicc_button(), - HoverColor(green), - InactiveColor(red), + HoverColor { + on: green, + off: red, + }, )) .add(AddChildLabel { text: String::from("Quit"), @@ -186,6 +196,6 @@ fn setup(mut commands: Commands) { impl Plugin for StartMenu { fn build(&self, app: &mut App) { app.add_systems(Startup, setup); - app.add_systems(Update, (hover_colors, click_quit, click_play)); + app.add_systems(Update, (hover_colors, click_quit, click_play, handle_play)); } }