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

View File

@@ -16,18 +16,22 @@ struct TilemapStuff {
img_id: Option<AssetId<Image>>,
}
#[derive(Resource)]
struct LevelData {
ldtk_proj: ldtk::Project,
}
fn main() {
let mut ldtk_proj: ldtk::Project =
let ldtk_proj: ldtk::Project =
serde_json::from_str(include_str!("../assets/test.ldtk")).unwrap();
println!("LDTK {ldtk_proj:?}");
return;
let mut app = App::new();
app.add_plugins(DefaultPlugins.set(ImagePlugin::default_nearest()))
.init_state::<AppState>()
.insert_resource(LevelData { ldtk_proj })
.insert_resource(TilemapStuff::default())
.add_systems(Startup, init)
.add_systems(Update, loadloop.run_if(in_state(AppState::Preload)))
@@ -42,9 +46,16 @@ fn init(
mut state: ResMut<NextState<AppState>>,
mut tmap_stuff: ResMut<TilemapStuff>,
mut t_layouts: ResMut<Assets<TextureAtlasLayout>>,
lvl: Res<LevelData>,
) {
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");
tmap_stuff.img_id = Some(sprite.id());
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_handle = t_layouts.add(t_layout);
for i in 0..18 {
let x = i as f32 * 20.;
commands.spawn((
Sprite::from_atlas_image(
sprite.clone(),
TextureAtlas {
layout: t_handle.clone(),
index: i,
},
),
Transform::from_xyz(x, 0., 0.),
));
}
// create map from lvl data
let mut orth_proj = OrthographicProjection::default_2d();
orth_proj.scale = 0.25;
commands.spawn((Camera2d, Projection::Orthographic(orth_proj)));
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((
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, 0.),
));
});
});
state.set(AppState::Preload);
}