First person camera

Input States
This commit is contained in:
2025-06-27 19:34:55 -04:00
parent e198ac3cd0
commit 036fc19567
11 changed files with 200 additions and 189 deletions

View File

@@ -37,3 +37,14 @@ impl Default for FollowTarget {
}; };
} }
} }
#[derive(Component, Default, Reflect)]
pub enum CameraMode {
#[default]
Player,
Ship,
Disabled,
}
#[derive(Component, Reflect)]
pub struct CameraAttachment(pub Entity);

View File

@@ -1,4 +1,5 @@
mod plugins; mod plugins;
mod states;
mod utils; mod utils;
use bevy::{prelude::*, window::PresentMode}; use bevy::{prelude::*, window::PresentMode};
use bevy_inspector_egui::{bevy_egui::EguiPlugin, quick::WorldInspectorPlugin}; use bevy_inspector_egui::{bevy_egui::EguiPlugin, quick::WorldInspectorPlugin};

61
src/plugins/camera.rs Normal file
View File

@@ -0,0 +1,61 @@
use bevy::prelude::*;
#[cfg(feature = "dev")]
use bevy::window::PrimaryWindow;
use crate::components::camera::*;
pub struct CameraPlugin;
impl Plugin for CameraPlugin {
fn build(&self, app: &mut App) {
app.add_systems(Update, camera_pitch);
app.add_systems(Update, camera_attachment);
#[cfg(feature = "dev")]
app.add_systems(Update, camera_toggle);
}
}
pub fn camera_attachment(
attachment_targets: Query<&GlobalTransform>,
cam: Single<(&mut Transform, &CameraAttachment, &CameraMode)>,
) {
let (mut transform, attach, mode) = cam.into_inner();
if let Ok(tgt) = attachment_targets.get(attach.0) {
match mode {
CameraMode::Player => {
transform.rotation = tgt.rotation();
transform.translation = tgt.translation();
}
CameraMode::Ship => todo!("Ship Mode"),
CameraMode::Disabled => (),
}
}
}
pub fn camera_pitch(cam_query: Query<(&mut Transform, &CameraPitch)>) {
for (mut cam_transform, pitch) in cam_query {
cam_transform.rotation = Quat::from_rotation_x(pitch.0);
}
}
#[cfg(feature = "dev")]
pub fn camera_toggle(
key: Res<ButtonInput<KeyCode>>,
mut window: Single<&mut Window, With<PrimaryWindow>>,
camera: Single<&mut CameraMode>,
) {
use bevy::window::CursorGrabMode;
let mut mode = camera.into_inner();
if key.just_pressed(KeyCode::Escape) {
if window.cursor_options.visible {
*mode = CameraMode::Player;
window.cursor_options.grab_mode = CursorGrabMode::Locked;
window.cursor_options.visible = false;
} else {
*mode = CameraMode::Disabled;
window.cursor_options.grab_mode = CursorGrabMode::None;
window.cursor_options.visible = true;
}
}
}

View File

@@ -1,71 +0,0 @@
use std::f32::consts::FRAC_PI_2;
use bevy::{input::mouse::MouseMotion, prelude::*};
use crate::{components::camera::*, utils::input::get_mouse_delta};
pub struct FreeCamPlugin;
impl Plugin for FreeCamPlugin {
fn build(&self, app: &mut App) {
app.add_systems(Update, ((camera_yaw, camera_pitch).chain(), free_camera));
}
}
const FLY_SPEED: f32 = 10.0;
pub fn free_camera(
cam_query: Single<(&mut Transform, &FreeCam), (With<CameraRoot>, Without<Unfocused>)>,
cam_pitch: Single<&CameraPitch>,
key: Res<ButtonInput<KeyCode>>,
time: Res<Time>,
) {
let mut move_dir = Vec3::ZERO;
let pitch = cam_pitch.0;
let (mut cam_transform, freecam) = cam_query.into_inner();
if !freecam.0 {
return;
}
if key.pressed(KeyCode::KeyW) {
move_dir.z = -1.0;
} else if key.pressed(KeyCode::KeyS) {
move_dir.z = 1.0;
}
if key.pressed(KeyCode::KeyD) {
move_dir.x = 1.0;
} else if key.pressed(KeyCode::KeyA) {
move_dir.x = -1.0;
}
move_dir = cam_transform.rotation
* Quat::from_rotation_x(pitch)
* move_dir.normalize_or_zero()
* time.delta_secs()
* FLY_SPEED;
cam_transform.translation += move_dir;
}
pub fn camera_yaw(
cam_yaw_query: Single<(&mut Transform, &FreeCam), (With<CameraRoot>, Without<Unfocused>)>,
cam_query: Single<&mut CameraPitch>,
mouse_motion: EventReader<MouseMotion>,
time: Res<Time>,
) {
let (mut yaw_transform, freecam) = cam_yaw_query.into_inner();
if !freecam.0 {
return;
}
let mut pitch = cam_query.into_inner();
let delta = get_mouse_delta(mouse_motion) * -time.delta_secs();
yaw_transform.rotate_y(delta.x);
pitch.0 = (pitch.0 + delta.y).clamp(-FRAC_PI_2 + 0.001, FRAC_PI_2 - 0.001);
}
pub fn camera_pitch(cam_query: Single<(&mut Transform, &CameraPitch)>) {
let (mut cam_transform, pitch) = cam_query.into_inner();
cam_transform.rotation = Quat::from_rotation_x(pitch.0);
}

View File

@@ -1,10 +1,11 @@
use crate::{ use crate::{
components::{ components::{
camera::{CameraPitch, CameraRoot, FollowCam, MainCamera, Unfocused}, camera::{CameraAttachment, CameraMode, CameraPitch, FollowCam, MainCamera},
player::{GravityDirection, MoveSpeed, PlayerDrag}, player::PlayerDrag,
tags::{Player, Ship}, tags::{Player, Ship},
}, },
plugins::*, plugins::{state_management::StateManagementPlugin, *},
states::input::{InputState, PlayerState},
}; };
use bevy::{ use bevy::{
prelude::*, prelude::*,
@@ -19,19 +20,18 @@ impl Plugin for GamePlugin {
fn build(&self, app: &mut bevy::app::App) { fn build(&self, app: &mut bevy::app::App) {
app.add_plugins(( app.add_plugins((
FollowCamPlugin, FollowCamPlugin,
CameraPlugin,
StateManagementPlugin,
// ShipPlugin, // ShipPlugin,
TypesPlugin, TypesPlugin,
PlayerPlugin, PlayerPlugin,
// CharacterControllerPlugin,
)); ));
app.add_plugins(( app.add_plugins((
RapierPhysicsPlugin::<NoUserData>::default(), RapierPhysicsPlugin::<NoUserData>::default(),
#[cfg(feature = "dev-phys")] #[cfg(feature = "dev-phys")]
RapierDebugRenderPlugin::default(), RapierDebugRenderPlugin::default(),
)); ));
app.add_systems(Startup, (setup_scene).chain()); app.add_systems(Startup, (setup_scene, spawn_ship).chain());
app.add_systems(Update, camera_toggle);
} }
} }
@@ -41,11 +41,17 @@ fn setup_scene(
mut materials: ResMut<Assets<StandardMaterial>>, mut materials: ResMut<Assets<StandardMaterial>>,
mut window: Single<&mut Window, With<PrimaryWindow>>, mut window: Single<&mut Window, With<PrimaryWindow>>,
) { ) {
// window.cursor_options.visible = false; window.cursor_options.visible = false;
// window.cursor_options.grab_mode = CursorGrabMode::Locked; window.cursor_options.grab_mode = CursorGrabMode::Locked;
let player = commands let player_eye = commands
.spawn(( .spawn((
Name::new("Eye"),
Transform::from_translation(Vec3::new(0.0, 1.0, 0.0)),
CameraPitch::default(),
))
.id();
let mut player_commands = commands.spawn((
Name::new("Player"), Name::new("Player"),
Player, Player,
PlayerDrag(0.5), PlayerDrag(0.5),
@@ -55,25 +61,17 @@ fn setup_scene(
Mesh3d(meshes.add(Capsule3d::new(0.5, 1.0))), Mesh3d(meshes.add(Capsule3d::new(0.5, 1.0))),
MeshMaterial3d(materials.add(Color::linear_rgb(1.0, 0.0, 0.2))), MeshMaterial3d(materials.add(Color::linear_rgb(1.0, 0.0, 0.2))),
Transform::from_translation(Vec3::new(0.0, 10.0, 10.0)), Transform::from_translation(Vec3::new(0.0, 10.0, 10.0)),
)) ));
.id(); player_commands.add_child(player_eye);
commands.spawn(( commands.spawn((
Name::new("Camera Root"), Name::new("Camera"),
Transform::from_xyz(0.0, 1.3, 0.0), Transform::from_xyz(0.0, 1.3, 0.0),
CameraRoot,
Visibility::default(), Visibility::default(),
FollowCam {
distance: 20.,
height: 10.,
target: player,
},
children![(
Camera3d::default(),
CameraPitch::default(),
MainCamera, MainCamera,
Transform::default() Camera3d::default(),
)], CameraMode::Player,
CameraAttachment(player_eye),
)); ));
commands.spawn(( commands.spawn((
@@ -102,14 +100,12 @@ fn spawn_ship(
mut commands: Commands, mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>, mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>, mut materials: ResMut<Assets<StandardMaterial>>,
camera: Single<Entity, With<MainCamera>>,
) { ) {
let window_material = materials.add(Color::linear_rgba(1.0, 0.0, 1.0, 0.5)); let window_material = materials.add(Color::linear_rgba(1.0, 0.0, 1.0, 0.5));
let material = materials.add(Color::BLACK); let material = materials.add(Color::BLACK);
let ship = commands commands.spawn((
.spawn((
Mesh3d(meshes.add(Cuboid::new(3.0, 0.1, 6.0))), Mesh3d(meshes.add(Cuboid::new(3.0, 0.1, 6.0))),
MeshMaterial3d(material.clone()), MeshMaterial3d(material.clone()),
Name::new("Ship"), Name::new("Ship"),
@@ -155,31 +151,5 @@ fn spawn_ship(
Transform::from_xyz(0.0, 2.0, 0.0), Transform::from_xyz(0.0, 2.0, 0.0),
) )
], ],
)) ));
.id();
commands.entity(camera.into_inner()).insert(FollowCam {
distance: 20.,
height: 10.,
target: ship,
});
}
pub fn camera_toggle(
key: Res<ButtonInput<KeyCode>>,
mut window: Single<&mut Window, With<PrimaryWindow>>,
camera: Single<Entity, With<CameraRoot>>,
mut commands: Commands,
) {
if key.just_pressed(KeyCode::Escape) {
if window.cursor_options.visible {
commands.entity(camera.into_inner()).remove::<Unfocused>();
window.cursor_options.grab_mode = CursorGrabMode::Locked;
window.cursor_options.visible = false;
} else {
commands.entity(camera.into_inner()).insert(Unfocused);
window.cursor_options.grab_mode = CursorGrabMode::None;
window.cursor_options.visible = true;
}
}
} }

View File

@@ -1,15 +1,16 @@
// mod character_controller; // mod character_controller;
mod camera;
mod follow_cam; mod follow_cam;
mod free_cam;
mod game; mod game;
mod player; mod player;
mod ship; mod ship;
mod ship_cam; mod ship_cam;
mod state_management;
mod types; mod types;
// pub use character_controller::*; // pub use character_controller::*;
pub use camera::*;
pub use follow_cam::*; pub use follow_cam::*;
pub use free_cam::*;
pub use game::*; pub use game::*;
pub use player::*; pub use player::*;
pub use ship::*; pub use ship::*;

View File

@@ -1,17 +1,16 @@
use std::f32::EPSILON; use std::f32::{EPSILON, consts::FRAC_PI_2};
use bevy::{input::mouse::MouseMotion, prelude::*}; use bevy::{input::mouse::MouseMotion, prelude::*};
use bevy_rapier3d::prelude::*; use bevy_rapier3d::prelude::*;
use crate::{ use crate::{
components::{ components::{
camera::CameraPitch,
player::{GravityDirection, JumpSpeed, MoveSpeed, PlayerDrag, PlayerForce, PlayerMotion, PlayerVelocity}, player::{GravityDirection, JumpSpeed, MoveSpeed, PlayerDrag, PlayerForce, PlayerMotion, PlayerVelocity},
tags::Player, tags::Player,
}, },
utils::{ states::input::PlayerInputSystems,
input::get_mouse_delta, utils::{input::get_mouse_delta, rotation::get_alignment_rotation_preserve_twist},
rotation::{get_alignment_rotation, get_alignment_rotation_preserve_twist},
},
}; };
pub struct PlayerPlugin; pub struct PlayerPlugin;
@@ -24,9 +23,6 @@ impl Plugin for PlayerPlugin {
} }
} }
#[derive(SystemSet, Debug, Clone, PartialEq, Eq, Hash)]
pub struct PlayerInputSystems;
fn apply_forces(player: Single<(&mut PlayerVelocity, &PlayerForce), With<Player>>, time: Res<Time>) { fn apply_forces(player: Single<(&mut PlayerVelocity, &PlayerForce), With<Player>>, time: Res<Time>) {
let (mut vel, force) = player.into_inner(); let (mut vel, force) = player.into_inner();
@@ -129,12 +125,14 @@ fn keyboard_input(
fn player_look( fn player_look(
mut player: Single<&mut Transform, With<Player>>, mut player: Single<&mut Transform, With<Player>>,
mut cam_pitch: Single<&mut CameraPitch>,
mouse_motion: EventReader<MouseMotion>, mouse_motion: EventReader<MouseMotion>,
time: Res<Time>, time: Res<Time>,
) { ) {
let delta = get_mouse_delta(mouse_motion) * -time.delta_secs(); let delta = get_mouse_delta(mouse_motion) * -time.delta_secs();
let up = player.up(); let up = player.up();
player.rotate_axis(up, delta.x); player.rotate_axis(up, delta.x);
cam_pitch.0 = (cam_pitch.0 + delta.y).clamp(-FRAC_PI_2 + 0.001, FRAC_PI_2 - 0.001);
} }
fn align_with_gravity( fn align_with_gravity(

View File

@@ -0,0 +1,16 @@
use bevy::prelude::*;
use crate::states::input::{InputState, PlayerInputSystems, PlayerState};
pub struct StateManagementPlugin;
impl Plugin for StateManagementPlugin {
fn build(&self, app: &mut App) {
app.init_state::<PlayerState>();
app.init_state::<InputState>();
app.configure_sets(PreUpdate, PlayerInputSystems.run_if(in_state(InputState::World)));
app.configure_sets(Update, PlayerInputSystems.run_if(in_state(InputState::World)));
app.configure_sets(PostUpdate, PlayerInputSystems.run_if(in_state(InputState::World)));
}
}

View File

@@ -1,7 +1,7 @@
use bevy::prelude::*; use bevy::prelude::*;
use crate::components::{ use crate::components::{
camera::{FollowCam, FollowTarget}, camera::{CameraAttachment, CameraMode, CameraPitch, FollowCam, FollowTarget},
player::{GravityDirection, MoveSpeed, PlayerForce, PlayerMotion, PlayerVelocity}, player::{GravityDirection, MoveSpeed, PlayerForce, PlayerMotion, PlayerVelocity},
}; };
@@ -16,5 +16,8 @@ impl Plugin for TypesPlugin {
app.register_type::<PlayerMotion>(); app.register_type::<PlayerMotion>();
app.register_type::<GravityDirection>(); app.register_type::<GravityDirection>();
app.register_type::<MoveSpeed>(); app.register_type::<MoveSpeed>();
app.register_type::<CameraPitch>();
app.register_type::<CameraAttachment>();
app.register_type::<CameraMode>();
} }
} }

20
src/states/input.rs Normal file
View File

@@ -0,0 +1,20 @@
use bevy::prelude::*;
#[derive(States, Debug, Default, Clone, PartialEq, Eq, Hash)]
pub enum InputState {
#[default]
World,
Menu,
Detached,
}
#[derive(SystemSet, Debug, Clone, PartialEq, Eq, Hash)]
pub struct PlayerInputSystems;
#[derive(States, Debug, Default, Clone, PartialEq, Eq, Hash)]
pub enum PlayerState {
#[default]
OnFoot,
FreeFloating,
Piloting,
}

1
src/states/mod.rs Normal file
View File

@@ -0,0 +1 @@
pub mod input;