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 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 {}));
}
}

@ -1,4 +1,6 @@
mod events;
mod game;
mod play;
mod start_menu;
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::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<Interaction>, With<InactiveColor>, With<HoverColor>),
(&Interaction, &mut BackgroundColor, &HoverColor),
(Changed<Interaction>, With<HoverColor>),
>,
) {
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<Interaction>, With<PlayButton>)>,
menu: Query<Entity, With<MainMenu>>,
mut commands: Commands,
mut play: EventWriter<events::Play>,
) {
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<Entity, With<MainMenu>>,
mut events: EventReader<events::Play>,
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));
}
}

Loading…
Cancel
Save