diff --git a/src/main.rs b/src/main.rs index 7abcb67..addfe49 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,7 +13,6 @@ mod main_menu; fn main() { App::new() - .init_state::() .add_plugins(( DefaultPlugins, asset_loading::plugin, @@ -22,7 +21,8 @@ fn main() { level_instantiation::map_plugin, main_menu::plugin )) - .add_systems(Startup, setup) + .init_state::() + .add_systems(OnEnter(GameState::Playing), setup) .run(); } diff --git a/src/main_menu.rs b/src/main_menu.rs index 8c5c4b1..c9aad81 100644 --- a/src/main_menu.rs +++ b/src/main_menu.rs @@ -8,7 +8,10 @@ struct OnMainMenuScreen; pub fn plugin(app: &mut App) { app .add_systems(OnEnter(GameState::Menu), setup_main_menu) - .add_systems(OnExit(GameState::Menu), despawn_screen::) + .add_systems(OnExit(GameState::Menu), ( + remove_camera_2d, + despawn_screen:: + )) .add_systems(Update, button_system.run_if(in_state(GameState::Menu))); } @@ -30,6 +33,8 @@ const HOVERED_PRESSED_BUTTON: Color = Color::srgb(0.25, 0.65, 0.25); const PRESSED_BUTTON: Color = Color::srgb(0.35, 0.75, 0.35); fn setup_main_menu(mut commands: Commands) { + commands.spawn(Camera2d); + let button_node = Node { width: Val::Px(300.0), height: Val::Px(65.0), @@ -135,6 +140,12 @@ fn button_system( } } +fn remove_camera_2d(mut commands: Commands, camera_query: Query>) { + for camera in camera_query.iter() { + commands.entity(camera).despawn_recursive(); + } +} + fn despawn_screen(to_despawn: Query>, mut commands: Commands) { for entity in &to_despawn { commands.entity(entity).despawn_recursive(); diff --git a/src/player_plugin.rs b/src/player_plugin.rs index 78a5e58..b65bc51 100644 --- a/src/player_plugin.rs +++ b/src/player_plugin.rs @@ -1,5 +1,5 @@ use bevy::{prelude::*, render::view::RenderLayers, input::mouse::AccumulatedMouseMotion, pbr::NotShadowCaster}; -use crate::physics_plugin::{AccumulatedInput, Velocity, PhysicalTranslation, PreviousPhysicalTranslation}; +use crate::{physics_plugin::{AccumulatedInput, PhysicalTranslation, PreviousPhysicalTranslation, Velocity}, GameState}; #[derive(Debug, Component)] pub struct Player; @@ -20,9 +20,9 @@ struct WorldModelCamera; pub struct PlayerPlugin; impl Plugin for PlayerPlugin { fn build(&self, app: &mut App) { - app.add_systems(Startup, init_player) - .add_systems(Update, move_camera) - .add_systems(RunFixedMainLoop, handle_input.in_set(RunFixedMainLoopSystem::BeforeFixedMainLoop)); + app.add_systems(OnEnter(GameState::Playing), init_player) + .add_systems(Update, move_camera.run_if(in_state(GameState::Playing))) + .add_systems(RunFixedMainLoop, handle_input.in_set(RunFixedMainLoopSystem::BeforeFixedMainLoop).run_if(in_state(GameState::Playing))); } } @@ -65,7 +65,7 @@ pub fn init_player( ..default() }), )); - + // we use a second layer ("framebuffer") to draw the player's arm on // there also a second camera that only views this buffer // this makes it easy because the second camera doesn't move with the player @@ -126,8 +126,8 @@ pub fn handle_input( let right = transform.right(); // Right direction (x axis) if keyboard_input.pressed(KeyCode::KeyW) { - input.x += forward.x; - input.z += forward.z; + input.x += forward.x; + input.z += forward.z; } if keyboard_input.pressed(KeyCode::KeyS) { input.x -= forward.x; @@ -146,5 +146,3 @@ pub fn handle_input( velocity.0 = input.normalize_or_zero() * SPEED; } } - -