From e5b041c2c9603ebdb51f85f41ae87acf3f6c8dab Mon Sep 17 00:00:00 2001 From: tuxmain Date: Sat, 27 Aug 2022 09:08:14 +0200 Subject: [PATCH 1/5] Win text follows camera --- README.md | 5 ++++- src/game.rs | 25 +++++++++++++++++++++---- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index bcce2c6..ca17f04 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,10 @@ * (?) multiplayer * more audio * "jumpable" component to avoid jumping on sensors -* bug: in level2, move the blue character to win, then reset. The characters are lighter than expected. +* bug: in level2, move the blue character to win, then reset. The characters are lighter than expected. (also level 4) +* wasm warning +* redshift warning +* itchio test ## Build diff --git a/src/game.rs b/src/game.rs index 2463d05..3c7c8fd 100644 --- a/src/game.rs +++ b/src/game.rs @@ -52,7 +52,8 @@ impl Plugin for GamePlugin { .with_system(player_movement_system) .with_system(level_keyboard_system) .with_system(move_camera) - .with_system(character_particle_effect_system), + .with_system(character_particle_effect_system) + .with_system(move_win_text_system), ) .add_system_to_stage(CoreStage::PostUpdate, collision_event_system); } @@ -101,6 +102,9 @@ pub struct CollisionCount(usize); #[derive(Component)] pub struct Melty(pub Color); +#[derive(Component)] +pub struct WinText; + // Systems fn setup( @@ -287,7 +291,7 @@ fn collision_event_system( // If color approximately white if app_state.current() == &AppState::Game - && Vec4::from(new_color).min_element() >= 0.9 + && new_color.min_element() >= 0.9 { app_state.replace(AppState::Win).ok(); } @@ -491,7 +495,8 @@ fn win_setup( transform: Transform::from_xyz(0., 0., 3.), ..default() }) - .insert(Level); + .insert(Level) + .insert(WinText); commands .spawn_bundle(Text2dBundle { text: Text::from_section( @@ -506,7 +511,8 @@ fn win_setup( transform: Transform::from_xyz(0., 0., 4.), ..Default::default() }) - .insert(Level); + .insert(Level) + .insert(WinText); } fn move_camera( @@ -535,6 +541,17 @@ fn move_camera( } } +fn move_win_text_system( + camera_query: Query<&Transform, With>, + mut win_text_query: Query<&mut Transform, (With, Without)>, +) { + let camera_pos = camera_query.single(); + for mut pos in win_text_query.iter_mut() { + pos.translation.x = camera_pos.translation.x; + pos.translation.y = camera_pos.translation.y; + } +} + fn level_keyboard_system( mut commands: Commands, mut current_level: ResMut, From f3f74f668adda4ff01a6baae9653357426b13555 Mon Sep 17 00:00:00 2001 From: tuxmain Date: Sat, 27 Aug 2022 09:34:21 +0200 Subject: [PATCH 2/5] Warn wasm users about audio not working --- README.md | 1 - src/menu.rs | 16 ++++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ca17f04..b53f02d 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,6 @@ * more audio * "jumpable" component to avoid jumping on sensors * bug: in level2, move the blue character to win, then reset. The characters are lighter than expected. (also level 4) -* wasm warning * redshift warning * itchio test diff --git a/src/menu.rs b/src/menu.rs index fff9778..f35f024 100644 --- a/src/menu.rs +++ b/src/menu.rs @@ -20,6 +20,22 @@ impl Plugin for MenuPlugin { fn setup(mut commands: Commands, asset_server: Res) { let font = asset_server.get_handle("UacariLegacy-Thin.ttf"); + #[cfg(target_arch = "wasm32")] + commands + .spawn_bundle(Text2dBundle { + text: Text::from_section( + "Note:\nAudio is NOT available in the WASM build.", + TextStyle { + font: font.clone(), + font_size: 24.0, + color: Color::rgba(1., 0.4, 0.4, 1.), + }, + ) + .with_alignment(TextAlignment::CENTER), + transform: Transform::from_xyz(0., -128.0, 0.), + ..Default::default() + }) + .insert(Menu); commands .spawn_bundle(Text2dBundle { text: Text::from_section( From 9f04ec861ed535d543bb3079df9ccb059431c42a Mon Sep 17 00:00:00 2001 From: tuxmain Date: Sat, 27 Aug 2022 09:35:01 +0200 Subject: [PATCH 3/5] opti(particles): fetch ThreadRng only once in a system --- src/game.rs | 3 +-- src/particle_effect.rs | 25 +++++++++++++++++-------- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/game.rs b/src/game.rs index 3c7c8fd..c98f48e 100644 --- a/src/game.rs +++ b/src/game.rs @@ -290,8 +290,7 @@ fn collision_event_system( .clamp(Vec4::ZERO, Vec4::ONE); // If color approximately white - if app_state.current() == &AppState::Game - && new_color.min_element() >= 0.9 + if app_state.current() == &AppState::Game && new_color.min_element() >= 0.9 { app_state.replace(AppState::Win).ok(); } diff --git a/src/particle_effect.rs b/src/particle_effect.rs index b711548..26e275a 100644 --- a/src/particle_effect.rs +++ b/src/particle_effect.rs @@ -1,5 +1,5 @@ use bevy::{prelude::*, sprite::Mesh2dHandle}; -use rand::Rng; +use rand::{rngs::ThreadRng, Rng}; use rand_distr::{Distribution, UnitCircle}; #[cfg(not(target_arch = "wasm32"))] @@ -68,15 +68,20 @@ pub struct ParticleComponent { } impl ParticleComponent { - pub fn new() -> Self { + pub fn new(rng: &mut ThreadRng) -> Self { let mut particle_component: Self = Self::default(); - particle_component.randomize_velocity(MIN_VELOCITY, MAX_VELOCITY); + particle_component.randomize_velocity(rng, 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); + pub fn randomize_velocity( + &mut self, + rng: &mut ThreadRng, + min_velocity: f32, + max_velocity: f32, + ) { + let random_direction: [f32; 2] = UnitCircle.sample(rng); + let random_magnitude: f32 = rng.gen_range(min_velocity..max_velocity); self.velocity = Vec3::new(random_direction[0], random_direction[1], 0.0) * random_magnitude; } } @@ -87,6 +92,8 @@ fn particle_effect_startup( particle_mesh: Res, mut materials: ResMut>, ) { + let mut rng = rand::thread_rng(); + for _p in 0..POOL_COUNT { let color_mesh = ColorMesh2dBundle { mesh: particle_mesh.square.clone(), @@ -96,7 +103,7 @@ fn particle_effect_startup( commands .spawn_bundle(color_mesh) - .insert(ParticleComponent::new()); + .insert(ParticleComponent::new(&mut rng)); } } @@ -110,6 +117,8 @@ fn particle_effect_system( mut particle_effect: ResMut, time: Res