Basic entity loading concept

This commit is contained in:
JP Stringham
2026-02-07 13:48:06 -05:00
parent e725ba2eba
commit 8227f52259
3 changed files with 60 additions and 9 deletions

View File

@@ -1254,19 +1254,35 @@
}, },
{ {
"__identifier": "Player", "__identifier": "Player",
"__grid": [11,14], "__grid": [10,11],
"__pivot": [0,0], "__pivot": [0,0],
"__tags": [], "__tags": [],
"__tile": { "tilesetUid": 44, "x": 64, "y": 112, "w": 16, "h": 16 }, "__tile": { "tilesetUid": 44, "x": 64, "y": 112, "w": 16, "h": 16 },
"__smartColor": "#E43B44", "__smartColor": "#E43B44",
"iid": "2d1115c0-fa90-11f0-916a-bbdb4e68f7ab", "iid": "4d700660-fa90-11f0-98a1-2f00d5268e32",
"width": 16, "width": 16,
"height": 16, "height": 16,
"defUid": 45, "defUid": 45,
"px": [176,224], "px": [160,176],
"fieldInstances": [], "fieldInstances": [],
"__worldX": 176, "__worldX": 160,
"__worldY": 224 "__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
} }
] ]
}, },

View File

@@ -1,4 +1,4 @@
use bevy::math::UVec2; use bevy::math::{IVec2, UVec2};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)] #[derive(Debug, Clone, Serialize, Deserialize)]
@@ -38,6 +38,8 @@ pub struct LayerInstance {
pub int_grid_tiles: Vec<u32>, pub int_grid_tiles: Vec<u32>,
#[serde(alias = "autoLayerTiles")] #[serde(alias = "autoLayerTiles")]
pub al_tiles: Vec<Tile>, pub al_tiles: Vec<Tile>,
#[serde(alias = "entityInstances")]
pub entities: Vec<EntityInstance>,
} }
#[derive(Debug, Clone, Serialize, Deserialize)] #[derive(Debug, Clone, Serialize, Deserialize)]
@@ -62,3 +64,11 @@ pub enum LayerType {
Tiles, Tiles,
AutoLayer, AutoLayer,
} }
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EntityInstance {
#[serde(alias = "__identifier")]
pub identifier: String,
#[serde(alias = "__grid")]
pub position: IVec2,
}

View File

@@ -29,7 +29,7 @@ fn main() {
let 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:?}");
let mut app = App::new(); let mut app = App::new();
@@ -128,11 +128,36 @@ fn levelload(
lyr_dep -= 0.1; 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(( commands.spawn((
Sprite::from_color(Color::WHITE, vec2(16., 16.)), Sprite::from_color(Color::WHITE, vec2(16., 16.)),
Player::default(), player,
Transform::from_xyz(128., 0., 20.), Transform::from_xyz(128., 0., 20.),
)); ));