some cleanup, do cargo fmt

This commit is contained in:
Felix Ableitner 2020-12-29 18:52:30 +01:00
parent 34e072c224
commit 464df31d43
4 changed files with 50 additions and 46 deletions

5
.rustfmt.toml Normal file
View file

@ -0,0 +1,5 @@
tab_spaces = 2
edition="2018"
imports_layout="HorizontalVertical"
merge_imports=true
reorder_imports=true

View file

@ -1 +1 @@
pub mod player; pub mod player;

View file

@ -1,14 +1,20 @@
use bevy::prelude::*; use bevy::prelude::*;
use rake::player::Player; use rake::player::{Player, PlayerPlugin};
use rake::player::PlayerPlugin;
use std::process::exit;
fn main() { fn main() {
App::build() App::build()
.add_resource(Msaa { samples: 4 }) .add_resource(Msaa { samples: 4 })
.add_resource(WindowDescriptor {
title: "Rake".to_string(),
width: 960.,
height: 540.,
..Default::default()
})
.add_plugins(DefaultPlugins) .add_plugins(DefaultPlugins)
.add_startup_system(init.system()) // note: debug renderer doesnt work for cylinder
//.add_plugin(RapierRenderPlugin)
.add_plugin(PlayerPlugin) .add_plugin(PlayerPlugin)
.add_startup_system(init.system())
.add_system(exit_game_system.system()) .add_system(exit_game_system.system())
.run(); .run();
} }
@ -18,13 +24,17 @@ fn init(
mut meshes: ResMut<Assets<Mesh>>, mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>, mut materials: ResMut<Assets<StandardMaterial>>,
) { ) {
let player = Player::create();
commands commands
.spawn(LightBundle { .spawn(LightBundle {
transform: Transform::from_translation(Vec3::new(4.0, 8.0, 4.0)), transform: Transform::from_translation(Vec3::new(4.0, 8.0, 4.0)),
..Default::default() ..Default::default()
}) })
.spawn(Camera3dBundle::default()) .spawn(Camera3dBundle {
.with(Player::default()); transform: Transform::from_translation(Vec3::new(0.0, 3.0, 3.0)),
..Default::default()
})
.with(player);
let box_mesh = meshes.add(Mesh::from(shape::Cube { size: 0.25 })); let box_mesh = meshes.add(Mesh::from(shape::Cube { size: 0.25 }));
let box_material = materials.add(Color::rgb(1.0, 0.2, 0.3).into()); let box_material = materials.add(Color::rgb(1.0, 0.2, 0.3).into());
@ -36,9 +46,7 @@ fn init(
commands.spawn(PbrBundle { commands.spawn(PbrBundle {
mesh: box_mesh.clone(), mesh: box_mesh.clone(),
material: box_material.clone(), material: box_material.clone(),
transform: Transform::from_translation(Vec3::new( transform: Transform::from_translation(Vec3::new(x as f32, y as f32, z as f32)),
x as f32, y as f32, z as f32,
)),
..Default::default() ..Default::default()
}); });
} }
@ -49,13 +57,16 @@ fn init(
commands.spawn(PbrBundle { commands.spawn(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Plane { size: 10. })), mesh: meshes.add(Mesh::from(shape::Plane { size: 10. })),
material: materials.add(Color::rgb(0.5, 0.9, 0.9).into()), material: materials.add(Color::rgb(0.5, 0.9, 0.9).into()),
transform: Transform::from_translation(Vec3::new(0.0, -3.0, 0.0)), transform: Transform::from_translation(Vec3::new(0.0, 0.2, 0.0)),
..Default::default() ..Default::default()
}); });
} }
fn exit_game_system(keyboard_input: Res<Input<KeyCode>>) { fn exit_game_system(
keyboard_input: Res<Input<KeyCode>>,
mut app_exit_events: ResMut<Events<bevy::app::AppExit>>,
) {
if keyboard_input.pressed(KeyCode::Escape) { if keyboard_input.pressed(KeyCode::Escape) {
exit(0); app_exit_events.send(bevy::app::AppExit);
} }
} }

View file

@ -14,8 +14,9 @@ pub struct Player {
/// The current velocity of the FlyCamera. This value is always up-to-date, enforced by [FlyCameraPlugin](struct.FlyCameraPlugin.html) /// The current velocity of the FlyCamera. This value is always up-to-date, enforced by [FlyCameraPlugin](struct.FlyCameraPlugin.html)
pub velocity: Vec3, pub velocity: Vec3,
} }
impl Default for Player {
fn default() -> Self { impl Player {
pub fn create() -> Self {
Self { Self {
speed: 1.5, speed: 1.5,
sensitivity: 6.0, sensitivity: 6.0,
@ -31,24 +32,16 @@ fn forward_vector(rotation: &Quat) -> Vec3 {
rotation.mul_vec3(Vec3::unit_z()).normalize() rotation.mul_vec3(Vec3::unit_z()).normalize()
} }
fn forward_walk_vector(rotation: &Quat) -> Vec3 { fn strafe_vector(rotation: &Quat) -> Vec3 {
let f = forward_vector(rotation); let f = forward_vector(rotation);
let f_flattened = Vec3::new(f.x, 0.0, f.z).normalize(); let f_flattened = Vec3::new(f.x, 0.0, f.z).normalize();
f_flattened
}
fn strafe_vector(rotation: &Quat) -> Vec3 {
// Rotate it 90 degrees to get the strafe direction // Rotate it 90 degrees to get the strafe direction
Quat::from_rotation_y(90.0f32.to_radians()) Quat::from_rotation_y(90.0f32.to_radians())
.mul_vec3(forward_walk_vector(rotation)) .mul_vec3(f_flattened)
.normalize() .normalize()
} }
fn movement_axis( fn movement_axis(input: &Res<Input<KeyCode>>, plus: KeyCode, minus: KeyCode) -> f32 {
input: &Res<Input<KeyCode>>,
plus: KeyCode,
minus: KeyCode,
) -> f32 {
let mut axis = 0.0; let mut axis = 0.0;
if input.pressed(plus) { if input.pressed(plus) {
axis += 1.0; axis += 1.0;
@ -64,45 +57,40 @@ fn camera_movement_system(
keyboard_input: Res<Input<KeyCode>>, keyboard_input: Res<Input<KeyCode>>,
mut query: Query<(&mut Player, &mut Transform)>, mut query: Query<(&mut Player, &mut Transform)>,
) { ) {
for (mut options, mut transform) in query.iter_mut() { for (mut player, mut transform) in query.iter_mut() {
let (axis_h, axis_v, axis_float) = (movement_axis(&keyboard_input, KeyCode::D, KeyCode::A), let (axis_h, axis_v, axis_float) = (
movement_axis( movement_axis(&keyboard_input, KeyCode::D, KeyCode::A),
&keyboard_input, movement_axis(&keyboard_input, KeyCode::S, KeyCode::W),
KeyCode::S, movement_axis(&keyboard_input, KeyCode::Space, KeyCode::LControl),
KeyCode::W, );
),
movement_axis(&keyboard_input, KeyCode::Space, KeyCode::LControl),
);
let rotation = transform.rotation; let rotation = transform.rotation;
let accel: Vec3 = (strafe_vector(&rotation) * axis_h) let accel: Vec3 = (strafe_vector(&rotation) * axis_h)
+ (forward_vector(&rotation) * axis_v) + (forward_vector(&rotation) * axis_v)
+ (Vec3::unit_y() * axis_float); + (Vec3::unit_y() * axis_float);
let accel: Vec3 = if accel.length() != 0.0 { let accel: Vec3 = if accel.length() != 0.0 {
accel.normalize() * options.speed accel.normalize() * player.speed
} else { } else {
Vec3::zero() Vec3::zero()
}; };
let friction: Vec3 = if options.velocity.length() != 0.0 { let friction: Vec3 = if player.velocity.length() != 0.0 {
options.velocity.normalize() * -1.0 * options.friction player.velocity.normalize() * -1.0 * player.friction
} else { } else {
Vec3::zero() Vec3::zero()
}; };
options.velocity += accel * time.delta_seconds(); player.velocity += accel * time.delta_seconds();
let delta_friction = friction * time.delta_seconds(); let delta_friction = friction * time.delta_seconds();
options.velocity = if (options.velocity + delta_friction).signum() player.velocity = if (player.velocity + delta_friction).signum() != player.velocity.signum() {
!= options.velocity.signum()
{
Vec3::zero() Vec3::zero()
} else { } else {
options.velocity + delta_friction player.velocity + delta_friction
}; };
transform.translation += options.velocity; transform.translation += player.velocity;
} }
} }
@ -149,4 +137,4 @@ impl Plugin for PlayerPlugin {
.add_system(camera_movement_system.system()) .add_system(camera_movement_system.system())
.add_system(mouse_motion_system.system()); .add_system(mouse_motion_system.system());
} }
} }