From 0db4cdafb74351ae3ae85080eada029ba68fe42b Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Thu, 31 Dec 2020 16:41:56 +0100 Subject: [PATCH] Move world into separate file/plugin --- src/lib.rs | 1 + src/main.rs | 50 +++++++++++--------------------------------------- src/player.rs | 11 +++++++---- src/world.rs | 44 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 63 insertions(+), 43 deletions(-) create mode 100644 src/world.rs diff --git a/src/lib.rs b/src/lib.rs index f28d7c2..50f34a8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1 +1,2 @@ pub mod player; +pub mod world; diff --git a/src/main.rs b/src/main.rs index 8161a04..8147798 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,12 @@ -use bevy::prelude::*; -use rake::player::{Player, PlayerPlugin}; -use bevy_rapier3d::rapier::dynamics::RigidBodyBuilder; -use bevy_rapier3d::rapier::geometry::ColliderBuilder; -use bevy_rapier3d::physics::RapierPhysicsPlugin; -use bevy::input::system::exit_on_esc_system; +use bevy::{input::system::exit_on_esc_system, prelude::*}; +use bevy_rapier3d::{ + physics::RapierPhysicsPlugin, + rapier::{dynamics::RigidBodyBuilder, geometry::ColliderBuilder}, +}; +use rake::{ + player::{Player, PlayerPlugin}, + world::WorldPlugin, +}; fn main() { App::build() @@ -19,16 +22,13 @@ fn main() { // note: debug renderer doesnt work for cylinder //.add_plugin(RapierRenderPlugin) .add_plugin(PlayerPlugin) + .add_plugin(WorldPlugin) .add_startup_system(init.system()) .add_system(exit_on_esc_system.system()) .run(); } -fn init( - commands: &mut Commands, - mut meshes: ResMut>, - mut materials: ResMut>, -) { +fn init(commands: &mut Commands) { let player = Player::create(); commands .spawn(LightBundle { @@ -42,32 +42,4 @@ fn init( .with(player) .with(RigidBodyBuilder::new_dynamic().translation(0.0, 3.0, 0.0)) .with(ColliderBuilder::cylinder(1.0, 0.5).friction(0.0)); - - 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()); - - const AMOUNT: i32 = 6; - for x in -(AMOUNT / 2)..(AMOUNT / 2) { - for y in -(AMOUNT / 2)..(AMOUNT / 2) { - for z in -(AMOUNT / 2)..(AMOUNT / 2) { - 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)), - ..Default::default() - }); - } - } - } - - // create a floor - let rigid_body1 = RigidBodyBuilder::new_static(); - let collider1 = ColliderBuilder::cuboid(10.0, 1.0, 10.0); - commands.spawn((rigid_body1, collider1)); - 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, 0.2, 0.0)), - ..Default::default() - }); } diff --git a/src/player.rs b/src/player.rs index 03d6d67..6e1522a 100644 --- a/src/player.rs +++ b/src/player.rs @@ -1,7 +1,9 @@ use bevy::{input::mouse::MouseMotion, math::clamp, prelude::*}; -use bevy_rapier3d::rapier::dynamics::{RigidBody, RigidBodySet}; -use bevy_rapier3d::physics::RigidBodyHandleComponent; -use bevy_rapier3d::na::{Matrix3x1}; +use bevy_rapier3d::{ + na::Matrix3x1, + physics::RigidBodyHandleComponent, + rapier::dynamics::{RigidBody, RigidBodySet}, +}; pub struct Player { /// The speed the FlyCamera moves at. Defaults to `1.0` @@ -132,7 +134,8 @@ fn mouse_motion_system( let pitch_radians = options.pitch.to_radians(); transform.rotation = Quat::from_axis_angle(Vec3::unit_y(), yaw_radians) - * Quat::from_axis_angle(-Vec3::unit_x(), pitch_radians) * 10.; + * Quat::from_axis_angle(-Vec3::unit_x(), pitch_radians) + * 10.; body.set_angvel(Matrix3x1::new(pitch_radians, yaw_radians, 0.), true); } diff --git a/src/world.rs b/src/world.rs new file mode 100644 index 0000000..ef6a0d1 --- /dev/null +++ b/src/world.rs @@ -0,0 +1,44 @@ +use bevy::prelude::*; +use bevy_rapier3d::rapier::{dynamics::RigidBodyBuilder, geometry::ColliderBuilder}; + +pub struct WorldPlugin; + +impl Plugin for WorldPlugin { + fn build(&self, app: &mut AppBuilder) { + app.add_startup_system(create_world.system()); + } +} + +fn create_world( + commands: &mut Commands, + mut meshes: ResMut>, + mut materials: ResMut>, +) { + 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()); + + const AMOUNT: i32 = 6; + for x in -(AMOUNT / 2)..(AMOUNT / 2) { + for y in -(AMOUNT / 2)..(AMOUNT / 2) { + for z in -(AMOUNT / 2)..(AMOUNT / 2) { + 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)), + ..Default::default() + }); + } + } + } + + // create a floor + let rigid_body1 = RigidBodyBuilder::new_static(); + let collider1 = ColliderBuilder::cuboid(10.0, 1.0, 10.0); + commands.spawn((rigid_body1, collider1)); + 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, 0.2, 0.0)), + ..Default::default() + }); +}