Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade bevy 0.9 #16

Merged
merged 3 commits into from
Nov 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:

env:
CARGO_TERM_COLOR: always
RUST_VERSION: 1.62.0
RUST_VERSION: 1.65.0

jobs:
test:
Expand Down
22 changes: 11 additions & 11 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "bevy_particle_systems"
version = "0.5.0"
edition = "2021"
rust-version = "1.59"
rust-version = "1.65"
authors = ["Abnormal Brain Studios"]
license = "MIT"
description = "A particle system plugin for Bevy"
Expand All @@ -11,16 +11,16 @@ keywords = ["game", "gamedev", "bevy"]
categories = ["game-development"]

[dependencies]
bevy_app = "0.8"
bevy_asset = "0.8"
bevy_ecs = "0.8"
bevy_hierarchy = "0.8"
bevy_math = "0.8"
bevy_render = "0.8"
bevy_sprite = "0.8"
bevy_time = "0.8"
bevy_transform = "0.8"
bevy_app = "0.9"
bevy_asset = "0.9"
bevy_ecs = "0.9"
bevy_hierarchy = "0.9"
bevy_math = "0.9"
bevy_render = "0.9"
bevy_sprite = "0.9"
bevy_time = "0.9"
bevy_transform = "0.9"
rand = "0.8"

[dev-dependencies]
bevy = { version = "0.8", default-features=false, features = ["bevy_asset", "render", "png", "bevy_winit", "x11"] }
bevy = { version = "0.9", default-features=false, features = ["bevy_asset", "render", "png", "bevy_winit", "x11"] }
19 changes: 11 additions & 8 deletions examples/basic.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use bevy::{
diagnostic::{EntityCountDiagnosticsPlugin, FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
prelude::{App, Camera2dBundle, ClearColor, Color, Commands, Res},
window::{PresentMode, WindowDescriptor},
window::{PresentMode, WindowDescriptor, WindowPlugin},
DefaultPlugins,
};
use bevy_app::PluginGroup;
use bevy_asset::AssetServer;
use bevy_particle_systems::{
ColorOverTime, ColorPoint, Gradient, JitteredValue, ParticleBurst, ParticleSystem,
Expand All @@ -13,24 +14,26 @@ use bevy_particle_systems::{
fn main() {
App::new()
.insert_resource(ClearColor(Color::BLACK))
.insert_resource(WindowDescriptor {
present_mode: PresentMode::Immediate,
..WindowDescriptor::default()
})
.add_plugin(EntityCountDiagnosticsPlugin)
.add_plugin(FrameTimeDiagnosticsPlugin::default())
.add_plugin(LogDiagnosticsPlugin::default())
.add_plugins(DefaultPlugins)
.add_plugins(DefaultPlugins.set(WindowPlugin {
window: WindowDescriptor {
present_mode: PresentMode::AutoNoVsync,
..Default::default()
},
..Default::default()
}))
.add_plugin(ParticleSystemPlugin::default()) // <-- Add the plugin
.add_startup_system(startup_system)
.run();
}

fn startup_system(mut commands: Commands, asset_server: Res<AssetServer>) {
commands.spawn_bundle(Camera2dBundle::default());
commands.spawn(Camera2dBundle::default());

commands
.spawn_bundle(ParticleSystemBundle {
.spawn(ParticleSystemBundle {
particle_system: ParticleSystem {
max_particles: 50_000,
default_sprite: asset_server.load("px.png"),
Expand Down
8 changes: 4 additions & 4 deletions examples/local_space.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ fn main() {
}

fn startup_system(mut commands: Commands, asset_server: Res<AssetServer>) {
commands.spawn_bundle(Camera2dBundle::default());
commands.spawn(Camera2dBundle::default());

commands
.spawn_bundle(ParticleSystemBundle {
.spawn(ParticleSystemBundle {
particle_system: ParticleSystem {
max_particles: 500,
emitter_shape: std::f32::consts::PI * 0.25,
Expand All @@ -62,7 +62,7 @@ fn startup_system(mut commands: Commands, asset_server: Res<AssetServer>) {
.insert(Circler::new(Vec3::new(50.0, 0.0, 0.0), 50.0));

commands
.spawn_bundle(ParticleSystemBundle {
.spawn(ParticleSystemBundle {
particle_system: ParticleSystem {
max_particles: 500,
emitter_shape: std::f32::consts::PI * 0.25,
Expand Down Expand Up @@ -105,7 +105,7 @@ pub fn circler(
time: Res<Time>,
mut particle_system_query: Query<(&Circler, &mut Transform), With<ParticleSystem>>,
) {
let rad = time.seconds_since_startup() as f32;
let rad = time.elapsed_seconds() as f32;
let quat = Quat::from_rotation_z(rad).normalize();
let dir = quat * Vec3::Y;
for (circler, mut transform) in &mut particle_system_query {
Expand Down
24 changes: 12 additions & 12 deletions examples/time_scaling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,23 @@ use bevy::{
use bevy_asset::AssetServer;
use bevy_particle_systems::{
ColorOverTime, ColorPoint, Gradient, JitteredValue, ParticleSpace, ParticleSystem,
ParticleSystemBundle, ParticleSystemPlugin, Playing, TimeScale,
ParticleSystemBundle, ParticleSystemPlugin, Playing,
};
use bevy_time::Time;
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugin(ParticleSystemPlugin::default()) // <-- Add the plugin
.add_startup_system(startup_system)
.insert_resource(Some(TimeScale::default()))
.add_system(time_scale_changer)
.run();
}

fn startup_system(mut commands: Commands, asset_server: Res<AssetServer>) {
commands.spawn_bundle(Camera2dBundle::default());
commands.spawn(Camera2dBundle::default());

commands
.spawn_bundle(ParticleSystemBundle {
.spawn(ParticleSystemBundle {
particle_system: ParticleSystem {
max_particles: 500,
emitter_shape: std::f32::consts::PI * 0.25,
Expand All @@ -54,7 +54,7 @@ fn startup_system(mut commands: Commands, asset_server: Res<AssetServer>) {
.insert(Playing);

commands
.spawn_bundle(ParticleSystemBundle {
.spawn(ParticleSystemBundle {
particle_system: ParticleSystem {
max_particles: 500,
emitter_shape: std::f32::consts::PI * 0.25,
Expand All @@ -80,15 +80,15 @@ fn startup_system(mut commands: Commands, asset_server: Res<AssetServer>) {
.insert(Playing);
}

fn time_scale_changer(keys: Res<Input<KeyCode>>, mut time_scale: ResMut<Option<TimeScale>>) {
fn time_scale_changer(keys: Res<Input<KeyCode>>, mut time: ResMut<Time>) {
for key_code in keys.get_just_pressed() {
match key_code {
KeyCode::Key1 => *time_scale = Some(TimeScale(1.0)),
KeyCode::Key2 => *time_scale = Some(TimeScale(2.0)),
KeyCode::Key3 => *time_scale = Some(TimeScale(4.0)),
KeyCode::Key4 => *time_scale = Some(TimeScale(8.0)),
KeyCode::Key5 => *time_scale = Some(TimeScale(10.0)),
KeyCode::Key0 => *time_scale = Some(TimeScale(0.0)),
KeyCode::Key1 => time.set_relative_speed(1.0),
KeyCode::Key2 => time.set_relative_speed(2.0),
KeyCode::Key3 => time.set_relative_speed(4.0),
KeyCode::Key4 => time.set_relative_speed(8.0),
KeyCode::Key5 => time.set_relative_speed(10.0),
KeyCode::Key0 => time.set_relative_speed(0.0),
_ => {}
}
}
Expand Down
18 changes: 2 additions & 16 deletions src/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ pub struct ParticleSystem {
/// What coordinate space particles should use.
pub space: ParticleSpace,

/// `true` indicates that the system will use scaled time if it is present, `false` will result in always using real time.
/// Dictates whether this system respects Bevy's global time scaling by using [`bevy_time::Time::delta_seconds`] when true, or [`bevy_time::Time::raw_delta_seconds`] when false.
pub use_scaled_time: bool,

/// Indicates that the entity the [`ParticleSystem`] is on should be despawned when the system completes and has no more particles.
Expand Down Expand Up @@ -291,28 +291,14 @@ pub struct ParticleSystemBundle {
pub burst_index: BurstIndex,

/// Required for child particles to be visible when running in Local space.
#[bundle]
pub visibility: VisibilityBundle,
}

#[derive(Debug, Bundle, Default)]
#[derive(Debug, Default, Bundle)]
pub(crate) struct ParticleBundle {
pub particle: Particle,
pub lifetime: Lifetime,
pub velocity: Velocity,
pub direction: Direction,
pub distance: DistanceTraveled,
}

/// Specifies the time scaling for all particle systems.
///
/// This can be used to speed up the particle system if the speed of time of the game changes.
/// The contained value is used as a multiplier for the frame delta time.
#[derive(Debug, Clone, Copy)]
pub struct TimeScale(pub f32);

impl Default for TimeScale {
fn default() -> Self {
Self(1.0)
}
}
3 changes: 1 addition & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ impl Plugin for ParticleSystemPlugin {
.add_system(particle_lifetime)
.add_system(particle_color)
.add_system(particle_transform)
.add_system(particle_cleanup)
.init_resource::<Option<TimeScale>>();
.add_system(particle_cleanup);
}
}
53 changes: 23 additions & 30 deletions src/systems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use rand::prelude::*;
use crate::{
components::{
BurstIndex, Direction, Lifetime, Particle, ParticleBundle, ParticleCount, ParticleSpace,
ParticleSystem, Playing, RunningState, TimeScale, Velocity,
ParticleSystem, Playing, RunningState, Velocity,
},
values::ColorOverTime,
DistanceTraveled,
Expand All @@ -35,7 +35,6 @@ pub fn partcle_spawner(
With<Playing>,
>,
time: Res<Time>,
time_scale: Res<Option<TimeScale>>,
mut commands: Commands,
) {
let mut rng = rand::thread_rng();
Expand All @@ -48,12 +47,11 @@ pub fn partcle_spawner(
mut burst_index,
) in particle_systems.iter_mut()
{
let time_scale = if particle_system.use_scaled_time {
time_scale.map_or(1.0, |t| t.0)
if particle_system.use_scaled_time {
running_state.running_time += time.delta_seconds();
} else {
1.0
};
running_state.running_time += time.delta_seconds() * time_scale;
running_state.running_time += time.raw_delta_seconds();
}

if running_state.running_time.floor() > running_state.current_second + 0.5 {
running_state.current_second = running_state.running_time.floor();
Expand Down Expand Up @@ -133,7 +131,7 @@ pub fn partcle_spawner(
match particle_system.space {
ParticleSpace::World => {
commands
.spawn_bundle(ParticleBundle {
.spawn(ParticleBundle {
particle: Particle {
parent_system: entity,
max_lifetime: particle_system.lifetime.get_value(&mut rng),
Expand All @@ -148,7 +146,7 @@ pub fn partcle_spawner(
),
..ParticleBundle::default()
})
.insert_bundle(SpriteBundle {
.insert(SpriteBundle {
sprite: Sprite {
color: particle_system.color.at_lifetime_pct(0.0),
..Sprite::default()
Expand All @@ -161,7 +159,7 @@ pub fn partcle_spawner(
ParticleSpace::Local => {
commands.entity(entity).with_children(|parent| {
parent
.spawn_bundle(ParticleBundle {
.spawn(ParticleBundle {
particle: Particle {
parent_system: entity,
max_lifetime: particle_system.lifetime.get_value(&mut rng),
Expand All @@ -176,7 +174,7 @@ pub fn partcle_spawner(
),
..ParticleBundle::default()
})
.insert_bundle(SpriteBundle {
.insert(SpriteBundle {
sprite: Sprite {
color: particle_system.color.at_lifetime_pct(0.0),
..Sprite::default()
Expand All @@ -198,19 +196,16 @@ pub fn partcle_spawner(
pub(crate) fn particle_lifetime(
mut lifetime_query: Query<(&mut Lifetime, &Particle)>,
time: Res<Time>,
time_scale: Res<Option<TimeScale>>,
particle_system_query: Query<&ParticleSystem>,
) {
lifetime_query.par_for_each_mut(512, |(mut lifetime, particle)| {
let mut scale_value = 1.0;
if let Some(t) = time_scale.as_ref() {
if let Ok(particle_system) = particle_system_query.get(particle.parent_system) {
if particle_system.use_scaled_time {
scale_value = t.0;
}
if let Ok(particle_system) = particle_system_query.get(particle.parent_system) {
if particle_system.use_scaled_time {
lifetime.0 += time.delta_seconds();
} else {
lifetime.0 += time.raw_delta_seconds();
}
}
lifetime.0 += time.delta_seconds() * scale_value;
});
}

Expand Down Expand Up @@ -242,25 +237,23 @@ pub(crate) fn particle_transform(
)>,
particle_system_query: Query<&ParticleSystem>,
time: Res<Time>,
time_scale: Res<Option<TimeScale>>,
) {
particle_query.par_for_each_mut(
512,
|(particle, lifetime, direction, mut distance, mut velocity, mut transform)| {
if let Ok(particle_system) = particle_system_query.get(particle.parent_system) {
let mut scale_value = 1.0;
if particle_system.use_scaled_time {
if let Some(t) = time_scale.as_ref() {
scale_value = t.0;
}
}
let lifetime_pct = lifetime.0 / particle.max_lifetime;
velocity.0 += particle_system.acceleration.at_lifetime_pct(lifetime_pct)
* time.delta_seconds();
let initial_position = transform.translation;
if particle_system.use_scaled_time {
velocity.0 += particle_system.acceleration.at_lifetime_pct(lifetime_pct)
* time.delta_seconds();
transform.translation += direction.0 * velocity.0 * time.delta_seconds();
} else {
velocity.0 += particle_system.acceleration.at_lifetime_pct(lifetime_pct)
* time.raw_delta_seconds();
transform.translation += direction.0 * velocity.0 * time.raw_delta_seconds();
}

transform.translation +=
direction.0 * velocity.0 * time.delta_seconds() * scale_value;
transform.scale = Vec3::splat(particle_system.scale.at_lifetime_pct(lifetime_pct));

distance.0 += transform.translation.distance(initial_position);
Expand Down