create_entities: Remove from MainScene struct

This commit is contained in:
Florian RICHER 2025-06-12 21:11:17 +02:00
parent 51a3f812fe
commit 5d4048d9a7
Signed by: florian.richer
GPG key ID: C73D37CBED7BFC77

View file

@ -138,7 +138,7 @@ impl AsScene for MainScene {
.chain(), .chain(),
); );
world.insert_resource(Timer::new()); world.insert_resource(Timer::new());
Self::create_entities(world, 100, 10.0, 10.0); create_entities(world, 100, 10.0, 10.0);
self.state = Some(MainSceneState { self.state = Some(MainSceneState {
square, square,
@ -370,67 +370,64 @@ impl AsScene for MainScene {
} }
} }
impl MainScene { fn create_entities(
// Function to create entities in the ECS world world: &mut World,
fn create_entities( num_instances: u32,
world: &mut World, instance_size: f32,
num_instances: u32, instance_spacing: f32,
instance_size: f32, ) {
instance_spacing: f32, let num_instances_per_row = (num_instances as f32 / instance_spacing).ceil() as u32;
) {
let num_instances_per_row = (num_instances as f32 / instance_spacing).ceil() as u32;
let square_instances = (0..num_instances) let square_instances = (0..num_instances)
.map(|i| { .map(|i| {
let x_index = i % num_instances_per_row; let x_index = i % num_instances_per_row;
let z_index = i / num_instances_per_row; let z_index = i / num_instances_per_row;
let transform = Transform::new( let transform = Transform::new(
Vec3::new( Vec3::new(
x_index as f32 * (instance_spacing + instance_size), x_index as f32 * (instance_spacing + instance_size),
0.0, 0.0,
z_index as f32 * (instance_spacing + instance_size), z_index as f32 * (instance_spacing + instance_size),
), ),
Quat::from_euler(EulerRot::XYZ, 0.0, rand::random_range(0.0..=360.0), 0.0), Quat::from_euler(EulerRot::XYZ, 0.0, rand::random_range(0.0..=360.0), 0.0),
Vec3::new(instance_size, instance_size, instance_size), Vec3::new(instance_size, instance_size, instance_size),
); );
let velocity = Velocity::new(Vec3::ZERO, Vec3::new(0.0, x_index as f32, 0.0)); let velocity = Velocity::new(Vec3::ZERO, Vec3::new(0.0, x_index as f32, 0.0));
(Square, transform, velocity) (Square, transform, velocity)
}) })
.collect::<Vec<_>>(); .collect::<Vec<_>>();
world.spawn_batch(square_instances); world.spawn_batch(square_instances);
let cube_instances = (0..num_instances) let cube_instances = (0..num_instances)
.map(|i| { .map(|i| {
let x_index = i % num_instances_per_row; let x_index = i % num_instances_per_row;
let z_index = i / num_instances_per_row; let z_index = i / num_instances_per_row;
let transform = Transform::new( let transform = Transform::new(
Vec3::new( Vec3::new(
x_index as f32 * (instance_spacing + instance_size), x_index as f32 * (instance_spacing + instance_size),
0.0, 0.0,
z_index as f32 * (instance_spacing + instance_size) * -1.0 z_index as f32 * (instance_spacing + instance_size) * -1.0
- instance_spacing * 2.0, - instance_spacing * 2.0,
), ),
Quat::from_euler(EulerRot::XYZ, 0.0, rand::random_range(0.0..=360.0), 0.0), Quat::from_euler(EulerRot::XYZ, 0.0, rand::random_range(0.0..=360.0), 0.0),
Vec3::new( Vec3::new(
instance_size * 0.5, instance_size * 0.5,
instance_size * 0.5, instance_size * 0.5,
instance_size * 0.5, instance_size * 0.5,
), ),
); );
let velocity = Velocity::new(Vec3::ZERO, Vec3::new(0.0, x_index as f32, 0.0)); let velocity = Velocity::new(Vec3::ZERO, Vec3::new(0.0, x_index as f32, 0.0));
(Cube, transform, velocity) (Cube, transform, velocity)
}) })
.collect::<Vec<_>>(); .collect::<Vec<_>>();
world.spawn_batch(cube_instances); world.spawn_batch(cube_instances);
}
} }
fn update_velocity_system(mut query: Query<(&mut Transform, &Velocity)>, time: Res<Timer>) { fn update_velocity_system(mut query: Query<(&mut Transform, &Velocity)>, time: Res<Timer>) {