hide cursor

This commit is contained in:
LorrensP-2158466
2025-04-05 19:39:59 +02:00
parent 1328caab5c
commit e71e840d9f
3 changed files with 54 additions and 25 deletions

22
src/bevy_plugin.rs Normal file
View File

@@ -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()
}),
);
}

View File

@@ -1,25 +1,23 @@
use std::default;
use asset_loading::ImageAssets; use asset_loading::ImageAssets;
use bevy::prelude::*; use bevy::prelude::*;
use player_plugin::PlayerPlugin;
use physics_plugin::PhysicsPlugin; use physics_plugin::PhysicsPlugin;
mod asset_loading; mod asset_loading;
mod player_plugin; mod bevy_plugin;
mod physics_plugin;
mod level_instantiation; mod level_instantiation;
mod main_menu; mod main_menu;
mod physics_plugin;
mod player;
fn main() { fn main() {
App::new() App::new()
.add_plugins(( .add_plugins((
DefaultPlugins, bevy_plugin::plugin,
asset_loading::plugin, asset_loading::plugin,
PlayerPlugin, player::plugin,
PhysicsPlugin, PhysicsPlugin,
level_instantiation::map_plugin, level_instantiation::map_plugin,
main_menu::plugin main_menu::plugin,
)) ))
.init_state::<GameState>() .init_state::<GameState>()
.add_systems(OnEnter(GameState::Playing), setup) .add_systems(OnEnter(GameState::Playing), setup)

View File

@@ -1,5 +1,13 @@
use bevy::{prelude::*, render::view::RenderLayers, input::mouse::AccumulatedMouseMotion, pbr::NotShadowCaster}; use crate::{
use crate::{physics_plugin::{AccumulatedInput, PhysicalTranslation, PreviousPhysicalTranslation, Velocity}, GameState}; GameState,
physics_plugin::{
AccumulatedInput, PhysicalTranslation, PreviousPhysicalTranslation, Velocity,
},
};
use bevy::{
input::mouse::AccumulatedMouseMotion, pbr::NotShadowCaster, prelude::*,
render::view::RenderLayers,
};
#[derive(Debug, Component)] #[derive(Debug, Component)]
pub struct Player; pub struct Player;
@@ -8,26 +16,24 @@ pub struct Player;
pub struct CameraSensitivity(Vec2); pub struct CameraSensitivity(Vec2);
impl Default for CameraSensitivity { impl Default for CameraSensitivity {
fn default() -> Self { fn default() -> Self {
Self( Self(Vec2::new(0.003, 0.002))
Vec2::new(0.003, 0.002),
)
} }
} }
#[derive(Debug, Component)] #[derive(Debug, Component)]
struct WorldModelCamera; struct WorldModelCamera;
pub struct PlayerPlugin; pub fn plugin(app: &mut App) {
impl Plugin for PlayerPlugin { app.add_systems(OnEnter(GameState::Playing), init_player)
fn build(&self, app: &mut App) { .add_systems(Update, move_camera.run_if(in_state(GameState::Playing)))
app.add_systems(OnEnter(GameState::Playing), init_player) .add_systems(
.add_systems(Update, move_camera.run_if(in_state(GameState::Playing))) RunFixedMainLoop,
.add_systems(RunFixedMainLoop, handle_input.in_set(RunFixedMainLoopSystem::BeforeFixedMainLoop).run_if(in_state(GameState::Playing))); handle_input
} .in_set(RunFixedMainLoopSystem::BeforeFixedMainLoop)
.run_if(in_state(GameState::Playing)),
);
} }
/// Used implicitly by all entities without a `RenderLayers` component. /// Used implicitly by all entities without a `RenderLayers` component.
/// Our world model camera and all objects other than the player are on this layer. /// Our world model camera and all objects other than the player are on this layer.
/// The light source belongs to both layers. /// The light source belongs to both layers.
@@ -92,7 +98,6 @@ pub fn init_player(
}); });
} }
use std::f32::consts::FRAC_PI_2; use std::f32::consts::FRAC_PI_2;
const PITCH_LIMIT: f32 = FRAC_PI_2 - 0.01; const PITCH_LIMIT: f32 = FRAC_PI_2 - 0.01;
@@ -141,8 +146,12 @@ pub fn handle_input(
input.x += right.x; input.x += right.x;
input.z += right.z; input.z += right.z;
} }
if keyboard_input.pressed(KeyCode::Space) { input.y += 1.0; } if keyboard_input.pressed(KeyCode::Space) {
if keyboard_input.pressed(KeyCode::ShiftLeft) { input.y -= 1.0; } input.y += 1.0;
}
if keyboard_input.pressed(KeyCode::ShiftLeft) {
input.y -= 1.0;
}
velocity.0 = input.normalize_or_zero() * SPEED; velocity.0 = input.normalize_or_zero() * SPEED;
} }
} }