main.rs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. use bevy::prelude::*;
  2. #[derive(Component)]
  3. struct Mover;
  4. fn main() {
  5. App::new()
  6. .insert_resource(Movement { dx: 0.0, dy: 0.0 })
  7. .add_plugins(DefaultPlugins)
  8. .add_systems(Startup, setup)
  9. .add_systems(Update, (input, decay, movement))
  10. .run();
  11. }
  12. fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
  13. commands.spawn((
  14. Camera3dBundle {
  15. transform: Transform::from_xyz(0.0, 12.0, 18.0).looking_at(Vec3::ZERO, Vec3::Y),
  16. ..default()
  17. },
  18. Mover,
  19. ));
  20. commands.spawn(PointLightBundle {
  21. point_light: PointLight {
  22. shadows_enabled: true,
  23. ..default()
  24. },
  25. transform: Transform::from_xyz(4.0, 8.0, 4.0),
  26. ..default()
  27. });
  28. for x in 0..24 {
  29. for y in 0..24 {
  30. let x = x - 12;
  31. let y = y - 12;
  32. let z = if x == -12 || x == 11 || y == -12 || y == 11 || rand::random::<f32>() < 0.25 {
  33. 0.0
  34. } else {
  35. -2.0
  36. };
  37. let rotation = match rand::random::<u8>() % 4 {
  38. 0 => 0.0,
  39. 1 => 0.5,
  40. 2 => 1.0,
  41. _ => 1.5,
  42. } * std::f32::consts::PI;
  43. commands.spawn(SceneBundle {
  44. scene: asset_server.load("test-cube.glb#Scene0"),
  45. transform: Transform::from_xyz((x * 2) as f32, z, (y * 2) as f32)
  46. .with_rotation(Quat::from_rotation_y(rotation)),
  47. ..default()
  48. });
  49. }
  50. }
  51. }
  52. #[derive(Debug, Resource)]
  53. struct Movement {
  54. dx: f32,
  55. dy: f32,
  56. }
  57. impl Default for Movement {
  58. fn default() -> Movement {
  59. Movement { dx: 0.0, dy: 0.0 }
  60. }
  61. }
  62. const ACCEL: f32 = 0.002;
  63. fn input(keyboard: Res<ButtonInput<KeyCode>>, mut mvmt: ResMut<Movement>) {
  64. if keyboard.pressed(KeyCode::ArrowUp) {
  65. mvmt.dy = (mvmt.dy - ACCEL).clamp(-0.5, 0.5);
  66. }
  67. if keyboard.pressed(KeyCode::ArrowDown) {
  68. mvmt.dy = (mvmt.dy + ACCEL).clamp(-0.5, 0.5);
  69. }
  70. if keyboard.pressed(KeyCode::ArrowLeft) {
  71. mvmt.dx = (mvmt.dx - ACCEL).clamp(-0.5, 0.5);
  72. }
  73. if keyboard.pressed(KeyCode::ArrowRight) {
  74. mvmt.dx = (mvmt.dx + ACCEL).clamp(-0.5, 0.5);
  75. }
  76. }
  77. fn decay(mut mvmt: ResMut<Movement>) {
  78. if mvmt.dx.abs() < 0.001 {
  79. mvmt.dx = 0.0;
  80. } else {
  81. mvmt.dx *= 0.90;
  82. }
  83. if mvmt.dy.abs() < 0.001 {
  84. mvmt.dy = 0.0;
  85. } else {
  86. mvmt.dy *= 0.90;
  87. }
  88. }
  89. fn movement(mut query: Query<&mut Transform, With<Mover>>, mvmt: Res<Movement>, time: Res<Time>) {
  90. if time.delta_seconds() == 0.0 {
  91. return;
  92. }
  93. for mut transform in &mut query {
  94. transform.translation += Vec3::new(
  95. mvmt.dx / time.delta_seconds(),
  96. 0.0,
  97. mvmt.dy / time.delta_seconds(),
  98. );
  99. }
  100. }