initialize particle effect pooling

This commit is contained in:
Nixon 2022-08-23 23:14:32 +08:00
parent d9232fd5f0
commit 4a0c2704fb
2 changed files with 58 additions and 0 deletions

View file

@ -1,6 +1,7 @@
mod game; mod game;
mod levels; mod levels;
mod menu; mod menu;
mod particle_effect;
use bevy::{ use bevy::{
core_pipeline::clear_color::ClearColorConfig, core_pipeline::clear_color::ClearColorConfig,

57
src/particle_effect.rs Normal file
View file

@ -0,0 +1,57 @@
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);
}
}