Skip to content

Commit

Permalink
Fix Bug With Hot Reload and New Loading Progress
Browse files Browse the repository at this point in the history
PR #164 broke hot reloading by accidentally triggering asset update
events every time it would check the game load progress.

This moves game load progress checking to a system condition instead
with a read-only borrow to the game asset to prevent the issue.
  • Loading branch information
zicklag committed Jul 29, 2022
1 parent d36eb6c commit 4d85333
Showing 1 changed file with 29 additions and 20 deletions.
49 changes: 29 additions & 20 deletions src/loading.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
use bevy::{asset::AssetStage, prelude::*};
use bevy_parallax::ParallaxResource;
use bevy_rapier2d::prelude::CollisionGroups;
use iyes_loopless::{
prelude::{ConditionSet, IntoConditionalSystem},
state::NextState,
};
use iyes_loopless::{prelude::*, state::NextState};

use rand::seq::SliceRandom;

Expand Down Expand Up @@ -48,7 +45,11 @@ impl Plugin for LoadingPlugin {
let engine_config = EngineConfig::from_web_params();

app.add_system(load_level.run_in_state(GameState::LoadingLevel))
.add_system(load_game.run_in_state(GameState::LoadingGame))
.add_system(
load_game
.run_in_state(GameState::LoadingGame)
.run_if(game_assets_loaded),
)
.add_system_set(
ConditionSet::new()
.run_in_state(GameState::InGame)
Expand Down Expand Up @@ -77,6 +78,28 @@ impl Plugin for LoadingPlugin {
}
}

// Condition system used to make sure game assets have loaded
fn game_assets_loaded(
game_handle: Res<Handle<GameMeta>>,
loading_resources: LoadingResources,
game_assets: Res<Assets<GameMeta>>,
) -> bool {
if let Some(game) = game_assets.get(game_handle.id) {
// Track load progress
let load_progress = game.load_progress(&loading_resources);
debug!(
%load_progress,
"Loading game assets: {:.2}% ",
load_progress.as_percent()
);

// Wait until assets are loaded to start game
load_progress.as_percent() >= 1.0
} else {
false
}
}

/// System param used to load and hot reload the game
#[derive(SystemParam)]
pub struct GameLoader<'w, 's> {
Expand All @@ -87,7 +110,6 @@ pub struct GameLoader<'w, 's> {
assets: ResMut<'w, Assets<GameMeta>>,
egui_ctx: ResMut<'w, EguiContext>,
events: EventReader<'w, 's, AssetEvent<GameMeta>>,
loading_resources: LoadingResources<'w, 's>,
}

impl<'w, 's> GameLoader<'w, 's> {
Expand All @@ -113,20 +135,7 @@ impl<'w, 's> GameLoader<'w, 's> {
..
} = self;

if let Some(game) = assets.get_mut(game_handle.clone_weak()) {
// Track load progress
let load_progress = game.load_progress(&self.loading_resources);
debug!(
%load_progress,
"Loading game assets: {:.2}% ",
load_progress.as_percent()
);

// Wait until assets are loaded to start game
if load_progress.as_percent() < 1.0 {
return;
}

if let Some(game) = assets.get_mut(game_handle.id) {
// Hot reload preparation
if is_hot_reload {
// Despawn previous camera
Expand Down

0 comments on commit 4d85333

Please sign in to comment.