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

View File

@@ -6,6 +6,21 @@ use bevy_rapier3d::prelude::*;
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)]
pub struct Player;
@@ -25,18 +40,6 @@ 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, 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.
const STATIC_LAYER: usize = 1;
@@ -54,7 +57,7 @@ pub fn init_player(
PlayerAction::default(),
CameraSensitivity::default(),
PlayerInput::default(),
toolbar::Item::none(),
// rapier
RigidBody::Dynamic,
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,
angular_damping: 1.0,
},
GravityScale(3.0),
Transform::from_xyz(0.0, 1.0, 0.0),
GlobalTransform::default(),
Visibility::default(),
InheritedVisibility::default(),
ViewVisibility::default(),
))
@@ -161,7 +163,7 @@ pub fn handle_input(
keyboard_input: Res<ButtonInput<KeyCode>>,
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 right = transform.right();
let mut movement_direction = Vec3::ZERO;
@@ -187,32 +189,29 @@ pub fn handle_input(
if keyboard_input.pressed(KeyCode::KeyE) {
*action = PlayerAction::Interact
}
velocity.0 = input.normalize_or_zero() * SPEED;
input.movement_direction = movement_direction.normalize_or_zero();
}
}
pub fn apply_player_movement(
mut player_query: Query<(&PlayerInput, &mut Velocity), With<Player>>,
) {
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.linvel.y = -SPEED;
}
}
}
}