Ship Controls
+ Refactoring
This commit is contained in:
@@ -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();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user