opti(particles): fetch ThreadRng only once in a system
This commit is contained in:
parent
f3f74f668a
commit
9f04ec861e
2 changed files with 18 additions and 10 deletions
|
@ -290,8 +290,7 @@ fn collision_event_system(
|
||||||
.clamp(Vec4::ZERO, Vec4::ONE);
|
.clamp(Vec4::ZERO, Vec4::ONE);
|
||||||
|
|
||||||
// If color approximately white
|
// If color approximately white
|
||||||
if app_state.current() == &AppState::Game
|
if app_state.current() == &AppState::Game && new_color.min_element() >= 0.9
|
||||||
&& new_color.min_element() >= 0.9
|
|
||||||
{
|
{
|
||||||
app_state.replace(AppState::Win).ok();
|
app_state.replace(AppState::Win).ok();
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
use bevy::{prelude::*, sprite::Mesh2dHandle};
|
use bevy::{prelude::*, sprite::Mesh2dHandle};
|
||||||
use rand::Rng;
|
use rand::{rngs::ThreadRng, Rng};
|
||||||
use rand_distr::{Distribution, UnitCircle};
|
use rand_distr::{Distribution, UnitCircle};
|
||||||
|
|
||||||
#[cfg(not(target_arch = "wasm32"))]
|
#[cfg(not(target_arch = "wasm32"))]
|
||||||
|
@ -68,15 +68,20 @@ pub struct ParticleComponent {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ParticleComponent {
|
impl ParticleComponent {
|
||||||
pub fn new() -> Self {
|
pub fn new(rng: &mut ThreadRng) -> Self {
|
||||||
let mut particle_component: Self = Self::default();
|
let mut particle_component: Self = Self::default();
|
||||||
particle_component.randomize_velocity(MIN_VELOCITY, MAX_VELOCITY);
|
particle_component.randomize_velocity(rng, MIN_VELOCITY, MAX_VELOCITY);
|
||||||
particle_component
|
particle_component
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn randomize_velocity(&mut self, min_velocity: f32, max_velocity: f32) {
|
pub fn randomize_velocity(
|
||||||
let random_direction: [f32; 2] = UnitCircle.sample(&mut rand::thread_rng());
|
&mut self,
|
||||||
let random_magnitude: f32 = rand::thread_rng().gen_range(min_velocity..max_velocity);
|
rng: &mut ThreadRng,
|
||||||
|
min_velocity: f32,
|
||||||
|
max_velocity: f32,
|
||||||
|
) {
|
||||||
|
let random_direction: [f32; 2] = UnitCircle.sample(rng);
|
||||||
|
let random_magnitude: f32 = rng.gen_range(min_velocity..max_velocity);
|
||||||
self.velocity = Vec3::new(random_direction[0], random_direction[1], 0.0) * random_magnitude;
|
self.velocity = Vec3::new(random_direction[0], random_direction[1], 0.0) * random_magnitude;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -87,6 +92,8 @@ fn particle_effect_startup(
|
||||||
particle_mesh: Res<ParticleMesh>,
|
particle_mesh: Res<ParticleMesh>,
|
||||||
mut materials: ResMut<Assets<ColorMaterial>>,
|
mut materials: ResMut<Assets<ColorMaterial>>,
|
||||||
) {
|
) {
|
||||||
|
let mut rng = rand::thread_rng();
|
||||||
|
|
||||||
for _p in 0..POOL_COUNT {
|
for _p in 0..POOL_COUNT {
|
||||||
let color_mesh = ColorMesh2dBundle {
|
let color_mesh = ColorMesh2dBundle {
|
||||||
mesh: particle_mesh.square.clone(),
|
mesh: particle_mesh.square.clone(),
|
||||||
|
@ -96,7 +103,7 @@ fn particle_effect_startup(
|
||||||
|
|
||||||
commands
|
commands
|
||||||
.spawn_bundle(color_mesh)
|
.spawn_bundle(color_mesh)
|
||||||
.insert(ParticleComponent::new());
|
.insert(ParticleComponent::new(&mut rng));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -110,6 +117,8 @@ fn particle_effect_system(
|
||||||
mut particle_effect: ResMut<ParticleEffectResource>,
|
mut particle_effect: ResMut<ParticleEffectResource>,
|
||||||
time: Res<Time>,
|
time: Res<Time>,
|
||||||
) {
|
) {
|
||||||
|
let mut rng = rand::thread_rng();
|
||||||
|
|
||||||
let delta_seconds: f32 = f32::max(0.001, time.delta_seconds());
|
let delta_seconds: f32 = f32::max(0.001, time.delta_seconds());
|
||||||
let delta_position: Vec3 = particle_effect.translation - particle_effect.prev_translation;
|
let delta_position: Vec3 = particle_effect.translation - particle_effect.prev_translation;
|
||||||
// let overall_velocity: Vec3 = delta_position / delta_seconds;
|
// let overall_velocity: Vec3 = delta_position / delta_seconds;
|
||||||
|
@ -124,7 +133,7 @@ fn particle_effect_system(
|
||||||
.distance_squared(particle_effect.translation);
|
.distance_squared(particle_effect.translation);
|
||||||
if squared_distance > particle_effect.radius_squared {
|
if squared_distance > particle_effect.radius_squared {
|
||||||
transform.translation = particle_effect.translation;
|
transform.translation = particle_effect.translation;
|
||||||
particle_component.randomize_velocity(MIN_VELOCITY, MAX_VELOCITY);
|
particle_component.randomize_velocity(&mut rng, MIN_VELOCITY, MAX_VELOCITY);
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(material) = materials.get_mut(color_material) {
|
if let Some(material) = materials.get_mut(color_material) {
|
||||||
|
|
Loading…
Reference in a new issue