From 9875417232e760f0ca6b2e76a399779745205ca0 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Fri, 1 Jan 2021 17:40:58 +0100 Subject: [PATCH] Rewrite and simplify movement, reorder player functions --- src/player.rs | 142 +++++++++++++++++++++----------------------------- 1 file changed, 59 insertions(+), 83 deletions(-) diff --git a/src/player.rs b/src/player.rs index 9328384..42534e8 100644 --- a/src/player.rs +++ b/src/player.rs @@ -8,17 +8,31 @@ use bevy_rapier3d::{ }, }; +pub struct PlayerPlugin; + +impl Plugin for PlayerPlugin { + fn build(&self, app: &mut AppBuilder) { + app + .init_resource::() + .add_system(player_movement_system.system()) + .add_system(player_look_system.system()) + .add_startup_system(init_player.system()); + } +} + +#[derive(Default)] +struct State { + mouse_motion_event_reader: EventReader, +} + struct Player { /// The current pitch of the FlyCamera in degrees. This value is always up-to-date, enforced by [FlyCameraPlugin](struct.FlyCameraPlugin.html) pitch: f32, /// The current pitch of the FlyCamera in degrees. This value is always up-to-date, enforced by [FlyCameraPlugin](struct.FlyCameraPlugin.html) yaw: f32, - /// The current velocity of the FlyCamera. This value is always up-to-date, enforced by [FlyCameraPlugin](struct.FlyCameraPlugin.html) - velocity: Vec3, } -const PLAYER_SPEED: f32 = 15.0; -const PLAYER_FRICTION: f32 = 1.0; +const PLAYER_SPEED: f32 = 320.0; const MOUSE_SENSITIVITY: f32 = 6.0; impl Player { @@ -26,11 +40,50 @@ impl Player { Self { pitch: 0.0, yaw: 0.0, - velocity: Vec3::zero(), } } } +fn init_player(commands: &mut Commands) { + commands + .spawn(Camera3dBundle { + transform: Transform::from_translation(Vec3::new(0.0, 3.0, 3.0)), + ..Default::default() + }) + .with(Player::create()) + .with( + RigidBodyBuilder::new_dynamic() + .translation(0.0, 3.0, 0.0) + .lock_rotations(), + ) + .with(ColliderBuilder::cylinder(1.0, 0.5).friction(0.0)); +} + +fn player_movement_system( + time: Res