bevyjam/src/particle_effect.rs

57 lines
1.3 KiB
Rust
Raw Normal View History

2022-08-23 15:14:32 +00:00
use bevy::{prelude::*, sprite::Mesh2dHandle};
pub struct ParticleEffectPlugin;
impl Plugin for ParticleEffectPlugin {
fn build(&self, app: &mut App) {
app
.init_resource::<ParticleMesh>()
.insert_resource(PoolCount(1000))
.add_startup_system(particle_effect_startup);
}
}
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();
Self {
square: meshes
.add(Mesh::from(shape::Quad {
size: Vec2 { x: 4.0, y: 4.0 },
flip: false,
}))
.into(),
}
}
}
pub struct PoolCount(usize);
#[derive(Component)]
pub struct ParticleComponent;
fn particle_effect_startup(
mut commands: Commands,
particle_mesh: Res<ParticleMesh>,
pool_count: ResMut<PoolCount>,
mut materials: ResMut<Assets<ColorMaterial>>,
) {
for _p in 0 .. pool_count.0 {
let color_mesh = ColorMesh2dBundle {
mesh: particle_mesh.square.clone(),
material: materials.add(ColorMaterial::from(Color::WHITE)),
// default to invisible
visibility: Visibility {is_visible: false},
..default()
};
commands
.spawn_bundle(color_mesh)
.insert(ParticleComponent);
}
}