mouse look and walking in same entity

This commit is contained in:
Felix Ableitner 2021-01-02 02:55:47 +01:00
parent f85b5cf7af
commit f2cb9f0cae

View file

@ -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,52 +30,66 @@ 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<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
fn init_player(commands: &mut Commands) {
commands
.spawn(Camera3dBundle {
transform: Transform::from_translation(Vec3::new(0.0, 1.0, 3.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()
})
.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));
});
});
}
fn player_movement_system(
time: Res<Time>,
mut bodies: ResMut<RigidBodySet>,
keyboard_input: Res<Input<KeyCode>>,
mut query: Query<(&mut Transform, &RigidBodyHandleComponent), With<Player>>,
// TODO: need to use better queries which filters by parent.player_marker
camera_transform_query: Query<&Transform, With<Camera>>,
mut player_query: Query<&RigidBodyHandleComponent, With<PlayerMarker>>,
) {
for (transform, handle) in query.iter_mut() {
let body: &mut RigidBody = bodies.get_mut(handle.handle()).unwrap();
for camera_transform in camera_transform_query.iter() {
for body_handle in player_query.iter_mut() {
let body: &mut RigidBody = bodies.get_mut(body_handle.handle()).unwrap();
let axis_straight = movement_axis(&keyboard_input, KeyCode::D, KeyCode::A);
let axis_strafe = movement_axis(&keyboard_input, KeyCode::S, KeyCode::W);
let rotation = transform.rotation;
let rotation = camera_transform.rotation;
let mut speed_2d: Vec3 =
(strafe_vector(&rotation) * axis_straight) + (forward_vector(&rotation) * axis_strafe);
if speed_2d.length() != 0.0 {
@ -84,6 +102,7 @@ fn player_movement_system(
body.set_linvel(velocity_3d, true);
}
}
}
fn forward_vector(rotation: &Quat) -> Vec3 {
rotation.mul_vec3(Vec3::unit_z()).normalize()
@ -113,7 +132,7 @@ fn player_look_system(
time: Res<Time>,
mut state: ResMut<State>,
mouse_motion_events: Res<Events<MouseMotion>>,
mut query: Query<&mut Transform, With<Player>>,
mut query: Query<&mut Transform, With<Camera>>,
) {
let mut delta: Vec2 = Vec2::zero();
for event in state.mouse_motion_event_reader.iter(&mouse_motion_events) {