From e71e840d9f8425a69ff5ecf69b3fe2fb4ef52a38 Mon Sep 17 00:00:00 2001 From: LorrensP-2158466 Date: Sat, 5 Apr 2025 19:39:59 +0200 Subject: [PATCH] hide cursor --- src/bevy_plugin.rs | 22 +++++++++++++++ src/main.rs | 14 ++++------ src/{player_plugin.rs => player.rs} | 43 +++++++++++++++++------------ 3 files changed, 54 insertions(+), 25 deletions(-) create mode 100644 src/bevy_plugin.rs rename src/{player_plugin.rs => player.rs} (82%) diff --git a/src/bevy_plugin.rs b/src/bevy_plugin.rs new file mode 100644 index 0000000..bbdbd27 --- /dev/null +++ b/src/bevy_plugin.rs @@ -0,0 +1,22 @@ +use bevy::{prelude::*, window::CursorOptions}; + +/// Overrides the default Bevy plugins and configures things like the screen settings. +pub(super) fn plugin(app: &mut App) { + app.add_plugins( + DefaultPlugins.set(WindowPlugin { + primary_window: Window { + title: "Among Me".into(), + // This will spawn an invisible window + // The window will be made visible in the make_visible() system after 3 frames. + // This is useful when you want to avoid the white window that shows up before the GPU is ready to render the app. + cursor_options: CursorOptions { + visible: false, + ..default() + }, + ..default() + } + .into(), + ..default() + }), + ); +} diff --git a/src/main.rs b/src/main.rs index addfe49..4bfb43a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,25 +1,23 @@ -use std::default; - use asset_loading::ImageAssets; use bevy::prelude::*; -use player_plugin::PlayerPlugin; use physics_plugin::PhysicsPlugin; mod asset_loading; -mod player_plugin; -mod physics_plugin; +mod bevy_plugin; mod level_instantiation; mod main_menu; +mod physics_plugin; +mod player; fn main() { App::new() .add_plugins(( - DefaultPlugins, + bevy_plugin::plugin, asset_loading::plugin, - PlayerPlugin, + player::plugin, PhysicsPlugin, level_instantiation::map_plugin, - main_menu::plugin + main_menu::plugin, )) .init_state::() .add_systems(OnEnter(GameState::Playing), setup) diff --git a/src/player_plugin.rs b/src/player.rs similarity index 82% rename from src/player_plugin.rs rename to src/player.rs index b65bc51..ea3310f 100644 --- a/src/player_plugin.rs +++ b/src/player.rs @@ -1,5 +1,13 @@ -use bevy::{prelude::*, render::view::RenderLayers, input::mouse::AccumulatedMouseMotion, pbr::NotShadowCaster}; -use crate::{physics_plugin::{AccumulatedInput, PhysicalTranslation, PreviousPhysicalTranslation, Velocity}, GameState}; +use crate::{ + GameState, + physics_plugin::{ + AccumulatedInput, PhysicalTranslation, PreviousPhysicalTranslation, Velocity, + }, +}; +use bevy::{ + input::mouse::AccumulatedMouseMotion, pbr::NotShadowCaster, prelude::*, + render::view::RenderLayers, +}; #[derive(Debug, Component)] pub struct Player; @@ -8,26 +16,24 @@ pub struct Player; pub struct CameraSensitivity(Vec2); impl Default for CameraSensitivity { fn default() -> Self { - Self( - Vec2::new(0.003, 0.002), - ) + Self(Vec2::new(0.003, 0.002)) } } #[derive(Debug, Component)] struct WorldModelCamera; -pub struct PlayerPlugin; -impl Plugin for PlayerPlugin { - fn build(&self, app: &mut App) { - 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))); - } +pub fn plugin(app: &mut App) { + 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)), + ); } - - /// Used implicitly by all entities without a `RenderLayers` component. /// Our world model camera and all objects other than the player are on this layer. /// The light source belongs to both layers. @@ -92,7 +98,6 @@ pub fn init_player( }); } - use std::f32::consts::FRAC_PI_2; const PITCH_LIMIT: f32 = FRAC_PI_2 - 0.01; @@ -141,8 +146,12 @@ pub fn handle_input( input.x += right.x; input.z += right.z; } - if keyboard_input.pressed(KeyCode::Space) { input.y += 1.0; } - if keyboard_input.pressed(KeyCode::ShiftLeft) { input.y -= 1.0; } + if keyboard_input.pressed(KeyCode::Space) { + input.y += 1.0; + } + if keyboard_input.pressed(KeyCode::ShiftLeft) { + input.y -= 1.0; + } velocity.0 = input.normalize_or_zero() * SPEED; } }