94 lines
2.5 KiB
Rust
94 lines
2.5 KiB
Rust
use rand::Rng;
|
|
|
|
use crate::{config::PARTICLE_SCALE, vertex};
|
|
|
|
pub enum ParticleType {
|
|
SolidColor = 0,
|
|
Texture,
|
|
}
|
|
|
|
#[derive(Debug, Copy, Clone, Default)]
|
|
pub struct Particle {
|
|
pub position: [f32; 3],
|
|
pub velocity: [f32; 3],
|
|
pub lifetime: f32,
|
|
}
|
|
|
|
impl Particle {
|
|
pub fn new(position: [f32; 3], velocity: [f32; 3], lifetime: f32) -> Self {
|
|
Self {
|
|
position,
|
|
velocity,
|
|
lifetime,
|
|
}
|
|
}
|
|
|
|
pub fn random_particle() -> Self {
|
|
let mut rng = rand::thread_rng();
|
|
|
|
let x = rng.gen_range(-1.0..1.0);
|
|
let y = rng.gen_range(-1.0..1.0);
|
|
let z = rng.gen_range(-1.0..1.0);
|
|
|
|
let x_vel = rng.gen_range(-0.001..0.001);
|
|
let y_vel = rng.gen_range(-0.001..0.001);
|
|
let z_vel = rng.gen_range(-0.001..0.001);
|
|
|
|
let lifetime = rng.gen_range(0.0..100000000000.0);
|
|
|
|
Self {
|
|
position: [x, y, z],
|
|
velocity: [x_vel, y_vel, z_vel],
|
|
lifetime,
|
|
}
|
|
}
|
|
}
|
|
|
|
pub fn generate_particle_vertexs(particle_list: &mut Vec<Particle>) -> Vec<vertex::Vertex> {
|
|
let mut shape = Vec::new();
|
|
|
|
for particle in particle_list.iter_mut() {
|
|
if particle.lifetime <= 0.0 {
|
|
drop(particle);
|
|
break;
|
|
}
|
|
|
|
let vert_1 = vertex::Vertex {
|
|
position: (
|
|
particle.position[0] - PARTICLE_SCALE,
|
|
particle.position[1] + PARTICLE_SCALE,
|
|
particle.position[2],
|
|
),
|
|
};
|
|
let vert_2 = vertex::Vertex {
|
|
position: (
|
|
particle.position[0] + PARTICLE_SCALE,
|
|
particle.position[1] + PARTICLE_SCALE,
|
|
particle.position[2],
|
|
),
|
|
};
|
|
let vert_3 = vertex::Vertex {
|
|
position: (
|
|
particle.position[0] - PARTICLE_SCALE,
|
|
particle.position[1] - PARTICLE_SCALE,
|
|
particle.position[2],
|
|
),
|
|
};
|
|
let vert_4 = vertex::Vertex {
|
|
position: (
|
|
particle.position[0] + PARTICLE_SCALE,
|
|
particle.position[1] - PARTICLE_SCALE,
|
|
particle.position[2],
|
|
),
|
|
};
|
|
|
|
shape.append(&mut vec![vert_1, vert_2, vert_3, vert_3, vert_4, vert_2]);
|
|
particle.lifetime -= 0.1;
|
|
particle.position[0] += particle.velocity[0];
|
|
particle.position[1] += particle.velocity[1];
|
|
particle.position[2] += particle.velocity[2];
|
|
}
|
|
|
|
shape
|
|
}
|