Ship Controls

+ Refactoring
This commit is contained in:
2025-06-14 14:30:44 -04:00
parent a8f0793865
commit fa9f927f8c
16 changed files with 520 additions and 283 deletions

39
src/components/camera.rs Normal file
View File

@@ -0,0 +1,39 @@
use bevy::prelude::*;
#[derive(Component, Reflect)]
pub struct FreeCam(pub bool);
#[derive(Component, Default, Reflect)]
pub struct MainCamera;
#[derive(Component, Default, Reflect)]
pub struct CameraRoot;
#[derive(Component, Default, Reflect)]
pub struct CameraPitch(pub f32);
#[derive(Component, Reflect)]
pub struct Unfocused;
#[derive(Component, Reflect)]
#[require(FollowTarget)]
pub struct FollowCam {
pub target: Entity,
pub distance: f32,
pub height: f32,
}
#[derive(Component, Reflect)]
pub struct FollowTarget {
pub pos: Vec3,
pub rot: Quat,
pub up: Dir3,
}
impl Default for FollowTarget {
fn default() -> Self {
return Self {
pos: default(),
rot: default(),
up: Dir3::Y,
};
}
}

2
src/components/mod.rs Normal file
View File

@@ -0,0 +1,2 @@
pub mod camera;
pub mod tags;

7
src/components/tags.rs Normal file
View File

@@ -0,0 +1,7 @@
use bevy::prelude::*;
#[derive(Component, Reflect)]
pub struct Player;
#[derive(Component, Reflect)]
pub struct Ship;

View File

@@ -1,7 +1,10 @@
mod plugins;
mod utils;
use bevy::{prelude::*, window::PresentMode};
use bevy_inspector_egui::{bevy_egui::EguiPlugin, quick::WorldInspectorPlugin};
use plugins::game::GamePlugin;
mod components;
mod resources;
fn main() {
App::new()
@@ -29,7 +32,7 @@ fn main() {
enable_multipass_for_primary_context: true,
},
WorldInspectorPlugin::new(),
GamePlugin::default(),
GamePlugin,
))
.run();
}

28
src/plugins/follow_cam.rs Normal file
View File

@@ -0,0 +1,28 @@
use bevy::prelude::*;
use crate::components::camera::{FollowCam, FollowTarget};
pub struct FollowCamPlugin;
impl Plugin for FollowCamPlugin {
fn build(&self, app: &mut App) {
app.add_systems(Update, (set_cam_target, follow_cam).chain());
}
}
fn set_cam_target(transforms: Query<&GlobalTransform>, cam_target: Single<(&mut FollowTarget, &FollowCam)>) {
let (mut tgt, cam) = cam_target.into_inner();
if let Ok(tgt_transform) = transforms.get(cam.target) {
tgt.pos = tgt_transform.translation();
tgt.rot = tgt_transform.rotation();
tgt.up = tgt_transform.up();
}
}
fn follow_cam(cam: Single<(&mut Transform, &FollowTarget, &FollowCam)>) {
let (mut transform, tgt, cam) = cam.into_inner();
let offset = tgt.rot * Vec3::new(0.0, cam.height, cam.distance);
transform.translation = offset + tgt.pos;
*transform = transform.looking_at(tgt.pos, tgt.up);
}

71
src/plugins/free_cam.rs Normal file
View File

@@ -0,0 +1,71 @@
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,38 +1,33 @@
use std::f32::consts::FRAC_PI_2;
use avian3d::{PhysicsPlugins, prelude::*};
use bevy::{
input::mouse::MouseMotion,
prelude::*,
window::{CursorGrabMode, PrimaryWindow},
};
use crate::{
components::{
camera::{CameraPitch, CameraRoot, FollowCam, MainCamera, Unfocused},
tags::Ship,
},
plugins::{follow_cam::FollowCamPlugin, free_cam::FreeCamPlugin, ship::ShipPlugin, types::TypesPlugin},
};
// use bevy_rapier3d::prelude::*;
#[derive(Default)]
pub struct GamePlugin;
#[derive(Component, Default)]
pub struct MainCamera;
#[derive(Component, Default)]
pub struct CameraRoot;
#[derive(Component, Default)]
pub struct CameraPitch(pub f32);
#[derive(Component)]
pub struct Unfocused;
#[derive(Component)]
pub struct Ship;
impl Plugin for GamePlugin {
fn build(&self, app: &mut bevy::app::App) {
// app.add_plugins(RapierPhysicsPlugin::<NoUserData>::default());
app.add_plugins((PhysicsPlugins::default(), PhysicsDebugPlugin::default()));
app.add_plugins((FollowCamPlugin, ShipPlugin, TypesPlugin));
app.add_plugins((
PhysicsPlugins::default(),
#[cfg(feature = "dev-phys")]
PhysicsDebugPlugin::default(),
));
app.insert_resource(Gravity::ZERO);
app.add_systems(Startup, (setup_scene, spawn_ship));
app.add_systems(Startup, (setup_scene, spawn_ship).chain());
app.add_systems(Update, ((camera_yaw, camera_pitch).chain(), camera_toggle, fly_camera));
app.add_systems(Update, ship_controls);
app.add_systems(Update, camera_toggle);
}
}
@@ -48,12 +43,12 @@ fn setup_scene(
commands.spawn((
Name::new("Camera Root"),
Transform::from_xyz(0.0, 1.3, 0.0),
CameraRoot::default(),
CameraRoot,
Visibility::default(),
children![(
Camera3d::default(),
CameraPitch::default(),
MainCamera::default(),
MainCamera,
Transform::default()
)],
));
@@ -84,58 +79,67 @@ fn spawn_ship(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
camera: Single<Entity, With<MainCamera>>,
) {
let window_material = materials.add(Color::lcha(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);
commands.spawn((
Mesh3d(meshes.add(Cuboid::new(3.0, 0.1, 6.0))),
MeshMaterial3d(material.clone()),
Name::new("Ship"),
Ship,
LinearDamping::default(),
AngularDamping::default(),
Transform::from_xyz(0.0, 1.0, 0.0),
RigidBody::Dynamic,
children![
(
Name::new("Back Wall"),
Mesh3d(meshes.add(Cuboid::new(3.0, 2.0, 0.1))),
Collider::cuboid(3.0, 2.0, 0.1),
MeshMaterial3d(material.clone()),
Transform::from_xyz(0.0, 1.0, 6.0 / 2.0),
),
(
Name::new("Front Window"),
Mesh3d(meshes.add(Cuboid::new(3.0, 2.0, 0.1))),
Collider::cuboid(3.0, 2.0, 0.1),
MeshMaterial3d(window_material),
Transform::from_xyz(0.0, 1.0, -6.0 / 2.0),
),
(
Name::new("Right Wall"),
Mesh3d(meshes.add(Cuboid::new(0.1, 2.0, 6.0))),
Collider::cuboid(0.1, 2.0, 6.0),
MeshMaterial3d(material.clone()),
Transform::from_xyz(3.0 / 2.0, 1.0, 0.0),
),
(
Name::new("Left Wall"),
Mesh3d(meshes.add(Cuboid::new(0.1, 2.0, 6.0))),
Collider::cuboid(0.1, 2.0, 6.0),
MeshMaterial3d(material.clone()),
Transform::from_xyz(-3.0 / 2.0, 1.0, 0.0),
),
(
Name::new("Roof"),
Mesh3d(meshes.add(Cuboid::new(3.0, 0.1, 6.0))),
Collider::cuboid(3.0, 0.1, 6.0),
MeshMaterial3d(material.clone()),
Transform::from_xyz(0.0, 2.0, 0.0),
)
],
));
let ship = commands
.spawn((
Mesh3d(meshes.add(Cuboid::new(3.0, 0.1, 6.0))),
MeshMaterial3d(material.clone()),
Name::new("Ship"),
Ship,
LinearDamping::default(),
AngularDamping::default(),
Transform::from_xyz(0.0, 1.0, 0.0),
RigidBody::Dynamic,
children![
(
Name::new("Back Wall"),
Mesh3d(meshes.add(Cuboid::new(3.0, 2.0, 0.1))),
Collider::cuboid(3.0, 2.0, 0.1),
MeshMaterial3d(material.clone()),
Transform::from_xyz(0.0, 1.0, 6.0 / 2.0),
),
(
Name::new("Front Window"),
Mesh3d(meshes.add(Cuboid::new(3.0, 2.0, 0.1))),
Collider::cuboid(3.0, 2.0, 0.1),
MeshMaterial3d(window_material),
Transform::from_xyz(0.0, 1.0, -6.0 / 2.0),
),
(
Name::new("Right Wall"),
Mesh3d(meshes.add(Cuboid::new(0.1, 2.0, 6.0))),
Collider::cuboid(0.1, 2.0, 6.0),
MeshMaterial3d(material.clone()),
Transform::from_xyz(3.0 / 2.0, 1.0, 0.0),
),
(
Name::new("Left Wall"),
Mesh3d(meshes.add(Cuboid::new(0.1, 2.0, 6.0))),
Collider::cuboid(0.1, 2.0, 6.0),
MeshMaterial3d(material.clone()),
Transform::from_xyz(-3.0 / 2.0, 1.0, 0.0),
),
(
Name::new("Roof"),
Mesh3d(meshes.add(Cuboid::new(3.0, 0.1, 6.0))),
Collider::cuboid(3.0, 0.1, 6.0),
MeshMaterial3d(material.clone()),
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(
@@ -156,117 +160,3 @@ pub fn camera_toggle(
}
}
}
const FLY_SPEED: f32 = 10.0;
pub fn fly_camera(
cam_query: Single<&mut Transform, (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 = cam_query.into_inner();
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, (With<CameraRoot>, Without<Unfocused>)>,
cam_query: Single<&mut CameraPitch>,
mouse_motion: EventReader<MouseMotion>,
time: Res<Time>,
) {
let mut yaw_transform = cam_yaw_query.into_inner();
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);
}
fn get_mouse_delta(mut mouse_motion: EventReader<MouseMotion>) -> Vec2 {
let mut delta = Vec2::ZERO;
for e in mouse_motion.read() {
delta += e.delta;
}
return delta;
}
fn ship_controls(
ship_query: Single<
(
&Transform,
&mut LinearVelocity,
&mut AngularVelocity,
&mut LinearDamping,
&mut AngularDamping,
),
With<Ship>,
>,
window: Single<&Window, With<PrimaryWindow>>,
key: Res<ButtonInput<KeyCode>>,
time: Res<Time>,
) {
if !window.cursor_options.visible {
return;
}
let (transform, mut vel, mut ang, mut ldamp, mut adamp) = ship_query.into_inner();
let mut move_vec = Vec3::ZERO;
if key.pressed(KeyCode::KeyW) {
move_vec.z = -1.0;
} else if key.pressed(KeyCode::KeyS) {
move_vec.z = 1.0;
}
if key.pressed(KeyCode::KeyA) {
move_vec.x = -1.0;
} else if key.pressed(KeyCode::KeyD) {
move_vec.x = 1.0;
}
if key.pressed(KeyCode::Space) {
move_vec.y = 1.0;
} else if key.pressed(KeyCode::ShiftLeft) {
move_vec.y = -1.0;
}
if key.pressed(KeyCode::ControlLeft) {
ldamp.0 = 0.8;
adamp.0 = 0.8;
} else {
ldamp.0 = 0.0;
adamp.0 = 0.0;
}
move_vec = transform.rotation * move_vec.normalize_or_zero();
vel.0 += move_vec * time.delta_secs();
}

View File

@@ -1 +1,6 @@
pub mod follow_cam;
pub mod free_cam;
pub mod game;
pub mod ship;
pub mod ship_cam;
pub mod types;

112
src/plugins/ship.rs Normal file
View File

@@ -0,0 +1,112 @@
use avian3d::prelude::*;
use bevy::{input::mouse::MouseMotion, prelude::*};
use crate::{components::tags::Ship, utils::input::get_mouse_delta};
pub struct ShipPlugin;
impl Plugin for ShipPlugin {
fn build(&self, app: &mut App) {
app.add_systems(Update, ship_controls);
#[cfg(feature = "dev-viz")]
app.add_systems(Update, ship_debug);
}
}
fn ship_controls(
ship_query: Single<
(
&Transform,
&mut LinearVelocity,
&mut AngularVelocity,
&mut LinearDamping,
&mut AngularDamping,
),
With<Ship>,
>,
key: Res<ButtonInput<KeyCode>>,
time: Res<Time>,
mut mouse_motion: EventReader<MouseMotion>,
) {
let (transform, mut vel, mut ang, mut ldamp, mut adamp) = ship_query.into_inner();
let mut move_vec = Vec3::ZERO;
if key.pressed(KeyCode::KeyW) {
move_vec.z = -1.0;
} else if key.pressed(KeyCode::KeyS) {
move_vec.z = 1.0;
}
if key.pressed(KeyCode::KeyA) {
move_vec.x = -1.0;
} else if key.pressed(KeyCode::KeyD) {
move_vec.x = 1.0;
}
if key.pressed(KeyCode::Space) {
move_vec.y = 1.0;
} else if key.pressed(KeyCode::ShiftLeft) {
move_vec.y = -1.0;
}
if key.pressed(KeyCode::ControlLeft) {
ldamp.0 = 0.8;
adamp.0 = 0.8;
} else {
ldamp.0 = 0.0;
adamp.0 = 0.0;
}
const ROLL_SPEED: f32 = 1.0;
const DEAD_ZONE: f32 = 0.1;
const INPUT_CURVE: f32 = 3.0;
let yaw = if key.pressed(KeyCode::KeyQ) {
Vec3::Y * ROLL_SPEED
} else if key.pressed(KeyCode::KeyE) {
Vec3::Y * -ROLL_SPEED
} else {
Vec3::ZERO
};
let mouse_delta = get_mouse_delta(mouse_motion).normalize_or_zero();
let mouse_input = mouse_delta
.abs()
.map(|v| if v < DEAD_ZONE { 0.0 } else { v })
.powf(INPUT_CURVE)
* mouse_delta.signum();
let roll = Vec3::NEG_Z * mouse_input.x;
let pitch = Vec3::X * mouse_input.y;
ang.0 += (transform.rotation * (roll + yaw + pitch)) * time.delta_secs();
move_vec = transform.rotation * move_vec.normalize_or_zero();
vel.0 += move_vec * time.delta_secs();
}
#[cfg(feature = "dev-viz")]
fn ship_debug(mut gizmos: Gizmos, ship: Single<&Transform, With<Ship>>) {
use std::ops::Range;
let base = ship.translation.floor();
const GRID: Range<i32> = -10..10;
for x in GRID {
for y in GRID {
for z in GRID {
let p = base + Vec3::new(x as f32, y as f32, z as f32);
let color = if x == 0 && y == 0 {
LinearRgba::BLUE
} else if x == 0 && z == 0 {
LinearRgba::GREEN
} else if y == 0 && z == 0 {
LinearRgba::RED
} else {
LinearRgba::gray(0.2).with_alpha(0.2)
};
gizmos.sphere(p, 0.01, color);
}
}
}
}

9
src/plugins/ship_cam.rs Normal file
View File

@@ -0,0 +1,9 @@
use bevy::prelude::*;
pub struct ShipCamPlugin;
impl Plugin for ShipCamPlugin {
fn build(&self, app: &mut App) {
todo!()
}
}

12
src/plugins/types.rs Normal file
View File

@@ -0,0 +1,12 @@
use bevy::prelude::*;
use crate::components::camera::{FollowCam, FollowTarget};
pub struct TypesPlugin;
impl Plugin for TypesPlugin {
fn build(&self, app: &mut App) {
app.register_type::<FollowTarget>();
app.register_type::<FollowCam>();
}
}

0
src/resources/mod.rs Normal file
View File

9
src/utils/input.rs Normal file
View File

@@ -0,0 +1,9 @@
use bevy::{input::mouse::MouseMotion, prelude::*};
pub fn get_mouse_delta(mut mouse_motion: EventReader<MouseMotion>) -> Vec2 {
let mut delta = Vec2::ZERO;
for e in mouse_motion.read() {
delta += e.delta;
}
return delta;
}

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

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