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",
|
||||
]
|
||||
|
||||
[[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]]
|
||||
name = "bevy_asset_macros"
|
||||
version = "0.16.1"
|
||||
@@ -4009,6 +4032,12 @@ version = "1.0.15"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a"
|
||||
|
||||
[[package]]
|
||||
name = "path-slash"
|
||||
version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1e91099d4268b0e11973f036e885d652fb0b21fedcf69738c627f94db6a44f42"
|
||||
|
||||
[[package]]
|
||||
name = "percent-encoding"
|
||||
version = "2.3.1"
|
||||
@@ -4798,6 +4827,7 @@ version = "0.1.0"
|
||||
dependencies = [
|
||||
"bevy",
|
||||
"bevy-inspector-egui",
|
||||
"bevy_asset_loader",
|
||||
"bevy_rapier3d",
|
||||
]
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@ edition = "2024"
|
||||
bevy = { version = "0.16.1", features = [] }
|
||||
# bevy_rapier3d = { version = "0.29.0", features = ["simd-stable", "parallel"] }
|
||||
bevy-inspector-egui = "0.31.0"
|
||||
bevy_asset_loader = "0.23.0"
|
||||
bevy_rapier3d = "0.30.0"
|
||||
|
||||
[features]
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
hard_tabs = true
|
||||
max_width = 120
|
||||
brace_style = "AlwaysNextLine"
|
||||
control_brace_style = "AlwaysNextLine"
|
||||
hard_tabs = true
|
||||
max_width = 120
|
||||
brace_style = "AlwaysNextLine"
|
||||
control_brace_style = "AlwaysNextLine"
|
||||
|
||||
@@ -1,50 +1,50 @@
|
||||
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,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Component, Default, Reflect)]
|
||||
pub enum CameraMode {
|
||||
#[default]
|
||||
Player,
|
||||
Ship,
|
||||
Disabled,
|
||||
}
|
||||
|
||||
#[derive(Component, Reflect)]
|
||||
pub struct CameraAttachment(pub Entity);
|
||||
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,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Component, Default, Reflect)]
|
||||
pub enum CameraMode {
|
||||
#[default]
|
||||
Player,
|
||||
Ship,
|
||||
Disabled,
|
||||
}
|
||||
|
||||
#[derive(Component, Reflect)]
|
||||
pub struct CameraAttachment(pub Entity);
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
pub mod camera;
|
||||
// pub mod character_controller;
|
||||
pub mod player;
|
||||
pub mod tags;
|
||||
pub mod camera;
|
||||
// pub mod character_controller;
|
||||
pub mod player;
|
||||
pub mod tags;
|
||||
|
||||
@@ -1,49 +1,49 @@
|
||||
use bevy::prelude::*;
|
||||
use bevy_rapier3d::prelude::GravityScale;
|
||||
|
||||
#[derive(Component, Default, Reflect)]
|
||||
#[require(PlayerVelocity, MoveSpeed, JumpSpeed, PlayerDrag)]
|
||||
pub struct PlayerMotion(pub Vec3);
|
||||
|
||||
#[derive(Component, Default, Reflect)]
|
||||
pub struct PlayerForce(pub Vec3);
|
||||
|
||||
#[derive(Component, Reflect)]
|
||||
#[require(GravityScale)]
|
||||
pub struct GravityDirection(pub Option<Dir3>);
|
||||
|
||||
impl Default for GravityDirection {
|
||||
fn default() -> Self {
|
||||
Self::DOWN
|
||||
}
|
||||
}
|
||||
|
||||
impl GravityDirection {
|
||||
pub const DOWN: GravityDirection = GravityDirection(Some(Dir3::NEG_Y));
|
||||
#[allow(dead_code)]
|
||||
pub const NONE: GravityDirection = GravityDirection(None);
|
||||
}
|
||||
|
||||
#[derive(Component, Default, Reflect)]
|
||||
pub struct PlayerVelocity(pub Vec3);
|
||||
|
||||
#[derive(Component, Reflect)]
|
||||
pub struct MoveSpeed(pub f32);
|
||||
|
||||
impl Default for MoveSpeed {
|
||||
fn default() -> Self {
|
||||
Self(10.0)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Component, Reflect)]
|
||||
pub struct JumpSpeed(pub f32);
|
||||
|
||||
impl Default for JumpSpeed {
|
||||
fn default() -> Self {
|
||||
Self(10.0)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Component, Default, Reflect)]
|
||||
pub struct PlayerDrag(pub f32);
|
||||
use bevy::prelude::*;
|
||||
use bevy_rapier3d::prelude::GravityScale;
|
||||
|
||||
#[derive(Component, Default, Reflect)]
|
||||
#[require(PlayerVelocity, MoveSpeed, JumpSpeed, PlayerDrag)]
|
||||
pub struct PlayerMotion(pub Vec3);
|
||||
|
||||
#[derive(Component, Default, Reflect)]
|
||||
pub struct PlayerForce(pub Vec3);
|
||||
|
||||
#[derive(Component, Reflect)]
|
||||
#[require(GravityScale)]
|
||||
pub struct GravityDirection(pub Option<Dir3>);
|
||||
|
||||
impl Default for GravityDirection {
|
||||
fn default() -> Self {
|
||||
Self::DOWN
|
||||
}
|
||||
}
|
||||
|
||||
impl GravityDirection {
|
||||
pub const DOWN: GravityDirection = GravityDirection(Some(Dir3::NEG_Y));
|
||||
#[allow(dead_code)]
|
||||
pub const NONE: GravityDirection = GravityDirection(None);
|
||||
}
|
||||
|
||||
#[derive(Component, Default, Reflect)]
|
||||
pub struct PlayerVelocity(pub Vec3);
|
||||
|
||||
#[derive(Component, Reflect)]
|
||||
pub struct MoveSpeed(pub f32);
|
||||
|
||||
impl Default for MoveSpeed {
|
||||
fn default() -> Self {
|
||||
Self(10.0)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Component, Reflect)]
|
||||
pub struct JumpSpeed(pub f32);
|
||||
|
||||
impl Default for JumpSpeed {
|
||||
fn default() -> Self {
|
||||
Self(10.0)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Component, Default, Reflect)]
|
||||
pub struct PlayerDrag(pub f32);
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
use bevy::prelude::*;
|
||||
|
||||
use crate::components::player::*;
|
||||
|
||||
#[derive(Component, Reflect)]
|
||||
#[require(PlayerMotion, PlayerForce, GravityDirection)]
|
||||
pub struct Player;
|
||||
|
||||
#[derive(Component, Reflect)]
|
||||
pub struct Ship;
|
||||
use bevy::prelude::*;
|
||||
|
||||
use crate::components::player::*;
|
||||
|
||||
#[derive(Component, Reflect)]
|
||||
#[require(PlayerMotion, PlayerForce, GravityDirection)]
|
||||
pub struct Player;
|
||||
|
||||
#[derive(Component, Reflect)]
|
||||
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 states;
|
||||
mod utils;
|
||||
use bevy::{prelude::*, window::PresentMode};
|
||||
use bevy_inspector_egui::{bevy_egui::EguiPlugin, quick::WorldInspectorPlugin};
|
||||
use plugins::GamePlugin;
|
||||
mod components;
|
||||
mod resources;
|
||||
const NAME: &str = "Space Game";
|
||||
fn main() {
|
||||
App::new()
|
||||
.add_plugins((
|
||||
DefaultPlugins
|
||||
.set(WindowPlugin {
|
||||
primary_window: Some(Window {
|
||||
title: NAME.into(),
|
||||
name: Some(NAME.into()),
|
||||
#[cfg(debug_assertions)]
|
||||
resolution: (1920., 1080.).into(),
|
||||
present_mode: PresentMode::AutoNoVsync,
|
||||
#[cfg(not(debug_assertions))]
|
||||
mode: bevy::window::WindowMode::BorderlessFullscreen,
|
||||
..default()
|
||||
}),
|
||||
..default()
|
||||
})
|
||||
.set(AssetPlugin {
|
||||
#[cfg(not(debug_assertions))]
|
||||
watch_for_changes_override: Some(true),
|
||||
..Default::default()
|
||||
}),
|
||||
EguiPlugin {
|
||||
enable_multipass_for_primary_context: true,
|
||||
},
|
||||
WorldInspectorPlugin::new(),
|
||||
GamePlugin,
|
||||
))
|
||||
.run();
|
||||
}
|
||||
mod macros;
|
||||
mod plugins;
|
||||
mod states;
|
||||
mod utils;
|
||||
use bevy::{prelude::*, window::PresentMode};
|
||||
use bevy_inspector_egui::{bevy_egui::EguiPlugin, quick::WorldInspectorPlugin};
|
||||
use plugins::GamePlugin;
|
||||
mod components;
|
||||
mod resources;
|
||||
const NAME: &str = "Space Game";
|
||||
fn main() {
|
||||
App::new()
|
||||
.add_plugins((
|
||||
DefaultPlugins
|
||||
.set(WindowPlugin {
|
||||
primary_window: Some(Window {
|
||||
title: NAME.into(),
|
||||
name: Some(NAME.into()),
|
||||
#[cfg(debug_assertions)]
|
||||
resolution: (1920., 1080.).into(),
|
||||
present_mode: PresentMode::AutoNoVsync,
|
||||
#[cfg(not(debug_assertions))]
|
||||
mode: bevy::window::WindowMode::BorderlessFullscreen,
|
||||
..default()
|
||||
}),
|
||||
..default()
|
||||
})
|
||||
.set(AssetPlugin {
|
||||
#[cfg(not(debug_assertions))]
|
||||
watch_for_changes_override: Some(true),
|
||||
..Default::default()
|
||||
}),
|
||||
EguiPlugin {
|
||||
enable_multipass_for_primary_context: true,
|
||||
},
|
||||
WorldInspectorPlugin::new(),
|
||||
GamePlugin,
|
||||
))
|
||||
.run();
|
||||
}
|
||||
|
||||
@@ -1,61 +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;
|
||||
}
|
||||
}
|
||||
}
|
||||
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,28 +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);
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -1,155 +1,173 @@
|
||||
use crate::{
|
||||
components::{
|
||||
camera::{CameraAttachment, CameraMode, CameraPitch, FollowCam, MainCamera},
|
||||
player::PlayerDrag,
|
||||
tags::{Player, Ship},
|
||||
},
|
||||
plugins::{state_management::StateManagementPlugin, *},
|
||||
states::input::{InputState, PlayerState},
|
||||
};
|
||||
use bevy::{
|
||||
prelude::*,
|
||||
window::{CursorGrabMode, PrimaryWindow},
|
||||
};
|
||||
use bevy_rapier3d::prelude::*;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct GamePlugin;
|
||||
|
||||
impl Plugin for GamePlugin {
|
||||
fn build(&self, app: &mut bevy::app::App) {
|
||||
app.add_plugins((
|
||||
FollowCamPlugin,
|
||||
CameraPlugin,
|
||||
StateManagementPlugin,
|
||||
// ShipPlugin,
|
||||
TypesPlugin,
|
||||
PlayerPlugin,
|
||||
));
|
||||
app.add_plugins((
|
||||
RapierPhysicsPlugin::<NoUserData>::default(),
|
||||
#[cfg(feature = "dev-phys")]
|
||||
RapierDebugRenderPlugin::default(),
|
||||
));
|
||||
app.add_systems(Startup, (setup_scene, spawn_ship).chain());
|
||||
}
|
||||
}
|
||||
|
||||
fn setup_scene(
|
||||
mut commands: Commands,
|
||||
mut meshes: ResMut<Assets<Mesh>>,
|
||||
mut materials: ResMut<Assets<StandardMaterial>>,
|
||||
mut window: Single<&mut Window, With<PrimaryWindow>>,
|
||||
) {
|
||||
window.cursor_options.visible = false;
|
||||
window.cursor_options.grab_mode = CursorGrabMode::Locked;
|
||||
|
||||
let player_eye = commands
|
||||
.spawn((
|
||||
Name::new("Eye"),
|
||||
Transform::from_translation(Vec3::new(0.0, 1.0, 0.0)),
|
||||
CameraPitch::default(),
|
||||
))
|
||||
.id();
|
||||
let mut player_commands = commands.spawn((
|
||||
Name::new("Player"),
|
||||
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((
|
||||
Name::new("Camera"),
|
||||
Transform::from_xyz(0.0, 1.3, 0.0),
|
||||
Visibility::default(),
|
||||
MainCamera,
|
||||
Camera3d::default(),
|
||||
CameraMode::Player,
|
||||
CameraAttachment(player_eye),
|
||||
));
|
||||
|
||||
commands.spawn((
|
||||
DirectionalLight {
|
||||
shadows_enabled: true,
|
||||
..default()
|
||||
},
|
||||
Transform::default().looking_to(
|
||||
Dir3::from_xyz(-1.0, -1.0, -1.0).expect("Invaid Direction for light"),
|
||||
Dir3::Y,
|
||||
),
|
||||
));
|
||||
|
||||
let cube = meshes.add(Cuboid::new(100.0, 0.1, 100.0));
|
||||
let material = materials.add(Color::WHITE);
|
||||
commands.spawn((
|
||||
Mesh3d(cube),
|
||||
MeshMaterial3d(material),
|
||||
Transform::default(),
|
||||
RigidBody::Fixed,
|
||||
Collider::cuboid(100.0, 0.1, 100.0),
|
||||
));
|
||||
}
|
||||
|
||||
fn spawn_ship(
|
||||
mut commands: Commands,
|
||||
mut meshes: ResMut<Assets<Mesh>>,
|
||||
mut materials: ResMut<Assets<StandardMaterial>>,
|
||||
) {
|
||||
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,
|
||||
Velocity::zero(),
|
||||
Damping::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),
|
||||
)
|
||||
],
|
||||
));
|
||||
}
|
||||
use crate::{
|
||||
components::{
|
||||
camera::{CameraAttachment, CameraMode, CameraPitch, FollowCam, MainCamera},
|
||||
player::PlayerDrag,
|
||||
tags::{Player, Ship},
|
||||
},
|
||||
plugins::{state_management::StateManagementPlugin, *},
|
||||
states::{
|
||||
game::*,
|
||||
input::{InputState, PlayerState},
|
||||
loading::{AssetLoadingState, StartupLoadingState},
|
||||
menu::MenuState,
|
||||
},
|
||||
};
|
||||
use bevy::{
|
||||
prelude::*,
|
||||
window::{CursorGrabMode, PrimaryWindow},
|
||||
};
|
||||
use bevy_asset_loader::prelude::*;
|
||||
use bevy_rapier3d::prelude::*;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct GamePlugin;
|
||||
|
||||
impl Plugin for GamePlugin {
|
||||
fn build(&self, app: &mut bevy::app::App) {
|
||||
app.init_state::<AssetLoadingState>();
|
||||
app.init_state::<StartupLoadingState>();
|
||||
app.insert_state(GameState::Startup);
|
||||
app.init_state::<MenuState>();
|
||||
app.init_state::<PlayerState>();
|
||||
app.add_loading_state(
|
||||
LoadingState::new(AssetLoadingState::Loading).continue_to_state(AssetLoadingState::Finalizing),
|
||||
)
|
||||
.add_loading_state(
|
||||
LoadingState::new(StartupLoadingState::Loading).continue_to_state(StartupLoadingState::Finalizing),
|
||||
);
|
||||
|
||||
app.add_plugins((
|
||||
FollowCamPlugin,
|
||||
CameraPlugin,
|
||||
StateManagementPlugin,
|
||||
// ShipPlugin,
|
||||
TypesPlugin,
|
||||
PlayerPlugin,
|
||||
));
|
||||
app.add_plugins((
|
||||
RapierPhysicsPlugin::<NoUserData>::default(),
|
||||
#[cfg(feature = "dev-phys")]
|
||||
RapierDebugRenderPlugin::default(),
|
||||
));
|
||||
app.add_systems(Startup, (setup_scene, spawn_ship).chain());
|
||||
}
|
||||
}
|
||||
|
||||
fn setup_scene(
|
||||
mut commands: Commands,
|
||||
mut meshes: ResMut<Assets<Mesh>>,
|
||||
mut materials: ResMut<Assets<StandardMaterial>>,
|
||||
mut window: Single<&mut Window, With<PrimaryWindow>>,
|
||||
) {
|
||||
window.cursor_options.visible = false;
|
||||
window.cursor_options.grab_mode = CursorGrabMode::Locked;
|
||||
|
||||
let player_eye = commands
|
||||
.spawn((
|
||||
Name::new("Eye"),
|
||||
Transform::from_translation(Vec3::new(0.0, 1.0, 0.0)),
|
||||
CameraPitch::default(),
|
||||
))
|
||||
.id();
|
||||
let mut player_commands = commands.spawn((
|
||||
Name::new("Player"),
|
||||
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((
|
||||
Name::new("Camera"),
|
||||
Transform::from_xyz(0.0, 1.3, 0.0),
|
||||
Visibility::default(),
|
||||
MainCamera,
|
||||
Camera3d::default(),
|
||||
CameraMode::Player,
|
||||
CameraAttachment(player_eye),
|
||||
));
|
||||
|
||||
commands.spawn((
|
||||
DirectionalLight {
|
||||
shadows_enabled: true,
|
||||
..default()
|
||||
},
|
||||
Transform::default().looking_to(
|
||||
Dir3::from_xyz(-1.0, -1.0, -1.0).expect("Invaid Direction for light"),
|
||||
Dir3::Y,
|
||||
),
|
||||
));
|
||||
|
||||
let cube = meshes.add(Cuboid::new(100.0, 0.1, 100.0));
|
||||
let material = materials.add(Color::WHITE);
|
||||
commands.spawn((
|
||||
Mesh3d(cube),
|
||||
MeshMaterial3d(material),
|
||||
Transform::default(),
|
||||
RigidBody::Fixed,
|
||||
Collider::cuboid(100.0, 0.1, 100.0),
|
||||
));
|
||||
}
|
||||
|
||||
fn spawn_ship(
|
||||
mut commands: Commands,
|
||||
mut meshes: ResMut<Assets<Mesh>>,
|
||||
mut materials: ResMut<Assets<StandardMaterial>>,
|
||||
) {
|
||||
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,
|
||||
Velocity::zero(),
|
||||
Damping::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),
|
||||
)
|
||||
],
|
||||
));
|
||||
}
|
||||
|
||||
@@ -1,18 +1,18 @@
|
||||
// mod character_controller;
|
||||
mod camera;
|
||||
mod follow_cam;
|
||||
mod game;
|
||||
mod player;
|
||||
mod ship;
|
||||
mod ship_cam;
|
||||
mod state_management;
|
||||
mod types;
|
||||
|
||||
// pub use character_controller::*;
|
||||
pub use camera::*;
|
||||
pub use follow_cam::*;
|
||||
pub use game::*;
|
||||
pub use player::*;
|
||||
pub use ship::*;
|
||||
pub use ship_cam::*;
|
||||
pub use types::*;
|
||||
// mod character_controller;
|
||||
mod camera;
|
||||
mod follow_cam;
|
||||
mod game;
|
||||
mod player;
|
||||
mod ship;
|
||||
mod ship_cam;
|
||||
mod state_management;
|
||||
mod types;
|
||||
|
||||
// pub use character_controller::*;
|
||||
pub use camera::*;
|
||||
pub use follow_cam::*;
|
||||
pub use game::*;
|
||||
pub use player::*;
|
||||
pub use ship::*;
|
||||
pub use ship_cam::*;
|
||||
pub use types::*;
|
||||
|
||||
@@ -1,164 +1,164 @@
|
||||
use std::f32::{EPSILON, consts::FRAC_PI_2};
|
||||
|
||||
use bevy::{input::mouse::MouseMotion, prelude::*};
|
||||
use bevy_rapier3d::prelude::*;
|
||||
|
||||
use crate::{
|
||||
components::{
|
||||
camera::CameraPitch,
|
||||
player::{GravityDirection, JumpSpeed, MoveSpeed, PlayerDrag, PlayerForce, PlayerMotion, PlayerVelocity},
|
||||
tags::Player,
|
||||
},
|
||||
states::input::PlayerInputSystems,
|
||||
utils::{input::get_mouse_delta, rotation::get_alignment_rotation_preserve_twist},
|
||||
};
|
||||
|
||||
pub struct PlayerPlugin;
|
||||
|
||||
impl Plugin for PlayerPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
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, align_with_gravity);
|
||||
}
|
||||
}
|
||||
|
||||
fn apply_forces(player: Single<(&mut PlayerVelocity, &PlayerForce), With<Player>>, time: Res<Time>) {
|
||||
let (mut vel, force) = player.into_inner();
|
||||
|
||||
vel.0 += force.0 * time.delta_secs();
|
||||
}
|
||||
|
||||
fn apply_gravity(
|
||||
player: Single<
|
||||
(
|
||||
&mut PlayerVelocity,
|
||||
&GravityDirection,
|
||||
&GravityScale,
|
||||
Option<&KinematicCharacterControllerOutput>,
|
||||
),
|
||||
With<Player>,
|
||||
>,
|
||||
time: Res<Time>,
|
||||
) {
|
||||
let (mut vel, dir, scale, output) = player.into_inner();
|
||||
let Some(grav) = dir.0.map(|d| d.as_vec3()) else {
|
||||
return;
|
||||
};
|
||||
let is_grounded = output.map(|o| o.grounded).unwrap_or(false);
|
||||
if !is_grounded {
|
||||
vel.0 += grav * scale.0 * 9.47 * time.delta_secs();
|
||||
} else {
|
||||
let dot = vel.0.dot(grav);
|
||||
if dot > EPSILON {
|
||||
vel.0 -= dot * grav;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn apply_drag(player: Single<(&mut PlayerVelocity, &PlayerDrag), With<Player>>, time: Res<Time>) {
|
||||
let (mut vel, drag) = player.into_inner();
|
||||
vel.0 *= (1.0 - drag.0 * time.delta_secs()).max(0.0);
|
||||
}
|
||||
|
||||
fn apply_motion(
|
||||
player: Single<
|
||||
(
|
||||
&mut KinematicCharacterController,
|
||||
&Transform,
|
||||
&PlayerVelocity,
|
||||
&PlayerMotion,
|
||||
),
|
||||
With<Player>,
|
||||
>,
|
||||
time: Res<Time>,
|
||||
) {
|
||||
let (mut controller, transform, vel, motion) = player.into_inner();
|
||||
// let max_vel = vel.0.clamp_length_max(500.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());
|
||||
}
|
||||
|
||||
fn keyboard_input(
|
||||
key: Res<ButtonInput<KeyCode>>,
|
||||
player: Single<
|
||||
(
|
||||
&mut PlayerMotion,
|
||||
&mut PlayerVelocity,
|
||||
&MoveSpeed,
|
||||
&JumpSpeed,
|
||||
&Transform,
|
||||
Option<&KinematicCharacterControllerOutput>,
|
||||
),
|
||||
With<Player>,
|
||||
>,
|
||||
) {
|
||||
let (mut motion, mut vel, speed, jump, transform, output) = player.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;
|
||||
}
|
||||
|
||||
move_vec = move_vec.normalize_or_zero() * speed.0;
|
||||
|
||||
let is_grounded = output.map(|o| o.grounded).unwrap_or(false);
|
||||
if key.just_pressed(KeyCode::Space) && is_grounded {
|
||||
vel.0 += transform.up() * jump.0;
|
||||
}
|
||||
|
||||
motion.0 = move_vec;
|
||||
|
||||
// transform.rotate_z(angle * time.delta_secs());
|
||||
// controller.up = transform.up().as_vec3();
|
||||
// move_vec += transform.down().as_vec3();
|
||||
// controller.translation = Some(move_vec * time.delta_secs());
|
||||
}
|
||||
|
||||
fn player_look(
|
||||
mut player: Single<&mut Transform, With<Player>>,
|
||||
mut cam_pitch: Single<&mut CameraPitch>,
|
||||
mouse_motion: EventReader<MouseMotion>,
|
||||
time: Res<Time>,
|
||||
) {
|
||||
let delta = get_mouse_delta(mouse_motion) * -time.delta_secs();
|
||||
let up = player.up();
|
||||
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(
|
||||
player: Single<
|
||||
(
|
||||
&mut Transform,
|
||||
&GravityDirection,
|
||||
Option<&KinematicCharacterControllerOutput>,
|
||||
),
|
||||
With<Player>,
|
||||
>,
|
||||
time: Res<Time>,
|
||||
) {
|
||||
let (mut transform, grav, output) = player.into_inner();
|
||||
let is_grounded = output.map(|o| o.grounded).unwrap_or(false);
|
||||
|
||||
if !is_grounded {
|
||||
return;
|
||||
}
|
||||
|
||||
let Some(grav_dir) = grav.0 else {
|
||||
return;
|
||||
};
|
||||
|
||||
let grav_up = grav_dir * -1.0;
|
||||
|
||||
let desired_rotation = get_alignment_rotation_preserve_twist(transform.rotation, grav_up);
|
||||
transform.rotation = transform.rotation.lerp(desired_rotation, time.delta_secs());
|
||||
}
|
||||
use std::f32::{EPSILON, consts::FRAC_PI_2};
|
||||
|
||||
use bevy::{input::mouse::MouseMotion, prelude::*};
|
||||
use bevy_rapier3d::prelude::*;
|
||||
|
||||
use crate::{
|
||||
components::{
|
||||
camera::CameraPitch,
|
||||
player::{GravityDirection, JumpSpeed, MoveSpeed, PlayerDrag, PlayerForce, PlayerMotion, PlayerVelocity},
|
||||
tags::Player,
|
||||
},
|
||||
states::input::PlayerInputSystems,
|
||||
utils::{input::get_mouse_delta, rotation::get_alignment_rotation_preserve_twist},
|
||||
};
|
||||
|
||||
pub struct PlayerPlugin;
|
||||
|
||||
impl Plugin for PlayerPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
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, align_with_gravity);
|
||||
}
|
||||
}
|
||||
|
||||
fn apply_forces(player: Single<(&mut PlayerVelocity, &PlayerForce), With<Player>>, time: Res<Time>) {
|
||||
let (mut vel, force) = player.into_inner();
|
||||
|
||||
vel.0 += force.0 * time.delta_secs();
|
||||
}
|
||||
|
||||
fn apply_gravity(
|
||||
player: Single<
|
||||
(
|
||||
&mut PlayerVelocity,
|
||||
&GravityDirection,
|
||||
&GravityScale,
|
||||
Option<&KinematicCharacterControllerOutput>,
|
||||
),
|
||||
With<Player>,
|
||||
>,
|
||||
time: Res<Time>,
|
||||
) {
|
||||
let (mut vel, dir, scale, output) = player.into_inner();
|
||||
let Some(grav) = dir.0.map(|d| d.as_vec3()) else {
|
||||
return;
|
||||
};
|
||||
let is_grounded = output.map(|o| o.grounded).unwrap_or(false);
|
||||
if !is_grounded {
|
||||
vel.0 += grav * scale.0 * 9.47 * time.delta_secs();
|
||||
} else {
|
||||
let dot = vel.0.dot(grav);
|
||||
if dot > EPSILON {
|
||||
vel.0 -= dot * grav;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn apply_drag(player: Single<(&mut PlayerVelocity, &PlayerDrag), With<Player>>, time: Res<Time>) {
|
||||
let (mut vel, drag) = player.into_inner();
|
||||
vel.0 *= (1.0 - drag.0 * time.delta_secs()).max(0.0);
|
||||
}
|
||||
|
||||
fn apply_motion(
|
||||
player: Single<
|
||||
(
|
||||
&mut KinematicCharacterController,
|
||||
&Transform,
|
||||
&PlayerVelocity,
|
||||
&PlayerMotion,
|
||||
),
|
||||
With<Player>,
|
||||
>,
|
||||
time: Res<Time>,
|
||||
) {
|
||||
let (mut controller, transform, vel, motion) = player.into_inner();
|
||||
// let max_vel = vel.0.clamp_length_max(500.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());
|
||||
}
|
||||
|
||||
fn keyboard_input(
|
||||
key: Res<ButtonInput<KeyCode>>,
|
||||
player: Single<
|
||||
(
|
||||
&mut PlayerMotion,
|
||||
&mut PlayerVelocity,
|
||||
&MoveSpeed,
|
||||
&JumpSpeed,
|
||||
&Transform,
|
||||
Option<&KinematicCharacterControllerOutput>,
|
||||
),
|
||||
With<Player>,
|
||||
>,
|
||||
) {
|
||||
let (mut motion, mut vel, speed, jump, transform, output) = player.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;
|
||||
}
|
||||
|
||||
move_vec = move_vec.normalize_or_zero() * speed.0;
|
||||
|
||||
let is_grounded = output.map(|o| o.grounded).unwrap_or(false);
|
||||
if key.just_pressed(KeyCode::Space) && is_grounded {
|
||||
vel.0 += transform.up() * jump.0;
|
||||
}
|
||||
|
||||
motion.0 = move_vec;
|
||||
|
||||
// transform.rotate_z(angle * time.delta_secs());
|
||||
// controller.up = transform.up().as_vec3();
|
||||
// move_vec += transform.down().as_vec3();
|
||||
// controller.translation = Some(move_vec * time.delta_secs());
|
||||
}
|
||||
|
||||
fn player_look(
|
||||
mut player: Single<&mut Transform, With<Player>>,
|
||||
mut cam_pitch: Single<&mut CameraPitch>,
|
||||
mouse_motion: EventReader<MouseMotion>,
|
||||
time: Res<Time>,
|
||||
) {
|
||||
let delta = get_mouse_delta(mouse_motion) * -time.delta_secs();
|
||||
let up = player.up();
|
||||
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(
|
||||
player: Single<
|
||||
(
|
||||
&mut Transform,
|
||||
&GravityDirection,
|
||||
Option<&KinematicCharacterControllerOutput>,
|
||||
),
|
||||
With<Player>,
|
||||
>,
|
||||
time: Res<Time>,
|
||||
) {
|
||||
let (mut transform, grav, output) = player.into_inner();
|
||||
let is_grounded = output.map(|o| o.grounded).unwrap_or(false);
|
||||
|
||||
if !is_grounded {
|
||||
return;
|
||||
}
|
||||
|
||||
let Some(grav_dir) = grav.0 else {
|
||||
return;
|
||||
};
|
||||
|
||||
let grav_up = grav_dir * -1.0;
|
||||
|
||||
let desired_rotation = get_alignment_rotation_preserve_twist(transform.rotation, grav_up);
|
||||
transform.rotation = transform.rotation.lerp(desired_rotation, time.delta_secs());
|
||||
}
|
||||
|
||||
@@ -1,103 +1,103 @@
|
||||
use bevy::{input::mouse::MouseMotion, prelude::*};
|
||||
use bevy_rapier3d::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 Velocity, &mut Damping), With<Ship>>,
|
||||
key: Res<ButtonInput<KeyCode>>,
|
||||
time: Res<Time>,
|
||||
mouse_motion: EventReader<MouseMotion>,
|
||||
) {
|
||||
let (transform, mut vel, mut damp) = 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) {
|
||||
damp.linear_damping = 0.8;
|
||||
damp.angular_damping = 0.8;
|
||||
} else {
|
||||
damp.linear_damping = 0.0;
|
||||
damp.angular_damping = 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;
|
||||
|
||||
vel.angvel += (transform.rotation * (roll + yaw + pitch)) * time.delta_secs();
|
||||
|
||||
move_vec = transform.rotation * move_vec.normalize_or_zero();
|
||||
|
||||
vel.linvel += 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
use bevy::{input::mouse::MouseMotion, prelude::*};
|
||||
use bevy_rapier3d::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 Velocity, &mut Damping), With<Ship>>,
|
||||
key: Res<ButtonInput<KeyCode>>,
|
||||
time: Res<Time>,
|
||||
mouse_motion: EventReader<MouseMotion>,
|
||||
) {
|
||||
let (transform, mut vel, mut damp) = 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) {
|
||||
damp.linear_damping = 0.8;
|
||||
damp.angular_damping = 0.8;
|
||||
} else {
|
||||
damp.linear_damping = 0.0;
|
||||
damp.angular_damping = 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;
|
||||
|
||||
vel.angvel += (transform.rotation * (roll + yaw + pitch)) * time.delta_secs();
|
||||
|
||||
move_vec = transform.rotation * move_vec.normalize_or_zero();
|
||||
|
||||
vel.linvel += 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
use bevy::prelude::*;
|
||||
|
||||
pub struct ShipCamPlugin;
|
||||
|
||||
impl Plugin for ShipCamPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
use bevy::prelude::*;
|
||||
|
||||
pub struct ShipCamPlugin;
|
||||
|
||||
impl Plugin for ShipCamPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,16 +1,41 @@
|
||||
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)));
|
||||
}
|
||||
}
|
||||
use bevy::prelude::*;
|
||||
|
||||
use crate::{
|
||||
configure_sets,
|
||||
states::{
|
||||
input::{InputState, PlayerInputSystems, PlayerState},
|
||||
menu::{
|
||||
MainMenuSystems, MenuCleanupSystems, MenuStartupSystems, MenuState, OptionsMenuSystems, SavesMenuSystems,
|
||||
},
|
||||
play::{PausedSystems, PlayCleanupSystems, PlayStartupSystems, PlayState, PlaySystems},
|
||||
},
|
||||
};
|
||||
|
||||
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 crate::components::{
|
||||
camera::{CameraAttachment, CameraMode, CameraPitch, FollowCam, FollowTarget},
|
||||
player::{GravityDirection, MoveSpeed, PlayerForce, PlayerMotion, PlayerVelocity},
|
||||
};
|
||||
|
||||
pub struct TypesPlugin;
|
||||
|
||||
impl Plugin for TypesPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.register_type::<FollowTarget>();
|
||||
app.register_type::<FollowCam>();
|
||||
app.register_type::<PlayerVelocity>();
|
||||
app.register_type::<PlayerForce>();
|
||||
app.register_type::<PlayerMotion>();
|
||||
app.register_type::<GravityDirection>();
|
||||
app.register_type::<MoveSpeed>();
|
||||
app.register_type::<CameraPitch>();
|
||||
app.register_type::<CameraAttachment>();
|
||||
app.register_type::<CameraMode>();
|
||||
}
|
||||
}
|
||||
use bevy::prelude::*;
|
||||
|
||||
use crate::components::{
|
||||
camera::{CameraAttachment, CameraMode, CameraPitch, FollowCam, FollowTarget},
|
||||
player::{GravityDirection, MoveSpeed, PlayerForce, PlayerMotion, PlayerVelocity},
|
||||
};
|
||||
|
||||
pub struct TypesPlugin;
|
||||
|
||||
impl Plugin for TypesPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.register_type::<FollowTarget>();
|
||||
app.register_type::<FollowCam>();
|
||||
app.register_type::<PlayerVelocity>();
|
||||
app.register_type::<PlayerForce>();
|
||||
app.register_type::<PlayerMotion>();
|
||||
app.register_type::<GravityDirection>();
|
||||
app.register_type::<MoveSpeed>();
|
||||
app.register_type::<CameraPitch>();
|
||||
app.register_type::<CameraAttachment>();
|
||||
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::*;
|
||||
|
||||
#[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,
|
||||
}
|
||||
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,
|
||||
}
|
||||
|
||||
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::*};
|
||||
|
||||
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;
|
||||
}
|
||||
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,2 +1,2 @@
|
||||
pub mod input;
|
||||
pub mod rotation;
|
||||
pub mod input;
|
||||
pub mod rotation;
|
||||
|
||||
@@ -1,38 +1,38 @@
|
||||
use std::f32::{EPSILON, consts::PI};
|
||||
|
||||
use bevy::prelude::*;
|
||||
|
||||
pub fn get_alignment_rotation(cur_dir: Dir3, target_dir: Vec3) -> Quat {
|
||||
let tgt = target_dir.normalize();
|
||||
let axis = cur_dir.cross(tgt);
|
||||
let axis_len = axis.length();
|
||||
if axis_len < EPSILON {
|
||||
let dot = cur_dir.dot(tgt);
|
||||
if dot > 0.999 {
|
||||
return Quat::IDENTITY;
|
||||
} else {
|
||||
let ortho = if cur_dir.x < 0.99 {
|
||||
cur_dir.cross(Vec3::X)
|
||||
} else {
|
||||
cur_dir.cross(Vec3::Z)
|
||||
};
|
||||
return Quat::from_axis_angle(ortho.normalize(), PI);
|
||||
}
|
||||
} else {
|
||||
let angle = cur_dir.angle_between(tgt);
|
||||
return Quat::from_axis_angle(axis.normalize(), angle);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_alignment_rotation_preserve_twist(cur_rot: Quat, target_up: Vec3) -> Quat {
|
||||
let target_up = target_up.normalize();
|
||||
|
||||
let cur_fwd = cur_rot * Vec3::Z;
|
||||
|
||||
let forward_projection = (cur_fwd - target_up * cur_fwd.dot(target_up)).normalize();
|
||||
let right = target_up.cross(forward_projection).normalize();
|
||||
let fwd = right.cross(target_up);
|
||||
let basis = Mat3::from_cols(right, target_up, fwd);
|
||||
|
||||
return Quat::from_mat3(&basis);
|
||||
}
|
||||
use std::f32::{EPSILON, consts::PI};
|
||||
|
||||
use bevy::prelude::*;
|
||||
|
||||
pub fn get_alignment_rotation(cur_dir: Dir3, target_dir: Vec3) -> Quat {
|
||||
let tgt = target_dir.normalize();
|
||||
let axis = cur_dir.cross(tgt);
|
||||
let axis_len = axis.length();
|
||||
if axis_len < EPSILON {
|
||||
let dot = cur_dir.dot(tgt);
|
||||
if dot > 0.999 {
|
||||
return Quat::IDENTITY;
|
||||
} else {
|
||||
let ortho = if cur_dir.x < 0.99 {
|
||||
cur_dir.cross(Vec3::X)
|
||||
} else {
|
||||
cur_dir.cross(Vec3::Z)
|
||||
};
|
||||
return Quat::from_axis_angle(ortho.normalize(), PI);
|
||||
}
|
||||
} else {
|
||||
let angle = cur_dir.angle_between(tgt);
|
||||
return Quat::from_axis_angle(axis.normalize(), angle);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_alignment_rotation_preserve_twist(cur_rot: Quat, target_up: Vec3) -> Quat {
|
||||
let target_up = target_up.normalize();
|
||||
|
||||
let cur_fwd = cur_rot * Vec3::Z;
|
||||
|
||||
let forward_projection = (cur_fwd - target_up * cur_fwd.dot(target_up)).normalize();
|
||||
let right = target_up.cross(forward_projection).normalize();
|
||||
let fwd = right.cross(target_up);
|
||||
let basis = Mat3::from_cols(right, target_up, fwd);
|
||||
|
||||
return Quat::from_mat3(&basis);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user