This commit is contained in:
Back777space
2025-04-05 22:06:08 +02:00
parent 4dd5b14ea1
commit 2f42713a9d
7 changed files with 410 additions and 124 deletions

318
Cargo.lock generated
View File

@@ -124,6 +124,7 @@ version = "0.1.0"
dependencies = [
"bevy",
"bevy_asset_loader",
"bevy_rapier3d",
]
[[package]]
@@ -939,6 +940,19 @@ version = "0.15.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "89fe0b0b919146939481a3a7c38864face2c6d0fd2c73ab3d430dc693ecd9b11"
[[package]]
name = "bevy_rapier3d"
version = "0.29.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "85f3856331b3f75e900db0fd47c34d7e63dc7f736f455e9d0e6d775a24099c37"
dependencies = [
"bevy",
"bitflags 2.9.0",
"log",
"nalgebra",
"rapier3d",
]
[[package]]
name = "bevy_reflect"
version = "0.15.3"
@@ -1304,7 +1318,7 @@ dependencies = [
"proc-macro2",
"quote",
"regex",
"rustc-hash",
"rustc-hash 1.1.0",
"shlex",
"syn",
]
@@ -1333,6 +1347,12 @@ version = "0.6.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "349f9b6a179ed607305526ca489b34ad0a41aed5f7980fa90eb03160b69598fb"
[[package]]
name = "bit-vec"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d2c54ff287cfc0a34f38a6b832ea1bd8e448a330b3e40a50859e6488bee07f22"
[[package]]
name = "bit-vec"
version = "0.8.0"
@@ -1681,7 +1701,7 @@ dependencies = [
"log",
"rangemap",
"rayon",
"rustc-hash",
"rustc-hash 1.1.0",
"rustybuzz",
"self_cell",
"swash",
@@ -1725,6 +1745,19 @@ dependencies = [
"cfg-if",
]
[[package]]
name = "crossbeam"
version = "0.8.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1137cd7e7fc0fb5d3c5a8678be38ec56e819125d8d7907411fe24ccb943faca8"
dependencies = [
"crossbeam-channel",
"crossbeam-deque",
"crossbeam-epoch",
"crossbeam-queue",
"crossbeam-utils",
]
[[package]]
name = "crossbeam-channel"
version = "0.5.14"
@@ -1753,6 +1786,15 @@ dependencies = [
"crossbeam-utils",
]
[[package]]
name = "crossbeam-queue"
version = "0.3.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0f58bbc28f91df819d0aa2a2c00cd19754769c2fad90579b3592b1c9ba7a3115"
dependencies = [
"crossbeam-utils",
]
[[package]]
name = "crossbeam-utils"
version = "0.8.21"
@@ -1862,6 +1904,15 @@ version = "1.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719"
[[package]]
name = "ena"
version = "0.14.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3d248bdd43ce613d87415282f69b9bb99d947d290b10962dd6c56233312c2ad5"
dependencies = [
"log",
]
[[package]]
name = "encase"
version = "0.10.0"
@@ -2319,6 +2370,15 @@ dependencies = [
"svg_fmt",
]
[[package]]
name = "hash32"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "47d60b12902ba28e2730cd37e95b8c9223af2808df9e902d4df49588d1470606"
dependencies = [
"byteorder",
]
[[package]]
name = "hashbrown"
version = "0.14.5"
@@ -2336,9 +2396,21 @@ version = "0.15.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bf151400ff0baff5465007dd2f3e717f3fe502074ca563069ce3a6629d07b289"
dependencies = [
"allocator-api2",
"equivalent",
"foldhash",
]
[[package]]
name = "heapless"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0bfb9eb618601c89945a70e254898da93b13be0388091d42117462b265bb3fad"
dependencies = [
"hash32",
"stable_deref_trait",
]
[[package]]
name = "hermit-abi"
version = "0.4.0"
@@ -2629,6 +2701,16 @@ dependencies = [
"regex-automata 0.1.10",
]
[[package]]
name = "matrixmultiply"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9380b911e3e96d10c1f415da0876389aaf1b56759054eeb0de7df940c456ba1a"
dependencies = [
"autocfg",
"rawpointer",
]
[[package]]
name = "memchr"
version = "2.7.4"
@@ -2690,7 +2772,7 @@ dependencies = [
"indexmap",
"log",
"pp-rs",
"rustc-hash",
"rustc-hash 1.1.0",
"spirv",
"termcolor",
"thiserror",
@@ -2711,12 +2793,40 @@ dependencies = [
"once_cell",
"regex",
"regex-syntax 0.8.5",
"rustc-hash",
"rustc-hash 1.1.0",
"thiserror",
"tracing",
"unicode-ident",
]
[[package]]
name = "nalgebra"
version = "0.33.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "26aecdf64b707efd1310e3544d709c5c0ac61c13756046aaaba41be5c4f66a3b"
dependencies = [
"approx",
"glam",
"matrixmultiply",
"nalgebra-macros",
"num-complex",
"num-rational",
"num-traits",
"simba",
"typenum",
]
[[package]]
name = "nalgebra-macros"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "254a5372af8fc138e36684761d3c0cdb758a4410e938babcff1c860ce14ddbfc"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "ndk"
version = "0.8.0"
@@ -2817,6 +2927,25 @@ dependencies = [
"winapi",
]
[[package]]
name = "num-bigint"
version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a5e44f723f1133c9deac646763579fdb3ac745e418f2a7af9cd0c431da1f20b9"
dependencies = [
"num-integer",
"num-traits",
]
[[package]]
name = "num-complex"
version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73f88a1307638156682bada9d7604135552957b7818057dcef22705b4d509495"
dependencies = [
"num-traits",
]
[[package]]
name = "num-derive"
version = "0.4.2"
@@ -2828,6 +2957,26 @@ dependencies = [
"syn",
]
[[package]]
name = "num-integer"
version = "0.1.46"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7969661fd2958a5cb096e56c8e1ad0444ac2bbcd0061bd28660485a44879858f"
dependencies = [
"num-traits",
]
[[package]]
name = "num-rational"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f83d14da390562dca69fc84082e73e548e1ad308d24accdedd2720017cb37824"
dependencies = [
"num-bigint",
"num-integer",
"num-traits",
]
[[package]]
name = "num-traits"
version = "0.2.19"
@@ -3128,6 +3277,15 @@ dependencies = [
"libredox",
]
[[package]]
name = "ordered-float"
version = "4.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7bb71e1b3fa6ca1c61f383464aaf2bb0e2f8e772a1f01d486832464de363b951"
dependencies = [
"num-traits",
]
[[package]]
name = "overload"
version = "0.1.1"
@@ -3163,6 +3321,32 @@ dependencies = [
"windows-targets 0.52.6",
]
[[package]]
name = "parry3d"
version = "0.18.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ecb572cea5c76b06bc744aaefdbb9e6ea9e5695f50f79c0227bddecd6ec925a4"
dependencies = [
"approx",
"arrayvec",
"bitflags 2.9.0",
"downcast-rs",
"either",
"ena",
"log",
"nalgebra",
"num-derive",
"num-traits",
"ordered-float",
"rstar",
"rustc-hash 2.1.1",
"simba",
"slab",
"smallvec",
"spade",
"thiserror",
]
[[package]]
name = "paste"
version = "1.0.15"
@@ -3336,6 +3520,19 @@ name = "profiling"
version = "1.0.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "afbdc74edc00b6f6a218ca6a5364d6226a259d4b8ea1af4a0ea063f27e179f4d"
dependencies = [
"profiling-procmacros",
]
[[package]]
name = "profiling-procmacros"
version = "1.0.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a65f2e60fbf1063868558d69c6beacf412dc755f9fc020f514b7955fc914fe30"
dependencies = [
"quote",
"syn",
]
[[package]]
name = "quote"
@@ -3410,12 +3607,42 @@ version = "1.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f60fcc7d6849342eff22c4350c8b9a989ee8ceabc4b481253e8946b9fe83d684"
[[package]]
name = "rapier3d"
version = "0.23.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "37d84644f385b81f065576058bc8f7e3d64e56a66f2f5fc40bfd917774659e12"
dependencies = [
"approx",
"arrayvec",
"bit-vec 0.7.0",
"bitflags 2.9.0",
"crossbeam",
"downcast-rs",
"log",
"nalgebra",
"num-derive",
"num-traits",
"ordered-float",
"parry3d",
"profiling",
"rustc-hash 2.1.1",
"simba",
"thiserror",
]
[[package]]
name = "raw-window-handle"
version = "0.6.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "20675572f6f24e9e76ef639bc5552774ed45f1c30e2951e1e99c59888861c539"
[[package]]
name = "rawpointer"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "60a357793950651c4ed0f3f52338f53b2f809f32d83a07f72909fa13e4c6c1e3"
[[package]]
name = "rayon"
version = "1.10.0"
@@ -3520,6 +3747,12 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "19b30a45b0cd0bcca8037f3d0dc3421eaf95327a17cad11964fb8179b4fc4832"
[[package]]
name = "robust"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cbf4a6aa5f6d6888f39e980649f3ad6b666acdce1d78e95b8a2cb076e687ae30"
[[package]]
name = "rodio"
version = "0.19.0"
@@ -3549,12 +3782,29 @@ version = "0.20.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c20b6793b5c2fa6553b250154b78d6d0db37e72700ae35fad9387a46f487c97"
[[package]]
name = "rstar"
version = "0.12.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "421400d13ccfd26dfa5858199c30a5d76f9c54e0dba7575273025b43c5175dbb"
dependencies = [
"heapless",
"num-traits",
"smallvec",
]
[[package]]
name = "rustc-hash"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2"
[[package]]
name = "rustc-hash"
version = "2.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "357703d41365b4b27c590e3ed91eabb1b663f07c4c084095e60cbed4362dff0d"
[[package]]
name = "rustix"
version = "0.38.44"
@@ -3606,6 +3856,15 @@ version = "1.0.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "28d3b2b1366ec20994f1fd18c3c594f05c5dd4bc44d8bb0c1c632c8d6829481f"
[[package]]
name = "safe_arch"
version = "0.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "96b02de82ddbe1b636e6170c21be622223aea188ef2e139be0a5b219ec215323"
dependencies = [
"bytemuck",
]
[[package]]
name = "same-file"
version = "1.0.6"
@@ -3680,6 +3939,19 @@ version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
[[package]]
name = "simba"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b3a386a501cd104797982c15ae17aafe8b9261315b5d07e3ec803f2ea26be0fa"
dependencies = [
"approx",
"num-complex",
"num-traits",
"paste",
"wide",
]
[[package]]
name = "simd-adler32"
version = "0.3.7"
@@ -3729,6 +4001,18 @@ dependencies = [
"serde",
]
[[package]]
name = "spade"
version = "2.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1ece03ff43cd2a9b57ebf776ea5e78bd30b3b4185a619f041079f4109f385034"
dependencies = [
"hashbrown 0.15.2",
"num-traits",
"robust",
"smallvec",
]
[[package]]
name = "spirv"
version = "0.3.0+sdk-1.3.268.0"
@@ -3738,6 +4022,12 @@ dependencies = [
"bitflags 2.9.0",
]
[[package]]
name = "stable_deref_trait"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3"
[[package]]
name = "stackfuture"
version = "0.3.0"
@@ -4008,6 +4298,12 @@ version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bc7d623258602320d5c55d1bc22793b57daff0ec7efc270ea7d55ce1d5f5471c"
[[package]]
name = "typenum"
version = "1.18.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1dccffe3ce07af9386bfd29e80c0ab1a8205a2fc34e4bcd40364df902cfa8f3f"
[[package]]
name = "unicode-bidi"
version = "0.3.18"
@@ -4255,7 +4551,7 @@ dependencies = [
"parking_lot",
"profiling",
"raw-window-handle",
"rustc-hash",
"rustc-hash 1.1.0",
"smallvec",
"thiserror",
"wgpu-hal",
@@ -4297,7 +4593,7 @@ dependencies = [
"range-alloc",
"raw-window-handle",
"renderdoc-sys",
"rustc-hash",
"rustc-hash 1.1.0",
"smallvec",
"thiserror",
"wasm-bindgen",
@@ -4318,6 +4614,16 @@ dependencies = [
"web-sys",
]
[[package]]
name = "wide"
version = "0.7.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "41b5576b9a81633f3e8df296ce0063042a73507636cbe956c61133dd7034ab22"
dependencies = [
"bytemuck",
"safe_arch",
]
[[package]]
name = "winapi"
version = "0.3.9"

View File

@@ -6,3 +6,4 @@ edition = "2024"
[dependencies]
bevy = { version="0.15.3", features = ["jpeg"]}
bevy_asset_loader = { version ="0.22.0", features = ["standard_dynamic_assets"] }
bevy_rapier3d = "0.29.0"

View File

@@ -30,8 +30,8 @@ pub(crate) struct AudioAssets {}
#[derive(AssetCollection, Resource, Clone)]
pub(crate) struct GltfAssets {
#[asset(key = "wall")]
pub(crate) wall: Handle<Gltf>,
// #[asset(key = "wall")]
// pub(crate) wall: Handle<Gltf>,
}
#[derive(AssetCollection, Resource, Clone)]

View File

@@ -11,7 +11,7 @@ fn spawn_level(
models: Res<Assets<Gltf>>,
gltf_assets: Res<GltfAssets>
) {
let gltf = models.get(&gltf_assets.wall).unwrap();
// let gltf = models.get(&gltf_assets.wall).unwrap();
commands.spawn(SceneRoot(gltf.scenes[0].clone()));
// commands.spawn(SceneRoot(gltf.scenes[0].clone()));
}

View File

@@ -1,11 +1,11 @@
use asset_loading::ImageAssets;
use bevy::prelude::*;
use bevy_rapier3d::prelude::*;
mod asset_loading;
mod bevy_plugin;
mod level_instantiation;
mod main_menu;
mod physics;
mod player;
fn main() {
@@ -13,10 +13,11 @@ fn main() {
.add_plugins((
bevy_plugin::plugin,
asset_loading::plugin,
physics::plugin,
level_instantiation::map_plugin,
player::plugin,
main_menu::plugin,
RapierPhysicsPlugin::<NoUserData>::default(),
RapierDebugRenderPlugin::default(),
))
.init_state::<GameState>()
.add_systems(OnEnter(GameState::Playing), setup)
@@ -49,11 +50,17 @@ fn setup(
MeshMaterial3d(materials.add(Color::WHITE)),
Transform::from_rotation(Quat::from_rotation_x(-std::f32::consts::FRAC_PI_2)),
));
commands.spawn((
RigidBody::Fixed,
Collider::cylinder(0.1, 4.0)
));
// cube
commands.spawn((
Mesh3d(meshes.add(Cuboid::new(1.0, 1.0, 1.0))),
MeshMaterial3d(materials.add(Color::srgb_u8(124, 144, 255))),
Transform::from_xyz(3.0, 0.5, 0.0),
RigidBody::Fixed,
Collider::cuboid(0.5, 0.5, 0.5),
));
// light
commands.spawn((

View File

@@ -1,72 +0,0 @@
use bevy::prelude::*;
#[derive(Debug, Component, Clone, Copy, PartialEq, Default, Deref, DerefMut)]
pub struct Velocity(pub Vec3);
/// The actual position of the player in the physics simulation.
/// This is separate from the `Transform`, which is merely a visual representation.
///
/// If you want to make sure that this component is always initialized
/// with the same value as the `Transform`'s translation, you can
/// use a [component lifecycle hook](https://docs.rs/bevy/0.14.0/bevy/ecs/component/struct.ComponentHooks.html)
#[derive(Debug, Component, Clone, Copy, PartialEq, Default, Deref, DerefMut)]
pub struct PhysicalTranslation(pub Vec3);
/// The value [`PhysicalTranslation`] had in the last fixed timestep.
/// Used for interpolation in the `interpolate_rendered_transform` system.
#[derive(Debug, Component, Clone, Copy, PartialEq, Default, Deref, DerefMut)]
pub struct PreviousPhysicalTranslation(pub Vec3);
#[derive(Debug, Component, Clone, Copy, PartialEq, Default, Deref, DerefMut)]
pub struct AccumulatedInput(pub Vec3);
pub fn plugin(app: &mut App) {
app.add_systems(
RunFixedMainLoop,
interpolate_rendered_transform.in_set(RunFixedMainLoopSystem::AfterFixedMainLoop),
)
.add_systems(FixedUpdate, step);
}
pub fn interpolate_rendered_transform(
fixed_time: Res<Time<Fixed>>,
mut query: Query<(
&mut Transform,
&PhysicalTranslation,
&PreviousPhysicalTranslation,
)>,
) {
for (mut transform, current_physical_translation, previous_physical_translation) in
query.iter_mut()
{
let previous = previous_physical_translation.0;
let current = current_physical_translation.0;
// The overstep fraction is a value between 0 and 1 that tells us how far we are between two fixed timesteps.
let alpha = fixed_time.overstep_fraction();
let rendered_translation = previous.lerp(current, alpha);
transform.translation = rendered_translation;
}
}
pub fn step(
fixed_time: Res<Time<Fixed>>,
mut query: Query<(
&mut PhysicalTranslation,
&mut PreviousPhysicalTranslation,
&mut AccumulatedInput,
&Velocity,
)>,
) {
for (
mut current_physical_translation,
mut previous_physical_translation,
mut input,
velocity,
) in query.iter_mut()
{
previous_physical_translation.0 = current_physical_translation.0;
current_physical_translation.0 += velocity.0 * fixed_time.delta_secs();
input.0 = Vec3::ZERO;
}
}

View File

@@ -1,11 +1,10 @@
use crate::{
GameState,
physics::{AccumulatedInput, PhysicalTranslation, PreviousPhysicalTranslation, Velocity},
};
use bevy::{
input::mouse::AccumulatedMouseMotion, pbr::NotShadowCaster, prelude::*,
render::view::RenderLayers, window::PrimaryWindow,
};
use bevy_rapier3d::prelude::*;
use crate::GameState;
#[derive(Debug, Component)]
pub struct Player;
@@ -21,24 +20,24 @@ impl Default for CameraSensitivity {
#[derive(Debug, Component)]
struct WorldModelCamera;
#[derive(Debug, Component, Default)]
pub struct PlayerInput {
movement_direction: Vec3,
}
pub fn plugin(app: &mut App) {
app.add_systems(OnEnter(GameState::Playing), (init_player, hide_cursor))
.add_systems(Update, move_camera.run_if(in_state(GameState::Playing)))
.add_systems(
RunFixedMainLoop,
handle_input
.in_set(RunFixedMainLoopSystem::BeforeFixedMainLoop)
.run_if(in_state(GameState::Playing)),
Update,
(move_camera, handle_input).run_if(in_state(GameState::Playing)),
)
.add_systems(
FixedUpdate,
apply_player_movement.run_if(in_state(GameState::Playing)),
);
}
/// Used implicitly by all entities without a `RenderLayers` component.
/// Our world model camera and all objects other than the player are on this layer.
/// The light source belongs to both layers.
// const DEFAULT_RENDER_LAYER: usize = 0;
/// Used by the view model camera and the player's arm.
/// The light source belongs to both layers.
/// used by the view model camera and the player's arm.
const STATIC_LAYER: usize = 1;
pub fn init_player(
@@ -53,12 +52,23 @@ pub fn init_player(
.spawn((
Player,
CameraSensitivity::default(),
PlayerInput::default(),
// rapier
RigidBody::Dynamic,
Collider::capsule(Vec3::new(0.0, -0.5, 0.0), Vec3::new(0.0, 0.5, 0.0), 0.5),
Velocity::zero(),
LockedAxes::ROTATION_LOCKED,
Damping {
linear_damping: 6.0,
angular_damping: 1.0,
},
Transform::from_xyz(0.0, 1.0, 0.0),
GlobalTransform::default(),
Visibility::default(),
AccumulatedInput::default(),
Velocity::default(),
PhysicalTranslation(Vec3::new(0.0, 1.0, 0.0)),
PreviousPhysicalTranslation(Vec3::new(0.0, 1.0, 0.0)),
InheritedVisibility::default(),
ViewVisibility::default(),
))
.with_children(|parent| {
parent.spawn((
@@ -68,11 +78,14 @@ pub fn init_player(
fov: 90.0_f32.to_radians(),
..default()
}),
Transform::default(),
GlobalTransform::default(),
Visibility::default(),
InheritedVisibility::default(),
ViewVisibility::default(),
));
// we use a second layer ("framebuffer") to draw the player's arm on
// there also a second camera that only views this buffer
// this makes it easy because the second camera doesn't move with the player
// camera voor pitslampke
parent.spawn((
Camera3d::default(),
Camera {
@@ -84,19 +97,29 @@ pub fn init_player(
..default()
}),
RenderLayers::layer(STATIC_LAYER),
Transform::default(),
GlobalTransform::default(),
Visibility::default(),
InheritedVisibility::default(),
ViewVisibility::default(),
));
// pitslampke
parent.spawn((
Mesh3d(arm),
MeshMaterial3d(arm_material),
Transform::from_xyz(0.2, -0.1, -0.25),
GlobalTransform::default(),
RenderLayers::layer(STATIC_LAYER),
NotShadowCaster,
Visibility::default(),
InheritedVisibility::default(),
ViewVisibility::default(),
));
});
}
fn hide_cursor(mut windows: Query<&mut Window, With<PrimaryWindow>>) {
// Query returns one window typically.
for mut window in windows.iter_mut() {
window.cursor_options.visible = false;
}
@@ -126,35 +149,56 @@ pub fn move_camera(
pub fn handle_input(
keyboard_input: Res<ButtonInput<KeyCode>>,
mut query: Query<(&Transform, &mut AccumulatedInput, &mut Velocity), With<Player>>,
mut query: Query<(&Transform, &mut PlayerInput), With<Player>>,
) {
const SPEED: f32 = 2.0;
for (transform, mut input, mut velocity) in query.iter_mut() {
let forward = transform.forward(); // Forward direction (z axis)
let right = transform.right(); // Right direction (x axis)
for (transform, mut input) in query.iter_mut() {
let forward = transform.forward();
let right = transform.right();
let mut movement_direction = Vec3::ZERO;
if keyboard_input.pressed(KeyCode::KeyW) {
input.x += forward.x;
input.z += forward.z;
movement_direction += Vec3::new(forward.x, 0.0, forward.z).normalize_or_zero();
}
if keyboard_input.pressed(KeyCode::KeyS) {
input.x -= forward.x;
input.z -= forward.z;
movement_direction -= Vec3::new(forward.x, 0.0, forward.z).normalize_or_zero();
}
if keyboard_input.pressed(KeyCode::KeyA) {
input.x -= right.x;
input.z -= right.z;
movement_direction -= Vec3::new(right.x, 0.0, right.z).normalize_or_zero();
}
if keyboard_input.pressed(KeyCode::KeyD) {
input.x += right.x;
input.z += right.z;
movement_direction += Vec3::new(right.x, 0.0, right.z).normalize_or_zero();
}
if keyboard_input.pressed(KeyCode::Space) {
input.y += 1.0;
movement_direction += Vec3::Y;
}
if keyboard_input.pressed(KeyCode::ShiftLeft) {
input.y -= 1.0;
movement_direction -= Vec3::Y;
}
input.movement_direction = movement_direction.normalize_or_zero();
}
}
pub fn apply_player_movement(
mut player_query: Query<(&PlayerInput, &mut Velocity), With<Player>>,
) {
const SPEED: f32 = 3.0;
const JUMP_FORCE: f32 = 4.0;
for (input, mut velocity) in player_query.iter_mut() {
let horizontal_movement = Vec3::new(
input.movement_direction.x * SPEED,
0.0,
input.movement_direction.z * SPEED,
);
velocity.linvel.x = horizontal_movement.x;
velocity.linvel.z = horizontal_movement.z;
if input.movement_direction.y > 0.0 {
velocity.linvel.y = JUMP_FORCE;
} else if input.movement_direction.y < 0.0 {
velocity.linvel.y = -SPEED;
}
velocity.0 = input.normalize_or_zero() * SPEED;
}
}