AutoLayer rendering working sorta
This commit is contained in:
@@ -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,
|
||||||
|
|||||||
54
src/main.rs
54
src/main.rs
@@ -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.;
|
|
||||||
commands.spawn((
|
|
||||||
Sprite::from_atlas_image(
|
|
||||||
sprite.clone(),
|
|
||||||
TextureAtlas {
|
|
||||||
layout: t_handle.clone(),
|
|
||||||
index: i,
|
|
||||||
},
|
|
||||||
),
|
|
||||||
Transform::from_xyz(x, 0., 0.),
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut orth_proj = OrthographicProjection::default_2d();
|
let lvl = &lvl.ldtk_proj.levels[0];
|
||||||
orth_proj.scale = 0.25;
|
|
||||||
commands.spawn((Camera2d, Projection::Orthographic(orth_proj)));
|
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);
|
state.set(AppState::Preload);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user