This commit is contained in:
LorrensP-2158466
2025-04-05 23:52:25 +02:00
parent 153a40cb73
commit be1caf37c1
6 changed files with 6501 additions and 38 deletions

6436
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -8,4 +8,3 @@ bevy = { version="0.15.3", features = ["jpeg"]}
bevy_asset_loader = { version ="0.22.0", features = ["standard_dynamic_assets"] } bevy_asset_loader = { version ="0.22.0", features = ["standard_dynamic_assets"] }
bevy_egui = "0.33.0" bevy_egui = "0.33.0"
bevy_rapier3d = "0.29.0" bevy_rapier3d = "0.29.0"
bevy_xpbd_3d = "0.5.0"

View File

@@ -1,4 +1,5 @@
use bevy::prelude::*; use bevy::{gltf::GltfMesh, math::Vec3A, prelude::*, render::mesh::MeshAabb};
use bevy_rapier3d::prelude::{Collider, RigidBody};
use crate::{GameState, asset_loading::GltfAssets}; use crate::{GameState, asset_loading::GltfAssets};
@@ -18,11 +19,22 @@ fn spawn(mut commands: Commands, models: Res<Assets<Gltf>>, gltf_assets: Res<Glt
let hammer = models.get(hammer).unwrap(); let hammer = models.get(hammer).unwrap();
let asset = hammer.default_scene.as_ref().unwrap(); let asset = hammer.default_scene.as_ref().unwrap();
// hammer // hammer
commands.spawn(( commands
Transform::from_xyz(0.0, 1.0, 0.0).with_scale(Vec3::splat(0.1)), .spawn((
Interact, Transform::from_xyz(0.0, 100.0, 0.0).with_scale(Vec3::splat(0.1)),
SceneRoot(asset.clone()), Interact,
)); RigidBody::Dynamic,
SceneRoot(asset.clone()),
))
.with_children(|parent| {
parent
.spawn(Collider::cuboid(0.8, 10f32, 0.8))
.insert(Transform::from_xyz(0.0, -5.0, 0.0));
parent
.spawn(Collider::cuboid(1.0, 1.0, 4.5))
// Position the collider relative to the rigid-body.
.insert(Transform::from_xyz(0.0, 4.2, 1.0));
});
//tools //tools
} }

View File

@@ -16,7 +16,6 @@ fn main() {
bevy_plugin::plugin, bevy_plugin::plugin,
asset_loading::plugin, asset_loading::plugin,
main_menu::plugin, main_menu::plugin,
physics::plugin,
level_instantiation::map_plugin, level_instantiation::map_plugin,
player::plugin, player::plugin,
interaction::plugin, interaction::plugin,
@@ -54,16 +53,13 @@ fn setup(
MeshMaterial3d(materials.add(Color::WHITE)), MeshMaterial3d(materials.add(Color::WHITE)),
Transform::from_rotation(Quat::from_rotation_x(-std::f32::consts::FRAC_PI_2)), Transform::from_rotation(Quat::from_rotation_x(-std::f32::consts::FRAC_PI_2)),
)); ));
commands.spawn(( commands.spawn((RigidBody::Fixed, Collider::cylinder(0.1, 4.0)));
RigidBody::Fixed,
Collider::cylinder(0.1, 4.0)
));
// cube // cube
commands.spawn(( commands.spawn((
Mesh3d(meshes.add(Cuboid::new(1.0, 1.0, 1.0))), Mesh3d(meshes.add(Cuboid::new(1.0, 1.0, 1.0))),
MeshMaterial3d(materials.add(Color::srgb_u8(124, 144, 255))), MeshMaterial3d(materials.add(Color::srgb_u8(124, 144, 255))),
Transform::from_xyz(3.0, 0.5, 0.0), Transform::from_xyz(3.0, 0.5, 0.0),
RigidBody::Fixed, RigidBody::Fixed,
Collider::cuboid(0.5, 0.5, 0.5), Collider::cuboid(0.5, 0.5, 0.5),
)); ));
// light // light

View File

@@ -6,6 +6,21 @@ use bevy_rapier3d::prelude::*;
use crate::GameState; use crate::GameState;
mod toolbar;
pub fn plugin(app: &mut App) {
app.add_plugins(toolbar::plugin)
.add_systems(OnEnter(GameState::Playing), (init_player, hide_cursor))
.add_systems(
Update,
(move_camera, handle_input).run_if(in_state(GameState::Playing)),
)
.add_systems(
FixedUpdate,
apply_player_movement.run_if(in_state(GameState::Playing)),
);
}
#[derive(Debug, Component)] #[derive(Debug, Component)]
pub struct Player; pub struct Player;
@@ -25,18 +40,6 @@ pub struct PlayerInput {
movement_direction: Vec3, movement_direction: Vec3,
} }
pub fn plugin(app: &mut App) {
app.add_systems(OnEnter(GameState::Playing), (init_player, hide_cursor))
.add_systems(
Update,
(move_camera, handle_input).run_if(in_state(GameState::Playing)),
)
.add_systems(
FixedUpdate,
apply_player_movement.run_if(in_state(GameState::Playing)),
);
}
/// used by the view model camera and the player's arm. /// used by the view model camera and the player's arm.
const STATIC_LAYER: usize = 1; const STATIC_LAYER: usize = 1;
@@ -54,7 +57,7 @@ pub fn init_player(
PlayerAction::default(), PlayerAction::default(),
CameraSensitivity::default(), CameraSensitivity::default(),
PlayerInput::default(), PlayerInput::default(),
toolbar::Item::none(),
// rapier // rapier
RigidBody::Dynamic, RigidBody::Dynamic,
Collider::capsule(Vec3::new(0.0, -0.5, 0.0), Vec3::new(0.0, 0.5, 0.0), 0.5), Collider::capsule(Vec3::new(0.0, -0.5, 0.0), Vec3::new(0.0, 0.5, 0.0), 0.5),
@@ -64,10 +67,9 @@ pub fn init_player(
linear_damping: 6.0, linear_damping: 6.0,
angular_damping: 1.0, angular_damping: 1.0,
}, },
GravityScale(3.0),
Transform::from_xyz(0.0, 1.0, 0.0), Transform::from_xyz(0.0, 1.0, 0.0),
GlobalTransform::default(), GlobalTransform::default(),
Visibility::default(),
InheritedVisibility::default(), InheritedVisibility::default(),
ViewVisibility::default(), ViewVisibility::default(),
)) ))
@@ -161,7 +163,7 @@ pub fn handle_input(
keyboard_input: Res<ButtonInput<KeyCode>>, keyboard_input: Res<ButtonInput<KeyCode>>,
mut query: Query<(&Transform, &mut PlayerInput, &mut PlayerAction), With<Player>>, mut query: Query<(&Transform, &mut PlayerInput, &mut PlayerAction), With<Player>>,
) { ) {
for (transform, mut input) in query.iter_mut() { for (transform, mut input, mut action) in query.iter_mut() {
let forward = transform.forward(); let forward = transform.forward();
let right = transform.right(); let right = transform.right();
let mut movement_direction = Vec3::ZERO; let mut movement_direction = Vec3::ZERO;
@@ -187,32 +189,29 @@ pub fn handle_input(
if keyboard_input.pressed(KeyCode::KeyE) { if keyboard_input.pressed(KeyCode::KeyE) {
*action = PlayerAction::Interact *action = PlayerAction::Interact
} }
velocity.0 = input.normalize_or_zero() * SPEED;
input.movement_direction = movement_direction.normalize_or_zero(); input.movement_direction = movement_direction.normalize_or_zero();
} }
} }
pub fn apply_player_movement( pub fn apply_player_movement(mut player_query: Query<(&PlayerInput, &mut Velocity), With<Player>>) {
mut player_query: Query<(&PlayerInput, &mut Velocity), With<Player>>,
) {
const SPEED: f32 = 3.0; const SPEED: f32 = 3.0;
const JUMP_FORCE: f32 = 4.0; const JUMP_FORCE: f32 = 4.0;
for (input, mut velocity) in player_query.iter_mut() { for (input, mut velocity) in player_query.iter_mut() {
let horizontal_movement = Vec3::new( let horizontal_movement = Vec3::new(
input.movement_direction.x * SPEED, input.movement_direction.x * SPEED,
0.0, 0.0,
input.movement_direction.z * SPEED, input.movement_direction.z * SPEED,
); );
velocity.linvel.x = horizontal_movement.x; velocity.linvel.x = horizontal_movement.x;
velocity.linvel.z = horizontal_movement.z; velocity.linvel.z = horizontal_movement.z;
if input.movement_direction.y > 0.0 { if input.movement_direction.y > 0.0 {
velocity.linvel.y = JUMP_FORCE; velocity.linvel.y = JUMP_FORCE;
} else if input.movement_direction.y < 0.0 { } else if input.movement_direction.y < 0.0 {
velocity.linvel.y = -SPEED; velocity.linvel.y = -SPEED;
} }
} }
} }

21
src/player/toolbar.rs Normal file
View File

@@ -0,0 +1,21 @@
use bevy::prelude::*;
use std::default::Default;
use crate::GameState;
use super::Player;
#[derive(Component, Default, Debug)]
pub struct Item(Option<Entity>);
impl Item {
pub fn none() -> Self {
Default::default()
}
}
pub fn plugin(app: &mut App) {
app.add_systems(Update, show_toolbar.run_if(in_state(GameState::Playing)));
}
fn show_toolbar(player_tool_query: Query<&Item, With<Player>>) {}