diff --git a/assets/test.ldtk b/assets/test.ldtk index 28dec8a..7decb3c 100644 --- a/assets/test.ldtk +++ b/assets/test.ldtk @@ -1254,19 +1254,35 @@ }, { "__identifier": "Player", - "__grid": [11,14], + "__grid": [10,11], "__pivot": [0,0], "__tags": [], "__tile": { "tilesetUid": 44, "x": 64, "y": 112, "w": 16, "h": 16 }, "__smartColor": "#E43B44", - "iid": "2d1115c0-fa90-11f0-916a-bbdb4e68f7ab", + "iid": "4d700660-fa90-11f0-98a1-2f00d5268e32", "width": 16, "height": 16, "defUid": 45, - "px": [176,224], + "px": [160,176], "fieldInstances": [], - "__worldX": 176, - "__worldY": 224 + "__worldX": 160, + "__worldY": 176 + }, + { + "__identifier": "Parsnip", + "__grid": [4,7], + "__pivot": [0,0], + "__tags": [], + "__tile": { "tilesetUid": 2, "x": 16, "y": 288, "w": 16, "h": 16 }, + "__smartColor": "#EAD4AA", + "iid": "a7707dc0-fa90-11f0-98a1-17befcef4621", + "width": 16, + "height": 16, + "defUid": 71, + "px": [64,112], + "fieldInstances": [], + "__worldX": 64, + "__worldY": 112 } ] }, diff --git a/src/ldtk/mod.rs b/src/ldtk/mod.rs index ecb8800..c5d4e50 100644 --- a/src/ldtk/mod.rs +++ b/src/ldtk/mod.rs @@ -1,4 +1,4 @@ -use bevy::math::UVec2; +use bevy::math::{IVec2, UVec2}; use serde::{Deserialize, Serialize}; #[derive(Debug, Clone, Serialize, Deserialize)] @@ -38,6 +38,8 @@ pub struct LayerInstance { pub int_grid_tiles: Vec, #[serde(alias = "autoLayerTiles")] pub al_tiles: Vec, + #[serde(alias = "entityInstances")] + pub entities: Vec, } #[derive(Debug, Clone, Serialize, Deserialize)] @@ -62,3 +64,11 @@ pub enum LayerType { Tiles, AutoLayer, } + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct EntityInstance { + #[serde(alias = "__identifier")] + pub identifier: String, + #[serde(alias = "__grid")] + pub position: IVec2, +} diff --git a/src/main.rs b/src/main.rs index e61fa98..1af223f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -29,7 +29,7 @@ fn main() { let ldtk_proj: ldtk::Project = serde_json::from_str(include_str!("../assets/test.ldtk")).unwrap(); - println!("LDTK {ldtk_proj:?}"); + // println!("LDTK {ldtk_proj:?}"); let mut app = App::new(); @@ -128,11 +128,36 @@ fn levelload( lyr_dep -= 0.1; }); - println!("{lyr_dep}"); + println!("Total layer depth: {lyr_dep}"); + println!("Loading entities..."); + + let mut player = Player::default(); + + // try to find the player spawn in an entities layer + lvl.layer_instances.iter().for_each(|li| { + if li.entities.is_empty() { + return; + } + + li.entities + .iter() + .for_each(|ei| match ei.identifier.as_str() { + "Player" => { + let pos = IVec2 { + x: ei.position.x * 16, + y: 256 - ei.position.y * 16, + }; + player = Player { pos }; + } + unk => { + warn!("Unhandled entity type {unk}") + } + }); + }); commands.spawn(( Sprite::from_color(Color::WHITE, vec2(16., 16.)), - Player::default(), + player, Transform::from_xyz(128., 0., 20.), ));