From 3533f653c376d8a0c04de9cbe6334dd619af0a18 Mon Sep 17 00:00:00 2001 From: mnmaita <47983254+mnmaita@users.noreply.github.com> Date: Sat, 11 Nov 2023 23:15:32 +0100 Subject: [PATCH] Adds textures module --- src/main.rs | 4 +++- src/textures.rs | 54 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 src/textures.rs diff --git a/src/main.rs b/src/main.rs index 76e63ff..f6b0e5b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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(); } diff --git a/src/textures.rs b/src/textures.rs new file mode 100644 index 0000000..f6fb2bd --- /dev/null +++ b/src/textures.rs @@ -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::(); + app.init_resource::(); + 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); + +fn load_textures(mut commands: Commands, asset_server: Res) { + 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, + textures_folder_handle: Res, + asset_server: Res, +) { + 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)) +}