bevy/examples/3d/spawner.rs

96 lines
3 KiB
Rust
Raw Normal View History

2020-05-04 20:42:49 +00:00
use bevy::{
diagnostic::{FrameTimeDiagnosticsPlugin, PrintDiagnosticsPlugin},
prelude::*,
};
2020-01-28 09:53:28 +00:00
use rand::{rngs::StdRng, Rng, SeedableRng};
2020-01-19 21:24:01 +00:00
fn main() {
App::build()
.add_default_plugins()
2020-05-04 20:21:14 +00:00
.add_plugin(FrameTimeDiagnosticsPlugin::default())
2020-05-04 20:42:49 +00:00
.add_plugin(PrintDiagnosticsPlugin::default())
2020-05-14 00:31:56 +00:00
.add_startup_system(setup.system())
.add_system(move_cubes.system())
2020-02-08 07:17:51 +00:00
.run();
2020-01-27 09:40:53 +00:00
}
2020-05-14 00:31:56 +00:00
fn move_cubes(
2020-05-14 00:52:47 +00:00
time: Res<Time>,
mut materials: ResMut<Assets<StandardMaterial>>,
2020-07-10 04:18:35 +00:00
mut query: Query<(&mut Translation, &Handle<StandardMaterial>)>,
2020-04-28 08:00:30 +00:00
) {
for (mut translation, material_handle) in &mut query.iter() {
let material = materials.get_mut(&material_handle).unwrap();
2020-07-17 02:23:47 +00:00
translation.0 += Vec3::new(1.0, 0.0, 0.0) * time.delta_seconds;
material.albedo += Color::rgb(-time.delta_seconds, -time.delta_seconds, time.delta_seconds);
}
2020-01-19 21:24:01 +00:00
}
2020-05-14 00:31:56 +00:00
fn setup(
2020-07-10 04:18:35 +00:00
mut commands: Commands,
2020-05-14 00:52:47 +00:00
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
2020-05-14 00:31:56 +00:00
) {
2020-05-16 07:21:04 +00:00
let cube_handle = meshes.add(Mesh::from(shape::Cube { size: 1.0 }));
2020-05-13 23:42:27 +00:00
let plane_handle = meshes.add(Mesh::from(shape::Plane { size: 10.0 }));
let cube_material_handle = materials.add(StandardMaterial {
albedo: Color::rgb(0.5, 0.4, 0.3),
..Default::default()
});
2020-05-13 23:42:27 +00:00
let plane_material_handle = materials.add(StandardMaterial {
albedo: Color::rgb(0.1, 0.2, 0.1),
..Default::default()
});
2020-01-19 21:24:01 +00:00
2020-07-10 04:18:35 +00:00
commands
2020-01-19 21:24:01 +00:00
// plane
2020-07-10 04:18:35 +00:00
.spawn(PbrComponents {
2020-02-24 07:50:44 +00:00
mesh: plane_handle,
material: plane_material_handle,
2020-02-11 17:31:49 +00:00
..Default::default()
2020-01-27 09:44:02 +00:00
})
2020-01-19 21:24:01 +00:00
// cube
2020-07-10 04:18:35 +00:00
.spawn(PbrComponents {
2020-02-24 07:50:44 +00:00
mesh: cube_handle,
material: cube_material_handle,
2020-01-19 21:24:01 +00:00
translation: Translation::new(0.0, 0.0, 1.0),
2020-02-11 17:31:49 +00:00
..Default::default()
2020-01-19 21:24:01 +00:00
})
// light
2020-07-10 04:18:35 +00:00
.spawn(LightComponents {
translation: Translation::new(4.0, -4.0, 5.0),
2020-02-18 03:06:12 +00:00
..Default::default()
})
2020-01-19 21:24:01 +00:00
// camera
.spawn(Camera3dComponents {
transform: Transform::new_sync_disabled(Mat4::face_toward(
2020-07-20 08:35:23 +00:00
Vec3::new(0.0, 150.0, -15.0),
2020-01-19 21:24:01 +00:00
Vec3::new(0.0, 0.0, 0.0),
Vec3::new(0.0, 1.0, 0.0),
2020-01-19 21:24:01 +00:00
)),
2020-03-22 04:55:33 +00:00
..Default::default()
2020-01-28 09:53:28 +00:00
});
let mut rng = StdRng::from_entropy();
for _ in 0..10000 {
2020-05-13 23:42:27 +00:00
let spawned_material_handle = materials.add(StandardMaterial {
albedo: Color::rgb(
rng.gen_range(0.0, 1.0),
rng.gen_range(0.0, 1.0),
rng.gen_range(0.0, 1.0),
),
..Default::default()
});
2020-07-10 04:18:35 +00:00
commands.spawn(PbrComponents {
2020-02-24 07:50:44 +00:00
mesh: cube_handle,
material: spawned_material_handle,
2020-02-08 07:17:51 +00:00
translation: Translation::new(
rng.gen_range(-50.0, 50.0),
0.0,
rng.gen_range(-50.0, 50.0),
2020-02-08 07:17:51 +00:00
),
2020-02-11 17:31:49 +00:00
..Default::default()
2020-04-07 20:25:01 +00:00
});
2020-01-28 09:53:28 +00:00
}
2020-01-19 21:24:01 +00:00
}