From 0d1fcf9333ae152dec74e475976a2cc605c92ef7 Mon Sep 17 00:00:00 2001 From: JP Stringham Date: Tue, 3 Feb 2026 21:11:15 -0500 Subject: [PATCH] Some refactoring to prep for better compartmentalization later --- src/main.rs | 64 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 36 insertions(+), 28 deletions(-) diff --git a/src/main.rs b/src/main.rs index 95357a0..5985d69 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,8 +5,9 @@ use bevy::{image::Image, prelude::*}; #[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, States)] enum AppState { #[default] - Setup, + Init, Preload, + LevelLoad, Main, } @@ -35,6 +36,7 @@ fn main() { .insert_resource(TilemapStuff::default()) .add_systems(Startup, init) .add_systems(Update, loadloop.run_if(in_state(AppState::Preload))) + .add_systems(OnEnter(AppState::LevelLoad), levelload) .add_systems(Update, gameloop.run_if(in_state(AppState::Main))); app.run(); @@ -45,8 +47,6 @@ fn init( mut commands: Commands, mut state: ResMut>, mut tmap_stuff: ResMut, - mut t_layouts: ResMut>, - lvl: Res, ) { println!("Hello Bevy!"); @@ -60,30 +60,6 @@ fn init( tmap_stuff.img_id = Some(sprite.id()); tmap_stuff.img_handle = Some(sprite.clone()); - let t_layout = TextureAtlasLayout::from_grid(uvec2(16, 16), 12, 8, Some(uvec2(1, 1)), None); - let t_handle = t_layouts.add(t_layout); - - // create map from lvl data - - let lvl = &lvl.ldtk_proj.levels[0]; - - let mut lyr_dep = 50f32; - lvl.layer_instances.iter().for_each(|li| { - li.al_tiles.iter().for_each(|t| { - commands.spawn(( - Sprite::from_atlas_image( - sprite.clone(), - TextureAtlas { - layout: t_handle.clone(), - index: t.ty as usize, - }, - ), - Transform::from_xyz(t.px_coords.x as f32, 256. - t.px_coords.y as f32, lyr_dep), - )); - }); - lyr_dep -= 0.1; - }); - state.set(AppState::Preload); } @@ -109,7 +85,7 @@ fn loadloop( if tmap_stuff.img_id.as_ref().unwrap() == id { println!("Loaded tmap"); - next_state.set(AppState::Main); + next_state.set(AppState::LevelLoad); } } _ => (), @@ -117,4 +93,36 @@ fn loadloop( } } +fn levelload( + mut commands: Commands, + lvl: Res, + tmap_stuff: Res, + mut ta_layouts: ResMut>, +) { + let tmap_sprite = tmap_stuff.img_handle.as_ref().unwrap(); + let ta_layout = TextureAtlasLayout::from_grid(uvec2(16, 16), 12, 8, Some(uvec2(1, 1)), None); + let ta_handle = ta_layouts.add(ta_layout); + + // create map from lvl data + + let lvl = &lvl.ldtk_proj.levels[0]; + + let mut lyr_dep = 50f32; + lvl.layer_instances.iter().for_each(|li| { + li.al_tiles.iter().for_each(|t| { + commands.spawn(( + Sprite::from_atlas_image( + tmap_sprite.clone(), + TextureAtlas { + layout: ta_handle.clone(), + index: t.ty as usize, + }, + ), + Transform::from_xyz(t.px_coords.x as f32, 256. - t.px_coords.y as f32, lyr_dep), + )); + }); + lyr_dep -= 0.1; + }); +} + fn gameloop() {}