oh look we can do events

main
Jordan Orelli 12 months ago
parent 3c4aa78a6d
commit 9de30b29cb

@ -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::<Play>();
}
}

@ -1,7 +1,7 @@
use bevy::prelude::*; use bevy::prelude::*;
use tracing::info; 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 /// The top-level structure of our game, which is a bevy plugin
pub struct Game {} pub struct Game {}
@ -9,7 +9,7 @@ pub struct Game {}
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");
app.add_plugins(StartMenu {}); app.add_plugins((Events {}, StartMenu {}, Play {}));
} }
} }

@ -1,4 +1,6 @@
mod events;
mod game; mod game;
mod play;
mod start_menu; mod start_menu;
use bevy::prelude::*; use bevy::prelude::*;

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

@ -1,3 +1,4 @@
use crate::events;
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;
@ -9,10 +10,10 @@ pub struct StartMenu {}
struct MainMenu; struct MainMenu;
#[derive(Component)] #[derive(Component)]
struct InactiveColor(Color); struct HoverColor {
on: Color,
#[derive(Component)] off: Color,
struct HoverColor(Color); }
#[derive(Component)] #[derive(Component)]
struct Title; struct Title;
@ -21,7 +22,7 @@ struct Title;
struct QuitButton; struct QuitButton;
#[derive(Component)] #[derive(Component)]
struct PlayButton; pub(crate) struct PlayButton;
fn title_text() -> TextBundle { fn title_text() -> TextBundle {
let style = TextStyle { let style = TextStyle {
@ -49,23 +50,18 @@ fn thicc_button() -> ButtonBundle {
fn hover_colors( fn hover_colors(
mut actions: Query< mut actions: Query<
( (&Interaction, &mut BackgroundColor, &HoverColor),
&Interaction, (Changed<Interaction>, With<HoverColor>),
&mut BackgroundColor,
&InactiveColor,
&HoverColor,
),
(Changed<Interaction>, With<InactiveColor>, With<HoverColor>),
>, >,
) { ) {
for (act, mut bg, off, on) in &mut actions { for (act, mut bg, colors) in &mut actions {
match act { match act {
Interaction::Pressed => {} Interaction::Pressed => {}
Interaction::Hovered => { Interaction::Hovered => {
bg.0 = on.0; bg.0 = colors.on;
} }
Interaction::None => { Interaction::None => {
bg.0 = off.0; bg.0 = colors.off;
} }
} }
} }
@ -73,17 +69,26 @@ fn hover_colors(
fn click_play( fn click_play(
mut actions: Query<&Interaction, (Changed<Interaction>, With<PlayButton>)>, mut actions: Query<&Interaction, (Changed<Interaction>, With<PlayButton>)>,
menu: Query<Entity, With<MainMenu>>, mut play: EventWriter<events::Play>,
mut commands: Commands,
) { ) {
for interaction in &mut actions { for interaction in &mut actions {
if matches!(interaction, Interaction::Pressed) { if matches!(interaction, Interaction::Pressed) {
info!("Play button clicked"); play.send_default();
}
}
}
fn handle_play(
menu: Query<Entity, With<MainMenu>>,
mut events: EventReader<events::Play>,
mut commands: Commands,
) {
for _ in events.iter() {
info!("Handling Play event");
for m in menu.iter() { for m in menu.iter() {
commands.entity(m).despawn_recursive(); commands.entity(m).despawn_recursive();
} }
} }
}
} }
fn click_quit( fn click_quit(
@ -163,19 +168,24 @@ fn setup(mut commands: Commands) {
menu.spawn(( menu.spawn((
PlayButton, PlayButton,
thicc_button(), thicc_button(),
HoverColor(green), HoverColor {
InactiveColor(red), on: green,
off: red,
},
)) ))
.add(AddChildLabel { .add(AddChildLabel {
text: String::from("Play"), text: String::from("Play"),
}); });
menu.spawn(vertical_spacer(32.0)); menu.spawn(vertical_spacer(32.0));
menu.spawn(( menu.spawn((
QuitButton, QuitButton,
thicc_button(), thicc_button(),
HoverColor(green), HoverColor {
InactiveColor(red), on: green,
off: red,
},
)) ))
.add(AddChildLabel { .add(AddChildLabel {
text: String::from("Quit"), text: String::from("Quit"),
@ -186,6 +196,6 @@ fn setup(mut commands: Commands) {
impl Plugin for StartMenu { impl Plugin for StartMenu {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.add_systems(Startup, setup); 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));
} }
} }

Loading…
Cancel
Save