Skip to content

Commit

Permalink
particles kinda working
Browse files Browse the repository at this point in the history
  • Loading branch information
mwbryant committed Apr 21, 2023
1 parent 117c3f2 commit 5a87154
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 43 deletions.
3 changes: 2 additions & 1 deletion src/art/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ pub struct RectParticleEmitter {
#[derive(Component, Default, Clone)]
pub struct ParticleDesc {
pub particle: Particle,
pub sprite: SpriteSheetBundle,
pub sprite: Handle<Image>,
pub sprite_size: (usize, usize),
pub falling: Option<FallingParticle>,
pub radial: Option<RadialParticle>,
pub rotating: Option<RotatingParticle>,
Expand Down
36 changes: 27 additions & 9 deletions src/art/particles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub fn create_new_rect_emitter(
) -> Entity {
let parent = commands
.spawn((
SpatialBundle::from_transform(Transform::from_xyz(position.x, position.y, PARTICLE_Z)),
SpatialBundle::from_transform(Transform::from_xyz(position.x, position.y, 0.0)),
Lifetime {
timer: Timer::from_seconds(
lifetime + particle_desc.particle.lifetime.remaining_secs(),
Expand All @@ -40,7 +40,7 @@ pub fn create_new_rect_emitter(

commands
.spawn((
SpatialBundle::from_transform(Transform::from_xyz(position.x, position.y, PARTICLE_Z)),
SpatialBundle::from_transform(Transform::from_xyz(position.x, position.y, 0.0)),
Lifetime {
timer: Timer::from_seconds(lifetime, TimerMode::Once),
},
Expand All @@ -60,6 +60,8 @@ fn particle_emitter_spawn(
mut commands: Commands,
//Global transforms allow for moving emitters and static parents
mut emitters: Query<(&mut RectParticleEmitter, &GlobalTransform)>,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
parents: Query<&GlobalTransform, With<ParticleParent>>,
time: Res<Time>,
) {
Expand All @@ -72,19 +74,35 @@ fn particle_emitter_spawn(
rng.gen_range((-emitter.size.x / 2.0)..(emitter.size.x / 2.0)),
rng.gen_range((-emitter.size.y / 2.0)..(emitter.size.y / 2.0)),
);
let emitter_to_parent_difference = emitter_transform.translation().truncate()
let emitter_to_parent_difference = emitter_transform.translation()
- parents
.get(emitter.particle_parent)
.expect("No parent")
.translation()
.truncate();
.translation();

//Faster to spawn batch or not noticible?
//TODO move all generic emitter work to a standalone function
let mut sprite = emitter.desc.sprite.clone();
sprite.sprite.index = rng.gen_range(0..emitter.varients);
sprite.transform.translation =
Vec3::new(x_offset, y_offset, 0.0) + emitter_to_parent_difference.extend(0.0);
let sprite = emitter.desc.sprite.clone();
let quad_handle = meshes.add(Mesh::from(shape::Quad::new(Vec2::new(1.0, 1.0))));

let material_handle = materials.add(StandardMaterial {
base_color_texture: Some(sprite),
alpha_mode: AlphaMode::Blend,
double_sided: true,
cull_mode: None,
..default()
});

let sprite = MaterialMeshBundle {
mesh: quad_handle,
material: material_handle,
transform: Transform::from_translation(
Vec3::new(x_offset, y_offset, 0.0) + emitter_to_parent_difference,
),
..default()
};

let index = rng.gen_range(0..emitter.varients);

let mut particle = commands.spawn((sprite, emitter.desc.particle.clone()));

Expand Down
41 changes: 8 additions & 33 deletions src/combat/graphic_effects.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use bevy_inspector_egui::egui::epaint::text;

use crate::prelude::*;

pub struct GraphicEffectsPlugin;
Expand Down Expand Up @@ -59,20 +61,11 @@ fn projectile_particles(
//Create new emitters, lazily
if projectile_emitter.get(projectile).is_err() {
let texture_handle = assets.load("my_art/smoke_particles.png");
let texture_atlas =
TextureAtlas::from_grid(texture_handle, Vec2::new(32.0, 32.0), 1, 1, None, None);
let particle_atlas = texture_atlases.add(texture_atlas);

let particle_desc = ParticleDesc {
particle: Particle::new(1.0),
sprite: SpriteSheetBundle {
sprite: TextureAtlasSprite {
custom_size: Some(Vec2::splat(0.53)),
..default()
},
texture_atlas: particle_atlas,
..default()
},
sprite: texture_handle,
sprite_size: (1, 1),
falling: Some(FallingParticle { speed: 1.0 }),
rotating: Some(RotatingParticle { speed: 2.0 }),
fading: Some(FadingParticle {}),
Expand Down Expand Up @@ -106,20 +99,11 @@ fn spawn_hit_particles(
let translation = target.get(hit.target).expect("No target").translation;

let texture_handle = assets.load("my_art/particles.png");
let texture_atlas =
TextureAtlas::from_grid(texture_handle, Vec2::new(16.0, 16.0), 2, 2, None, None);
let particle_atlas = texture_atlases.add(texture_atlas);

let particle_desc = ParticleDesc {
particle: Particle::new(1.0),
sprite: SpriteSheetBundle {
sprite: TextureAtlasSprite {
custom_size: Some(Vec2::splat(0.13)),
..default()
},
texture_atlas: particle_atlas,
..default()
},
sprite: texture_handle,
sprite_size: (2, 2),
//falling: Some(FallingParticle { speed: 3.0 }),
rotating: Some(RotatingParticle { speed: 10.0 }),
fading: Some(FadingParticle {}),
Expand All @@ -145,20 +129,11 @@ fn spawn_player_win_particles(
mut texture_atlases: ResMut<Assets<TextureAtlas>>,
) {
let texture_handle = assets.load("my_art/particles.png");
let texture_atlas =
TextureAtlas::from_grid(texture_handle, Vec2::new(16.0, 16.0), 2, 2, None, None);
let particle_atlas = texture_atlases.add(texture_atlas);

let particle_desc = ParticleDesc {
particle: Particle::new(3.0),
sprite: SpriteSheetBundle {
sprite: TextureAtlasSprite {
custom_size: Some(Vec2::splat(0.13)),
..default()
},
texture_atlas: particle_atlas,
..default()
},
sprite: texture_handle,
sprite_size: (2, 2),
falling: Some(FallingParticle { speed: 4.0 }),
rotating: Some(RotatingParticle { speed: 10.0 }),
fading: Some(FadingParticle {}),
Expand Down

0 comments on commit 5a87154

Please sign in to comment.