Skip to content

Commit

Permalink
Add particles
Browse files Browse the repository at this point in the history
  • Loading branch information
billyrieger committed Apr 9, 2023
1 parent 77629ab commit a65135d
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
57 changes: 57 additions & 0 deletions src/level.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::{
use bevy::{prelude::*, render::view::RenderLayers, utils::HashMap};
use bevy_ecs_ldtk::prelude::*;
use bevy_ecs_tilemap::prelude::*;
use bevy_particle_systems::*;

const LEVEL_SPAWN_DELAY_SEC: f32 = 1.;
const ACTIVE_LEVEL_COLOR: Color = Color::rgb(1., 1., 1.);
Expand All @@ -27,6 +28,8 @@ impl Plugin for LevelPlugin {
.add_systems(
(
load_level,
add_particles_to_goals.run_if(resource_exists::<CurrentMetaLevel>()),
move_particles_up,
reload_level.run_if(resource_exists::<CurrentMetaLevel>()),
setup_ldtk_levels_on_spawn.run_if(resource_exists::<CurrentMetaLevel>()),
darken_inactive_levels,
Expand Down Expand Up @@ -227,6 +230,9 @@ pub struct GoalBundle {
tile_type: TileType,
}

#[derive(Component)]
pub struct GoalParticles;

#[derive(Component, Default)]
pub struct Wall;

Expand Down Expand Up @@ -335,6 +341,47 @@ fn prepare_level_data(
commands.insert_resource(all_levels);
}

fn add_particles_to_goals(
current_level: Res<CurrentMetaLevel>,
mut commands: Commands,
game_assets: Res<GameAssets>,
goals: Query<(&GridCoords, &Parent), Added<Goal>>,
) {
for (&goal_coords, parent) in &goals {
commands.entity(parent.get()).with_children(|parent| {
parent.spawn(GoalParticles).insert(ParticleSystemBundle {
particle_system: ParticleSystem {
max_particles: 500,
texture: ParticleTexture::Sprite(game_assets.pixel.clone()),
initial_speed: JitteredValue::jittered(500.0, -300.0..300.0),
velocity_modifiers: vec![VelocityModifier::Drag(0.05.into())],
lifetime: JitteredValue::jittered(0.5, -0.25..0.25),
color: ColorOverTime::Gradient(Curve::new(vec![
CurvePoint::new(Color::rgb(0., 1., 0.), 0.0),
CurvePoint::new(Color::rgba(0., 1., 0., 0.), 1.0),
])),
spawn_rate_per_second: ValueOverTime::Constant(0.),
despawn_particles_with_system: true,
space: ParticleSpace::World,
looping: false,
system_duration_seconds: 2.,
max_distance: Some(500.),
scale: 2.0.into(),
bursts: vec![ParticleBurst::new(0.0, 1000)],
..ParticleSystem::default()
},
transform: Transform::from_translation(
current_level
.0
.grid_coords_to_translation(goal_coords)
.extend(0.),
),
..ParticleSystemBundle::default()
});
});
}
}

fn load_level(
mut commands: Commands,
all_levels: Res<AllMetaLevels>,
Expand Down Expand Up @@ -430,6 +477,7 @@ fn check_all_goal_tiles(
current_level: Res<CurrentMetaLevel>,
level_spawn_countdown: Option<Res<LevelSpawnCountdown>>,
goal_query: Query<&Goal>,
goal_particles: Query<Entity, With<GoalParticles>>,
) {
// only continue if we're not already waiting to load a new level
if level_spawn_countdown.is_some() {
Expand All @@ -441,6 +489,9 @@ fn check_all_goal_tiles(
timer: Timer::from_seconds(LEVEL_SPAWN_DELAY_SEC, TimerMode::Once),
level_num: current_level.0.level_num + 1,
});
for goal_particles in &goal_particles {
commands.entity(goal_particles).insert(Playing);
}
}
}

Expand Down Expand Up @@ -490,3 +541,9 @@ fn darken_inactive_levels(
}
}
}

fn move_particles_up(mut particles: Query<&mut Transform, With<Particle>>) {
for mut transform in &mut particles {
transform.translation.z = 20.;
}
}
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use bevy_ecs_ldtk::prelude::*;
#[cfg(not(debug_assertions))]
use bevy_embedded_assets::EmbeddedAssetPlugin;
use bevy_tweening::*;
use bevy_particle_systems::ParticleSystemPlugin;

pub const WIDTH: i32 = 640;
pub const HEIGHT: i32 = 480;
Expand Down Expand Up @@ -48,6 +49,7 @@ impl Plugin for GamePlugin {
// third-party plugins
.add_plugin(LdtkPlugin)
.add_plugin(TweeningPlugin)
.add_plugin(ParticleSystemPlugin)
// .add_plugin(bevy::diagnostic::FrameTimeDiagnosticsPlugin)
// .add_plugin(bevy::diagnostic::LogDiagnosticsPlugin::default())
// game stuff
Expand Down
2 changes: 2 additions & 0 deletions src/loading.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ pub struct GameAssets {
pub levels: Handle<bevy_ecs_ldtk::LdtkAsset>,
#[asset(path = "fonts/Kenney Pixel Square.ttf")]
pub main_font: Handle<Font>,
#[asset(path = "px.png")]
pub pixel: Handle<Image>,
}

0 comments on commit a65135d

Please sign in to comment.