| 1 |
|
| 2 |
|
| 3 |
use std::f32::consts::PI; |
| 4 |
|
| 5 |
use bevy::{color::palettes::basic::YELLOW, prelude::*}; |
| 6 |
|
| 7 |
|
| 8 |
#[derive(Component)] |
| 9 |
struct CubeState { |
| 10 |
start_pos: Vec3, |
| 11 |
move_speed: f32, |
| 12 |
turn_speed: f32, |
| 13 |
} |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
#[derive(Component)] |
| 18 |
struct Center { |
| 19 |
max_size: f32, |
| 20 |
min_size: f32, |
| 21 |
scale_factor: f32, |
| 22 |
} |
| 23 |
|
| 24 |
fn main() { |
| 25 |
App::new() |
| 26 |
.add_plugins(DefaultPlugins) |
| 27 |
.add_systems(Startup, setup) |
| 28 |
.add_systems( |
| 29 |
Update, |
| 30 |
( |
| 31 |
move_cube, |
| 32 |
rotate_cube, |
| 33 |
scale_down_sphere_proportional_to_cube_travel_distance, |
| 34 |
) |
| 35 |
.chain(), |
| 36 |
) |
| 37 |
.run(); |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
fn setup( |
| 42 |
mut commands: Commands, |
| 43 |
mut meshes: ResMut<Assets<Mesh>>, |
| 44 |
mut materials: ResMut<Assets<StandardMaterial>>, |
| 45 |
) { |
| 46 |
|
| 47 |
commands.spawn(( |
| 48 |
Mesh3d(meshes.add(Sphere::new(3.0).mesh().ico(32).unwrap())), |
| 49 |
MeshMaterial3d(materials.add(Color::from(YELLOW))), |
| 50 |
Transform::from_translation(Vec3::ZERO), |
| 51 |
Center { |
| 52 |
max_size: 1.0, |
| 53 |
min_size: 0.1, |
| 54 |
scale_factor: 0.05, |
| 55 |
}, |
| 56 |
)); |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
let cube_spawn = |
| 64 |
Transform::from_translation(Vec3::Z * -10.0).with_rotation(Quat::from_rotation_y(PI / 2.)); |
| 65 |
commands.spawn(( |
| 66 |
Mesh3d(meshes.add(Cuboid::default())), |
| 67 |
MeshMaterial3d(materials.add(Color::WHITE)), |
| 68 |
cube_spawn, |
| 69 |
CubeState { |
| 70 |
start_pos: cube_spawn.translation, |
| 71 |
move_speed: 2.0, |
| 72 |
turn_speed: 0.2, |
| 73 |
}, |
| 74 |
)); |
| 75 |
|
| 76 |
|
| 77 |
commands.spawn(( |
| 78 |
Camera3d::default(), |
| 79 |
Transform::from_xyz(0.0, 10.0, 20.0).looking_at(Vec3::ZERO, Vec3::Y), |
| 80 |
)); |
| 81 |
|
| 82 |
|
| 83 |
commands.spawn(( |
| 84 |
DirectionalLight::default(), |
| 85 |
Transform::from_xyz(3.0, 3.0, 3.0).looking_at(Vec3::ZERO, Vec3::Y), |
| 86 |
)); |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
fn move_cube(mut cubes: Query<(&mut Transform, &mut CubeState)>, timer: Res<Time>) { |
| 91 |
for (mut transform, cube) in &mut cubes { |
| 92 |
|
| 93 |
let forward = transform.forward(); |
| 94 |
transform.translation += forward * cube.move_speed * timer.delta_secs(); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
fn rotate_cube( |
| 102 |
mut cubes: Query<(&mut Transform, &mut CubeState), Without<Center>>, |
| 103 |
center_spheres: Query<&Transform, With<Center>>, |
| 104 |
timer: Res<Time>, |
| 105 |
) { |
| 106 |
|
| 107 |
let mut center: Vec3 = Vec3::ZERO; |
| 108 |
for sphere in ¢er_spheres { |
| 109 |
center += sphere.translation; |
| 110 |
} |
| 111 |
|
| 112 |
for (mut transform, cube) in &mut cubes { |
| 113 |
|
| 114 |
let look_at_sphere = transform.looking_at(center, *transform.local_y()); |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
let incremental_turn_weight = cube.turn_speed * timer.delta_secs(); |
| 119 |
let old_rotation = transform.rotation; |
| 120 |
transform.rotation = old_rotation.lerp(look_at_sphere.rotation, incremental_turn_weight); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
fn scale_down_sphere_proportional_to_cube_travel_distance( |
| 127 |
cubes: Query<(&Transform, &CubeState), Without<Center>>, |
| 128 |
mut centers: Query<(&mut Transform, &Center)>, |
| 129 |
) { |
| 130 |
|
| 131 |
|
| 132 |
let mut distances = 0.0; |
| 133 |
for (cube_transform, cube_state) in &cubes { |
| 134 |
distances += (cube_state.start_pos - cube_transform.translation).length(); |
| 135 |
} |
| 136 |
|
| 137 |
for (mut transform, center) in &mut centers { |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
let mut new_size: f32 = center.max_size - center.scale_factor * distances; |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
new_size = new_size.max(center.min_size); |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
transform.scale = Vec3::splat(new_size); |
| 150 |
} |
| 151 |
} |