A Quadrilateral Cowboy clone intended to help me learn Game Dev

Fixed loading code

+23 -14
+2 -1
assets/levels/level1.level.ron
··· 1 1 ( 2 - mesh_path: "levels/level1/level1.gltf" 2 + mesh_path: "levels/level1/level1.gltf", 3 + name: "Level 1" 3 4 )
+21 -13
src/level.rs
··· 5 5 6 6 pub fn plugin(app: &mut App) { 7 7 app.add_plugins(RonAssetPlugin::<Level>::new(&["level.ron"])) 8 - .add_systems(OnEnter(GameState::Loading), load_levels); 8 + .add_systems(OnEnter(GameState::Loading), load_levels) 9 + .add_systems(OnEnter(GameState::Main), list_levels); 9 10 } 10 11 11 12 #[derive(serde::Deserialize, bevy::asset::Asset, bevy::reflect::TypePath)] 12 13 pub struct Level { 13 - mesh_path: String 14 + mesh_path: String, 15 + pub name: String 14 16 } 15 - 16 - #[derive(Resource)] 17 - struct LevelHandle(Handle<Level>); 18 17 19 18 #[derive(Resource)] 20 19 pub struct Levels { 21 - pub levels: Vec<LevelHandle> 20 + pub levels: Vec<Handle<Level>> 22 21 } 23 22 24 23 fn load_levels(mut commands: Commands, asset_server: Res<AssetServer>, mut next_state: ResMut<NextState<GameState>>) { 25 24 commands.insert_resource(Levels{ 26 - levels: fs::read_dir(path::Path::new("assets").join("levels")).unwrap().map(|entry| -> Option<LevelHandle> { 25 + levels: fs::read_dir(path::Path::new("assets").join("levels")).unwrap().map(|entry| -> Option<Handle<Level>> { 27 26 let entry = entry.unwrap(); 28 - 29 - if entry.path().ends_with(".level.ron") { 30 - Some(LevelHandle(asset_server.load(entry.path()))) 27 + 28 + if entry.path().is_dir() { 29 + None 30 + } else if entry.path().to_str().unwrap().ends_with(".level.ron") { 31 + println!("Loading {}", entry.path().to_str().unwrap()); 32 + Some(asset_server.load(entry.path().to_str().unwrap().replace("assets/", ""))) 31 33 } else { 32 34 None 33 - } 35 + } 34 36 }).filter(|val| -> bool { 35 37 matches!(val, Some(_)) 36 - }).map(|val| -> LevelHandle { 38 + }).map(|val| -> Handle<Level> { 37 39 val.unwrap() 38 - }).collect::<Vec<LevelHandle>>() 40 + }).collect::<Vec<Handle<Level>>>() 39 41 }); 40 42 41 43 next_state.set(GameState::Main); 44 + } 45 + 46 + fn list_levels(levels: Res<Levels>, level_data: Res<Assets<Level>>) { 47 + println!("Levels: {}", levels.levels.iter().map(|val| { 48 + level_data.get(val).unwrap().name.clone() 49 + }).collect::<Vec<String>>().join(", ")); 42 50 }