AutoLayer rendering working sorta

This commit is contained in:
JP Stringham
2026-02-03 20:27:05 -05:00
parent f8ac1528dc
commit 91e9e622de
2 changed files with 38 additions and 20 deletions

View File

@@ -36,6 +36,8 @@ pub struct LayerInstance {
pub grid_tiles: Vec<Tile>, pub grid_tiles: Vec<Tile>,
#[serde(alias = "intGridCsv")] #[serde(alias = "intGridCsv")]
pub int_grid_tiles: Vec<u32>, pub int_grid_tiles: Vec<u32>,
#[serde(alias = "autoLayerTiles")]
pub al_tiles: Vec<Tile>,
} }
#[derive(Debug, Clone, Serialize, Deserialize)] #[derive(Debug, Clone, Serialize, Deserialize)]
@@ -53,7 +55,7 @@ pub struct Tile {
pub px_coords: UVec2, pub px_coords: UVec2,
} }
#[derive(Debug, Clone, Serialize, Deserialize)] #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum LayerType { pub enum LayerType {
IntGrid, IntGrid,
Entities, Entities,

View File

@@ -16,18 +16,22 @@ struct TilemapStuff {
img_id: Option<AssetId<Image>>, img_id: Option<AssetId<Image>>,
} }
#[derive(Resource)]
struct LevelData {
ldtk_proj: ldtk::Project,
}
fn main() { fn main() {
let mut ldtk_proj: ldtk::Project = let ldtk_proj: ldtk::Project =
serde_json::from_str(include_str!("../assets/test.ldtk")).unwrap(); serde_json::from_str(include_str!("../assets/test.ldtk")).unwrap();
println!("LDTK {ldtk_proj:?}"); println!("LDTK {ldtk_proj:?}");
return;
let mut app = App::new(); let mut app = App::new();
app.add_plugins(DefaultPlugins.set(ImagePlugin::default_nearest())) app.add_plugins(DefaultPlugins.set(ImagePlugin::default_nearest()))
.init_state::<AppState>() .init_state::<AppState>()
.insert_resource(LevelData { ldtk_proj })
.insert_resource(TilemapStuff::default()) .insert_resource(TilemapStuff::default())
.add_systems(Startup, init) .add_systems(Startup, init)
.add_systems(Update, loadloop.run_if(in_state(AppState::Preload))) .add_systems(Update, loadloop.run_if(in_state(AppState::Preload)))
@@ -42,9 +46,16 @@ fn init(
mut state: ResMut<NextState<AppState>>, mut state: ResMut<NextState<AppState>>,
mut tmap_stuff: ResMut<TilemapStuff>, mut tmap_stuff: ResMut<TilemapStuff>,
mut t_layouts: ResMut<Assets<TextureAtlasLayout>>, mut t_layouts: ResMut<Assets<TextureAtlasLayout>>,
lvl: Res<LevelData>,
) { ) {
println!("Hello Bevy!"); println!("Hello Bevy!");
let mut orth_proj = OrthographicProjection::default_2d();
orth_proj.scale = 0.5;
orth_proj.viewport_origin = Vec2::ZERO;
commands.spawn((Camera2d, Projection::Orthographic(orth_proj)));
// load tilemap
let sprite = assets.load("tilemap.png"); let sprite = assets.load("tilemap.png");
tmap_stuff.img_id = Some(sprite.id()); tmap_stuff.img_id = Some(sprite.id());
tmap_stuff.img_handle = Some(sprite.clone()); tmap_stuff.img_handle = Some(sprite.clone());
@@ -52,23 +63,28 @@ fn init(
let t_layout = TextureAtlasLayout::from_grid(uvec2(16, 16), 12, 8, Some(uvec2(1, 1)), None); let t_layout = TextureAtlasLayout::from_grid(uvec2(16, 16), 12, 8, Some(uvec2(1, 1)), None);
let t_handle = t_layouts.add(t_layout); let t_handle = t_layouts.add(t_layout);
for i in 0..18 { // create map from lvl data
let x = i as f32 * 20.;
let lvl = &lvl.ldtk_proj.levels[0];
lvl.layer_instances.iter().for_each(|li| {
if li.ty != ldtk::LayerType::AutoLayer {
return;
}
li.al_tiles.iter().for_each(|t| {
commands.spawn(( commands.spawn((
Sprite::from_atlas_image( Sprite::from_atlas_image(
sprite.clone(), sprite.clone(),
TextureAtlas { TextureAtlas {
layout: t_handle.clone(), layout: t_handle.clone(),
index: i, index: t.ty as usize,
}, },
), ),
Transform::from_xyz(x, 0., 0.), Transform::from_xyz(t.px_coords.x as f32, 256. - t.px_coords.y as f32, 0.),
)); ));
} });
});
let mut orth_proj = OrthographicProjection::default_2d();
orth_proj.scale = 0.25;
commands.spawn((Camera2d, Projection::Orthographic(orth_proj)));
state.set(AppState::Preload); state.set(AppState::Preload);
} }