From 6809a30faa3b88ffc8efd8ee67f1fa57d9f71cd2 Mon Sep 17 00:00:00 2001 From: tuxmain Date: Wed, 24 Aug 2022 09:58:56 +0200 Subject: [PATCH] fix: particles on win, fmt --- Cargo.toml | 1 - README.md | 3 -- src/game.rs | 20 +++++------- src/particle_effect.rs | 74 ++++++++++++++++++++++++------------------ 4 files changed, 50 insertions(+), 48 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f0c9072..91c777b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,7 +7,6 @@ edition = "2021" [dependencies] bevy = "0.8.0" -# bevy_hanabi = "0.3.1" bevy-inspector-egui = "0.12.1" bevy_rapier2d = "0.16.2" cpal = "0.14.0" diff --git a/README.md b/README.md index fe29ac0..35639f5 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,6 @@ * level design * (?) can jump only from a surface (no mid-air jump) * (?) multiplayer -* make WASM build work again (replace hanabi) * level reset * more audio @@ -25,8 +24,6 @@ cargo build --release ### WASM -Currently `bevy_hanabi` does not compile for WASM, and audio does not work on WASM. - ```bash rustup target add wasm32-unknown-unknown cargo install wasm-bindgen-cli diff --git a/src/game.rs b/src/game.rs index 3016b66..1b6ef95 100644 --- a/src/game.rs +++ b/src/game.rs @@ -32,14 +32,13 @@ impl Plugin for GamePlugin { SystemSet::on_update(AppState::Game) .with_system(crate::levels::post_setup_level) .with_system(keyboard_input_system) - .with_system(character_particle_effect_system) - .with_system(move_camera), + .with_system(move_camera) .with_system(character_particle_effect_system), ) .add_system_set( SystemSet::on_update(AppState::Win) .with_system(keyboard_input_system) - .with_system(move_camera), + .with_system(move_camera) .with_system(character_particle_effect_system), ) .add_system_to_stage(CoreStage::PostUpdate, collision_event_system); @@ -221,7 +220,6 @@ fn keyboard_input_system( if let Ok((mut selected_character_id, character_id_list)) = level_query.get_single_mut() { if keyboard_input.just_pressed(KeyCode::Tab) { let selected = if let Some(selected_character_id) = &mut selected_character_id.0 { - *selected_character_id = *character_id_list .0 .range(*selected_character_id..) @@ -251,9 +249,8 @@ fn keyboard_input_system( if let Some(selected_character_id) = &selected_character_id.0 { if let Some((_character_id, mut velocity, _color)) = characters .iter_mut() - .find(|(character_id, _velocity, _color)| { - *character_id == selected_character_id - }) { + .find(|(character_id, _velocity, _color)| *character_id == selected_character_id) + { velocity.linvel.x = 200. * (right_pressed as i8 - left_pressed as i8) as f32; if keyboard_input.just_pressed(KeyCode::Space) { @@ -271,16 +268,15 @@ fn keyboard_input_system( fn character_particle_effect_system( mut characters: Query<(&CharacterId, &Transform, &CharacterColor)>, - mut particle_effect: ResMut, - mut level_query: Query<(&SelectedCharacterId)>, + mut particle_effect: ResMut, + mut level_query: Query<&SelectedCharacterId>, ) { if let Ok(selected_character_id) = level_query.get_single_mut() { if let Some(selected_character_id) = &selected_character_id.0 { if let Some((_character_id, transform, color)) = characters .iter_mut() - .find(|(character_id, _transform, _color)| { - *character_id == selected_character_id - }) { + .find(|(character_id, _transform, _color)| *character_id == selected_character_id) + { particle_effect.translation = transform.translation; particle_effect.color = color.0; } diff --git a/src/particle_effect.rs b/src/particle_effect.rs index 42afdc0..1a21793 100644 --- a/src/particle_effect.rs +++ b/src/particle_effect.rs @@ -1,24 +1,25 @@ use bevy::{prelude::*, sprite::Mesh2dHandle}; use rand::Rng; -use rand_distr::{UnitCircle, Distribution}; +use rand_distr::{Distribution, UnitCircle}; pub const POOL_COUNT: usize = 100; pub const MIN_VELOCITY: f32 = 10.0; pub const MAX_VELOCITY: f32 = 100.0; -pub const VELOCITY_SCALE: f32 = 0.1; +pub const _VELOCITY_SCALE: f32 = 0.1; pub const PARTICLE_EFFECT_RADIUS: f32 = 70.0; pub struct ParticleEffectPlugin; impl Plugin for ParticleEffectPlugin { - fn build(&self, app: &mut App) { - app - .init_resource::() - .insert_resource(ParticleEffectResource::new(Vec3::new(10000.0, 10000.0, 0.0))) - .add_startup_system(particle_effect_startup) + fn build(&self, app: &mut App) { + app.init_resource::() + .insert_resource(ParticleEffectResource::new(Vec3::new( + 10000.0, 10000.0, 0.0, + ))) + .add_startup_system(particle_effect_startup) .add_system(particle_effect_system); - } + } } pub struct ParticleMesh { @@ -42,9 +43,9 @@ impl FromWorld for ParticleMesh { #[derive(bevy_inspector_egui::Inspectable)] pub struct ParticleEffectResource { - pub translation: Vec3, + pub translation: Vec3, pub prev_translation: Vec3, - pub radius_squared: f32, + pub radius_squared: f32, pub color: Color, } @@ -67,60 +68,69 @@ pub struct ParticleComponent { impl ParticleComponent { pub fn new() -> Self { let mut particle_component: Self = Self::default(); - particle_component.randomize_velocity(MIN_VELOCITY, MAX_VELOCITY); - return particle_component; + particle_component.randomize_velocity(MIN_VELOCITY, MAX_VELOCITY); + particle_component } pub fn randomize_velocity(&mut self, min_velocity: f32, max_velocity: f32) { - let random_direction: [f32; 2] = UnitCircle.sample(&mut rand::thread_rng()); - let random_magnitude: f32 = rand::thread_rng().gen_range(min_velocity .. max_velocity); - self.velocity = Vec3::new(random_direction[0], random_direction[1], 0.0) * random_magnitude; + let random_direction: [f32; 2] = UnitCircle.sample(&mut rand::thread_rng()); + let random_magnitude: f32 = rand::thread_rng().gen_range(min_velocity..max_velocity); + self.velocity = Vec3::new(random_direction[0], random_direction[1], 0.0) * random_magnitude; } } fn particle_effect_startup( - mut commands: Commands, + mut commands: Commands, particle_mesh: Res, mut materials: ResMut>, ) { - for _p in 0 .. POOL_COUNT { - let color_mesh = ColorMesh2dBundle { + for _p in 0..POOL_COUNT { + let color_mesh = ColorMesh2dBundle { mesh: particle_mesh.square.clone(), material: materials.add(ColorMaterial::from(Color::WHITE)), ..default() }; - commands - .spawn_bundle(color_mesh) - .insert(ParticleComponent::new()); - } + commands + .spawn_bundle(color_mesh) + .insert(ParticleComponent::new()); + } } fn particle_effect_system( mut materials: ResMut>, - mut query: Query<(&mut Transform, &mut ParticleComponent, &Handle)>, - mut particle_effect: ResMut, - time: Res