rustfmt examples

This commit is contained in:
Carter Anderson 2020-01-11 01:59:39 -08:00
parent fdbe42dd7d
commit ee03942e40
6 changed files with 279 additions and 234 deletions

View file

@ -1,7 +1,5 @@
use bevy::*;
fn main() {
AppBuilder::new()
.add_defaults()
.run();
}
AppBuilder::new().add_defaults().run();
}

View file

@ -1,6 +1,10 @@
use bevy::*;
use bevy::{render::*, asset::{Asset, AssetStorage, Handle}, math::{Mat4, Vec3}, Schedulable};
use rand::{rngs::StdRng, Rng, SeedableRng, random};
use bevy::{
asset::{Asset, AssetStorage, Handle},
math::{Mat4, Vec3},
render::*,
Schedulable, *,
};
use rand::{random, rngs::StdRng, Rng, SeedableRng};
use std::collections::VecDeque;
struct Person;
@ -21,16 +25,16 @@ struct Wander {
}
fn main() {
AppBuilder::new()
.add_defaults()
.setup(&setup)
.run();
AppBuilder::new().add_defaults().setup(&setup).run();
}
fn setup(world: &mut World, scheduler: &mut SystemScheduler<AppStage>) {
let cube = Mesh::load(MeshType::Cube);
let cube_handle = {
let mut mesh_storage = world.resources.get_mut::<AssetStorage<Mesh, MeshType>>().unwrap();
let mut mesh_storage = world
.resources
.get_mut::<AssetStorage<Mesh, MeshType>>()
.unwrap();
mesh_storage.add(cube, "cube")
};
@ -41,54 +45,64 @@ fn setup(world: &mut World, scheduler: &mut SystemScheduler<AppStage>) {
scheduler.add_system(AppStage::Update, build_move_system());
scheduler.add_system(AppStage::Update, build_print_status_system());
world.insert((), vec![
// lights
(
Light {
color: wgpu::Color {
r: 0.8,
g: 0.8,
b: 0.5,
a: 1.0,
world.insert(
(),
vec![
// lights
(
Light {
color: wgpu::Color {
r: 0.8,
g: 0.8,
b: 0.5,
a: 1.0,
},
fov: f32::to_radians(60.0),
depth: 0.1..50.0,
target_view: None,
},
fov: f32::to_radians(60.0),
depth: 0.1 .. 50.0,
target_view: None,
},
Material::new(math::vec4(0.5, 0.3, 0.3, 1.0)),
LocalToWorld::identity(),
Translation::new(4.0, -4.0, 5.0),
Rotation::from_euler_angles(0.0, 0.0, 0.0)
),
]);
Material::new(math::vec4(0.5, 0.3, 0.3, 1.0)),
LocalToWorld::identity(),
Translation::new(4.0, -4.0, 5.0),
Rotation::from_euler_angles(0.0, 0.0, 0.0),
),
],
);
world.insert((), vec![
// camera
(
Camera::new(CameraType::Projection {
fov: std::f32::consts::PI / 4.0,
near: 1.0,
far: 1000.0,
aspect_ratio: 1.0,
}),
ActiveCamera,
LocalToWorld(Mat4::look_at_rh(
Vec3::new(6.0, -40.0, 20.0),
Vec3::new(0.0, 0.0, 0.0),
Vec3::new(0.0, 0.0, 1.0),)),
)
]);
world.insert(
(),
vec![
// camera
(
Camera::new(CameraType::Projection {
fov: std::f32::consts::PI / 4.0,
near: 1.0,
far: 1000.0,
aspect_ratio: 1.0,
}),
ActiveCamera,
LocalToWorld(Mat4::look_at_rh(
Vec3::new(6.0, -40.0, 20.0),
Vec3::new(0.0, 0.0, 0.0),
Vec3::new(0.0, 0.0, 1.0),
)),
),
],
);
let mut rng = StdRng::from_entropy();
for _ in 0 .. 70000 {
create_person(world, cube_handle.clone(),
Translation::new(rng.gen_range(-50.0, 50.0), 0.0, rng.gen_range(-50.0, 50.0)));
for _ in 0..70000 {
create_person(
world,
cube_handle.clone(),
Translation::new(rng.gen_range(-50.0, 50.0), 0.0, rng.gen_range(-50.0, 50.0)),
);
}
}
fn build_wander_system() -> Box<dyn Schedulable> {
let mut rng = StdRng::from_entropy();
SystemBuilder::new("Wander")
.read_resource::<Time>()
.with_query(<(
@ -97,10 +111,7 @@ fn build_wander_system() -> Box<dyn Schedulable> {
Write<Wander>,
Write<NavigationPoint>,
)>::query())
.build(move |
_, world,
time ,
person_query| {
.build(move |_, world, time, person_query| {
for (_, translation, mut wander, mut navigation_point) in person_query.iter(world) {
wander.elapsed += time.delta_seconds;
if wander.elapsed >= wander.duration {
@ -108,11 +119,14 @@ fn build_wander_system() -> Box<dyn Schedulable> {
rng.gen_range(-1.0, 1.0),
rng.gen_range(-1.0, 1.0),
rng.gen_range(0.0, 0.001),
).normalize();
let distance = rng.gen_range(wander.distance_bounds.x(), wander.distance_bounds.y());
)
.normalize();
let distance =
rng.gen_range(wander.distance_bounds.x(), wander.distance_bounds.y());
navigation_point.target = translation.0 + direction * distance;
wander.elapsed = 0.0;
wander.duration = rng.gen_range(wander.duration_bounds.x(), wander.duration_bounds.y());
wander.duration =
rng.gen_range(wander.duration_bounds.x(), wander.duration_bounds.y());
}
}
})
@ -126,9 +140,7 @@ fn build_navigate_system() -> Box<dyn Schedulable> {
Write<Velocity>,
Write<NavigationPoint>,
)>::query())
.build(move |
_, world,
_, person_query| {
.build(move |_, world, _, person_query| {
for (_, translation, mut velocity, navigation_point) in person_query.iter(world) {
let distance = navigation_point.target - translation.0;
if distance.length() > 0.01 {
@ -144,11 +156,8 @@ fn build_navigate_system() -> Box<dyn Schedulable> {
fn build_move_system() -> Box<dyn Schedulable> {
SystemBuilder::new("Move")
.read_resource::<Time>()
.with_query(<(
Write<Translation>,
Read<Velocity>,
)>::query())
.build(move |_, world, time , person_query| {
.with_query(<(Write<Translation>, Read<Velocity>)>::query())
.build(move |_, world, time, person_query| {
for (mut translation, velocity) in person_query.iter(world) {
translation.0 += velocity.value * time.delta_seconds;
}
@ -163,10 +172,8 @@ fn build_print_status_system() -> Box<dyn Schedulable> {
let mut frame_time_values = VecDeque::new();
SystemBuilder::new("PrintStatus")
.read_resource::<Time>()
.with_query(<(
Read<Person>,
)>::query())
.build(move |_, world, time , person_query| {
.with_query(<(Read<Person>,)>::query())
.build(move |_, world, time, person_query| {
elapsed += time.delta_seconds;
frame_time_values.push_front(time.delta_seconds);
frame_time_total += time.delta_seconds;
@ -177,8 +184,11 @@ fn build_print_status_system() -> Box<dyn Schedulable> {
}
if elapsed > 1.0 {
// println!("fps: {}", if time.delta_seconds == 0.0 { 0.0 } else { 1.0 / time.delta_seconds });
if frame_time_count > 0 && frame_time_total > 0.0 {
println!("fps: {}", 1.0 / (frame_time_total / frame_time_count as f32))
if frame_time_count > 0 && frame_time_total > 0.0 {
println!(
"fps: {}",
1.0 / (frame_time_total / frame_time_count as f32)
)
}
println!("peeps: {}", person_query.iter(world).count());
elapsed = 0.0;
@ -187,9 +197,10 @@ fn build_print_status_system() -> Box<dyn Schedulable> {
}
fn create_person(world: &mut World, mesh_handle: Handle<Mesh>, translation: Translation) {
world.insert((), vec![
(
Person{},
world.insert(
(),
vec![(
Person {},
Wander {
duration_bounds: math::vec2(3.0, 10.0),
distance_bounds: math::vec2(-50.0, 50.0),
@ -206,7 +217,7 @@ fn create_person(world: &mut World, mesh_handle: Handle<Mesh>, translation: Tran
Material::new(math::vec4(0.5, 0.3, 0.3, 1.0) * random::<f32>()),
mesh_handle,
LocalToWorld::identity(),
translation
),
]);
}
translation,
)],
);
}

View file

@ -2,7 +2,5 @@ use bevy::*;
fn main() {
asset::load_gltf("examples/assets/Box.gltf").unwrap();
AppBuilder::new()
.add_defaults()
.run();
}
AppBuilder::new().add_defaults().run();
}

View file

@ -1,22 +1,21 @@
use bevy::{*, render::*, asset::{Asset, AssetStorage}, math::{Mat4, Quat, Vec3}, Schedulable, Parent};
use bevy::{
asset::{Asset, AssetStorage},
math::{Mat4, Quat, Vec3},
render::*,
Parent, Schedulable, *,
};
struct Rotator;
fn main() {
AppBuilder::new()
.add_defaults()
.setup(&setup)
.run();
AppBuilder::new().add_defaults().setup(&setup).run();
}
fn build_rotator_system() -> Box<dyn Schedulable> {
SystemBuilder::new("Rotator")
.read_resource::<Time>()
.with_query(<(
Write<Rotator>,
Write<Rotation>,
)>::query())
.build(move |_, world, time , light_query| {
.with_query(<(Write<Rotator>, Write<Rotation>)>::query())
.build(move |_, world, time, light_query| {
for (_, mut rotation) in light_query.iter(world) {
rotation.0 = rotation.0 * Quat::from_rotation_x(3.0 * time.delta_seconds);
}
@ -25,11 +24,17 @@ fn build_rotator_system() -> Box<dyn Schedulable> {
fn setup(world: &mut World, scheduler: &mut SystemScheduler<AppStage>) {
let cube = Mesh::load(MeshType::Cube);
let plane = Mesh::load(MeshType::Plane{ size: 10.0 });
let plane = Mesh::load(MeshType::Plane { size: 10.0 });
let (cube_handle, plane_handle) = {
let mut mesh_storage = world.resources.get_mut::<AssetStorage<Mesh, MeshType>>().unwrap();
(mesh_storage.add(cube, "cube"), mesh_storage.add(plane, "plane"))
let mut mesh_storage = world
.resources
.get_mut::<AssetStorage<Mesh, MeshType>>()
.unwrap();
(
mesh_storage.add(cube, "cube"),
mesh_storage.add(plane, "plane"),
)
};
let transform_system_bundle = transform_system_bundle::build(world);
@ -37,42 +42,49 @@ fn setup(world: &mut World, scheduler: &mut SystemScheduler<AppStage>) {
scheduler.add_system(AppStage::Update, build_rotator_system());
// plane
world.insert((), vec![
(
world.insert(
(),
vec![(
plane_handle.clone(),
Material::new(math::vec4(0.1, 0.2, 0.1, 1.0)),
LocalToWorld::identity(),
Translation::new(0.0, 0.0, -5.0)
),
]);
// cube
let parent_cube = *world.insert((), vec![
(
cube_handle.clone(),
Material::new(math::vec4(0.5, 0.3, 0.3, 1.0)),
LocalToWorld::identity(),
Translation::new(0.0, 0.0, 1.0),
Rotation::from_euler_angles(0.0, 0.0, 0.0),
Rotator,
)
]).first().unwrap();
Translation::new(0.0, 0.0, -5.0),
)],
);
// cube
world.insert((), vec![
(
let parent_cube = *world
.insert(
(),
vec![(
cube_handle.clone(),
Material::new(math::vec4(0.5, 0.3, 0.3, 1.0)),
LocalToWorld::identity(),
Translation::new(0.0, 0.0, 1.0),
Rotation::from_euler_angles(0.0, 0.0, 0.0),
Rotator,
)],
)
.first()
.unwrap();
// cube
world.insert(
(),
vec![(
cube_handle,
Material::new(math::vec4(0.5, 0.3, 0.3, 1.0)),
LocalToWorld::identity(),
Translation::new(0.0, 0.0, 3.0),
Parent(parent_cube),
LocalToParent::identity(),
)
]);
)],
);
// light
world.insert((), vec![
(
world.insert(
(),
vec![(
Light {
color: wgpu::Color {
r: 0.8,
@ -81,19 +93,20 @@ fn setup(world: &mut World, scheduler: &mut SystemScheduler<AppStage>) {
a: 1.0,
},
fov: f32::to_radians(60.0),
depth: 0.1 .. 50.0,
depth: 0.1..50.0,
target_view: None,
},
Material::new(math::vec4(0.5, 0.3, 0.3, 1.0)),
LocalToWorld::identity(),
Translation::new(4.0, -4.0, 5.0),
Rotation::from_euler_angles(0.0, 0.0, 0.0)
),
]);
Rotation::from_euler_angles(0.0, 0.0, 0.0),
)],
);
// camera
world.insert((), vec![
(
world.insert(
(),
vec![(
Camera::new(CameraType::Projection {
fov: std::f32::consts::PI / 4.0,
near: 1.0,
@ -104,7 +117,8 @@ fn setup(world: &mut World, scheduler: &mut SystemScheduler<AppStage>) {
LocalToWorld(Mat4::look_at_rh(
Vec3::new(3.0, -15.0, 8.0),
Vec3::new(0.0, 0.0, 0.0),
Vec3::new(0.0, 0.0, 1.0),)),
)
]);
}
Vec3::new(0.0, 0.0, 1.0),
)),
)],
);
}

View file

@ -1,47 +1,58 @@
use bevy::{*, render::*, asset::{Asset, AssetStorage}, math::{Mat4, Vec3}};
use bevy::{
asset::{Asset, AssetStorage},
math::{Mat4, Vec3},
render::*,
*,
};
fn main() {
AppBuilder::new()
.add_defaults()
.setup(&setup)
.run();
AppBuilder::new().add_defaults().setup(&setup).run();
}
fn setup(world: &mut World, scheduler: &mut SystemScheduler<AppStage>) {
let cube = Mesh::load(MeshType::Cube);
let plane = Mesh::load(MeshType::Plane{ size: 10.0 });
let plane = Mesh::load(MeshType::Plane { size: 10.0 });
let (cube_handle, plane_handle) = {
let mut mesh_storage = world.resources.get_mut::<AssetStorage<Mesh, MeshType>>().unwrap();
(mesh_storage.add(cube, "cube"), mesh_storage.add(plane, "plane"))
let mut mesh_storage = world
.resources
.get_mut::<AssetStorage<Mesh, MeshType>>()
.unwrap();
(
mesh_storage.add(cube, "cube"),
mesh_storage.add(plane, "plane"),
)
};
let transform_system_bundle = transform_system_bundle::build(world);
scheduler.add_systems(AppStage::Update, transform_system_bundle);
// plane
world.insert((), vec![
(
world.insert(
(),
vec![(
plane_handle.clone(),
Material::new(math::vec4(0.1, 0.2, 0.1, 1.0)),
LocalToWorld::identity(),
Translation::new(0.0, 0.0, 0.0)
),
]);
Translation::new(0.0, 0.0, 0.0),
)],
);
// cube
world.insert((), vec![
(
world.insert(
(),
vec![(
cube_handle,
Material::new(math::vec4(0.5, 0.3, 0.3, 1.0)),
LocalToWorld::identity(),
Translation::new(0.0, 0.0, 1.0)
)
]);
Translation::new(0.0, 0.0, 1.0),
)],
);
// light
world.insert((), vec![
(
world.insert(
(),
vec![(
Light {
color: wgpu::Color {
r: 0.8,
@ -50,19 +61,20 @@ fn setup(world: &mut World, scheduler: &mut SystemScheduler<AppStage>) {
a: 1.0,
},
fov: f32::to_radians(60.0),
depth: 0.1 .. 50.0,
depth: 0.1..50.0,
target_view: None,
},
Material::new(math::vec4(0.5, 0.3, 0.3, 1.0)),
LocalToWorld::identity(),
Translation::new(4.0, -4.0, 5.0),
Rotation::from_euler_angles(0.0, 0.0, 0.0)
),
]);
Rotation::from_euler_angles(0.0, 0.0, 0.0),
)],
);
// camera
world.insert((), vec![
(
world.insert(
(),
vec![(
Camera::new(CameraType::Projection {
fov: std::f32::consts::PI / 4.0,
near: 1.0,
@ -73,7 +85,8 @@ fn setup(world: &mut World, scheduler: &mut SystemScheduler<AppStage>) {
LocalToWorld(Mat4::look_at_rh(
Vec3::new(3.0, -8.0, 5.0),
Vec3::new(0.0, 0.0, 0.0),
Vec3::new(0.0, 0.0, 1.0),)),
)
]);
}
Vec3::new(0.0, 0.0, 1.0),
)),
)],
);
}

View file

@ -1,32 +1,39 @@
use bevy::{*, render::*, asset::{Asset, AssetStorage}, math::{Mat4, Vec3}};
use bevy::{
asset::{Asset, AssetStorage},
math::{Mat4, Vec3},
render::*,
*,
};
fn main() {
AppBuilder::new()
.add_defaults()
.setup(&setup)
.run();
AppBuilder::new().add_defaults().setup(&setup).run();
}
fn setup(world: &mut World, scheduler: &mut SystemScheduler<AppStage>) {
let cube = Mesh::load(MeshType::Cube);
let cube_handle = {
let mut mesh_storage = world.resources.get_mut::<AssetStorage<Mesh, MeshType>>().unwrap();
let mut mesh_storage = world
.resources
.get_mut::<AssetStorage<Mesh, MeshType>>()
.unwrap();
mesh_storage.add(cube, "cube")
};
let transform_system_bundle = transform_system_bundle::build(world);
scheduler.add_systems(AppStage::Update, transform_system_bundle);
world.insert((), vec![
(
world.insert(
(),
vec![(
cube_handle,
LocalToWorld::identity(),
Material::new(math::vec4(0.5, 0.3, 0.3, 1.0)),
)
]);
)],
);
world.insert((), vec![
(
world.insert(
(),
vec![(
Light {
color: wgpu::Color {
r: 0.8,
@ -35,74 +42,78 @@ fn setup(world: &mut World, scheduler: &mut SystemScheduler<AppStage>) {
a: 1.0,
},
fov: f32::to_radians(60.0),
depth: 0.1 .. 50.0,
depth: 0.1..50.0,
target_view: None,
},
LocalToWorld::identity(),
Translation::new(4.0, -4.0, 5.0),
Rotation::from_euler_angles(0.0, 0.0, 0.0)
),
]);
Rotation::from_euler_angles(0.0, 0.0, 0.0),
)],
);
world.insert((), vec![
// camera
(
Camera::new(CameraType::Projection {
fov: std::f32::consts::PI / 4.0,
near: 1.0,
far: 1000.0,
aspect_ratio: 1.0,
}),
ActiveCamera,
LocalToWorld(Mat4::look_at_rh(
Vec3::new(3.0, -5.0, 3.0),
Vec3::new(0.0, 0.0, 0.0),
Vec3::new(0.0, 0.0, 1.0),)),
)
]);
world.insert(
(),
vec![
// camera
(
Camera::new(CameraType::Projection {
fov: std::f32::consts::PI / 4.0,
near: 1.0,
far: 1000.0,
aspect_ratio: 1.0,
}),
ActiveCamera,
LocalToWorld(Mat4::look_at_rh(
Vec3::new(3.0, -5.0, 3.0),
Vec3::new(0.0, 0.0, 0.0),
Vec3::new(0.0, 0.0, 1.0),
)),
),
],
);
world.insert((), vec![
// camera
(
Camera::new(CameraType::Orthographic {
left: 0.0,
right: 0.0,
bottom: 0.0,
top: 0.0,
near: 0.0,
far: 1.0,
}),
ActiveCamera2d,
)
]);
world.insert(
(),
vec![
// camera
(
Camera::new(CameraType::Orthographic {
left: 0.0,
right: 0.0,
bottom: 0.0,
top: 0.0,
near: 0.0,
far: 1.0,
}),
ActiveCamera2d,
),
],
);
world.insert((), vec![
(
Rect {
position: math::vec2(75.0, 75.0),
dimensions: math::vec2(100.0, 100.0),
color: math::vec4(0.0, 1.0, 0.0, 1.0),
},
)
]);
world.insert(
(),
vec![(Rect {
position: math::vec2(75.0, 75.0),
dimensions: math::vec2(100.0, 100.0),
color: math::vec4(0.0, 1.0, 0.0, 1.0),
},)],
);
world.insert((), vec![
(
Rect {
position: math::vec2(50.0, 50.0),
dimensions: math::vec2(100.0, 100.0),
color: math::vec4(1.0, 0.0, 0.0, 1.0),
},
)
]);
world.insert(
(),
vec![(Rect {
position: math::vec2(50.0, 50.0),
dimensions: math::vec2(100.0, 100.0),
color: math::vec4(1.0, 0.0, 0.0, 1.0),
},)],
);
world.insert((), vec![
(
Rect {
position: math::vec2(100.0, 100.0),
dimensions: math::vec2(100.0, 100.0),
color: math::vec4(0.0, 0.0, 1.0, 1.0),
},
)
]);
}
world.insert(
(),
vec![(Rect {
position: math::vec2(100.0, 100.0),
dimensions: math::vec2(100.0, 100.0),
color: math::vec4(0.0, 0.0, 1.0, 1.0),
},)],
);
}