From f2cb9f0cae3343d9acfa4a8c34382e9d020b1f25 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Sat, 2 Jan 2021 02:55:47 +0100 Subject: [PATCH] mouse look and walking in same entity --- src/player.rs | 107 +++++++++++++++++++++++++++++--------------------- 1 file changed, 63 insertions(+), 44 deletions(-) diff --git a/src/player.rs b/src/player.rs index d112119..e5dc46d 100644 --- a/src/player.rs +++ b/src/player.rs @@ -1,4 +1,8 @@ -use bevy::{input::mouse::MouseMotion, prelude::*}; +use bevy::{ + input::mouse::MouseMotion, + prelude::*, + render::camera::{Camera, PerspectiveProjection, VisibleEntities}, +}; use bevy_rapier3d::{ na::Matrix3x1, physics::RigidBodyHandleComponent, @@ -26,62 +30,77 @@ struct State { } #[derive(Default)] -struct Player; +struct PlayerMarker; + +#[derive(Bundle)] +pub struct PlayerBundle { + pub perspective_projection: PerspectiveProjection, + pub visible_entities: VisibleEntities, + pub transform: Transform, + pub global_transform: GlobalTransform, + player_marker: PlayerMarker, + rigid_body: RigidBodyBuilder, + collider: ColliderBuilder, +} + +impl Default for PlayerBundle { + fn default() -> Self { + PlayerBundle { + rigid_body: RigidBodyBuilder::new_dynamic() + .translation(0.0, 3.0, 0.0) + .lock_rotations(), + collider: ColliderBuilder::cylinder(PLAYER_HEIGHT / 2., PLAYER_WIDTH).friction(0.0), + perspective_projection: Default::default(), + visible_entities: Default::default(), + transform: Default::default(), + global_transform: Default::default(), + player_marker: Default::default(), + } + } +} const PLAYER_SPEED: f32 = 320.0; const MOUSE_SENSITIVITY: f32 = 1.0; +const PLAYER_HEIGHT: f32 = 1.8; +const PLAYER_WIDTH: f32 = 0.25; -fn init_player( - commands: &mut Commands, - mut meshes: ResMut>, - mut materials: ResMut>, -) { +fn init_player(commands: &mut Commands) { commands - .spawn(Camera3dBundle { - transform: Transform::from_translation(Vec3::new(0.0, 1.0, 3.0)), - ..Default::default() - }) - .with(Player::default()); - - // TODO: temporarily create a separate bundle for movement, to test mouse look - commands - .spawn(PbrBundle { - mesh: meshes.add(Mesh::from(shape::Cube { size: 0.25 })), - material: materials.add(Color::rgb(0.0, 1.0, 0.3).into()), - transform: Transform::from_translation(Vec3::new(0.0, 0.0, 0.0)), - ..Default::default() - }) - .with(Player::default()) - .with( - RigidBodyBuilder::new_dynamic() - .translation(0.0, 3.0, 0.0) - .lock_rotations(), - ) - .with(ColliderBuilder::cylinder(1.0, 0.5).friction(0.0)); + .spawn(PlayerBundle::default()) + .with_children(|parent| { + parent.spawn(Camera3dBundle { + transform: Transform::from_translation(Vec3::new(0.0, PLAYER_HEIGHT / 2., 0.0)), + ..Default::default() + }); + }); } fn player_movement_system( time: Res