First person camera
Input States
This commit is contained in:
@@ -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);
|
||||||
|
|||||||
@@ -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
61
src/plugins/camera.rs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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);
|
|
||||||
}
|
|
||||||
@@ -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,39 +41,37 @@ 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("Player"),
|
Name::new("Eye"),
|
||||||
Player,
|
Transform::from_translation(Vec3::new(0.0, 1.0, 0.0)),
|
||||||
PlayerDrag(0.5),
|
CameraPitch::default(),
|
||||||
Collider::capsule_y(0.5, 0.5),
|
|
||||||
RigidBody::KinematicPositionBased,
|
|
||||||
KinematicCharacterController::default(),
|
|
||||||
Mesh3d(meshes.add(Capsule3d::new(0.5, 1.0))),
|
|
||||||
MeshMaterial3d(materials.add(Color::linear_rgb(1.0, 0.0, 0.2))),
|
|
||||||
Transform::from_translation(Vec3::new(0.0, 10.0, 10.0)),
|
|
||||||
))
|
))
|
||||||
.id();
|
.id();
|
||||||
|
let mut player_commands = commands.spawn((
|
||||||
|
Name::new("Player"),
|
||||||
|
Player,
|
||||||
|
PlayerDrag(0.5),
|
||||||
|
Collider::capsule_y(0.5, 0.5),
|
||||||
|
RigidBody::KinematicPositionBased,
|
||||||
|
KinematicCharacterController::default(),
|
||||||
|
Mesh3d(meshes.add(Capsule3d::new(0.5, 1.0))),
|
||||||
|
MeshMaterial3d(materials.add(Color::linear_rgb(1.0, 0.0, 0.2))),
|
||||||
|
Transform::from_translation(Vec3::new(0.0, 10.0, 10.0)),
|
||||||
|
));
|
||||||
|
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 {
|
MainCamera,
|
||||||
distance: 20.,
|
Camera3d::default(),
|
||||||
height: 10.,
|
CameraMode::Player,
|
||||||
target: player,
|
CameraAttachment(player_eye),
|
||||||
},
|
|
||||||
children![(
|
|
||||||
Camera3d::default(),
|
|
||||||
CameraPitch::default(),
|
|
||||||
MainCamera,
|
|
||||||
Transform::default()
|
|
||||||
)],
|
|
||||||
));
|
));
|
||||||
|
|
||||||
commands.spawn((
|
commands.spawn((
|
||||||
@@ -102,84 +100,56 @@ 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"),
|
Ship,
|
||||||
Ship,
|
Velocity::zero(),
|
||||||
Velocity::zero(),
|
Damping::default(),
|
||||||
Damping::default(),
|
Transform::from_xyz(0.0, 1.0, 0.0),
|
||||||
Transform::from_xyz(0.0, 1.0, 0.0),
|
RigidBody::Dynamic,
|
||||||
RigidBody::Dynamic,
|
children![
|
||||||
children![
|
(
|
||||||
(
|
Name::new("Back Wall"),
|
||||||
Name::new("Back Wall"),
|
Mesh3d(meshes.add(Cuboid::new(3.0, 2.0, 0.1))),
|
||||||
Mesh3d(meshes.add(Cuboid::new(3.0, 2.0, 0.1))),
|
Collider::cuboid(3.0, 2.0, 0.1),
|
||||||
Collider::cuboid(3.0, 2.0, 0.1),
|
MeshMaterial3d(material.clone()),
|
||||||
MeshMaterial3d(material.clone()),
|
Transform::from_xyz(0.0, 1.0, 6.0 / 2.0),
|
||||||
Transform::from_xyz(0.0, 1.0, 6.0 / 2.0),
|
),
|
||||||
),
|
(
|
||||||
(
|
Name::new("Front Window"),
|
||||||
Name::new("Front Window"),
|
Mesh3d(meshes.add(Cuboid::new(3.0, 2.0, 0.1))),
|
||||||
Mesh3d(meshes.add(Cuboid::new(3.0, 2.0, 0.1))),
|
Collider::cuboid(3.0, 2.0, 0.1),
|
||||||
Collider::cuboid(3.0, 2.0, 0.1),
|
MeshMaterial3d(window_material),
|
||||||
MeshMaterial3d(window_material),
|
Transform::from_xyz(0.0, 1.0, -6.0 / 2.0),
|
||||||
Transform::from_xyz(0.0, 1.0, -6.0 / 2.0),
|
),
|
||||||
),
|
(
|
||||||
(
|
Name::new("Right Wall"),
|
||||||
Name::new("Right Wall"),
|
Mesh3d(meshes.add(Cuboid::new(0.1, 2.0, 6.0))),
|
||||||
Mesh3d(meshes.add(Cuboid::new(0.1, 2.0, 6.0))),
|
Collider::cuboid(0.1, 2.0, 6.0),
|
||||||
Collider::cuboid(0.1, 2.0, 6.0),
|
MeshMaterial3d(material.clone()),
|
||||||
MeshMaterial3d(material.clone()),
|
Transform::from_xyz(3.0 / 2.0, 1.0, 0.0),
|
||||||
Transform::from_xyz(3.0 / 2.0, 1.0, 0.0),
|
),
|
||||||
),
|
(
|
||||||
(
|
Name::new("Left Wall"),
|
||||||
Name::new("Left Wall"),
|
Mesh3d(meshes.add(Cuboid::new(0.1, 2.0, 6.0))),
|
||||||
Mesh3d(meshes.add(Cuboid::new(0.1, 2.0, 6.0))),
|
Collider::cuboid(0.1, 2.0, 6.0),
|
||||||
Collider::cuboid(0.1, 2.0, 6.0),
|
MeshMaterial3d(material.clone()),
|
||||||
MeshMaterial3d(material.clone()),
|
Transform::from_xyz(-3.0 / 2.0, 1.0, 0.0),
|
||||||
Transform::from_xyz(-3.0 / 2.0, 1.0, 0.0),
|
),
|
||||||
),
|
(
|
||||||
(
|
Name::new("Roof"),
|
||||||
Name::new("Roof"),
|
Mesh3d(meshes.add(Cuboid::new(3.0, 0.1, 6.0))),
|
||||||
Mesh3d(meshes.add(Cuboid::new(3.0, 0.1, 6.0))),
|
Collider::cuboid(3.0, 0.1, 6.0),
|
||||||
Collider::cuboid(3.0, 0.1, 6.0),
|
MeshMaterial3d(material.clone()),
|
||||||
MeshMaterial3d(material.clone()),
|
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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::*;
|
||||||
|
|||||||
@@ -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(
|
||||||
|
|||||||
16
src/plugins/state_management.rs
Normal file
16
src/plugins/state_management.rs
Normal 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)));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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
20
src/states/input.rs
Normal 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
1
src/states/mod.rs
Normal file
@@ -0,0 +1 @@
|
|||||||
|
pub mod input;
|
||||||
Reference in New Issue
Block a user