cargo fmt

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

@ -4,12 +4,17 @@ use games::*;
fn main() {
App::new()
.init_resource::<Thing>()
.add_plugins(BaseGamePlugin { camera: CameraType::Camera3d })
.add_plugins(BaseGamePlugin {
camera: CameraType::Camera3d,
})
.add_systems(Startup, init_ui)
.add_systems(Update, (
.add_systems(
Update,
(
sync_resource_to_ui::<Thing>.run_if(resource_changed::<Thing>),
update_foo.run_if(on_event::<MouseMotion>)
))
update_foo.run_if(on_event::<MouseMotion>),
),
)
.run();
}
@ -22,9 +27,7 @@ impl Display for Thing {
}
}
fn init_ui(
mut commands: Commands
) {
fn init_ui(mut commands: Commands) {
commands.spawn((
Node {
align_self: AlignSelf::Start,
@ -33,17 +36,18 @@ fn init_ui(
},
Text("Displays a sync'd resource tracking the number of mouse events (mod 256)".into()),
));
commands.spawn((Node {
commands.spawn((
Node {
align_self: AlignSelf::Center,
justify_self: JustifySelf::Center,
..default()
}, Text("Placeholder".into()), SyncResource::<Thing>::default()));
},
Text("Placeholder".into()),
SyncResource::<Thing>::default(),
));
}
fn update_foo(
mut events: EventReader<MouseMotion>,
mut thing: ResMut<Thing>,
) {
fn update_foo(mut events: EventReader<MouseMotion>, mut thing: ResMut<Thing>) {
events.read().for_each(|_| {
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
pub struct BaseGamePlugin {
pub camera: CameraType
pub camera: CameraType,
}
impl Plugin for BaseGamePlugin {
fn build(&self, app: &mut App) {
app.add_plugins(DefaultPlugins)
.add_plugins(DebuggingPlugin);
app.add_plugins(DefaultPlugins).add_plugins(DebuggingPlugin);
// Add a camera setup system and startup
app.add_systems(Startup, match self.camera {
app.add_systems(
Startup,
match self.camera {
CameraType::Camera2d => setup_camera_2d,
CameraType::Camera3d => setup_camera_3d,
});
},
);
}
}
/// For selecting the type of camera this game requires
pub enum CameraType {
Camera2d,
Camera3d
Camera3d,
}
/// 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)| {
if curr.get() == s {
*v = Visibility::Inherited;
@ -37,14 +41,10 @@ pub fn toggle_state_visibility<S: States + Component>(mut q: Query<(Entity, &mut
});
}
fn setup_camera_2d(
mut commands: Commands,
) {
fn setup_camera_2d(mut commands: Commands) {
commands.spawn((Camera { ..default() }, Camera2d));
}
fn setup_camera_3d(
mut commands: Commands,
) {
fn setup_camera_3d(mut commands: Commands) {
commands.spawn((Camera { ..default() }, Camera3d { ..default() }));
}

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

@ -5,7 +5,11 @@ mod ui;
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 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
pub fn on_keyboard_press(key: KeyCode) -> impl FnMut(Res<ButtonInput<KeyCode>>) -> bool {
move |keys: Res<ButtonInput<KeyCode>>| {
keys.just_pressed(key)
}
move |keys: Res<ButtonInput<KeyCode>>| keys.just_pressed(key)
}

Loading…
Cancel
Save