Skip to content

Commit

Permalink
dev: collected common systems
Browse files Browse the repository at this point in the history
  • Loading branch information
roboteng committed Oct 14, 2023
1 parent 0f6ccc8 commit 7c0be04
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 44 deletions.
43 changes: 31 additions & 12 deletions game/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,27 @@ fn setup_2d(mut commands: Commands) {
commands.spawn(Camera2dBundle::default());
}

fn button_interaction<T: Component>(
query: Query<&Interaction, (Changed<Interaction>, With<T>)>,
mut window: Query<&mut Window>,
) {
for interaction in &query {
for mut window in &mut window {
window.cursor.icon = match interaction {
Interaction::Pressed => CursorIcon::Default,
Interaction::Hovered => CursorIcon::Hand,
Interaction::None => CursorIcon::Default,
}
}
}
}

pub fn teardown<T: Component>(mut commands: Commands, query: Query<Entity, With<T>>) {
for e in &query {
commands.entity(e).despawn_recursive();
}
}

#[derive(Debug, Default, Hash, PartialEq, Eq, Clone, States)]
enum GameState {
#[default]
Expand All @@ -25,14 +46,18 @@ pub mod settings_page {
use bevy::prelude::*;
use bevy_ui_dsl::*;

use crate::{classes::*, GameState};
use crate::{classes::*, *};

pub struct SettingsPlugin;
impl Plugin for SettingsPlugin {
fn build(&self, app: &mut App) {
app.add_systems(OnEnter(GameState::Settings), draw_settings)
.add_systems(Update, button_actions.run_if(in_state(GameState::Settings)))
.add_systems(OnExit(GameState::Settings), teardown);
.add_systems(
Update,
(button_actions, button_interaction::<SettingsButton>)
.run_if(in_state(GameState::Settings)),
)
.add_systems(OnExit(GameState::Settings), teardown::<OnSettings>);
}
}

Expand All @@ -56,21 +81,15 @@ pub mod settings_page {
query: Query<(&Interaction, &SettingsButton), Changed<Interaction>>,
mut state: ResMut<NextState<GameState>>,
) {
for i in &query {
if i.0 == &Interaction::Pressed {
match i.1 {
for (interaction, button) in &query {
if interaction == &Interaction::Pressed {
match button {
SettingsButton::Back => state.set(GameState::MainMenu),
}
}
}
}

fn teardown(mut commands: Commands, query: Query<Entity, With<OnSettings>>) {
for e in &query {
commands.entity(e).despawn_recursive();
}
}

#[derive(Component)]
enum SettingsButton {
Back,
Expand Down
44 changes: 12 additions & 32 deletions game/src/main_menu.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
use bevy::{app::AppExit, prelude::*};
use bevy_ui_dsl::*;

use crate::{classes::*, GameState};
use crate::{classes::*, *};

pub struct MainMenuPlugin;
impl Plugin for MainMenuPlugin {
fn build(&self, app: &mut App) {
app.add_systems(OnEnter(GameState::MainMenu), draw_main_menu)
.add_systems(
Update,
(button_interaction, button_click).run_if(in_state(GameState::MainMenu)),
(button_interaction::<MainMenuButton>, button_click)
.run_if(in_state(GameState::MainMenu)),
)
.add_systems(OnExit(GameState::MainMenu), teardown);
.add_systems(OnExit(GameState::MainMenu), teardown::<OnMenuScreen>);
}
}

Expand All @@ -34,7 +35,7 @@ fn c_title_test(_: &AssetServer, a: &mut TextStyle) {
struct OnMenuScreen;

#[derive(Component)]
enum MyButton {
enum MainMenuButton {
SinglePlayer,
MultiPlayer,
Settings,
Expand All @@ -51,58 +52,37 @@ fn draw_main_menu(mut commands: Commands, assets: Res<AssetServer>) {
node(c_main_menu, p, |p| {
text("The Tales our\nAncestors Told", (), c_title_test, p);

buttoni(c_button, MyButton::SinglePlayer, p, |p| {
buttoni(c_button, MainMenuButton::SinglePlayer, p, |p| {
text("Single Player", (), c_button_text, p);
});
buttoni(c_button, MyButton::MultiPlayer, p, |p| {
buttoni(c_button, MainMenuButton::MultiPlayer, p, |p| {
text("Multi Player", (), c_button_text, p);
});
buttoni(c_button, MyButton::Settings, p, |p| {
buttoni(c_button, MainMenuButton::Settings, p, |p| {
text("Settings", (), c_button_text, p);
});
buttoni(c_button, MyButton::Quit, p, |p| {
buttoni(c_button, MainMenuButton::Quit, p, |p| {
text("Quit", (), c_button_text, p);
});
});
},
);
}

fn button_interaction(
query: Query<&Interaction, (Changed<Interaction>, With<MyButton>)>,
mut window: Query<&mut Window>,
) {
for interaction in &query {
for mut window in &mut window {
window.cursor.icon = match interaction {
Interaction::Pressed => CursorIcon::Default,
Interaction::Hovered => CursorIcon::Hand,
Interaction::None => CursorIcon::Default,
}
}
}
}

fn button_click(
query: Query<(&Interaction, &MyButton), Changed<Interaction>>,
query: Query<(&Interaction, &MainMenuButton), Changed<Interaction>>,
mut exit_event: ResMut<Events<AppExit>>,
mut states: ResMut<NextState<GameState>>,
) {
for (interaction, button) in &query {
if let Interaction::Pressed = interaction {
match button {
MyButton::Quit => exit_event.send(AppExit),
MyButton::Settings => {
MainMenuButton::Quit => exit_event.send(AppExit),
MainMenuButton::Settings => {
states.set(GameState::Settings);
}
_ => {}
}
}
}
}

fn teardown(mut commands: Commands, q: Query<Entity, With<OnMenuScreen>>) {
for e in &q {
commands.entity(e).despawn_recursive();
}
}

0 comments on commit 7c0be04

Please sign in to comment.