fix flow a little bit

This commit is contained in:
AmadeusWM
2025-04-05 19:26:05 +02:00
parent f1788fc942
commit 1328caab5c
3 changed files with 21 additions and 12 deletions

View File

@@ -13,7 +13,6 @@ mod main_menu;
fn main() {
App::new()
.init_state::<GameState>()
.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::<GameState>()
.add_systems(OnEnter(GameState::Playing), setup)
.run();
}

View File

@@ -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::<OnMainMenuScreen>)
.add_systems(OnExit(GameState::Menu), (
remove_camera_2d,
despawn_screen::<OnMainMenuScreen>
))
.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<Entity, With<Camera2d>>) {
for camera in camera_query.iter() {
commands.entity(camera).despawn_recursive();
}
}
fn despawn_screen<T: Component>(to_despawn: Query<Entity, With<T>>, mut commands: Commands) {
for entity in &to_despawn {
commands.entity(entity).despawn_recursive();

View File

@@ -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;
}
}