move everything into player/world files, disable external player rotation

This commit is contained in:
Felix Ableitner 2021-01-01 01:03:43 +01:00
parent fb312eb557
commit db8f352517
3 changed files with 36 additions and 34 deletions

View file

@ -1,12 +1,6 @@
use bevy::{input::system::exit_on_esc_system, prelude::*}; use bevy::{input::system::exit_on_esc_system, prelude::*};
use bevy_rapier3d::{ use bevy_rapier3d::physics::RapierPhysicsPlugin;
physics::RapierPhysicsPlugin, use rake::{player::PlayerPlugin, world::WorldPlugin};
rapier::{dynamics::RigidBodyBuilder, geometry::ColliderBuilder},
};
use rake::{
player::{Player, PlayerPlugin},
world::WorldPlugin,
};
fn main() { fn main() {
App::build() App::build()
@ -23,23 +17,6 @@ fn main() {
//.add_plugin(RapierRenderPlugin) //.add_plugin(RapierRenderPlugin)
.add_plugin(PlayerPlugin) .add_plugin(PlayerPlugin)
.add_plugin(WorldPlugin) .add_plugin(WorldPlugin)
.add_startup_system(init.system())
.add_system(exit_on_esc_system.system()) .add_system(exit_on_esc_system.system())
.run(); .run();
} }
fn init(commands: &mut Commands) {
let player = Player::create();
commands
.spawn(LightBundle {
transform: Transform::from_translation(Vec3::new(4.0, 8.0, 4.0)),
..Default::default()
})
.spawn(Camera3dBundle {
transform: Transform::from_translation(Vec3::new(0.0, 3.0, 3.0)),
..Default::default()
})
.with(player)
.with(RigidBodyBuilder::new_dynamic().translation(0.0, 3.0, 0.0))
.with(ColliderBuilder::cylinder(1.0, 0.5).friction(0.0));
}

View file

@ -2,16 +2,19 @@ use bevy::{input::mouse::MouseMotion, math::clamp, prelude::*};
use bevy_rapier3d::{ use bevy_rapier3d::{
na::Matrix3x1, na::Matrix3x1,
physics::RigidBodyHandleComponent, physics::RigidBodyHandleComponent,
rapier::dynamics::{RigidBody, RigidBodySet}, rapier::{
dynamics::{RigidBody, RigidBodyBuilder, RigidBodySet},
geometry::ColliderBuilder,
},
}; };
pub struct Player { struct Player {
/// The current pitch of the FlyCamera in degrees. This value is always up-to-date, enforced by [FlyCameraPlugin](struct.FlyCameraPlugin.html) /// The current pitch of the FlyCamera in degrees. This value is always up-to-date, enforced by [FlyCameraPlugin](struct.FlyCameraPlugin.html)
pub pitch: f32, pitch: f32,
/// The current pitch of the FlyCamera in degrees. This value is always up-to-date, enforced by [FlyCameraPlugin](struct.FlyCameraPlugin.html) /// The current pitch of the FlyCamera in degrees. This value is always up-to-date, enforced by [FlyCameraPlugin](struct.FlyCameraPlugin.html)
pub yaw: f32, yaw: f32,
/// 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, velocity: Vec3,
} }
const PLAYER_SPEED: f32 = 15.0; const PLAYER_SPEED: f32 = 15.0;
@ -19,7 +22,7 @@ const PLAYER_FRICTION: f32 = 1.0;
const MOUSE_SENSITIVITY: f32 = 6.0; const MOUSE_SENSITIVITY: f32 = 6.0;
impl Player { impl Player {
pub fn create() -> Self { fn create() -> Self {
Self { Self {
pitch: 0.0, pitch: 0.0,
yaw: 0.0, yaw: 0.0,
@ -143,6 +146,22 @@ impl Plugin for PlayerPlugin {
app app
.init_resource::<State>() .init_resource::<State>()
.add_system(camera_movement_system.system()) .add_system(camera_movement_system.system())
.add_system(mouse_motion_system.system()); .add_system(mouse_motion_system.system())
.add_startup_system(init_player.system());
} }
} }
fn init_player(commands: &mut Commands) {
commands
.spawn(Camera3dBundle {
transform: Transform::from_translation(Vec3::new(0.0, 3.0, 3.0)),
..Default::default()
})
.with(Player::create())
.with(
RigidBodyBuilder::new_dynamic()
.translation(0.0, 3.0, 0.0)
.lock_rotations(),
)
.with(ColliderBuilder::cylinder(1.0, 0.5).friction(0.0));
}

View file

@ -13,11 +13,11 @@ fn create_world(
commands: &mut Commands, commands: &mut Commands,
mut meshes: ResMut<Assets<Mesh>>, mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>, mut materials: ResMut<Assets<StandardMaterial>>,
asset_server: Res<AssetServer> asset_server: Res<AssetServer>,
) { ) {
// some boxes to help with orientation
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());
const AMOUNT: i32 = 6; const AMOUNT: i32 = 6;
for x in -(AMOUNT / 2)..(AMOUNT / 2) { for x in -(AMOUNT / 2)..(AMOUNT / 2) {
for y in -(AMOUNT / 2)..(AMOUNT / 2) { for y in -(AMOUNT / 2)..(AMOUNT / 2) {
@ -32,6 +32,12 @@ fn create_world(
} }
} }
// a simple light
commands.spawn(LightBundle {
transform: Transform::from_translation(Vec3::new(4.0, 8.0, 4.0)),
..Default::default()
});
// create a floor // create a floor
let floor_texture_handle = materials.add(asset_server.load("textures/blocks17floor2.png").into()); let floor_texture_handle = materials.add(asset_server.load("textures/blocks17floor2.png").into());
let rigid_body1 = RigidBodyBuilder::new_static(); let rigid_body1 = RigidBodyBuilder::new_static();