Add Asset loader, added and configured various states
This commit is contained in:
30
Cargo.lock
generated
30
Cargo.lock
generated
@@ -565,6 +565,29 @@ dependencies = [
|
|||||||
"web-sys",
|
"web-sys",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "bevy_asset_loader"
|
||||||
|
version = "0.23.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "653857e8685ba3c6f237a7aa67620ae440c87e975f8a843ef098c90c49b9dde6"
|
||||||
|
dependencies = [
|
||||||
|
"anyhow",
|
||||||
|
"bevy",
|
||||||
|
"bevy_asset_loader_derive",
|
||||||
|
"path-slash",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "bevy_asset_loader_derive"
|
||||||
|
version = "0.23.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "8ea90451960d44a9908e95de892511dead119b909da68e56b92527efcfac8691"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "bevy_asset_macros"
|
name = "bevy_asset_macros"
|
||||||
version = "0.16.1"
|
version = "0.16.1"
|
||||||
@@ -4009,6 +4032,12 @@ version = "1.0.15"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a"
|
checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "path-slash"
|
||||||
|
version = "0.2.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1e91099d4268b0e11973f036e885d652fb0b21fedcf69738c627f94db6a44f42"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "percent-encoding"
|
name = "percent-encoding"
|
||||||
version = "2.3.1"
|
version = "2.3.1"
|
||||||
@@ -4798,6 +4827,7 @@ version = "0.1.0"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"bevy",
|
"bevy",
|
||||||
"bevy-inspector-egui",
|
"bevy-inspector-egui",
|
||||||
|
"bevy_asset_loader",
|
||||||
"bevy_rapier3d",
|
"bevy_rapier3d",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ edition = "2024"
|
|||||||
bevy = { version = "0.16.1", features = [] }
|
bevy = { version = "0.16.1", features = [] }
|
||||||
# bevy_rapier3d = { version = "0.29.0", features = ["simd-stable", "parallel"] }
|
# bevy_rapier3d = { version = "0.29.0", features = ["simd-stable", "parallel"] }
|
||||||
bevy-inspector-egui = "0.31.0"
|
bevy-inspector-egui = "0.31.0"
|
||||||
|
bevy_asset_loader = "0.23.0"
|
||||||
bevy_rapier3d = "0.30.0"
|
bevy_rapier3d = "0.30.0"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
hard_tabs = true
|
hard_tabs = true
|
||||||
max_width = 120
|
max_width = 120
|
||||||
brace_style = "AlwaysNextLine"
|
brace_style = "AlwaysNextLine"
|
||||||
control_brace_style = "AlwaysNextLine"
|
control_brace_style = "AlwaysNextLine"
|
||||||
|
|||||||
@@ -1,50 +1,50 @@
|
|||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
|
|
||||||
#[derive(Component, Reflect)]
|
#[derive(Component, Reflect)]
|
||||||
pub struct FreeCam(pub bool);
|
pub struct FreeCam(pub bool);
|
||||||
|
|
||||||
#[derive(Component, Default, Reflect)]
|
#[derive(Component, Default, Reflect)]
|
||||||
pub struct MainCamera;
|
pub struct MainCamera;
|
||||||
|
|
||||||
#[derive(Component, Default, Reflect)]
|
#[derive(Component, Default, Reflect)]
|
||||||
pub struct CameraRoot;
|
pub struct CameraRoot;
|
||||||
#[derive(Component, Default, Reflect)]
|
#[derive(Component, Default, Reflect)]
|
||||||
pub struct CameraPitch(pub f32);
|
pub struct CameraPitch(pub f32);
|
||||||
#[derive(Component, Reflect)]
|
#[derive(Component, Reflect)]
|
||||||
pub struct Unfocused;
|
pub struct Unfocused;
|
||||||
|
|
||||||
#[derive(Component, Reflect)]
|
#[derive(Component, Reflect)]
|
||||||
#[require(FollowTarget)]
|
#[require(FollowTarget)]
|
||||||
pub struct FollowCam {
|
pub struct FollowCam {
|
||||||
pub target: Entity,
|
pub target: Entity,
|
||||||
pub distance: f32,
|
pub distance: f32,
|
||||||
pub height: f32,
|
pub height: f32,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Component, Reflect)]
|
#[derive(Component, Reflect)]
|
||||||
pub struct FollowTarget {
|
pub struct FollowTarget {
|
||||||
pub pos: Vec3,
|
pub pos: Vec3,
|
||||||
pub rot: Quat,
|
pub rot: Quat,
|
||||||
pub up: Dir3,
|
pub up: Dir3,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for FollowTarget {
|
impl Default for FollowTarget {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
return Self {
|
return Self {
|
||||||
pos: default(),
|
pos: default(),
|
||||||
rot: default(),
|
rot: default(),
|
||||||
up: Dir3::Y,
|
up: Dir3::Y,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Component, Default, Reflect)]
|
#[derive(Component, Default, Reflect)]
|
||||||
pub enum CameraMode {
|
pub enum CameraMode {
|
||||||
#[default]
|
#[default]
|
||||||
Player,
|
Player,
|
||||||
Ship,
|
Ship,
|
||||||
Disabled,
|
Disabled,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Component, Reflect)]
|
#[derive(Component, Reflect)]
|
||||||
pub struct CameraAttachment(pub Entity);
|
pub struct CameraAttachment(pub Entity);
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
pub mod camera;
|
pub mod camera;
|
||||||
// pub mod character_controller;
|
// pub mod character_controller;
|
||||||
pub mod player;
|
pub mod player;
|
||||||
pub mod tags;
|
pub mod tags;
|
||||||
|
|||||||
@@ -1,49 +1,49 @@
|
|||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
use bevy_rapier3d::prelude::GravityScale;
|
use bevy_rapier3d::prelude::GravityScale;
|
||||||
|
|
||||||
#[derive(Component, Default, Reflect)]
|
#[derive(Component, Default, Reflect)]
|
||||||
#[require(PlayerVelocity, MoveSpeed, JumpSpeed, PlayerDrag)]
|
#[require(PlayerVelocity, MoveSpeed, JumpSpeed, PlayerDrag)]
|
||||||
pub struct PlayerMotion(pub Vec3);
|
pub struct PlayerMotion(pub Vec3);
|
||||||
|
|
||||||
#[derive(Component, Default, Reflect)]
|
#[derive(Component, Default, Reflect)]
|
||||||
pub struct PlayerForce(pub Vec3);
|
pub struct PlayerForce(pub Vec3);
|
||||||
|
|
||||||
#[derive(Component, Reflect)]
|
#[derive(Component, Reflect)]
|
||||||
#[require(GravityScale)]
|
#[require(GravityScale)]
|
||||||
pub struct GravityDirection(pub Option<Dir3>);
|
pub struct GravityDirection(pub Option<Dir3>);
|
||||||
|
|
||||||
impl Default for GravityDirection {
|
impl Default for GravityDirection {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self::DOWN
|
Self::DOWN
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl GravityDirection {
|
impl GravityDirection {
|
||||||
pub const DOWN: GravityDirection = GravityDirection(Some(Dir3::NEG_Y));
|
pub const DOWN: GravityDirection = GravityDirection(Some(Dir3::NEG_Y));
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
pub const NONE: GravityDirection = GravityDirection(None);
|
pub const NONE: GravityDirection = GravityDirection(None);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Component, Default, Reflect)]
|
#[derive(Component, Default, Reflect)]
|
||||||
pub struct PlayerVelocity(pub Vec3);
|
pub struct PlayerVelocity(pub Vec3);
|
||||||
|
|
||||||
#[derive(Component, Reflect)]
|
#[derive(Component, Reflect)]
|
||||||
pub struct MoveSpeed(pub f32);
|
pub struct MoveSpeed(pub f32);
|
||||||
|
|
||||||
impl Default for MoveSpeed {
|
impl Default for MoveSpeed {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self(10.0)
|
Self(10.0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Component, Reflect)]
|
#[derive(Component, Reflect)]
|
||||||
pub struct JumpSpeed(pub f32);
|
pub struct JumpSpeed(pub f32);
|
||||||
|
|
||||||
impl Default for JumpSpeed {
|
impl Default for JumpSpeed {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self(10.0)
|
Self(10.0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Component, Default, Reflect)]
|
#[derive(Component, Default, Reflect)]
|
||||||
pub struct PlayerDrag(pub f32);
|
pub struct PlayerDrag(pub f32);
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
|
|
||||||
use crate::components::player::*;
|
use crate::components::player::*;
|
||||||
|
|
||||||
#[derive(Component, Reflect)]
|
#[derive(Component, Reflect)]
|
||||||
#[require(PlayerMotion, PlayerForce, GravityDirection)]
|
#[require(PlayerMotion, PlayerForce, GravityDirection)]
|
||||||
pub struct Player;
|
pub struct Player;
|
||||||
|
|
||||||
#[derive(Component, Reflect)]
|
#[derive(Component, Reflect)]
|
||||||
pub struct Ship;
|
pub struct Ship;
|
||||||
|
|||||||
16
src/macros.rs
Normal file
16
src/macros.rs
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
#[macro_export]
|
||||||
|
macro_rules! configure_sets {
|
||||||
|
(
|
||||||
|
$app: expr,
|
||||||
|
$system_set: expr,
|
||||||
|
$condition: expr
|
||||||
|
) => {
|
||||||
|
$app.configure_sets(PreUpdate, $system_set.run_if($condition))
|
||||||
|
.configure_sets(Update, $system_set.run_if($condition))
|
||||||
|
.configure_sets(PostUpdate, $system_set.run_if($condition))
|
||||||
|
.configure_sets(Last, $system_set.run_if($condition));
|
||||||
|
$app.configure_sets(FixedPreUpdate, $system_set.run_if($condition))
|
||||||
|
.configure_sets(FixedUpdate, $system_set.run_if($condition))
|
||||||
|
.configure_sets(FixedPostUpdate, $system_set.run_if($condition));
|
||||||
|
};
|
||||||
|
}
|
||||||
79
src/main.rs
79
src/main.rs
@@ -1,39 +1,40 @@
|
|||||||
mod plugins;
|
mod macros;
|
||||||
mod states;
|
mod plugins;
|
||||||
mod utils;
|
mod states;
|
||||||
use bevy::{prelude::*, window::PresentMode};
|
mod utils;
|
||||||
use bevy_inspector_egui::{bevy_egui::EguiPlugin, quick::WorldInspectorPlugin};
|
use bevy::{prelude::*, window::PresentMode};
|
||||||
use plugins::GamePlugin;
|
use bevy_inspector_egui::{bevy_egui::EguiPlugin, quick::WorldInspectorPlugin};
|
||||||
mod components;
|
use plugins::GamePlugin;
|
||||||
mod resources;
|
mod components;
|
||||||
const NAME: &str = "Space Game";
|
mod resources;
|
||||||
fn main() {
|
const NAME: &str = "Space Game";
|
||||||
App::new()
|
fn main() {
|
||||||
.add_plugins((
|
App::new()
|
||||||
DefaultPlugins
|
.add_plugins((
|
||||||
.set(WindowPlugin {
|
DefaultPlugins
|
||||||
primary_window: Some(Window {
|
.set(WindowPlugin {
|
||||||
title: NAME.into(),
|
primary_window: Some(Window {
|
||||||
name: Some(NAME.into()),
|
title: NAME.into(),
|
||||||
#[cfg(debug_assertions)]
|
name: Some(NAME.into()),
|
||||||
resolution: (1920., 1080.).into(),
|
#[cfg(debug_assertions)]
|
||||||
present_mode: PresentMode::AutoNoVsync,
|
resolution: (1920., 1080.).into(),
|
||||||
#[cfg(not(debug_assertions))]
|
present_mode: PresentMode::AutoNoVsync,
|
||||||
mode: bevy::window::WindowMode::BorderlessFullscreen,
|
#[cfg(not(debug_assertions))]
|
||||||
..default()
|
mode: bevy::window::WindowMode::BorderlessFullscreen,
|
||||||
}),
|
..default()
|
||||||
..default()
|
}),
|
||||||
})
|
..default()
|
||||||
.set(AssetPlugin {
|
})
|
||||||
#[cfg(not(debug_assertions))]
|
.set(AssetPlugin {
|
||||||
watch_for_changes_override: Some(true),
|
#[cfg(not(debug_assertions))]
|
||||||
..Default::default()
|
watch_for_changes_override: Some(true),
|
||||||
}),
|
..Default::default()
|
||||||
EguiPlugin {
|
}),
|
||||||
enable_multipass_for_primary_context: true,
|
EguiPlugin {
|
||||||
},
|
enable_multipass_for_primary_context: true,
|
||||||
WorldInspectorPlugin::new(),
|
},
|
||||||
GamePlugin,
|
WorldInspectorPlugin::new(),
|
||||||
))
|
GamePlugin,
|
||||||
.run();
|
))
|
||||||
}
|
.run();
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,61 +1,61 @@
|
|||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
#[cfg(feature = "dev")]
|
#[cfg(feature = "dev")]
|
||||||
use bevy::window::PrimaryWindow;
|
use bevy::window::PrimaryWindow;
|
||||||
|
|
||||||
use crate::components::camera::*;
|
use crate::components::camera::*;
|
||||||
|
|
||||||
pub struct CameraPlugin;
|
pub struct CameraPlugin;
|
||||||
|
|
||||||
impl Plugin for CameraPlugin {
|
impl Plugin for CameraPlugin {
|
||||||
fn build(&self, app: &mut App) {
|
fn build(&self, app: &mut App) {
|
||||||
app.add_systems(Update, camera_pitch);
|
app.add_systems(Update, camera_pitch);
|
||||||
app.add_systems(Update, camera_attachment);
|
app.add_systems(Update, camera_attachment);
|
||||||
#[cfg(feature = "dev")]
|
#[cfg(feature = "dev")]
|
||||||
app.add_systems(Update, camera_toggle);
|
app.add_systems(Update, camera_toggle);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn camera_attachment(
|
pub fn camera_attachment(
|
||||||
attachment_targets: Query<&GlobalTransform>,
|
attachment_targets: Query<&GlobalTransform>,
|
||||||
cam: Single<(&mut Transform, &CameraAttachment, &CameraMode)>,
|
cam: Single<(&mut Transform, &CameraAttachment, &CameraMode)>,
|
||||||
) {
|
) {
|
||||||
let (mut transform, attach, mode) = cam.into_inner();
|
let (mut transform, attach, mode) = cam.into_inner();
|
||||||
|
|
||||||
if let Ok(tgt) = attachment_targets.get(attach.0) {
|
if let Ok(tgt) = attachment_targets.get(attach.0) {
|
||||||
match mode {
|
match mode {
|
||||||
CameraMode::Player => {
|
CameraMode::Player => {
|
||||||
transform.rotation = tgt.rotation();
|
transform.rotation = tgt.rotation();
|
||||||
transform.translation = tgt.translation();
|
transform.translation = tgt.translation();
|
||||||
}
|
}
|
||||||
CameraMode::Ship => todo!("Ship Mode"),
|
CameraMode::Ship => todo!("Ship Mode"),
|
||||||
CameraMode::Disabled => (),
|
CameraMode::Disabled => (),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn camera_pitch(cam_query: Query<(&mut Transform, &CameraPitch)>) {
|
pub fn camera_pitch(cam_query: Query<(&mut Transform, &CameraPitch)>) {
|
||||||
for (mut cam_transform, pitch) in cam_query {
|
for (mut cam_transform, pitch) in cam_query {
|
||||||
cam_transform.rotation = Quat::from_rotation_x(pitch.0);
|
cam_transform.rotation = Quat::from_rotation_x(pitch.0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "dev")]
|
#[cfg(feature = "dev")]
|
||||||
pub fn camera_toggle(
|
pub fn camera_toggle(
|
||||||
key: Res<ButtonInput<KeyCode>>,
|
key: Res<ButtonInput<KeyCode>>,
|
||||||
mut window: Single<&mut Window, With<PrimaryWindow>>,
|
mut window: Single<&mut Window, With<PrimaryWindow>>,
|
||||||
camera: Single<&mut CameraMode>,
|
camera: Single<&mut CameraMode>,
|
||||||
) {
|
) {
|
||||||
use bevy::window::CursorGrabMode;
|
use bevy::window::CursorGrabMode;
|
||||||
let mut mode = camera.into_inner();
|
let mut mode = camera.into_inner();
|
||||||
if key.just_pressed(KeyCode::Escape) {
|
if key.just_pressed(KeyCode::Escape) {
|
||||||
if window.cursor_options.visible {
|
if window.cursor_options.visible {
|
||||||
*mode = CameraMode::Player;
|
*mode = CameraMode::Player;
|
||||||
window.cursor_options.grab_mode = CursorGrabMode::Locked;
|
window.cursor_options.grab_mode = CursorGrabMode::Locked;
|
||||||
window.cursor_options.visible = false;
|
window.cursor_options.visible = false;
|
||||||
} else {
|
} else {
|
||||||
*mode = CameraMode::Disabled;
|
*mode = CameraMode::Disabled;
|
||||||
window.cursor_options.grab_mode = CursorGrabMode::None;
|
window.cursor_options.grab_mode = CursorGrabMode::None;
|
||||||
window.cursor_options.visible = true;
|
window.cursor_options.visible = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,28 +1,28 @@
|
|||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
|
|
||||||
use crate::components::camera::{FollowCam, FollowTarget};
|
use crate::components::camera::{FollowCam, FollowTarget};
|
||||||
|
|
||||||
pub struct FollowCamPlugin;
|
pub struct FollowCamPlugin;
|
||||||
|
|
||||||
impl Plugin for FollowCamPlugin {
|
impl Plugin for FollowCamPlugin {
|
||||||
fn build(&self, app: &mut App) {
|
fn build(&self, app: &mut App) {
|
||||||
app.add_systems(Update, (set_cam_target, follow_cam).chain());
|
app.add_systems(Update, (set_cam_target, follow_cam).chain());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_cam_target(transforms: Query<&GlobalTransform>, cam_target: Single<(&mut FollowTarget, &FollowCam)>) {
|
fn set_cam_target(transforms: Query<&GlobalTransform>, cam_target: Single<(&mut FollowTarget, &FollowCam)>) {
|
||||||
let (mut tgt, cam) = cam_target.into_inner();
|
let (mut tgt, cam) = cam_target.into_inner();
|
||||||
if let Ok(tgt_transform) = transforms.get(cam.target) {
|
if let Ok(tgt_transform) = transforms.get(cam.target) {
|
||||||
tgt.pos = tgt_transform.translation();
|
tgt.pos = tgt_transform.translation();
|
||||||
tgt.rot = tgt_transform.rotation();
|
tgt.rot = tgt_transform.rotation();
|
||||||
tgt.up = tgt_transform.up();
|
tgt.up = tgt_transform.up();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn follow_cam(cam: Single<(&mut Transform, &FollowTarget, &FollowCam)>) {
|
fn follow_cam(cam: Single<(&mut Transform, &FollowTarget, &FollowCam)>) {
|
||||||
let (mut transform, tgt, cam) = cam.into_inner();
|
let (mut transform, tgt, cam) = cam.into_inner();
|
||||||
|
|
||||||
let offset = tgt.rot * Vec3::new(0.0, cam.height, cam.distance);
|
let offset = tgt.rot * Vec3::new(0.0, cam.height, cam.distance);
|
||||||
transform.translation = offset + tgt.pos;
|
transform.translation = offset + tgt.pos;
|
||||||
*transform = transform.looking_at(tgt.pos, tgt.up);
|
*transform = transform.looking_at(tgt.pos, tgt.up);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,155 +1,173 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
components::{
|
components::{
|
||||||
camera::{CameraAttachment, CameraMode, CameraPitch, FollowCam, MainCamera},
|
camera::{CameraAttachment, CameraMode, CameraPitch, FollowCam, MainCamera},
|
||||||
player::PlayerDrag,
|
player::PlayerDrag,
|
||||||
tags::{Player, Ship},
|
tags::{Player, Ship},
|
||||||
},
|
},
|
||||||
plugins::{state_management::StateManagementPlugin, *},
|
plugins::{state_management::StateManagementPlugin, *},
|
||||||
states::input::{InputState, PlayerState},
|
states::{
|
||||||
};
|
game::*,
|
||||||
use bevy::{
|
input::{InputState, PlayerState},
|
||||||
prelude::*,
|
loading::{AssetLoadingState, StartupLoadingState},
|
||||||
window::{CursorGrabMode, PrimaryWindow},
|
menu::MenuState,
|
||||||
};
|
},
|
||||||
use bevy_rapier3d::prelude::*;
|
};
|
||||||
|
use bevy::{
|
||||||
#[derive(Default)]
|
prelude::*,
|
||||||
pub struct GamePlugin;
|
window::{CursorGrabMode, PrimaryWindow},
|
||||||
|
};
|
||||||
impl Plugin for GamePlugin {
|
use bevy_asset_loader::prelude::*;
|
||||||
fn build(&self, app: &mut bevy::app::App) {
|
use bevy_rapier3d::prelude::*;
|
||||||
app.add_plugins((
|
|
||||||
FollowCamPlugin,
|
#[derive(Default)]
|
||||||
CameraPlugin,
|
pub struct GamePlugin;
|
||||||
StateManagementPlugin,
|
|
||||||
// ShipPlugin,
|
impl Plugin for GamePlugin {
|
||||||
TypesPlugin,
|
fn build(&self, app: &mut bevy::app::App) {
|
||||||
PlayerPlugin,
|
app.init_state::<AssetLoadingState>();
|
||||||
));
|
app.init_state::<StartupLoadingState>();
|
||||||
app.add_plugins((
|
app.insert_state(GameState::Startup);
|
||||||
RapierPhysicsPlugin::<NoUserData>::default(),
|
app.init_state::<MenuState>();
|
||||||
#[cfg(feature = "dev-phys")]
|
app.init_state::<PlayerState>();
|
||||||
RapierDebugRenderPlugin::default(),
|
app.add_loading_state(
|
||||||
));
|
LoadingState::new(AssetLoadingState::Loading).continue_to_state(AssetLoadingState::Finalizing),
|
||||||
app.add_systems(Startup, (setup_scene, spawn_ship).chain());
|
)
|
||||||
}
|
.add_loading_state(
|
||||||
}
|
LoadingState::new(StartupLoadingState::Loading).continue_to_state(StartupLoadingState::Finalizing),
|
||||||
|
);
|
||||||
fn setup_scene(
|
|
||||||
mut commands: Commands,
|
app.add_plugins((
|
||||||
mut meshes: ResMut<Assets<Mesh>>,
|
FollowCamPlugin,
|
||||||
mut materials: ResMut<Assets<StandardMaterial>>,
|
CameraPlugin,
|
||||||
mut window: Single<&mut Window, With<PrimaryWindow>>,
|
StateManagementPlugin,
|
||||||
) {
|
// ShipPlugin,
|
||||||
window.cursor_options.visible = false;
|
TypesPlugin,
|
||||||
window.cursor_options.grab_mode = CursorGrabMode::Locked;
|
PlayerPlugin,
|
||||||
|
));
|
||||||
let player_eye = commands
|
app.add_plugins((
|
||||||
.spawn((
|
RapierPhysicsPlugin::<NoUserData>::default(),
|
||||||
Name::new("Eye"),
|
#[cfg(feature = "dev-phys")]
|
||||||
Transform::from_translation(Vec3::new(0.0, 1.0, 0.0)),
|
RapierDebugRenderPlugin::default(),
|
||||||
CameraPitch::default(),
|
));
|
||||||
))
|
app.add_systems(Startup, (setup_scene, spawn_ship).chain());
|
||||||
.id();
|
}
|
||||||
let mut player_commands = commands.spawn((
|
}
|
||||||
Name::new("Player"),
|
|
||||||
Player,
|
fn setup_scene(
|
||||||
PlayerDrag(0.5),
|
mut commands: Commands,
|
||||||
Collider::capsule_y(0.5, 0.5),
|
mut meshes: ResMut<Assets<Mesh>>,
|
||||||
RigidBody::KinematicPositionBased,
|
mut materials: ResMut<Assets<StandardMaterial>>,
|
||||||
KinematicCharacterController::default(),
|
mut window: Single<&mut Window, With<PrimaryWindow>>,
|
||||||
Mesh3d(meshes.add(Capsule3d::new(0.5, 1.0))),
|
) {
|
||||||
MeshMaterial3d(materials.add(Color::linear_rgb(1.0, 0.0, 0.2))),
|
window.cursor_options.visible = false;
|
||||||
Transform::from_translation(Vec3::new(0.0, 10.0, 10.0)),
|
window.cursor_options.grab_mode = CursorGrabMode::Locked;
|
||||||
));
|
|
||||||
player_commands.add_child(player_eye);
|
let player_eye = commands
|
||||||
|
.spawn((
|
||||||
commands.spawn((
|
Name::new("Eye"),
|
||||||
Name::new("Camera"),
|
Transform::from_translation(Vec3::new(0.0, 1.0, 0.0)),
|
||||||
Transform::from_xyz(0.0, 1.3, 0.0),
|
CameraPitch::default(),
|
||||||
Visibility::default(),
|
))
|
||||||
MainCamera,
|
.id();
|
||||||
Camera3d::default(),
|
let mut player_commands = commands.spawn((
|
||||||
CameraMode::Player,
|
Name::new("Player"),
|
||||||
CameraAttachment(player_eye),
|
Player,
|
||||||
));
|
PlayerDrag(0.5),
|
||||||
|
Collider::capsule_y(0.5, 0.5),
|
||||||
commands.spawn((
|
RigidBody::KinematicPositionBased,
|
||||||
DirectionalLight {
|
KinematicCharacterController::default(),
|
||||||
shadows_enabled: true,
|
Mesh3d(meshes.add(Capsule3d::new(0.5, 1.0))),
|
||||||
..default()
|
MeshMaterial3d(materials.add(Color::linear_rgb(1.0, 0.0, 0.2))),
|
||||||
},
|
Transform::from_translation(Vec3::new(0.0, 10.0, 10.0)),
|
||||||
Transform::default().looking_to(
|
));
|
||||||
Dir3::from_xyz(-1.0, -1.0, -1.0).expect("Invaid Direction for light"),
|
player_commands.add_child(player_eye);
|
||||||
Dir3::Y,
|
|
||||||
),
|
commands.spawn((
|
||||||
));
|
Name::new("Camera"),
|
||||||
|
Transform::from_xyz(0.0, 1.3, 0.0),
|
||||||
let cube = meshes.add(Cuboid::new(100.0, 0.1, 100.0));
|
Visibility::default(),
|
||||||
let material = materials.add(Color::WHITE);
|
MainCamera,
|
||||||
commands.spawn((
|
Camera3d::default(),
|
||||||
Mesh3d(cube),
|
CameraMode::Player,
|
||||||
MeshMaterial3d(material),
|
CameraAttachment(player_eye),
|
||||||
Transform::default(),
|
));
|
||||||
RigidBody::Fixed,
|
|
||||||
Collider::cuboid(100.0, 0.1, 100.0),
|
commands.spawn((
|
||||||
));
|
DirectionalLight {
|
||||||
}
|
shadows_enabled: true,
|
||||||
|
..default()
|
||||||
fn spawn_ship(
|
},
|
||||||
mut commands: Commands,
|
Transform::default().looking_to(
|
||||||
mut meshes: ResMut<Assets<Mesh>>,
|
Dir3::from_xyz(-1.0, -1.0, -1.0).expect("Invaid Direction for light"),
|
||||||
mut materials: ResMut<Assets<StandardMaterial>>,
|
Dir3::Y,
|
||||||
) {
|
),
|
||||||
let window_material = materials.add(Color::linear_rgba(1.0, 0.0, 1.0, 0.5));
|
));
|
||||||
|
|
||||||
let material = materials.add(Color::BLACK);
|
let cube = meshes.add(Cuboid::new(100.0, 0.1, 100.0));
|
||||||
|
let material = materials.add(Color::WHITE);
|
||||||
commands.spawn((
|
commands.spawn((
|
||||||
Mesh3d(meshes.add(Cuboid::new(3.0, 0.1, 6.0))),
|
Mesh3d(cube),
|
||||||
MeshMaterial3d(material.clone()),
|
MeshMaterial3d(material),
|
||||||
Name::new("Ship"),
|
Transform::default(),
|
||||||
Ship,
|
RigidBody::Fixed,
|
||||||
Velocity::zero(),
|
Collider::cuboid(100.0, 0.1, 100.0),
|
||||||
Damping::default(),
|
));
|
||||||
Transform::from_xyz(0.0, 1.0, 0.0),
|
}
|
||||||
RigidBody::Dynamic,
|
|
||||||
children![
|
fn spawn_ship(
|
||||||
(
|
mut commands: Commands,
|
||||||
Name::new("Back Wall"),
|
mut meshes: ResMut<Assets<Mesh>>,
|
||||||
Mesh3d(meshes.add(Cuboid::new(3.0, 2.0, 0.1))),
|
mut materials: ResMut<Assets<StandardMaterial>>,
|
||||||
Collider::cuboid(3.0, 2.0, 0.1),
|
) {
|
||||||
MeshMaterial3d(material.clone()),
|
let window_material = materials.add(Color::linear_rgba(1.0, 0.0, 1.0, 0.5));
|
||||||
Transform::from_xyz(0.0, 1.0, 6.0 / 2.0),
|
|
||||||
),
|
let material = materials.add(Color::BLACK);
|
||||||
(
|
|
||||||
Name::new("Front Window"),
|
commands.spawn((
|
||||||
Mesh3d(meshes.add(Cuboid::new(3.0, 2.0, 0.1))),
|
Mesh3d(meshes.add(Cuboid::new(3.0, 0.1, 6.0))),
|
||||||
Collider::cuboid(3.0, 2.0, 0.1),
|
MeshMaterial3d(material.clone()),
|
||||||
MeshMaterial3d(window_material),
|
Name::new("Ship"),
|
||||||
Transform::from_xyz(0.0, 1.0, -6.0 / 2.0),
|
Ship,
|
||||||
),
|
Velocity::zero(),
|
||||||
(
|
Damping::default(),
|
||||||
Name::new("Right Wall"),
|
Transform::from_xyz(0.0, 1.0, 0.0),
|
||||||
Mesh3d(meshes.add(Cuboid::new(0.1, 2.0, 6.0))),
|
RigidBody::Dynamic,
|
||||||
Collider::cuboid(0.1, 2.0, 6.0),
|
children![
|
||||||
MeshMaterial3d(material.clone()),
|
(
|
||||||
Transform::from_xyz(3.0 / 2.0, 1.0, 0.0),
|
Name::new("Back Wall"),
|
||||||
),
|
Mesh3d(meshes.add(Cuboid::new(3.0, 2.0, 0.1))),
|
||||||
(
|
Collider::cuboid(3.0, 2.0, 0.1),
|
||||||
Name::new("Left Wall"),
|
MeshMaterial3d(material.clone()),
|
||||||
Mesh3d(meshes.add(Cuboid::new(0.1, 2.0, 6.0))),
|
Transform::from_xyz(0.0, 1.0, 6.0 / 2.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("Front Window"),
|
||||||
),
|
Mesh3d(meshes.add(Cuboid::new(3.0, 2.0, 0.1))),
|
||||||
(
|
Collider::cuboid(3.0, 2.0, 0.1),
|
||||||
Name::new("Roof"),
|
MeshMaterial3d(window_material),
|
||||||
Mesh3d(meshes.add(Cuboid::new(3.0, 0.1, 6.0))),
|
Transform::from_xyz(0.0, 1.0, -6.0 / 2.0),
|
||||||
Collider::cuboid(3.0, 0.1, 6.0),
|
),
|
||||||
MeshMaterial3d(material.clone()),
|
(
|
||||||
Transform::from_xyz(0.0, 2.0, 0.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),
|
||||||
|
)
|
||||||
|
],
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,18 +1,18 @@
|
|||||||
// mod character_controller;
|
// mod character_controller;
|
||||||
mod camera;
|
mod camera;
|
||||||
mod follow_cam;
|
mod follow_cam;
|
||||||
mod game;
|
mod game;
|
||||||
mod player;
|
mod player;
|
||||||
mod ship;
|
mod ship;
|
||||||
mod ship_cam;
|
mod ship_cam;
|
||||||
mod state_management;
|
mod state_management;
|
||||||
mod types;
|
mod types;
|
||||||
|
|
||||||
// pub use character_controller::*;
|
// pub use character_controller::*;
|
||||||
pub use camera::*;
|
pub use camera::*;
|
||||||
pub use follow_cam::*;
|
pub use follow_cam::*;
|
||||||
pub use game::*;
|
pub use game::*;
|
||||||
pub use player::*;
|
pub use player::*;
|
||||||
pub use ship::*;
|
pub use ship::*;
|
||||||
pub use ship_cam::*;
|
pub use ship_cam::*;
|
||||||
pub use types::*;
|
pub use types::*;
|
||||||
|
|||||||
@@ -1,164 +1,164 @@
|
|||||||
use std::f32::{EPSILON, consts::FRAC_PI_2};
|
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,
|
camera::CameraPitch,
|
||||||
player::{GravityDirection, JumpSpeed, MoveSpeed, PlayerDrag, PlayerForce, PlayerMotion, PlayerVelocity},
|
player::{GravityDirection, JumpSpeed, MoveSpeed, PlayerDrag, PlayerForce, PlayerMotion, PlayerVelocity},
|
||||||
tags::Player,
|
tags::Player,
|
||||||
},
|
},
|
||||||
states::input::PlayerInputSystems,
|
states::input::PlayerInputSystems,
|
||||||
utils::{input::get_mouse_delta, rotation::get_alignment_rotation_preserve_twist},
|
utils::{input::get_mouse_delta, rotation::get_alignment_rotation_preserve_twist},
|
||||||
};
|
};
|
||||||
|
|
||||||
pub struct PlayerPlugin;
|
pub struct PlayerPlugin;
|
||||||
|
|
||||||
impl Plugin for PlayerPlugin {
|
impl Plugin for PlayerPlugin {
|
||||||
fn build(&self, app: &mut App) {
|
fn build(&self, app: &mut App) {
|
||||||
app.add_systems(PreUpdate, (keyboard_input, player_look).in_set(PlayerInputSystems));
|
app.add_systems(PreUpdate, (keyboard_input, player_look).in_set(PlayerInputSystems));
|
||||||
app.add_systems(Update, (apply_gravity, apply_forces, apply_motion, apply_drag).chain());
|
app.add_systems(Update, (apply_gravity, apply_forces, apply_motion, apply_drag).chain());
|
||||||
app.add_systems(Update, align_with_gravity);
|
app.add_systems(Update, align_with_gravity);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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();
|
||||||
|
|
||||||
vel.0 += force.0 * time.delta_secs();
|
vel.0 += force.0 * time.delta_secs();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn apply_gravity(
|
fn apply_gravity(
|
||||||
player: Single<
|
player: Single<
|
||||||
(
|
(
|
||||||
&mut PlayerVelocity,
|
&mut PlayerVelocity,
|
||||||
&GravityDirection,
|
&GravityDirection,
|
||||||
&GravityScale,
|
&GravityScale,
|
||||||
Option<&KinematicCharacterControllerOutput>,
|
Option<&KinematicCharacterControllerOutput>,
|
||||||
),
|
),
|
||||||
With<Player>,
|
With<Player>,
|
||||||
>,
|
>,
|
||||||
time: Res<Time>,
|
time: Res<Time>,
|
||||||
) {
|
) {
|
||||||
let (mut vel, dir, scale, output) = player.into_inner();
|
let (mut vel, dir, scale, output) = player.into_inner();
|
||||||
let Some(grav) = dir.0.map(|d| d.as_vec3()) else {
|
let Some(grav) = dir.0.map(|d| d.as_vec3()) else {
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
let is_grounded = output.map(|o| o.grounded).unwrap_or(false);
|
let is_grounded = output.map(|o| o.grounded).unwrap_or(false);
|
||||||
if !is_grounded {
|
if !is_grounded {
|
||||||
vel.0 += grav * scale.0 * 9.47 * time.delta_secs();
|
vel.0 += grav * scale.0 * 9.47 * time.delta_secs();
|
||||||
} else {
|
} else {
|
||||||
let dot = vel.0.dot(grav);
|
let dot = vel.0.dot(grav);
|
||||||
if dot > EPSILON {
|
if dot > EPSILON {
|
||||||
vel.0 -= dot * grav;
|
vel.0 -= dot * grav;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn apply_drag(player: Single<(&mut PlayerVelocity, &PlayerDrag), With<Player>>, time: Res<Time>) {
|
fn apply_drag(player: Single<(&mut PlayerVelocity, &PlayerDrag), With<Player>>, time: Res<Time>) {
|
||||||
let (mut vel, drag) = player.into_inner();
|
let (mut vel, drag) = player.into_inner();
|
||||||
vel.0 *= (1.0 - drag.0 * time.delta_secs()).max(0.0);
|
vel.0 *= (1.0 - drag.0 * time.delta_secs()).max(0.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn apply_motion(
|
fn apply_motion(
|
||||||
player: Single<
|
player: Single<
|
||||||
(
|
(
|
||||||
&mut KinematicCharacterController,
|
&mut KinematicCharacterController,
|
||||||
&Transform,
|
&Transform,
|
||||||
&PlayerVelocity,
|
&PlayerVelocity,
|
||||||
&PlayerMotion,
|
&PlayerMotion,
|
||||||
),
|
),
|
||||||
With<Player>,
|
With<Player>,
|
||||||
>,
|
>,
|
||||||
time: Res<Time>,
|
time: Res<Time>,
|
||||||
) {
|
) {
|
||||||
let (mut controller, transform, vel, motion) = player.into_inner();
|
let (mut controller, transform, vel, motion) = player.into_inner();
|
||||||
// let max_vel = vel.0.clamp_length_max(500.0);
|
// let max_vel = vel.0.clamp_length_max(500.0);
|
||||||
let global_motion = transform.rotation * motion.0;
|
let global_motion = transform.rotation * motion.0;
|
||||||
controller.translation = Some((vel.0 + global_motion) * controller.custom_mass.unwrap_or(1.0) * time.delta_secs());
|
controller.translation = Some((vel.0 + global_motion) * controller.custom_mass.unwrap_or(1.0) * time.delta_secs());
|
||||||
}
|
}
|
||||||
|
|
||||||
fn keyboard_input(
|
fn keyboard_input(
|
||||||
key: Res<ButtonInput<KeyCode>>,
|
key: Res<ButtonInput<KeyCode>>,
|
||||||
player: Single<
|
player: Single<
|
||||||
(
|
(
|
||||||
&mut PlayerMotion,
|
&mut PlayerMotion,
|
||||||
&mut PlayerVelocity,
|
&mut PlayerVelocity,
|
||||||
&MoveSpeed,
|
&MoveSpeed,
|
||||||
&JumpSpeed,
|
&JumpSpeed,
|
||||||
&Transform,
|
&Transform,
|
||||||
Option<&KinematicCharacterControllerOutput>,
|
Option<&KinematicCharacterControllerOutput>,
|
||||||
),
|
),
|
||||||
With<Player>,
|
With<Player>,
|
||||||
>,
|
>,
|
||||||
) {
|
) {
|
||||||
let (mut motion, mut vel, speed, jump, transform, output) = player.into_inner();
|
let (mut motion, mut vel, speed, jump, transform, output) = player.into_inner();
|
||||||
let mut move_vec = Vec3::ZERO;
|
let mut move_vec = Vec3::ZERO;
|
||||||
|
|
||||||
if key.pressed(KeyCode::KeyW) {
|
if key.pressed(KeyCode::KeyW) {
|
||||||
move_vec.z = -1.0;
|
move_vec.z = -1.0;
|
||||||
} else if key.pressed(KeyCode::KeyS) {
|
} else if key.pressed(KeyCode::KeyS) {
|
||||||
move_vec.z = 1.0;
|
move_vec.z = 1.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if key.pressed(KeyCode::KeyA) {
|
if key.pressed(KeyCode::KeyA) {
|
||||||
move_vec.x = -1.0;
|
move_vec.x = -1.0;
|
||||||
} else if key.pressed(KeyCode::KeyD) {
|
} else if key.pressed(KeyCode::KeyD) {
|
||||||
move_vec.x = 1.0;
|
move_vec.x = 1.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
move_vec = move_vec.normalize_or_zero() * speed.0;
|
move_vec = move_vec.normalize_or_zero() * speed.0;
|
||||||
|
|
||||||
let is_grounded = output.map(|o| o.grounded).unwrap_or(false);
|
let is_grounded = output.map(|o| o.grounded).unwrap_or(false);
|
||||||
if key.just_pressed(KeyCode::Space) && is_grounded {
|
if key.just_pressed(KeyCode::Space) && is_grounded {
|
||||||
vel.0 += transform.up() * jump.0;
|
vel.0 += transform.up() * jump.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
motion.0 = move_vec;
|
motion.0 = move_vec;
|
||||||
|
|
||||||
// transform.rotate_z(angle * time.delta_secs());
|
// transform.rotate_z(angle * time.delta_secs());
|
||||||
// controller.up = transform.up().as_vec3();
|
// controller.up = transform.up().as_vec3();
|
||||||
// move_vec += transform.down().as_vec3();
|
// move_vec += transform.down().as_vec3();
|
||||||
// controller.translation = Some(move_vec * time.delta_secs());
|
// controller.translation = Some(move_vec * time.delta_secs());
|
||||||
}
|
}
|
||||||
|
|
||||||
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>,
|
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);
|
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(
|
||||||
player: Single<
|
player: Single<
|
||||||
(
|
(
|
||||||
&mut Transform,
|
&mut Transform,
|
||||||
&GravityDirection,
|
&GravityDirection,
|
||||||
Option<&KinematicCharacterControllerOutput>,
|
Option<&KinematicCharacterControllerOutput>,
|
||||||
),
|
),
|
||||||
With<Player>,
|
With<Player>,
|
||||||
>,
|
>,
|
||||||
time: Res<Time>,
|
time: Res<Time>,
|
||||||
) {
|
) {
|
||||||
let (mut transform, grav, output) = player.into_inner();
|
let (mut transform, grav, output) = player.into_inner();
|
||||||
let is_grounded = output.map(|o| o.grounded).unwrap_or(false);
|
let is_grounded = output.map(|o| o.grounded).unwrap_or(false);
|
||||||
|
|
||||||
if !is_grounded {
|
if !is_grounded {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let Some(grav_dir) = grav.0 else {
|
let Some(grav_dir) = grav.0 else {
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
|
||||||
let grav_up = grav_dir * -1.0;
|
let grav_up = grav_dir * -1.0;
|
||||||
|
|
||||||
let desired_rotation = get_alignment_rotation_preserve_twist(transform.rotation, grav_up);
|
let desired_rotation = get_alignment_rotation_preserve_twist(transform.rotation, grav_up);
|
||||||
transform.rotation = transform.rotation.lerp(desired_rotation, time.delta_secs());
|
transform.rotation = transform.rotation.lerp(desired_rotation, time.delta_secs());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,103 +1,103 @@
|
|||||||
use bevy::{input::mouse::MouseMotion, prelude::*};
|
use bevy::{input::mouse::MouseMotion, prelude::*};
|
||||||
use bevy_rapier3d::prelude::*;
|
use bevy_rapier3d::prelude::*;
|
||||||
|
|
||||||
use crate::{components::tags::Ship, utils::input::get_mouse_delta};
|
use crate::{components::tags::Ship, utils::input::get_mouse_delta};
|
||||||
|
|
||||||
pub struct ShipPlugin;
|
pub struct ShipPlugin;
|
||||||
|
|
||||||
impl Plugin for ShipPlugin {
|
impl Plugin for ShipPlugin {
|
||||||
fn build(&self, app: &mut App) {
|
fn build(&self, app: &mut App) {
|
||||||
app.add_systems(Update, ship_controls);
|
app.add_systems(Update, ship_controls);
|
||||||
#[cfg(feature = "dev-viz")]
|
#[cfg(feature = "dev-viz")]
|
||||||
app.add_systems(Update, ship_debug);
|
app.add_systems(Update, ship_debug);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn ship_controls(
|
fn ship_controls(
|
||||||
ship_query: Single<(&Transform, &mut Velocity, &mut Damping), With<Ship>>,
|
ship_query: Single<(&Transform, &mut Velocity, &mut Damping), With<Ship>>,
|
||||||
key: Res<ButtonInput<KeyCode>>,
|
key: Res<ButtonInput<KeyCode>>,
|
||||||
time: Res<Time>,
|
time: Res<Time>,
|
||||||
mouse_motion: EventReader<MouseMotion>,
|
mouse_motion: EventReader<MouseMotion>,
|
||||||
) {
|
) {
|
||||||
let (transform, mut vel, mut damp) = ship_query.into_inner();
|
let (transform, mut vel, mut damp) = ship_query.into_inner();
|
||||||
|
|
||||||
let mut move_vec = Vec3::ZERO;
|
let mut move_vec = Vec3::ZERO;
|
||||||
|
|
||||||
if key.pressed(KeyCode::KeyW) {
|
if key.pressed(KeyCode::KeyW) {
|
||||||
move_vec.z = -1.0;
|
move_vec.z = -1.0;
|
||||||
} else if key.pressed(KeyCode::KeyS) {
|
} else if key.pressed(KeyCode::KeyS) {
|
||||||
move_vec.z = 1.0;
|
move_vec.z = 1.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if key.pressed(KeyCode::KeyA) {
|
if key.pressed(KeyCode::KeyA) {
|
||||||
move_vec.x = -1.0;
|
move_vec.x = -1.0;
|
||||||
} else if key.pressed(KeyCode::KeyD) {
|
} else if key.pressed(KeyCode::KeyD) {
|
||||||
move_vec.x = 1.0;
|
move_vec.x = 1.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if key.pressed(KeyCode::Space) {
|
if key.pressed(KeyCode::Space) {
|
||||||
move_vec.y = 1.0;
|
move_vec.y = 1.0;
|
||||||
} else if key.pressed(KeyCode::ShiftLeft) {
|
} else if key.pressed(KeyCode::ShiftLeft) {
|
||||||
move_vec.y = -1.0;
|
move_vec.y = -1.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if key.pressed(KeyCode::ControlLeft) {
|
if key.pressed(KeyCode::ControlLeft) {
|
||||||
damp.linear_damping = 0.8;
|
damp.linear_damping = 0.8;
|
||||||
damp.angular_damping = 0.8;
|
damp.angular_damping = 0.8;
|
||||||
} else {
|
} else {
|
||||||
damp.linear_damping = 0.0;
|
damp.linear_damping = 0.0;
|
||||||
damp.angular_damping = 0.0;
|
damp.angular_damping = 0.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
const ROLL_SPEED: f32 = 1.0;
|
const ROLL_SPEED: f32 = 1.0;
|
||||||
const DEAD_ZONE: f32 = 0.1;
|
const DEAD_ZONE: f32 = 0.1;
|
||||||
const INPUT_CURVE: f32 = 3.0;
|
const INPUT_CURVE: f32 = 3.0;
|
||||||
let yaw = if key.pressed(KeyCode::KeyQ) {
|
let yaw = if key.pressed(KeyCode::KeyQ) {
|
||||||
Vec3::Y * ROLL_SPEED
|
Vec3::Y * ROLL_SPEED
|
||||||
} else if key.pressed(KeyCode::KeyE) {
|
} else if key.pressed(KeyCode::KeyE) {
|
||||||
Vec3::Y * -ROLL_SPEED
|
Vec3::Y * -ROLL_SPEED
|
||||||
} else {
|
} else {
|
||||||
Vec3::ZERO
|
Vec3::ZERO
|
||||||
};
|
};
|
||||||
|
|
||||||
let mouse_delta = get_mouse_delta(mouse_motion).normalize_or_zero();
|
let mouse_delta = get_mouse_delta(mouse_motion).normalize_or_zero();
|
||||||
let mouse_input = mouse_delta
|
let mouse_input = mouse_delta
|
||||||
.abs()
|
.abs()
|
||||||
.map(|v| if v < DEAD_ZONE { 0.0 } else { v })
|
.map(|v| if v < DEAD_ZONE { 0.0 } else { v })
|
||||||
.powf(INPUT_CURVE)
|
.powf(INPUT_CURVE)
|
||||||
* mouse_delta.signum();
|
* mouse_delta.signum();
|
||||||
|
|
||||||
let roll = Vec3::NEG_Z * mouse_input.x;
|
let roll = Vec3::NEG_Z * mouse_input.x;
|
||||||
let pitch = Vec3::X * mouse_input.y;
|
let pitch = Vec3::X * mouse_input.y;
|
||||||
|
|
||||||
vel.angvel += (transform.rotation * (roll + yaw + pitch)) * time.delta_secs();
|
vel.angvel += (transform.rotation * (roll + yaw + pitch)) * time.delta_secs();
|
||||||
|
|
||||||
move_vec = transform.rotation * move_vec.normalize_or_zero();
|
move_vec = transform.rotation * move_vec.normalize_or_zero();
|
||||||
|
|
||||||
vel.linvel += move_vec * time.delta_secs();
|
vel.linvel += move_vec * time.delta_secs();
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "dev-viz")]
|
#[cfg(feature = "dev-viz")]
|
||||||
fn ship_debug(mut gizmos: Gizmos, ship: Single<&Transform, With<Ship>>) {
|
fn ship_debug(mut gizmos: Gizmos, ship: Single<&Transform, With<Ship>>) {
|
||||||
use std::ops::Range;
|
use std::ops::Range;
|
||||||
|
|
||||||
let base = ship.translation.floor();
|
let base = ship.translation.floor();
|
||||||
const GRID: Range<i32> = -10..10;
|
const GRID: Range<i32> = -10..10;
|
||||||
for x in GRID {
|
for x in GRID {
|
||||||
for y in GRID {
|
for y in GRID {
|
||||||
for z in GRID {
|
for z in GRID {
|
||||||
let p = base + Vec3::new(x as f32, y as f32, z as f32);
|
let p = base + Vec3::new(x as f32, y as f32, z as f32);
|
||||||
let color = if x == 0 && y == 0 {
|
let color = if x == 0 && y == 0 {
|
||||||
LinearRgba::BLUE
|
LinearRgba::BLUE
|
||||||
} else if x == 0 && z == 0 {
|
} else if x == 0 && z == 0 {
|
||||||
LinearRgba::GREEN
|
LinearRgba::GREEN
|
||||||
} else if y == 0 && z == 0 {
|
} else if y == 0 && z == 0 {
|
||||||
LinearRgba::RED
|
LinearRgba::RED
|
||||||
} else {
|
} else {
|
||||||
LinearRgba::gray(0.2).with_alpha(0.2)
|
LinearRgba::gray(0.2).with_alpha(0.2)
|
||||||
};
|
};
|
||||||
gizmos.sphere(p, 0.01, color);
|
gizmos.sphere(p, 0.01, color);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
|
|
||||||
pub struct ShipCamPlugin;
|
pub struct ShipCamPlugin;
|
||||||
|
|
||||||
impl Plugin for ShipCamPlugin {
|
impl Plugin for ShipCamPlugin {
|
||||||
fn build(&self, app: &mut App) {
|
fn build(&self, app: &mut App) {
|
||||||
todo!()
|
todo!()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,16 +1,41 @@
|
|||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
|
|
||||||
use crate::states::input::{InputState, PlayerInputSystems, PlayerState};
|
use crate::{
|
||||||
|
configure_sets,
|
||||||
pub struct StateManagementPlugin;
|
states::{
|
||||||
|
input::{InputState, PlayerInputSystems, PlayerState},
|
||||||
impl Plugin for StateManagementPlugin {
|
menu::{
|
||||||
fn build(&self, app: &mut App) {
|
MainMenuSystems, MenuCleanupSystems, MenuStartupSystems, MenuState, OptionsMenuSystems, SavesMenuSystems,
|
||||||
app.init_state::<PlayerState>();
|
},
|
||||||
app.init_state::<InputState>();
|
play::{PausedSystems, PlayCleanupSystems, PlayStartupSystems, PlayState, PlaySystems},
|
||||||
|
},
|
||||||
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)));
|
pub struct StateManagementPlugin;
|
||||||
}
|
|
||||||
}
|
impl Plugin for StateManagementPlugin {
|
||||||
|
fn build(&self, app: &mut App) {
|
||||||
|
app.init_state::<PlayerState>();
|
||||||
|
app.init_state::<InputState>();
|
||||||
|
|
||||||
|
//Menu
|
||||||
|
configure_sets!(app, MenuStartupSystems, in_state(MenuState::Startup));
|
||||||
|
configure_sets!(app, MainMenuSystems, in_state(MenuState::Main));
|
||||||
|
configure_sets!(app, OptionsMenuSystems, in_state(MenuState::Options));
|
||||||
|
configure_sets!(app, SavesMenuSystems, in_state(MenuState::Saves));
|
||||||
|
configure_sets!(app, MenuCleanupSystems, in_state(MenuState::Cleanup));
|
||||||
|
|
||||||
|
//Play
|
||||||
|
configure_sets!(app, PlayStartupSystems, in_state(PlayState::Startup));
|
||||||
|
configure_sets!(app, PausedSystems, in_state(PlayState::Paused));
|
||||||
|
configure_sets!(app, PlaySystems, in_state(PlayState::Playing));
|
||||||
|
configure_sets!(app, PlayCleanupSystems, in_state(PlayState::Cleanup));
|
||||||
|
|
||||||
|
//Input
|
||||||
|
configure_sets!(
|
||||||
|
app,
|
||||||
|
PlayerInputSystems,
|
||||||
|
in_state(InputState::World).and(in_state(PlayState::Playing))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,23 +1,23 @@
|
|||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
|
|
||||||
use crate::components::{
|
use crate::components::{
|
||||||
camera::{CameraAttachment, CameraMode, CameraPitch, FollowCam, FollowTarget},
|
camera::{CameraAttachment, CameraMode, CameraPitch, FollowCam, FollowTarget},
|
||||||
player::{GravityDirection, MoveSpeed, PlayerForce, PlayerMotion, PlayerVelocity},
|
player::{GravityDirection, MoveSpeed, PlayerForce, PlayerMotion, PlayerVelocity},
|
||||||
};
|
};
|
||||||
|
|
||||||
pub struct TypesPlugin;
|
pub struct TypesPlugin;
|
||||||
|
|
||||||
impl Plugin for TypesPlugin {
|
impl Plugin for TypesPlugin {
|
||||||
fn build(&self, app: &mut App) {
|
fn build(&self, app: &mut App) {
|
||||||
app.register_type::<FollowTarget>();
|
app.register_type::<FollowTarget>();
|
||||||
app.register_type::<FollowCam>();
|
app.register_type::<FollowCam>();
|
||||||
app.register_type::<PlayerVelocity>();
|
app.register_type::<PlayerVelocity>();
|
||||||
app.register_type::<PlayerForce>();
|
app.register_type::<PlayerForce>();
|
||||||
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::<CameraPitch>();
|
||||||
app.register_type::<CameraAttachment>();
|
app.register_type::<CameraAttachment>();
|
||||||
app.register_type::<CameraMode>();
|
app.register_type::<CameraMode>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
9
src/states/game.rs
Normal file
9
src/states/game.rs
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
use bevy::prelude::*;
|
||||||
|
|
||||||
|
#[derive(States, Debug, Reflect, Default, Clone, Copy, PartialEq, Eq, Hash)]
|
||||||
|
pub enum GameState {
|
||||||
|
#[default]
|
||||||
|
Startup,
|
||||||
|
MainMenu,
|
||||||
|
PlayGame,
|
||||||
|
}
|
||||||
@@ -1,20 +1,20 @@
|
|||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
|
|
||||||
#[derive(States, Debug, Default, Clone, PartialEq, Eq, Hash)]
|
#[derive(States, Debug, Default, Clone, PartialEq, Eq, Hash)]
|
||||||
pub enum InputState {
|
pub enum InputState {
|
||||||
#[default]
|
#[default]
|
||||||
World,
|
World,
|
||||||
Menu,
|
Menu,
|
||||||
Detached,
|
Detached,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(SystemSet, Debug, Clone, PartialEq, Eq, Hash)]
|
#[derive(SystemSet, Debug, Clone, PartialEq, Eq, Hash)]
|
||||||
pub struct PlayerInputSystems;
|
pub struct PlayerInputSystems;
|
||||||
|
|
||||||
#[derive(States, Debug, Default, Clone, PartialEq, Eq, Hash)]
|
#[derive(States, Debug, Default, Clone, PartialEq, Eq, Hash)]
|
||||||
pub enum PlayerState {
|
pub enum PlayerState {
|
||||||
#[default]
|
#[default]
|
||||||
OnFoot,
|
OnFoot,
|
||||||
FreeFloating,
|
FreeFloating,
|
||||||
Piloting,
|
Piloting,
|
||||||
}
|
}
|
||||||
|
|||||||
18
src/states/loading.rs
Normal file
18
src/states/loading.rs
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
use bevy::prelude::*;
|
||||||
|
#[derive(States, Debug, Default, Clone, PartialEq, Eq, Hash)]
|
||||||
|
pub enum StartupLoadingState {
|
||||||
|
#[default]
|
||||||
|
Pending,
|
||||||
|
Loading,
|
||||||
|
Finalizing,
|
||||||
|
Done,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(States, Debug, Default, Clone, PartialEq, Eq, Hash)]
|
||||||
|
pub enum AssetLoadingState {
|
||||||
|
#[default]
|
||||||
|
Pending,
|
||||||
|
Loading,
|
||||||
|
Finalizing,
|
||||||
|
Done,
|
||||||
|
}
|
||||||
27
src/states/menu.rs
Normal file
27
src/states/menu.rs
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
use bevy::prelude::*;
|
||||||
|
|
||||||
|
#[derive(States, Debug, Reflect, Default, Clone, Copy, PartialEq, Eq, Hash)]
|
||||||
|
pub enum MenuState {
|
||||||
|
#[default]
|
||||||
|
Loading,
|
||||||
|
Startup,
|
||||||
|
Main,
|
||||||
|
Saves,
|
||||||
|
Options,
|
||||||
|
Cleanup,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(SystemSet, Debug, Clone, PartialEq, Eq, Hash)]
|
||||||
|
pub struct MenuStartupSystems;
|
||||||
|
|
||||||
|
#[derive(SystemSet, Debug, Clone, PartialEq, Eq, Hash)]
|
||||||
|
pub struct MainMenuSystems;
|
||||||
|
|
||||||
|
#[derive(SystemSet, Debug, Clone, PartialEq, Eq, Hash)]
|
||||||
|
pub struct OptionsMenuSystems;
|
||||||
|
|
||||||
|
#[derive(SystemSet, Debug, Clone, PartialEq, Eq, Hash)]
|
||||||
|
pub struct SavesMenuSystems;
|
||||||
|
|
||||||
|
#[derive(SystemSet, Debug, Clone, PartialEq, Eq, Hash)]
|
||||||
|
pub struct MenuCleanupSystems;
|
||||||
@@ -1 +1,5 @@
|
|||||||
pub mod input;
|
pub mod game;
|
||||||
|
pub mod input;
|
||||||
|
pub mod loading;
|
||||||
|
pub mod menu;
|
||||||
|
pub mod play;
|
||||||
|
|||||||
23
src/states/play.rs
Normal file
23
src/states/play.rs
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
use bevy::prelude::*;
|
||||||
|
|
||||||
|
#[derive(States, Debug, Reflect, Default, Clone, Copy, PartialEq, Eq, Hash)]
|
||||||
|
pub enum PlayState {
|
||||||
|
#[default]
|
||||||
|
Loading,
|
||||||
|
Startup,
|
||||||
|
Playing,
|
||||||
|
Paused,
|
||||||
|
Cleanup,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(SystemSet, Debug, Clone, PartialEq, Eq, Hash)]
|
||||||
|
pub struct PlayStartupSystems;
|
||||||
|
|
||||||
|
#[derive(SystemSet, Debug, Clone, PartialEq, Eq, Hash)]
|
||||||
|
pub struct PlaySystems;
|
||||||
|
|
||||||
|
#[derive(SystemSet, Debug, Clone, PartialEq, Eq, Hash)]
|
||||||
|
pub struct PlayCleanupSystems;
|
||||||
|
|
||||||
|
#[derive(SystemSet, Debug, Clone, PartialEq, Eq, Hash)]
|
||||||
|
pub struct PausedSystems;
|
||||||
@@ -1,9 +1,9 @@
|
|||||||
use bevy::{input::mouse::MouseMotion, prelude::*};
|
use bevy::{input::mouse::MouseMotion, prelude::*};
|
||||||
|
|
||||||
pub fn get_mouse_delta(mut mouse_motion: EventReader<MouseMotion>) -> Vec2 {
|
pub fn get_mouse_delta(mut mouse_motion: EventReader<MouseMotion>) -> Vec2 {
|
||||||
let mut delta = Vec2::ZERO;
|
let mut delta = Vec2::ZERO;
|
||||||
for e in mouse_motion.read() {
|
for e in mouse_motion.read() {
|
||||||
delta += e.delta;
|
delta += e.delta;
|
||||||
}
|
}
|
||||||
return delta;
|
return delta;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,2 +1,2 @@
|
|||||||
pub mod input;
|
pub mod input;
|
||||||
pub mod rotation;
|
pub mod rotation;
|
||||||
|
|||||||
@@ -1,38 +1,38 @@
|
|||||||
use std::f32::{EPSILON, consts::PI};
|
use std::f32::{EPSILON, consts::PI};
|
||||||
|
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
|
|
||||||
pub fn get_alignment_rotation(cur_dir: Dir3, target_dir: Vec3) -> Quat {
|
pub fn get_alignment_rotation(cur_dir: Dir3, target_dir: Vec3) -> Quat {
|
||||||
let tgt = target_dir.normalize();
|
let tgt = target_dir.normalize();
|
||||||
let axis = cur_dir.cross(tgt);
|
let axis = cur_dir.cross(tgt);
|
||||||
let axis_len = axis.length();
|
let axis_len = axis.length();
|
||||||
if axis_len < EPSILON {
|
if axis_len < EPSILON {
|
||||||
let dot = cur_dir.dot(tgt);
|
let dot = cur_dir.dot(tgt);
|
||||||
if dot > 0.999 {
|
if dot > 0.999 {
|
||||||
return Quat::IDENTITY;
|
return Quat::IDENTITY;
|
||||||
} else {
|
} else {
|
||||||
let ortho = if cur_dir.x < 0.99 {
|
let ortho = if cur_dir.x < 0.99 {
|
||||||
cur_dir.cross(Vec3::X)
|
cur_dir.cross(Vec3::X)
|
||||||
} else {
|
} else {
|
||||||
cur_dir.cross(Vec3::Z)
|
cur_dir.cross(Vec3::Z)
|
||||||
};
|
};
|
||||||
return Quat::from_axis_angle(ortho.normalize(), PI);
|
return Quat::from_axis_angle(ortho.normalize(), PI);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
let angle = cur_dir.angle_between(tgt);
|
let angle = cur_dir.angle_between(tgt);
|
||||||
return Quat::from_axis_angle(axis.normalize(), angle);
|
return Quat::from_axis_angle(axis.normalize(), angle);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_alignment_rotation_preserve_twist(cur_rot: Quat, target_up: Vec3) -> Quat {
|
pub fn get_alignment_rotation_preserve_twist(cur_rot: Quat, target_up: Vec3) -> Quat {
|
||||||
let target_up = target_up.normalize();
|
let target_up = target_up.normalize();
|
||||||
|
|
||||||
let cur_fwd = cur_rot * Vec3::Z;
|
let cur_fwd = cur_rot * Vec3::Z;
|
||||||
|
|
||||||
let forward_projection = (cur_fwd - target_up * cur_fwd.dot(target_up)).normalize();
|
let forward_projection = (cur_fwd - target_up * cur_fwd.dot(target_up)).normalize();
|
||||||
let right = target_up.cross(forward_projection).normalize();
|
let right = target_up.cross(forward_projection).normalize();
|
||||||
let fwd = right.cross(target_up);
|
let fwd = right.cross(target_up);
|
||||||
let basis = Mat3::from_cols(right, target_up, fwd);
|
let basis = Mat3::from_cols(right, target_up, fwd);
|
||||||
|
|
||||||
return Quat::from_mat3(&basis);
|
return Quat::from_mat3(&basis);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user