2022-08-23 15:14:32 +00:00
|
|
|
use bevy::{prelude::*, sprite::Mesh2dHandle};
|
2022-08-27 07:35:01 +00:00
|
|
|
use rand::{rngs::ThreadRng, Rng};
|
2022-08-24 07:58:56 +00:00
|
|
|
use rand_distr::{Distribution, UnitCircle};
|
2022-08-24 06:35:05 +00:00
|
|
|
|
2022-08-24 09:31:17 +00:00
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
2022-08-24 06:35:05 +00:00
|
|
|
pub const POOL_COUNT: usize = 100;
|
2022-08-24 09:31:17 +00:00
|
|
|
#[cfg(target_arch = "wasm32")]
|
|
|
|
pub const POOL_COUNT: usize = 50;
|
2022-08-24 06:35:05 +00:00
|
|
|
|
|
|
|
pub const MIN_VELOCITY: f32 = 10.0;
|
|
|
|
pub const MAX_VELOCITY: f32 = 100.0;
|
2022-08-24 07:58:56 +00:00
|
|
|
pub const _VELOCITY_SCALE: f32 = 0.1;
|
2022-08-24 06:35:05 +00:00
|
|
|
pub const PARTICLE_EFFECT_RADIUS: f32 = 70.0;
|
2022-08-23 15:14:32 +00:00
|
|
|
|
|
|
|
pub struct ParticleEffectPlugin;
|
|
|
|
|
|
|
|
impl Plugin for ParticleEffectPlugin {
|
2022-08-24 07:58:56 +00:00
|
|
|
fn build(&self, app: &mut App) {
|
|
|
|
app.init_resource::<ParticleMesh>()
|
|
|
|
.insert_resource(ParticleEffectResource::new(Vec3::new(
|
|
|
|
10000.0, 10000.0, 0.0,
|
|
|
|
)))
|
|
|
|
.add_startup_system(particle_effect_startup)
|
2022-08-24 06:35:05 +00:00
|
|
|
.add_system(particle_effect_system);
|
2022-08-24 07:58:56 +00:00
|
|
|
}
|
2022-08-23 15:14:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct ParticleMesh {
|
|
|
|
square: Mesh2dHandle,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromWorld for ParticleMesh {
|
|
|
|
fn from_world(world: &mut World) -> Self {
|
|
|
|
let mut meshes = world.get_resource_mut::<Assets<Mesh>>().unwrap();
|
2022-08-24 06:35:05 +00:00
|
|
|
|
2022-08-23 15:14:32 +00:00
|
|
|
Self {
|
|
|
|
square: meshes
|
|
|
|
.add(Mesh::from(shape::Quad {
|
|
|
|
size: Vec2 { x: 4.0, y: 4.0 },
|
|
|
|
flip: false,
|
|
|
|
}))
|
|
|
|
.into(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-24 06:35:05 +00:00
|
|
|
pub struct ParticleEffectResource {
|
2022-08-24 07:58:56 +00:00
|
|
|
pub translation: Vec3,
|
2022-08-24 06:35:05 +00:00
|
|
|
pub prev_translation: Vec3,
|
2022-08-24 07:58:56 +00:00
|
|
|
pub radius_squared: f32,
|
2022-08-24 06:35:05 +00:00
|
|
|
pub color: Color,
|
|
|
|
}
|
2022-08-23 15:14:32 +00:00
|
|
|
|
2022-08-24 06:35:05 +00:00
|
|
|
impl ParticleEffectResource {
|
|
|
|
fn new(init_translation: Vec3) -> Self {
|
|
|
|
Self {
|
|
|
|
translation: init_translation,
|
|
|
|
prev_translation: init_translation,
|
|
|
|
radius_squared: PARTICLE_EFFECT_RADIUS * PARTICLE_EFFECT_RADIUS,
|
|
|
|
color: Color::WHITE,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default, Component)]
|
|
|
|
pub struct ParticleComponent {
|
|
|
|
pub velocity: Vec3,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ParticleComponent {
|
2022-08-27 07:35:01 +00:00
|
|
|
pub fn new(rng: &mut ThreadRng) -> Self {
|
2022-08-24 06:35:05 +00:00
|
|
|
let mut particle_component: Self = Self::default();
|
2022-08-27 07:35:01 +00:00
|
|
|
particle_component.randomize_velocity(rng, MIN_VELOCITY, MAX_VELOCITY);
|
2022-08-24 07:58:56 +00:00
|
|
|
particle_component
|
2022-08-24 06:35:05 +00:00
|
|
|
}
|
|
|
|
|
2022-08-27 07:35:01 +00:00
|
|
|
pub fn randomize_velocity(
|
|
|
|
&mut self,
|
|
|
|
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);
|
2022-08-24 07:58:56 +00:00
|
|
|
self.velocity = Vec3::new(random_direction[0], random_direction[1], 0.0) * random_magnitude;
|
2022-08-24 06:35:05 +00:00
|
|
|
}
|
|
|
|
}
|
2022-08-23 15:14:32 +00:00
|
|
|
|
|
|
|
fn particle_effect_startup(
|
2022-08-24 07:58:56 +00:00
|
|
|
mut commands: Commands,
|
2022-08-23 15:14:32 +00:00
|
|
|
|
|
|
|
particle_mesh: Res<ParticleMesh>,
|
|
|
|
mut materials: ResMut<Assets<ColorMaterial>>,
|
|
|
|
) {
|
2022-08-27 07:35:01 +00:00
|
|
|
let mut rng = rand::thread_rng();
|
|
|
|
|
2022-08-24 07:58:56 +00:00
|
|
|
for _p in 0..POOL_COUNT {
|
|
|
|
let color_mesh = ColorMesh2dBundle {
|
2022-08-23 15:14:32 +00:00
|
|
|
mesh: particle_mesh.square.clone(),
|
|
|
|
material: materials.add(ColorMaterial::from(Color::WHITE)),
|
|
|
|
..default()
|
|
|
|
};
|
|
|
|
|
2022-08-24 07:58:56 +00:00
|
|
|
commands
|
|
|
|
.spawn_bundle(color_mesh)
|
2022-08-27 07:35:01 +00:00
|
|
|
.insert(ParticleComponent::new(&mut rng));
|
2022-08-24 07:58:56 +00:00
|
|
|
}
|
2022-08-24 06:35:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn particle_effect_system(
|
|
|
|
mut materials: ResMut<Assets<ColorMaterial>>,
|
2022-08-24 07:58:56 +00:00
|
|
|
mut query: Query<(
|
|
|
|
&mut Transform,
|
|
|
|
&mut ParticleComponent,
|
|
|
|
&Handle<ColorMaterial>,
|
|
|
|
)>,
|
|
|
|
mut particle_effect: ResMut<ParticleEffectResource>,
|
|
|
|
time: Res<Time>,
|
2022-08-24 06:35:05 +00:00
|
|
|
) {
|
2022-08-27 07:35:01 +00:00
|
|
|
let mut rng = rand::thread_rng();
|
|
|
|
|
2022-08-24 07:58:56 +00:00
|
|
|
let delta_seconds: f32 = f32::max(0.001, time.delta_seconds());
|
2022-08-24 06:35:05 +00:00
|
|
|
let delta_position: Vec3 = particle_effect.translation - particle_effect.prev_translation;
|
|
|
|
// let overall_velocity: Vec3 = delta_position / delta_seconds;
|
|
|
|
particle_effect.prev_translation = particle_effect.translation;
|
|
|
|
|
|
|
|
for (mut transform, mut particle_component, color_material) in query.iter_mut() {
|
2022-08-24 07:58:56 +00:00
|
|
|
// particle_component.velocity -= overall_velocity * VELOCITY_SCALE;
|
|
|
|
transform.translation += particle_component.velocity * delta_seconds + delta_position;
|
2022-08-24 06:35:05 +00:00
|
|
|
|
2022-08-24 07:58:56 +00:00
|
|
|
let squared_distance: f32 = transform
|
|
|
|
.translation
|
|
|
|
.distance_squared(particle_effect.translation);
|
|
|
|
if squared_distance > particle_effect.radius_squared {
|
2022-08-24 06:35:05 +00:00
|
|
|
transform.translation = particle_effect.translation;
|
2022-08-27 07:35:01 +00:00
|
|
|
particle_component.randomize_velocity(&mut rng, MIN_VELOCITY, MAX_VELOCITY);
|
2022-08-24 06:35:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(material) = materials.get_mut(color_material) {
|
|
|
|
material.color = particle_effect.color;
|
2022-08-24 07:58:56 +00:00
|
|
|
material.color.set_a(
|
|
|
|
(particle_effect.radius_squared - squared_distance)
|
|
|
|
/ particle_effect.radius_squared,
|
|
|
|
);
|
2022-08-24 06:35:05 +00:00
|
|
|
}
|
2022-08-27 17:24:31 +00:00
|
|
|
transform.translation.z = 0.005;
|
2022-08-24 06:35:05 +00:00
|
|
|
}
|
2022-08-24 07:58:56 +00:00
|
|
|
}
|