Move world into separate file/plugin

This commit is contained in:
Felix Ableitner 2020-12-31 16:41:56 +01:00
parent 88063a3fc9
commit 0db4cdafb7
4 changed files with 63 additions and 43 deletions

View file

@ -1 +1,2 @@
pub mod player; pub mod player;
pub mod world;

View file

@ -1,9 +1,12 @@
use bevy::prelude::*; use bevy::{input::system::exit_on_esc_system, prelude::*};
use rake::player::{Player, PlayerPlugin}; use bevy_rapier3d::{
use bevy_rapier3d::rapier::dynamics::RigidBodyBuilder; physics::RapierPhysicsPlugin,
use bevy_rapier3d::rapier::geometry::ColliderBuilder; rapier::{dynamics::RigidBodyBuilder, geometry::ColliderBuilder},
use bevy_rapier3d::physics::RapierPhysicsPlugin; };
use bevy::input::system::exit_on_esc_system; use rake::{
player::{Player, PlayerPlugin},
world::WorldPlugin,
};
fn main() { fn main() {
App::build() App::build()
@ -19,16 +22,13 @@ fn main() {
// note: debug renderer doesnt work for cylinder // note: debug renderer doesnt work for cylinder
//.add_plugin(RapierRenderPlugin) //.add_plugin(RapierRenderPlugin)
.add_plugin(PlayerPlugin) .add_plugin(PlayerPlugin)
.add_plugin(WorldPlugin)
.add_startup_system(init.system()) .add_startup_system(init.system())
.add_system(exit_on_esc_system.system()) .add_system(exit_on_esc_system.system())
.run(); .run();
} }
fn init( fn init(commands: &mut Commands) {
commands: &mut Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
let player = Player::create(); let player = Player::create();
commands commands
.spawn(LightBundle { .spawn(LightBundle {
@ -42,32 +42,4 @@ fn init(
.with(player) .with(player)
.with(RigidBodyBuilder::new_dynamic().translation(0.0, 3.0, 0.0)) .with(RigidBodyBuilder::new_dynamic().translation(0.0, 3.0, 0.0))
.with(ColliderBuilder::cylinder(1.0, 0.5).friction(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()
});
} }

View file

@ -1,7 +1,9 @@
use bevy::{input::mouse::MouseMotion, math::clamp, prelude::*}; use bevy::{input::mouse::MouseMotion, math::clamp, prelude::*};
use bevy_rapier3d::rapier::dynamics::{RigidBody, RigidBodySet}; use bevy_rapier3d::{
use bevy_rapier3d::physics::RigidBodyHandleComponent; na::Matrix3x1,
use bevy_rapier3d::na::{Matrix3x1}; physics::RigidBodyHandleComponent,
rapier::dynamics::{RigidBody, RigidBodySet},
};
pub struct Player { pub struct Player {
/// The speed the FlyCamera moves at. Defaults to `1.0` /// 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(); let pitch_radians = options.pitch.to_radians();
transform.rotation = Quat::from_axis_angle(Vec3::unit_y(), yaw_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); body.set_angvel(Matrix3x1::new(pitch_radians, yaw_radians, 0.), true);
} }

44
src/world.rs Normal file
View file

@ -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<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
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()
});
}