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 rake::player::Player;
use rake::player::PlayerPlugin;
use std::process::exit;
use rake::player::{Player, PlayerPlugin};
fn main() {
App::build()
.add_resource(Msaa { samples: 4 })
.add_resource(WindowDescriptor {
title: "Rake".to_string(),
width: 960.,
height: 540.,
..Default::default()
})
.add_plugins(DefaultPlugins)
.add_startup_system(init.system())
// note: debug renderer doesnt work for cylinder
//.add_plugin(RapierRenderPlugin)
.add_plugin(PlayerPlugin)
.add_startup_system(init.system())
.add_system(exit_game_system.system())
.run();
}
@ -18,13 +24,17 @@ fn init(
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
let player = Player::create();
commands
.spawn(LightBundle {
transform: Transform::from_translation(Vec3::new(4.0, 8.0, 4.0)),
..Default::default()
})
.spawn(Camera3dBundle::default())
.with(Player::default());
.spawn(Camera3dBundle {
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_material = materials.add(Color::rgb(1.0, 0.2, 0.3).into());
@ -36,9 +46,7 @@ fn init(
commands.spawn(PbrBundle {
mesh: box_mesh.clone(),
material: box_material.clone(),
transform: Transform::from_translation(Vec3::new(
x as f32, y as f32, z as f32,
)),
transform: Transform::from_translation(Vec3::new(x as f32, y as f32, z as f32)),
..Default::default()
});
}
@ -49,13 +57,16 @@ fn init(
commands.spawn(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Plane { size: 10. })),
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()
});
}
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) {
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)
pub velocity: Vec3,
}
impl Default for Player {
fn default() -> Self {
impl Player {
pub fn create() -> Self {
Self {
speed: 1.5,
sensitivity: 6.0,
@ -31,24 +32,16 @@ fn forward_vector(rotation: &Quat) -> Vec3 {
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_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
Quat::from_rotation_y(90.0f32.to_radians())
.mul_vec3(forward_walk_vector(rotation))
.mul_vec3(f_flattened)
.normalize()
}
fn movement_axis(
input: &Res<Input<KeyCode>>,
plus: KeyCode,
minus: KeyCode,
) -> f32 {
fn movement_axis(input: &Res<Input<KeyCode>>, plus: KeyCode, minus: KeyCode) -> f32 {
let mut axis = 0.0;
if input.pressed(plus) {
axis += 1.0;
@ -64,45 +57,40 @@ fn camera_movement_system(
keyboard_input: Res<Input<KeyCode>>,
mut query: Query<(&mut Player, &mut Transform)>,
) {
for (mut options, mut transform) in query.iter_mut() {
let (axis_h, axis_v, axis_float) = (movement_axis(&keyboard_input, KeyCode::D, KeyCode::A),
movement_axis(
&keyboard_input,
KeyCode::S,
KeyCode::W,
),
movement_axis(&keyboard_input, KeyCode::Space, KeyCode::LControl),
);
for (mut player, mut transform) in query.iter_mut() {
let (axis_h, axis_v, axis_float) = (
movement_axis(&keyboard_input, KeyCode::D, KeyCode::A),
movement_axis(&keyboard_input, KeyCode::S, KeyCode::W),
movement_axis(&keyboard_input, KeyCode::Space, KeyCode::LControl),
);
let rotation = transform.rotation;
let accel: Vec3 = (strafe_vector(&rotation) * axis_h)
+ (forward_vector(&rotation) * axis_v)
+ (Vec3::unit_y() * axis_float);
let accel: Vec3 = if accel.length() != 0.0 {
accel.normalize() * options.speed
accel.normalize() * player.speed
} else {
Vec3::zero()
};
let friction: Vec3 = if options.velocity.length() != 0.0 {
options.velocity.normalize() * -1.0 * options.friction
let friction: Vec3 = if player.velocity.length() != 0.0 {
player.velocity.normalize() * -1.0 * player.friction
} else {
Vec3::zero()
};
options.velocity += accel * time.delta_seconds();
player.velocity += accel * time.delta_seconds();
let delta_friction = friction * time.delta_seconds();
options.velocity = if (options.velocity + delta_friction).signum()
!= options.velocity.signum()
{
player.velocity = if (player.velocity + delta_friction).signum() != player.velocity.signum() {
Vec3::zero()
} 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(mouse_motion_system.system());
}
}
}