diff --git a/src/physics/projectiles.rs b/src/physics/projectiles.rs index 1a6c770..ed03031 100644 --- a/src/physics/projectiles.rs +++ b/src/physics/projectiles.rs @@ -59,7 +59,7 @@ fn on_bullet_fired_event( mut events: EventReader, resources: Res, ) { - for event in events.read() + let to_bullet_entity = |event: &BulletFiredEvent| { let rotation = Quat::from_scaled_axis(Vec3::new( rand::random::() * f32::to_radians(360.0), @@ -68,21 +68,24 @@ fn on_bullet_fired_event( )); let transform = Transform::IDENTITY .with_translation(event.position) - .with_scale(Vec3::splat(event.radius)) .with_rotation(rotation) + .with_scale(Vec3::splat(event.radius)) .looking_to(event.direction, Vec3::Y); - commands.spawn(( + ( Bullet, resources.mesh.clone(), resources.material.clone(), resources.collider.clone(), transform, - ColliderDensity(event.density), - AngularVelocity(event.gyro * event.direction), RigidBody::Dynamic, - FrameRaycastAntiGhost, + ColliderDensity(event.density), LinearVelocity(event.velocity * event.direction), - )); - } + AngularVelocity(event.gyro * event.direction), + FrameRaycastAntiGhost, + ) + }; + + let bullet_entities = events.read().map(to_bullet_entity).collect::>(); + commands.spawn_batch(bullet_entities); }