beginning of interaction
This commit is contained in:
@@ -52,6 +52,7 @@ pub fn init_player(
|
||||
commands
|
||||
.spawn((
|
||||
Player,
|
||||
PlayerAction::default(),
|
||||
CameraSensitivity::default(),
|
||||
Transform::from_xyz(0.0, 1.0, 0.0),
|
||||
Visibility::default(),
|
||||
@@ -124,37 +125,62 @@ pub fn move_camera(
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Component, Debug, Clone, Copy, Eq, PartialEq, Hash, Reflect, Default)]
|
||||
pub(crate) enum PlayerAction {
|
||||
#[default]
|
||||
Move,
|
||||
Sprint,
|
||||
Jump,
|
||||
Interact,
|
||||
}
|
||||
|
||||
pub fn handle_input(
|
||||
keyboard_input: Res<ButtonInput<KeyCode>>,
|
||||
mut query: Query<(&Transform, &mut AccumulatedInput, &mut Velocity), With<Player>>,
|
||||
mut query: Query<
|
||||
(
|
||||
&Transform,
|
||||
&mut AccumulatedInput,
|
||||
&mut Velocity,
|
||||
&mut PlayerAction,
|
||||
),
|
||||
With<Player>,
|
||||
>,
|
||||
) {
|
||||
const SPEED: f32 = 2.0;
|
||||
for (transform, mut input, mut velocity) in query.iter_mut() {
|
||||
for (transform, mut input, mut velocity, mut action) in query.iter_mut() {
|
||||
let forward = transform.forward(); // Forward direction (z axis)
|
||||
let right = transform.right(); // Right direction (x axis)
|
||||
|
||||
if keyboard_input.pressed(KeyCode::KeyW) {
|
||||
input.x += forward.x;
|
||||
input.z += forward.z;
|
||||
*action = PlayerAction::Move;
|
||||
}
|
||||
if keyboard_input.pressed(KeyCode::KeyS) {
|
||||
input.x -= forward.x;
|
||||
input.z -= forward.z;
|
||||
*action = PlayerAction::Move;
|
||||
}
|
||||
if keyboard_input.pressed(KeyCode::KeyA) {
|
||||
input.x -= right.x;
|
||||
input.z -= right.z;
|
||||
*action = PlayerAction::Move;
|
||||
}
|
||||
if keyboard_input.pressed(KeyCode::KeyD) {
|
||||
input.x += right.x;
|
||||
input.z += right.z;
|
||||
*action = PlayerAction::Move;
|
||||
}
|
||||
if keyboard_input.pressed(KeyCode::Space) {
|
||||
input.y += 1.0;
|
||||
*action = PlayerAction::Jump;
|
||||
}
|
||||
if keyboard_input.pressed(KeyCode::ShiftLeft) {
|
||||
input.y -= 1.0;
|
||||
}
|
||||
if keyboard_input.pressed(KeyCode::KeyE) {
|
||||
*action = PlayerAction::Interact
|
||||
}
|
||||
velocity.0 = input.normalize_or_zero() * SPEED;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user