refactor: move player code to separate file
Former-commit-id: cbb76f5546b8f782f0d86ed219f27b4cf2367f7a
This commit is contained in:
parent
7101a52cc6
commit
992d386845
70
src/main.rs
70
src/main.rs
|
@ -8,6 +8,9 @@ use std::collections::HashMap;
|
|||
mod helpers;
|
||||
use crate::helpers::*;
|
||||
|
||||
mod player;
|
||||
use crate::player::*;
|
||||
|
||||
const VERSION: &str = env!("CARGO_PKG_VERSION");
|
||||
const PKGNAME: &str = env!("CARGO_PKG_NAME");
|
||||
|
||||
|
@ -67,13 +70,6 @@ struct OpenWindows {
|
|||
login_open: bool,
|
||||
}
|
||||
|
||||
// Define the player component
|
||||
#[derive(Component)]
|
||||
struct Player {
|
||||
movement_speed: f32,
|
||||
rotation_speed: f32,
|
||||
}
|
||||
|
||||
// Bevy engine setup
|
||||
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
|
||||
commands.spawn(Camera2dBundle {
|
||||
|
@ -112,66 +108,6 @@ fn setup_ui(mut contexts: EguiContexts) {
|
|||
contexts.ctx_mut().set_fonts(fonts);
|
||||
}
|
||||
|
||||
// Define the player movement system
|
||||
fn movement(
|
||||
time: Res<Time>,
|
||||
keys: Res<Input<KeyCode>>,
|
||||
mut windows: Query<&mut Window>,
|
||||
mut query: Query<(&Player, &mut Transform)>,
|
||||
) {
|
||||
let (player, mut transform) = query.single_mut();
|
||||
|
||||
let mut rotation_factor = 0.;
|
||||
let mut movement_factor = 0.;
|
||||
let mut blink_factor = 0.;
|
||||
|
||||
if keys.pressed(KeyCode::W) {
|
||||
movement_factor += 1.;
|
||||
}
|
||||
if keys.pressed(KeyCode::S) {
|
||||
movement_factor -= 1.;
|
||||
}
|
||||
if keys.pressed(KeyCode::A) {
|
||||
rotation_factor += 1.;
|
||||
}
|
||||
if keys.pressed(KeyCode::D) {
|
||||
rotation_factor -= 1.;
|
||||
}
|
||||
if keys.pressed(KeyCode::Space) {
|
||||
blink_factor += 4.;
|
||||
}
|
||||
if keys.pressed(KeyCode::Space) && keys.just_released(KeyCode::Right) {
|
||||
blink_factor += 4.;
|
||||
};
|
||||
|
||||
// Get the player's *forward* vector
|
||||
let movement_direction = transform.rotation * Vec3::Y;
|
||||
|
||||
// Initialise the movement distance variable (to bring it into scope)
|
||||
let movement_distance: f32;
|
||||
|
||||
if blink_factor == 0. {
|
||||
movement_distance = movement_factor * player.movement_speed * time.delta_seconds();
|
||||
// Change the player rotation around the Z-axis only if not blinking
|
||||
transform.rotate_z(rotation_factor * player.rotation_speed * time.delta_seconds());
|
||||
} else {
|
||||
movement_distance = blink_factor * player.movement_speed * 0.01;
|
||||
}
|
||||
|
||||
// Create the translation using the movement direction and distance
|
||||
let translation_delta = movement_direction * movement_distance;
|
||||
// Update the player translation with the created translation
|
||||
transform.translation += translation_delta;
|
||||
|
||||
// Define the bounds of play (the window size)
|
||||
let window = windows.single_mut();
|
||||
let bounds = Vec3::from((
|
||||
Vec2::new(window.resolution.width(), window.resolution.height()) / 2.,
|
||||
0.,
|
||||
));
|
||||
transform.translation = transform.translation.min(bounds).max(-bounds);
|
||||
}
|
||||
|
||||
// On update: render the UI
|
||||
fn render_ui(
|
||||
mut contexts: EguiContexts,
|
||||
|
|
68
src/player.rs
Normal file
68
src/player.rs
Normal file
|
@ -0,0 +1,68 @@
|
|||
use bevy::{prelude::*, window::*};
|
||||
|
||||
// Define the player component
|
||||
#[derive(Component)]
|
||||
pub struct Player {
|
||||
pub movement_speed: f32,
|
||||
pub rotation_speed: f32,
|
||||
}
|
||||
|
||||
// Define the player movement system
|
||||
pub fn movement(
|
||||
time: Res<Time>,
|
||||
keys: Res<Input<KeyCode>>,
|
||||
mut windows: Query<&mut Window>,
|
||||
mut query: Query<(&Player, &mut Transform)>,
|
||||
) {
|
||||
let (player, mut transform) = query.single_mut();
|
||||
|
||||
let mut rotation_factor = 0.;
|
||||
let mut movement_factor = 0.;
|
||||
let mut blink_factor = 0.;
|
||||
|
||||
if keys.pressed(KeyCode::W) {
|
||||
movement_factor += 1.;
|
||||
}
|
||||
if keys.pressed(KeyCode::S) {
|
||||
movement_factor -= 1.;
|
||||
}
|
||||
if keys.pressed(KeyCode::A) {
|
||||
rotation_factor += 1.;
|
||||
}
|
||||
if keys.pressed(KeyCode::D) {
|
||||
rotation_factor -= 1.;
|
||||
}
|
||||
if keys.pressed(KeyCode::Space) {
|
||||
blink_factor += 4.;
|
||||
}
|
||||
if keys.pressed(KeyCode::Space) && keys.just_released(KeyCode::Right) {
|
||||
blink_factor += 4.;
|
||||
};
|
||||
|
||||
// Get the player's *forward* vector
|
||||
let movement_direction = transform.rotation * Vec3::Y;
|
||||
|
||||
// Initialise the movement distance variable (to bring it into scope)
|
||||
let movement_distance: f32;
|
||||
|
||||
if blink_factor == 0. {
|
||||
movement_distance = movement_factor * player.movement_speed * time.delta_seconds();
|
||||
// Change the player rotation around the Z-axis only if not blinking
|
||||
transform.rotate_z(rotation_factor * player.rotation_speed * time.delta_seconds());
|
||||
} else {
|
||||
movement_distance = blink_factor * player.movement_speed * 0.01;
|
||||
}
|
||||
|
||||
// Create the translation using the movement direction and distance
|
||||
let translation_delta = movement_direction * movement_distance;
|
||||
// Update the player translation with the created translation
|
||||
transform.translation += translation_delta;
|
||||
|
||||
// Define the bounds of play (the window size)
|
||||
let window = windows.single_mut();
|
||||
let bounds = Vec3::from((
|
||||
Vec2::new(window.resolution.width(), window.resolution.height()) / 2.,
|
||||
0.,
|
||||
));
|
||||
transform.translation = transform.translation.min(bounds).max(-bounds);
|
||||
}
|
Reference in a new issue