| 1 |
|
| 2 |
|
| 3 |
use bevy::prelude::*; |
| 4 |
|
| 5 |
use std::f32::consts::TAU; |
| 6 |
|
| 7 |
|
| 8 |
#[derive(Component)] |
| 9 |
struct Rotatable { |
| 10 |
speed: f32, |
| 11 |
} |
| 12 |
|
| 13 |
fn main() { |
| 14 |
App::new() |
| 15 |
.add_plugins(DefaultPlugins) |
| 16 |
.add_systems(Startup, setup) |
| 17 |
.add_systems(Update, rotate_cube) |
| 18 |
.run(); |
| 19 |
} |
| 20 |
|
| 21 |
fn setup( |
| 22 |
mut commands: Commands, |
| 23 |
mut meshes: ResMut<Assets<Mesh>>, |
| 24 |
mut materials: ResMut<Assets<StandardMaterial>>, |
| 25 |
) { |
| 26 |
|
| 27 |
commands.spawn(( |
| 28 |
Mesh3d(meshes.add(Cuboid::default())), |
| 29 |
MeshMaterial3d(materials.add(Color::WHITE)), |
| 30 |
Transform::from_translation(Vec3::ZERO), |
| 31 |
Rotatable { speed: 0.3 }, |
| 32 |
)); |
| 33 |
|
| 34 |
|
| 35 |
commands.spawn(( |
| 36 |
Camera3d::default(), |
| 37 |
Transform::from_xyz(0.0, 10.0, 20.0).looking_at(Vec3::ZERO, Vec3::Y), |
| 38 |
)); |
| 39 |
|
| 40 |
|
| 41 |
commands.spawn(( |
| 42 |
DirectionalLight::default(), |
| 43 |
Transform::from_xyz(3.0, 3.0, 3.0).looking_at(Vec3::ZERO, Vec3::Y), |
| 44 |
)); |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
fn rotate_cube(mut cubes: Query<(&mut Transform, &Rotatable)>, timer: Res<Time>) { |
| 49 |
for (mut transform, cube) in &mut cubes { |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
transform.rotate_y(cube.speed * TAU * timer.delta_secs()); |
| 54 |
} |
| 55 |
} |