Some refactoring to prep for better compartmentalization later

This commit is contained in:
JP Stringham
2026-02-03 21:11:15 -05:00
parent 210006be9c
commit 0d1fcf9333

View File

@@ -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<NextState<AppState>>,
mut tmap_stuff: ResMut<TilemapStuff>,
mut t_layouts: ResMut<Assets<TextureAtlasLayout>>,
lvl: Res<LevelData>,
) {
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<LevelData>,
tmap_stuff: Res<TilemapStuff>,
mut ta_layouts: ResMut<Assets<TextureAtlasLayout>>,
) {
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() {}