Skip to content

Commit

Permalink
Adds textures module
Browse files Browse the repository at this point in the history
  • Loading branch information
mnmaita committed Nov 11, 2023
1 parent c203cc6 commit 3533f65
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use audio::AudioPlugin;
use bevy::prelude::*;
use textures::TexturesPlugin;

mod audio;
mod textures;

fn main() {
let mut app = App::new();

app.add_plugins((DefaultPlugins, AudioPlugin));
app.add_plugins((DefaultPlugins, AudioPlugin, TexturesPlugin));

app.run();
}
54 changes: 54 additions & 0 deletions src/textures.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use bevy::{
asset::{LoadedFolder, RecursiveDependencyLoadState},
prelude::*,
};

pub const ASSET_FOLDER_TEXTURES: &str = "textures";

pub struct TexturesPlugin;

impl Plugin for TexturesPlugin {
fn build(&self, app: &mut App) {
app.init_resource::<TexturesLoadState>();
app.init_resource::<TextureFolderHandle>();
app.add_systems(Startup, load_textures);
app.add_systems(
Update,
update_texture_assets_load_state.run_if(resource_equals(TexturesLoadState::default())),
);
}
}

#[derive(Resource, PartialEq)]
struct TexturesLoadState(RecursiveDependencyLoadState);

impl Default for TexturesLoadState {
fn default() -> Self {
Self(RecursiveDependencyLoadState::NotLoaded)
}
}

impl TexturesLoadState {
pub const LOADED: Self = Self(RecursiveDependencyLoadState::Loaded);
}

#[derive(Resource, Default, Deref, DerefMut)]
struct TextureFolderHandle(Handle<LoadedFolder>);

fn load_textures(mut commands: Commands, asset_server: Res<AssetServer>) {
let textures_folder_handle = asset_server.load_folder(ASSET_FOLDER_TEXTURES);
commands.insert_resource(TextureFolderHandle(textures_folder_handle));
}

fn update_texture_assets_load_state(
mut textures_load_state: ResMut<TexturesLoadState>,
textures_folder_handle: Res<TextureFolderHandle>,
asset_server: Res<AssetServer>,
) {
textures_load_state.0 =
asset_server.recursive_dependency_load_state(textures_folder_handle.id());
}

pub fn texture_assets_loaded() -> impl Condition<()> {
IntoSystem::into_system(resource_equals(TexturesLoadState::LOADED))
}

0 comments on commit 3533f65

Please sign in to comment.