main.rs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. use bevy::prelude::*;
  2. #[derive(Component)]
  3. struct Rotator;
  4. fn main() {
  5. App::new()
  6. .add_plugins(DefaultPlugins)
  7. .add_systems(Startup, setup)
  8. .add_systems(Update, rotate)
  9. .run();
  10. }
  11. fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
  12. commands.spawn(Camera3dBundle {
  13. transform: Transform::from_xyz(0.0, 12.0, 18.0).looking_at(Vec3::ZERO, Vec3::Y),
  14. ..default()
  15. });
  16. commands.spawn(PointLightBundle {
  17. point_light: PointLight {
  18. shadows_enabled: true,
  19. ..default()
  20. },
  21. transform: Transform::from_xyz(4.0, 8.0, 4.0),
  22. ..default()
  23. });
  24. for x in 0..24 {
  25. for y in 0..24 {
  26. let x = x - 12;
  27. let y = y - 12;
  28. let z = if x == -12 || x == 11 || y == -12 || y == 11 || rand::random::<f32>() < 0.25 {
  29. 0.0
  30. } else {
  31. -2.0
  32. };
  33. let rotation = match rand::random::<u8>() % 4 {
  34. 0 => 0.0,
  35. 1 => 0.5,
  36. 2 => 1.0,
  37. _ => 1.5,
  38. } * std::f32::consts::PI;
  39. commands.spawn(
  40. SceneBundle {
  41. scene: asset_server.load("test-cube.glb#Scene0"),
  42. transform: Transform::from_xyz((x * 2) as f32, z, (y * 2) as f32).with_rotation(Quat::from_rotation_y(rotation)),
  43. ..default()
  44. }
  45. );
  46. }
  47. }
  48. }
  49. fn rotate(mut query: Query<&mut Transform, With<Rotator>>, time: Res<Time>) {
  50. for mut transform in &mut query {
  51. transform.rotate_y(time.delta_seconds() / 2.);
  52. }
  53. }