cargo fmt

main
Elijah Voigt 4 months ago
parent 93814e9841
commit 989e328718

@ -4,12 +4,17 @@ use games::*;
fn main() { fn main() {
App::new() App::new()
.init_resource::<Thing>() .init_resource::<Thing>()
.add_plugins(BaseGamePlugin { camera: CameraType::Camera3d }) .add_plugins(BaseGamePlugin {
camera: CameraType::Camera3d,
})
.add_systems(Startup, init_ui) .add_systems(Startup, init_ui)
.add_systems(Update, ( .add_systems(
sync_resource_to_ui::<Thing>.run_if(resource_changed::<Thing>), Update,
update_foo.run_if(on_event::<MouseMotion>) (
)) sync_resource_to_ui::<Thing>.run_if(resource_changed::<Thing>),
update_foo.run_if(on_event::<MouseMotion>),
),
)
.run(); .run();
} }
@ -22,28 +27,27 @@ impl Display for Thing {
} }
} }
fn init_ui( fn init_ui(mut commands: Commands) {
mut commands: Commands
) {
commands.spawn(( commands.spawn((
Node { Node {
align_self: AlignSelf::Start, align_self: AlignSelf::Start,
justify_self: JustifySelf::Center, justify_self: JustifySelf::Center,
..default() ..default()
}, },
Text("Displays a sync'd resource tracking the number of mouse events (mod 256)".into()), Text("Displays a sync'd resource tracking the number of mouse events (mod 256)".into()),
)); ));
commands.spawn((Node { commands.spawn((
align_self: AlignSelf::Center, Node {
justify_self: JustifySelf::Center, align_self: AlignSelf::Center,
..default() justify_self: JustifySelf::Center,
}, Text("Placeholder".into()), SyncResource::<Thing>::default())); ..default()
},
Text("Placeholder".into()),
SyncResource::<Thing>::default(),
));
} }
fn update_foo( fn update_foo(mut events: EventReader<MouseMotion>, mut thing: ResMut<Thing>) {
mut events: EventReader<MouseMotion>,
mut thing: ResMut<Thing>,
) {
events.read().for_each(|_| { events.read().for_each(|_| {
thing.0 = thing.0.overflowing_add(1).0; thing.0 = thing.0.overflowing_add(1).0;
}); });

@ -2,31 +2,35 @@ use super::*;
/// A good starting place for creating a game building on top of the base Bevy app /// A good starting place for creating a game building on top of the base Bevy app
pub struct BaseGamePlugin { pub struct BaseGamePlugin {
pub camera: CameraType pub camera: CameraType,
} }
impl Plugin for BaseGamePlugin { impl Plugin for BaseGamePlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.add_plugins(DefaultPlugins) app.add_plugins(DefaultPlugins).add_plugins(DebuggingPlugin);
.add_plugins(DebuggingPlugin);
// Add a camera setup system and startup // Add a camera setup system and startup
app.add_systems(Startup, match self.camera { app.add_systems(
CameraType::Camera2d => setup_camera_2d, Startup,
CameraType::Camera3d => setup_camera_3d, match self.camera {
}); CameraType::Camera2d => setup_camera_2d,
CameraType::Camera3d => setup_camera_3d,
},
);
} }
} }
/// For selecting the type of camera this game requires /// For selecting the type of camera this game requires
pub enum CameraType { pub enum CameraType {
Camera2d, Camera2d,
Camera3d Camera3d,
} }
/// System to toggle the visibility of entities based on their state /// System to toggle the visibility of entities based on their state
pub fn toggle_state_visibility<S: States + Component>(mut q: Query<(Entity, &mut Visibility, &S)>, curr: Res<State<S>>) { pub fn toggle_state_visibility<S: States + Component>(
mut q: Query<(Entity, &mut Visibility, &S)>,
curr: Res<State<S>>,
) {
q.iter_mut().for_each(|(e, mut v, s)| { q.iter_mut().for_each(|(e, mut v, s)| {
if curr.get() == s { if curr.get() == s {
*v = Visibility::Inherited; *v = Visibility::Inherited;
@ -37,14 +41,10 @@ pub fn toggle_state_visibility<S: States + Component>(mut q: Query<(Entity, &mut
}); });
} }
fn setup_camera_2d( fn setup_camera_2d(mut commands: Commands) {
mut commands: Commands,
) {
commands.spawn((Camera { ..default() }, Camera2d)); commands.spawn((Camera { ..default() }, Camera2d));
} }
fn setup_camera_3d( fn setup_camera_3d(mut commands: Commands) {
mut commands: Commands,
) {
commands.spawn((Camera { ..default() }, Camera3d { ..default() })); commands.spawn((Camera { ..default() }, Camera3d { ..default() }));
} }

@ -7,10 +7,14 @@ impl Plugin for DebuggingPlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.init_state::<DebuggingState>() app.init_state::<DebuggingState>()
.add_systems(Startup, init_debug_ui) .add_systems(Startup, init_debug_ui)
.add_systems(Update, ( .add_systems(
toggle_state_visibility::<DebuggingState>.run_if(state_changed::<DebuggingState>), Update,
toggle_debug_state.run_if(on_keyboard_press(KeyCode::F12)) (
)); toggle_state_visibility::<DebuggingState>
.run_if(state_changed::<DebuggingState>),
toggle_debug_state.run_if(on_keyboard_press(KeyCode::F12)),
),
);
} }
} }
@ -39,9 +43,7 @@ fn toggle_debug_state(
} }
/// Create the Debugging UI /// Create the Debugging UI
fn init_debug_ui( fn init_debug_ui(mut commands: Commands) {
mut commands: Commands
) {
commands.spawn(( commands.spawn((
DebuggingState::On, DebuggingState::On,
Text(" Debug: ON ".into()), Text(" Debug: ON ".into()),
@ -52,6 +54,6 @@ fn init_debug_ui(
align_self: AlignSelf::Center, align_self: AlignSelf::Center,
justify_self: JustifySelf::End, justify_self: JustifySelf::End,
..default() ..default()
} },
)); ));
} }

@ -5,7 +5,11 @@ mod ui;
pub use std::fmt::Display; pub use std::fmt::Display;
pub use bevy::{prelude::*, input::{keyboard::KeyboardInput, ButtonState}, color::palettes::css::{BLACK, RED, WHITE}}; pub use bevy::{
color::palettes::css::{BLACK, RED, WHITE},
input::{ButtonState, keyboard::KeyboardInput},
prelude::*,
};
pub use base_game::*; pub use base_game::*;
pub use debug::*; pub use debug::*;

@ -12,7 +12,5 @@ pub fn any_component_changed<T: Component>(q: Query<Entity, Changed<T>>) -> bool
/// Scheduling condition to trigger a system when a specific key is pressed /// Scheduling condition to trigger a system when a specific key is pressed
pub fn on_keyboard_press(key: KeyCode) -> impl FnMut(Res<ButtonInput<KeyCode>>) -> bool { pub fn on_keyboard_press(key: KeyCode) -> impl FnMut(Res<ButtonInput<KeyCode>>) -> bool {
move |keys: Res<ButtonInput<KeyCode>>| { move |keys: Res<ButtonInput<KeyCode>>| keys.just_pressed(key)
keys.just_pressed(key)
}
} }

Loading…
Cancel
Save