improve controller (#5)
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
use std::time::Duration;
|
||||
use std::{f32::consts::PI, time::Duration};
|
||||
|
||||
use crate::{
|
||||
DebugVisuals,
|
||||
@@ -27,6 +27,9 @@ struct PlayerAnimations;
|
||||
#[derive(Component, Default)]
|
||||
struct PlayerHead;
|
||||
|
||||
#[derive(Component, Default)]
|
||||
struct PlayerRig;
|
||||
|
||||
#[derive(Resource, Default)]
|
||||
struct PlayerSpawned {
|
||||
spawned: bool,
|
||||
@@ -101,6 +104,7 @@ fn spawn(
|
||||
))
|
||||
.with_child((
|
||||
Name::from("body rig"),
|
||||
PlayerRig,
|
||||
Transform::from_translation(Vec3::new(0., -3., 0.))
|
||||
.with_rotation(Quat::from_rotation_y(std::f32::consts::PI))
|
||||
.with_scale(Vec3::splat(1.5)),
|
||||
@@ -117,14 +121,13 @@ fn spawn(
|
||||
|
||||
fn cursor_events(
|
||||
mut mouse: EventReader<MouseMotion>,
|
||||
mut player: Query<&mut Transform, With<Player>>,
|
||||
// todo: Put the player head as a child of the rig to avoid this mess:
|
||||
mut player: Query<&mut Transform, Or<(With<PlayerRig>, With<PlayerHead>)>>,
|
||||
) {
|
||||
let Ok(mut player) = player.get_single_mut() else {
|
||||
return;
|
||||
};
|
||||
|
||||
for ev in mouse.read() {
|
||||
player.rotate_y(ev.delta.x * -0.001);
|
||||
for mut tr in &mut player {
|
||||
tr.rotate_y(ev.delta.x * -0.001);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -152,7 +155,7 @@ fn initial_grab_cursor(mut primary_window: Query<&mut Window, With<PrimaryWindow
|
||||
fn apply_controls(
|
||||
keyboard: Res<ButtonInput<KeyCode>>,
|
||||
mut query: Query<&mut TnuaController>,
|
||||
player: Query<&Transform, With<Player>>,
|
||||
player: Query<&Transform, With<PlayerRig>>,
|
||||
mut controls: ResMut<PlayerMovement>,
|
||||
) {
|
||||
let Ok(mut controller) = query.get_single_mut() else {
|
||||
@@ -166,16 +169,16 @@ fn apply_controls(
|
||||
let mut direction = Vec3::ZERO;
|
||||
|
||||
if keyboard.pressed(KeyCode::KeyW) {
|
||||
direction = player.forward().as_vec3();
|
||||
}
|
||||
if keyboard.pressed(KeyCode::KeyS) {
|
||||
direction = player.forward().as_vec3() * -1.;
|
||||
}
|
||||
if keyboard.pressed(KeyCode::KeyS) {
|
||||
direction = player.forward().as_vec3();
|
||||
}
|
||||
if keyboard.pressed(KeyCode::KeyA) {
|
||||
direction += player.left().as_vec3();
|
||||
direction += player.right().as_vec3();
|
||||
}
|
||||
if keyboard.pressed(KeyCode::KeyD) {
|
||||
direction += player.right().as_vec3();
|
||||
direction += player.left().as_vec3();
|
||||
}
|
||||
|
||||
if controls.any_direction != (direction != Vec3::ZERO) {
|
||||
@@ -206,7 +209,7 @@ fn apply_controls(
|
||||
}
|
||||
|
||||
fn update_camera(
|
||||
player: Query<&Transform, With<Player>>,
|
||||
player: Query<&GlobalTransform, With<PlayerRig>>,
|
||||
mut rig: Single<&mut Rig>,
|
||||
res: Res<DebugVisuals>,
|
||||
) {
|
||||
@@ -218,8 +221,10 @@ fn update_camera(
|
||||
return;
|
||||
}
|
||||
|
||||
rig.driver_mut::<GameCameraRig>()
|
||||
.set_position_target(player.translation, player.rotation);
|
||||
rig.driver_mut::<GameCameraRig>().set_position_target(
|
||||
player.translation(),
|
||||
player.rotation() * Quat::from_rotation_y(PI),
|
||||
);
|
||||
}
|
||||
|
||||
fn collect_cash(
|
||||
|
||||
Reference in New Issue
Block a user