WIP prefabs loader
This commit is contained in:
17
game/main/.zed/debug.json
Normal file
17
game/main/.zed/debug.json
Normal file
@@ -0,0 +1,17 @@
|
||||
// Project-local debug tasks
|
||||
//
|
||||
// For more documentation on how to configure debug tasks,
|
||||
// see: https://zed.dev/docs/debugger
|
||||
[
|
||||
{
|
||||
"label": "Debug Run",
|
||||
"build": {
|
||||
"command": "cargo",
|
||||
"args": ["build"]
|
||||
},
|
||||
"program": "$ZED_WORKTREE_ROOT/target/debug/space-game.exe",
|
||||
"cwd": "$ZED_WORKTREE_ROOT",
|
||||
"request": "launch",
|
||||
"adapter": "GDB"
|
||||
}
|
||||
]
|
||||
6408
game/main/Cargo.lock
generated
Normal file
6408
game/main/Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
59
game/main/Cargo.toml
Normal file
59
game/main/Cargo.toml
Normal file
@@ -0,0 +1,59 @@
|
||||
[package]
|
||||
name = "space-game"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
# avian3d = { version = "0.3.0", features = [] }
|
||||
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"
|
||||
prefabs = { path = "../../engine/prefabs" }
|
||||
|
||||
[features]
|
||||
default = ["dev"]
|
||||
dev = [
|
||||
"bevy/bevy_dev_tools",
|
||||
"bevy/bevy_ui_debug",
|
||||
"bevy/track_location",
|
||||
"bevy/file_watcher",
|
||||
"bevy/embedded_watcher",
|
||||
"dev-viz"
|
||||
]
|
||||
dev-viz = []
|
||||
dev-phys = []
|
||||
|
||||
[lints.clippy]
|
||||
# Bevy supplies arguments to systems via dependency injection, so it's natural for systems to
|
||||
# request more than 7 arguments, which would undesirably trigger this lint.
|
||||
too_many_arguments = "allow"
|
||||
# Queries may access many components, which would undesirably trigger this lint.
|
||||
type_complexity = "allow"
|
||||
# Make sure macros use their standard braces, such as `[]` for `bevy_ecs::children!`.
|
||||
nonstandard_macro_braces = "warn"
|
||||
needless_return = "allow"
|
||||
|
||||
# Enable a small amount of optimization in debug mode
|
||||
[profile.dev]
|
||||
opt-level = 1
|
||||
|
||||
# Enable high optimizations for dependencies (incl. Bevy), but not for our code:
|
||||
[profile.dev.package."*"]
|
||||
opt-level = 3
|
||||
|
||||
[profile.release]
|
||||
codegen-units = 1
|
||||
lto = "thin"
|
||||
|
||||
|
||||
# Optimize for build time in CI.
|
||||
[profile.ci]
|
||||
inherits = "dev"
|
||||
opt-level = 0
|
||||
debug = "line-tables-only"
|
||||
codegen-units = 4
|
||||
|
||||
[profile.ci.package."*"]
|
||||
opt-level = 0
|
||||
BIN
game/main/assets/models/Ship.glb
Normal file
BIN
game/main/assets/models/Ship.glb
Normal file
Binary file not shown.
BIN
game/main/raw_assets/Models/Ship.blend
Normal file
BIN
game/main/raw_assets/Models/Ship.blend
Normal file
Binary file not shown.
50
game/main/src/components/camera.rs
Normal file
50
game/main/src/components/camera.rs
Normal file
@@ -0,0 +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);
|
||||
4
game/main/src/components/mod.rs
Normal file
4
game/main/src/components/mod.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
pub mod camera;
|
||||
// pub mod character_controller;
|
||||
pub mod player;
|
||||
pub mod tags;
|
||||
49
game/main/src/components/player.rs
Normal file
49
game/main/src/components/player.rs
Normal file
@@ -0,0 +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);
|
||||
10
game/main/src/components/tags.rs
Normal file
10
game/main/src/components/tags.rs
Normal file
@@ -0,0 +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;
|
||||
15
game/main/src/macros.rs
Normal file
15
game/main/src/macros.rs
Normal file
@@ -0,0 +1,15 @@
|
||||
#[macro_export]
|
||||
macro_rules! configure_sets {
|
||||
(
|
||||
$app: expr,
|
||||
$condition: expr
|
||||
) => {
|
||||
$app.configure_sets(PreUpdate, $condition)
|
||||
.configure_sets(Update, $condition)
|
||||
.configure_sets(PostUpdate, $condition)
|
||||
.configure_sets(Last, $condition);
|
||||
$app.configure_sets(FixedPreUpdate, $condition)
|
||||
.configure_sets(FixedUpdate, $condition)
|
||||
.configure_sets(FixedPostUpdate, $condition);
|
||||
};
|
||||
}
|
||||
40
game/main/src/main.rs
Normal file
40
game/main/src/main.rs
Normal file
@@ -0,0 +1,40 @@
|
||||
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();
|
||||
}
|
||||
66
game/main/src/plugins/camera.rs
Normal file
66
game/main/src/plugins/camera.rs
Normal file
@@ -0,0 +1,66 @@
|
||||
use bevy::prelude::*;
|
||||
#[cfg(feature = "dev")]
|
||||
use bevy::window::PrimaryWindow;
|
||||
use bevy_rapier3d::plugin::PhysicsSet;
|
||||
|
||||
use crate::{components::camera::*, states::play::PlaySystems};
|
||||
|
||||
pub struct CameraPlugin;
|
||||
|
||||
impl Plugin for CameraPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.add_systems(Update, camera_pitch.in_set(PlaySystems));
|
||||
app.add_systems(
|
||||
PostUpdate,
|
||||
//Update after physics moves entities
|
||||
camera_attachment.in_set(PlaySystems).after(PhysicsSet::Writeback),
|
||||
);
|
||||
#[cfg(feature = "dev")]
|
||||
app.add_systems(Update, camera_toggle.in_set(PlaySystems));
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
31
game/main/src/plugins/follow_cam.rs
Normal file
31
game/main/src/plugins/follow_cam.rs
Normal file
@@ -0,0 +1,31 @@
|
||||
use bevy::prelude::*;
|
||||
|
||||
use crate::{
|
||||
components::camera::{FollowCam, FollowTarget},
|
||||
states::play::PlaySystems,
|
||||
};
|
||||
|
||||
pub struct FollowCamPlugin;
|
||||
|
||||
impl Plugin for FollowCamPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.add_systems(Update, (set_cam_target, follow_cam).chain().in_set(PlaySystems));
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
163
game/main/src/plugins/game.rs
Normal file
163
game/main/src/plugins/game.rs
Normal file
@@ -0,0 +1,163 @@
|
||||
use crate::{
|
||||
components::{
|
||||
camera::{CameraAttachment, CameraMode, CameraPitch, MainCamera},
|
||||
player::PlayerDrag,
|
||||
tags::{Player, Ship},
|
||||
},
|
||||
plugins::{state_management::StateManagementPlugin, *},
|
||||
states::play::PlayStartupSystems,
|
||||
};
|
||||
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,
|
||||
MainMenuPlugin,
|
||||
ShipPlugin,
|
||||
TypesPlugin,
|
||||
PlayerPlugin,
|
||||
));
|
||||
app.add_plugins((
|
||||
RapierPhysicsPlugin::<NoUserData>::default(),
|
||||
#[cfg(feature = "dev-phys")]
|
||||
RapierDebugRenderPlugin::default(),
|
||||
));
|
||||
app.add_systems(Update, (setup_scene).chain().in_set(PlayStartupSystems));
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
autostep: Some(CharacterAutostep {
|
||||
include_dynamic_bodies: true,
|
||||
max_height: CharacterLength::Absolute(0.25),
|
||||
..default()
|
||||
}),
|
||||
..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),
|
||||
)
|
||||
],
|
||||
));
|
||||
}
|
||||
38
game/main/src/plugins/main_menu.rs
Normal file
38
game/main/src/plugins/main_menu.rs
Normal file
@@ -0,0 +1,38 @@
|
||||
use bevy::prelude::*;
|
||||
|
||||
use crate::states::{
|
||||
game::GameState,
|
||||
menu::{MainMenuSystems, MenuCleanupSystems, MenuStartupSystems, MenuState},
|
||||
play::PlayState,
|
||||
};
|
||||
|
||||
pub struct MainMenuPlugin;
|
||||
|
||||
impl Plugin for MainMenuPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.add_systems(Update, spawn_menu.in_set(MenuStartupSystems));
|
||||
app.add_systems(Update, clean_menu.in_set(MenuCleanupSystems));
|
||||
app.add_systems(Update, main_menu.in_set(MainMenuSystems));
|
||||
}
|
||||
}
|
||||
|
||||
fn spawn_menu(mut next: ResMut<NextState<MenuState>>) {
|
||||
next.set(MenuState::Main);
|
||||
info_once!("Moving to MenuState:{:?}", MenuState::Main);
|
||||
}
|
||||
|
||||
fn clean_menu(mut next: ResMut<NextState<MenuState>>) {
|
||||
next.set(MenuState::Idle);
|
||||
info_once!("Moving to MenuState:{:?}", MenuState::Loading);
|
||||
}
|
||||
|
||||
fn main_menu(
|
||||
mut next_menu: ResMut<NextState<MenuState>>,
|
||||
mut next_game: ResMut<NextState<GameState>>,
|
||||
mut next_play_state: ResMut<NextState<PlayState>>,
|
||||
) {
|
||||
next_menu.set(MenuState::Cleanup);
|
||||
next_game.set(GameState::InGame);
|
||||
next_play_state.set(PlayState::Loading);
|
||||
info_once!("Moving to InGame");
|
||||
}
|
||||
22
game/main/src/plugins/mod.rs
Normal file
22
game/main/src/plugins/mod.rs
Normal file
@@ -0,0 +1,22 @@
|
||||
// mod character_controller;
|
||||
mod camera;
|
||||
mod follow_cam;
|
||||
mod game;
|
||||
mod main_menu;
|
||||
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 main_menu::*;
|
||||
pub use player::*;
|
||||
#[allow(unused_imports)]
|
||||
pub use ship::*;
|
||||
#[allow(unused_imports)]
|
||||
pub use ship_cam::*;
|
||||
pub use types::*;
|
||||
169
game/main/src/plugins/player.rs
Normal file
169
game/main/src/plugins/player.rs
Normal file
@@ -0,0 +1,169 @@
|
||||
use std::f32::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::InputWorldSystems, play::PlaySystems},
|
||||
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(InputWorldSystems));
|
||||
app.add_systems(
|
||||
Update,
|
||||
(apply_gravity, apply_forces, apply_motion, apply_drag)
|
||||
.chain()
|
||||
.in_set(PlaySystems),
|
||||
);
|
||||
app.add_systems(Update, align_with_gravity.in_set(PlaySystems));
|
||||
}
|
||||
}
|
||||
|
||||
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 > f32::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());
|
||||
}
|
||||
135
game/main/src/plugins/ship.rs
Normal file
135
game/main/src/plugins/ship.rs
Normal file
@@ -0,0 +1,135 @@
|
||||
use bevy::{input::mouse::MouseMotion, prelude::*};
|
||||
use bevy_rapier3d::prelude::*;
|
||||
|
||||
use crate::{
|
||||
components::tags::Ship,
|
||||
states::play::{PlayStartupSystems, PlaySystems},
|
||||
utils::input::get_mouse_delta,
|
||||
};
|
||||
|
||||
pub struct ShipPlugin;
|
||||
|
||||
impl Plugin for ShipPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.add_systems(Update, spawn_ship.in_set(PlayStartupSystems));
|
||||
|
||||
app.add_systems(Update, ship_controls.in_set(PlaySystems));
|
||||
#[cfg(feature = "dev-viz")]
|
||||
app.add_systems(Update, ship_debug.in_set(PlaySystems));
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Resource, Debug, Reflect)]
|
||||
struct ShipMesh(Handle<Scene>);
|
||||
|
||||
fn spawn_ship(mut commads: Commands, assets: Res<AssetServer>) {
|
||||
let scene = assets.load(GltfAssetLabel::Scene(0).from_asset("models/Ship.glb"));
|
||||
|
||||
commads.insert_resource(ShipMesh(scene.clone()));
|
||||
|
||||
commads.spawn((
|
||||
SceneRoot(scene),
|
||||
Transform::from_xyz(0.0, 3.0, 0.0),
|
||||
RigidBody::KinematicVelocityBased,
|
||||
GravityScale(0.0),
|
||||
children![
|
||||
(
|
||||
Transform::from_xyz(-2.0, -2.7, 0.0),
|
||||
Collider::round_cuboid(6.0, 0.1, 3.0, 0.05)
|
||||
),
|
||||
(
|
||||
Transform::from_xyz(-2.0, 2.8, 0.0),
|
||||
Collider::round_cuboid(6.0, 0.1, 3.0, 0.05)
|
||||
)
|
||||
],
|
||||
));
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
9
game/main/src/plugins/ship_cam.rs
Normal file
9
game/main/src/plugins/ship_cam.rs
Normal file
@@ -0,0 +1,9 @@
|
||||
use bevy::prelude::*;
|
||||
|
||||
pub struct ShipCamPlugin;
|
||||
|
||||
impl Plugin for ShipCamPlugin {
|
||||
fn build(&self, _app: &mut App) {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
143
game/main/src/plugins/state_management.rs
Normal file
143
game/main/src/plugins/state_management.rs
Normal file
@@ -0,0 +1,143 @@
|
||||
use bevy::prelude::*;
|
||||
use bevy_asset_loader::prelude::*;
|
||||
|
||||
use crate::{
|
||||
configure_sets,
|
||||
states::{
|
||||
game::{GameState, InGameSystems, MenuSystems},
|
||||
input::{
|
||||
InputDetachedSystems, InputMenuSystems, InputState, InputWorldSystems, PlayerFreeFloatingSystems,
|
||||
PlayerOnFootSystems, PlayerPilotingSystems, PlayerState,
|
||||
},
|
||||
menu::{
|
||||
MainMenuSystems, MenuCleanupSystems, MenuStartupSystems, MenuState, OptionsMenuSystems, SavesMenuSystems,
|
||||
},
|
||||
play::{
|
||||
PausedSystems, PlayAssetFinalizeSystems, 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>();
|
||||
app.init_state::<GameState>();
|
||||
app.init_state::<MenuState>();
|
||||
app.init_state::<PlayState>();
|
||||
|
||||
//Loading
|
||||
app.add_loading_state(LoadingState::new(MenuState::Loading).continue_to_state(MenuState::Startup))
|
||||
.add_loading_state(LoadingState::new(PlayState::Loading).continue_to_state(PlayState::AssetFinalize));
|
||||
|
||||
//Game
|
||||
configure_sets!(app, MenuSystems.run_if(in_state(GameState::MainMenu)));
|
||||
configure_sets!(app, InGameSystems.run_if(in_state(GameState::InGame)));
|
||||
|
||||
//Menu
|
||||
configure_sets!(
|
||||
app,
|
||||
MenuStartupSystems
|
||||
.run_if(in_state(MenuState::Startup))
|
||||
.in_set(MenuSystems)
|
||||
);
|
||||
configure_sets!(
|
||||
app,
|
||||
MainMenuSystems.run_if(in_state(MenuState::Main)).in_set(MenuSystems)
|
||||
);
|
||||
configure_sets!(
|
||||
app,
|
||||
OptionsMenuSystems
|
||||
.run_if(in_state(MenuState::Options))
|
||||
.in_set(MenuSystems)
|
||||
);
|
||||
configure_sets!(
|
||||
app,
|
||||
SavesMenuSystems.run_if(in_state(MenuState::Saves)).in_set(MenuSystems)
|
||||
);
|
||||
configure_sets!(app, MenuCleanupSystems.run_if(in_state(MenuState::Cleanup)));
|
||||
|
||||
//Play
|
||||
configure_sets!(
|
||||
app,
|
||||
PlayAssetFinalizeSystems
|
||||
.run_if(in_state(PlayState::AssetFinalize))
|
||||
.in_set(InGameSystems)
|
||||
);
|
||||
configure_sets!(
|
||||
app,
|
||||
PlayStartupSystems
|
||||
.run_if(in_state(PlayState::Startup))
|
||||
.in_set(InGameSystems)
|
||||
);
|
||||
configure_sets!(
|
||||
app,
|
||||
PausedSystems.run_if(in_state(PlayState::Paused)).in_set(InGameSystems)
|
||||
);
|
||||
configure_sets!(
|
||||
app,
|
||||
PlaySystems.run_if(in_state(PlayState::Playing)).in_set(InGameSystems)
|
||||
);
|
||||
configure_sets!(app, PlayCleanupSystems.run_if(in_state(PlayState::Cleanup)));
|
||||
|
||||
//Input
|
||||
configure_sets!(
|
||||
app,
|
||||
InputWorldSystems
|
||||
.run_if(in_state(InputState::World))
|
||||
.in_set(PlaySystems)
|
||||
);
|
||||
configure_sets!(
|
||||
app,
|
||||
InputMenuSystems.run_if(in_state(InputState::Menu)).in_set(PlaySystems)
|
||||
);
|
||||
configure_sets!(
|
||||
app,
|
||||
InputDetachedSystems
|
||||
.run_if(in_state(InputState::Detached))
|
||||
.in_set(PlaySystems)
|
||||
);
|
||||
|
||||
//Player
|
||||
configure_sets!(
|
||||
app,
|
||||
PlayerOnFootSystems
|
||||
.run_if(in_state(PlayerState::OnFoot))
|
||||
.in_set(PlaySystems)
|
||||
);
|
||||
configure_sets!(
|
||||
app,
|
||||
PlayerFreeFloatingSystems
|
||||
.run_if(in_state(PlayerState::FreeFloating))
|
||||
.in_set(PlaySystems)
|
||||
);
|
||||
configure_sets!(
|
||||
app,
|
||||
PlayerPilotingSystems
|
||||
.run_if(in_state(PlayerState::Piloting))
|
||||
.in_set(PlaySystems)
|
||||
);
|
||||
|
||||
//State Transitions
|
||||
app.add_systems(Update, game_asset_finalize.in_set(PlayAssetFinalizeSystems));
|
||||
app.add_systems(Update, game_startup.in_set(PlayStartupSystems));
|
||||
app.add_systems(Update, game_cleanup.in_set(PlayCleanupSystems));
|
||||
}
|
||||
}
|
||||
|
||||
fn game_asset_finalize(mut next: ResMut<NextState<PlayState>>) {
|
||||
next.set(PlayState::Startup);
|
||||
info_once!("Moving to PlayState:{:?}", PlayState::Startup);
|
||||
}
|
||||
|
||||
fn game_startup(mut next: ResMut<NextState<PlayState>>) {
|
||||
next.set(PlayState::Playing);
|
||||
info_once!("Moving to PlayState:{:?}", PlayState::Playing);
|
||||
}
|
||||
|
||||
fn game_cleanup(mut next: ResMut<NextState<PlayState>>) {
|
||||
next.set(PlayState::Idle);
|
||||
info_once!("Moving to PlayState:{:?}", PlayState::Idle);
|
||||
}
|
||||
23
game/main/src/plugins/types.rs
Normal file
23
game/main/src/plugins/types.rs
Normal file
@@ -0,0 +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>();
|
||||
}
|
||||
}
|
||||
0
game/main/src/resources/mod.rs
Normal file
0
game/main/src/resources/mod.rs
Normal file
14
game/main/src/states/game.rs
Normal file
14
game/main/src/states/game.rs
Normal file
@@ -0,0 +1,14 @@
|
||||
use bevy::prelude::*;
|
||||
|
||||
#[derive(States, Debug, Reflect, Default, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub enum GameState {
|
||||
#[default]
|
||||
MainMenu,
|
||||
InGame,
|
||||
}
|
||||
|
||||
#[derive(SystemSet, Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub struct MenuSystems;
|
||||
|
||||
#[derive(SystemSet, Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub struct InGameSystems;
|
||||
35
game/main/src/states/input.rs
Normal file
35
game/main/src/states/input.rs
Normal file
@@ -0,0 +1,35 @@
|
||||
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 InputWorldSystems;
|
||||
|
||||
#[derive(SystemSet, Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub struct InputMenuSystems;
|
||||
|
||||
#[derive(SystemSet, Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub struct InputDetachedSystems;
|
||||
|
||||
#[derive(States, Debug, Default, Clone, PartialEq, Eq, Hash)]
|
||||
pub enum PlayerState {
|
||||
#[default]
|
||||
OnFoot,
|
||||
FreeFloating,
|
||||
Piloting,
|
||||
}
|
||||
|
||||
#[derive(SystemSet, Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub struct PlayerOnFootSystems;
|
||||
|
||||
#[derive(SystemSet, Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub struct PlayerFreeFloatingSystems;
|
||||
|
||||
#[derive(SystemSet, Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub struct PlayerPilotingSystems;
|
||||
29
game/main/src/states/menu.rs
Normal file
29
game/main/src/states/menu.rs
Normal file
@@ -0,0 +1,29 @@
|
||||
use bevy::prelude::*;
|
||||
|
||||
#[derive(States, Debug, Reflect, Default, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub enum MenuState {
|
||||
Idle,
|
||||
#[default]
|
||||
Loading,
|
||||
AssetFinalize,
|
||||
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;
|
||||
4
game/main/src/states/mod.rs
Normal file
4
game/main/src/states/mod.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
pub mod game;
|
||||
pub mod input;
|
||||
pub mod menu;
|
||||
pub mod play;
|
||||
28
game/main/src/states/play.rs
Normal file
28
game/main/src/states/play.rs
Normal file
@@ -0,0 +1,28 @@
|
||||
use bevy::prelude::*;
|
||||
|
||||
#[derive(States, Debug, Reflect, Default, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub enum PlayState {
|
||||
Idle,
|
||||
#[default]
|
||||
Loading,
|
||||
AssetFinalize,
|
||||
Startup,
|
||||
Playing,
|
||||
Paused,
|
||||
Cleanup,
|
||||
}
|
||||
|
||||
#[derive(SystemSet, Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub struct PlayAssetFinalizeSystems;
|
||||
|
||||
#[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;
|
||||
9
game/main/src/utils/input.rs
Normal file
9
game/main/src/utils/input.rs
Normal file
@@ -0,0 +1,9 @@
|
||||
use bevy::{input::mouse::MouseMotion, prelude::*};
|
||||
|
||||
pub fn get_mouse_delta(mut mouse_motion: EventReader<MouseMotion>) -> Vec2 {
|
||||
let mut delta = Vec2::ZERO;
|
||||
for e in mouse_motion.read() {
|
||||
delta += e.delta;
|
||||
}
|
||||
return delta;
|
||||
}
|
||||
2
game/main/src/utils/mod.rs
Normal file
2
game/main/src/utils/mod.rs
Normal file
@@ -0,0 +1,2 @@
|
||||
pub mod input;
|
||||
pub mod rotation;
|
||||
39
game/main/src/utils/rotation.rs
Normal file
39
game/main/src/utils/rotation.rs
Normal file
@@ -0,0 +1,39 @@
|
||||
use std::f32::consts::PI;
|
||||
|
||||
use bevy::prelude::*;
|
||||
|
||||
#[allow(dead_code)]
|
||||
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 < f32::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