Newer
Older
#[cfg(feature = "ldtk_1_0_0")]
mod data_1_0_0;
#[cfg(any(feature = "ldtk_1_1_1", feature = "ldtk_1_1_0"))]
mod data_1_1_0;
#[cfg(any(feature = "ldtk_1_1_3", feature = "ldtk_1_1_2"))]
mod data_1_1_2;
#[cfg(any(feature = "ldtk_1_2_1", feature = "ldtk_1_2_0"))]
mod data_1_2_1;
#[cfg(any(feature = "ldtk_1_2_3", feature = "ldtk_1_2_2"))]
mod data_1_2_2;
#[cfg(feature = "ldtk_1_2_4")]
mod data_1_2_4;
#[cfg(feature = "ldtk_1_2_5")]
mod data_1_2_5;
use bevy::asset::{AssetLoader, AssetPath, BoxedFuture, LoadContext, LoadedAsset};
use bevy::reflect::{TypePath, TypeUuid, Uuid};
#[cfg(feature = "ldtk_1_0_0")]
pub use data_1_0_0::*;
#[cfg(any(feature = "ldtk_1_1_1", feature = "ldtk_1_1_0"))]
pub use data_1_1_0::*;
#[cfg(any(feature = "ldtk_1_1_3", feature = "ldtk_1_1_2"))]
pub use data_1_1_2::*;
#[cfg(any(feature = "ldtk_1_2_1", feature = "ldtk_1_2_0"))]
pub use data_1_2_1::*;
#[cfg(any(feature = "ldtk_1_2_3", feature = "ldtk_1_2_2"))]
pub use data_1_2_2::*;
#[cfg(feature = "ldtk_1_2_4")]
pub use data_1_2_4::*;
#[cfg(feature = "ldtk_1_2_5")]
pub use data_1_2_5::*;
pub enum ParseError {
#[error("Failed to parse file: {0}")]
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
pub trait LdtkFromBytes<'a>: Deserialize<'a> {
fn from_bytes(bytes: &'a [u8]) -> Result<Self, ParseError> {
serde_json::from_slice(bytes).map_err(|e| ParseError::SerdeError(format!("{}", e)))
}
}
macro_rules! impl_from_bytes {
($type: tt) => {
impl<'a> From<&'a [u8]> for $type {
fn from(value: &'a [u8]) -> Self {
#[cfg(feature = "no_panic")]
{
match $type::from_bytes(value) {
Ok(val) => val,
Err(e) => {
log::error!("{}", e);
std::process::abort();
}
}
}
#[cfg(not(feature = "no_panic"))]
{
$type::from_bytes(value).expect("Failed to parse ldtk file")
}
}
}
};
}
impl<'a> LdtkFromBytes<'a> for Level {}
impl<'a> LdtkFromBytes<'a> for Project {}
impl_from_bytes!(Level);
impl_from_bytes!(Project);
impl TypeUuid for Project {
const TYPE_UUID: Uuid = Uuid::from_u128(87988914102923589138720617793417023455);
}
impl TypePath for Project {
fn type_path() -> &'static str {
"micro_ldtk::ldtk::Project"
}
fn short_type_path() -> &'static str {
"Project"
}
}
impl TypeUuid for Level {
const TYPE_UUID: Uuid = Uuid::from_u128(18486863672600588966868281477384349187);
}
impl TypePath for Level {
fn type_path() -> &'static str {
"micro_ld0tk::ldtk::Level"
fn short_type_path() -> &'static str {
"Level"
}
}
impl Project {
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
self.worlds
.iter()
.flat_map(|world| world.levels.iter())
.collect()
} else {
self.levels.iter().collect()
}
}
#[cfg(any(
feature = "ldtk_1_2_5",
feature = "ldtk_1_2_4",
feature = "ldtk_1_2_3",
feature = "ldtk_1_2_2",
feature = "ldtk_1_2_1",
feature = "ldtk_1_2_0",
feature = "ldtk_1_1_3",
feature = "ldtk_1_1_2",
feature = "ldtk_1_1_1",
feature = "ldtk_1_1_0",
feature = "ldtk_1_0_0"
))]
pub fn get_world_levels(&self, identifier: impl ToString) -> Vec<&Level> {
vec![]
}
#[cfg(any(feature = "ldtk_1_3_0",))]
pub fn get_world_levels(&self, identifier: impl ToString) -> Vec<&Level> {
let id = identifier.to_string();
self.worlds
.iter()
.find(|world| world.identifier == id)
.map(|list| list.levels.iter().collect())
.unwrap_or_else(Vec::new)
}
}
pub type LdtkProject = Project;
#[derive(Default)]
pub struct LdtkLoader;
impl AssetLoader for LdtkLoader {
fn load<'a>(
&'a self,
bytes: &'a [u8],
load_context: &'a mut LoadContext,
) -> BoxedFuture<'a, anyhow::Result<(), anyhow::Error>> {
Box::pin(async move {
let project = Project::from_bytes(bytes)?;
let sub_levels = project
.levels
.iter()
.flat_map(|level| {
level
.external_rel_path
.as_ref()
.map(|rel_path| (level.identifier.clone(), rel_path.clone()))
})
.collect::<Vec<(String, String)>>();
let asset = LoadedAsset::new(project).with_dependencies(
sub_levels
.into_iter()
.flat_map(|(id, path)| {
load_context
.path()
.join(path)
.canonicalize()
.map(|path| AssetPath::new(path, Some(id)))
.ok()
})
.collect(),
);
load_context.set_default_asset(asset);
Ok(())
})
}
fn extensions(&self) -> &[&str] {
&["ldtk"]
}
}
#[derive(Default)]
pub struct LdtkLevelLoader;
impl AssetLoader for LdtkLevelLoader {
fn load<'a>(
&'a self,
bytes: &'a [u8],
load_context: &'a mut LoadContext,
) -> BoxedFuture<'a, anyhow::Result<(), anyhow::Error>> {
Box::pin(async move {
load_context.set_default_asset(LoadedAsset::new(Level::from_bytes(bytes)?));
Ok(())
})
}
fn extensions(&self) -> &[&str] {
&["ldtkl"]
}
}
#[cfg(feature = "autotile")]
mod autotile_support {
use micro_autotile::{AutoRuleSet, AutoTileRule, TileMatcher, TileOutput, TileStatus};
use crate::ldtk::{AutoLayerRuleGroup, Project};
impl From<&AutoLayerRuleGroup> for AutoRuleSet {
fn from(value: &AutoLayerRuleGroup) -> Self {
let set = value
.rules
.iter()
.filter_map(|rule| match rule.size {
1 => {
let rule = AutoTileRule {
chance: rule.chance as f32,
output: TileOutput::Random(
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
),
matcher: TileMatcher::single_match(TileStatus::from_ldtk_value(
rule.pattern[0],
)),
};
Some(rule)
}
3 => {
if rule.pattern.len() == 9 {
let matcher = TileMatcher([
TileStatus::from_ldtk_value(rule.pattern[0]),
TileStatus::from_ldtk_value(rule.pattern[1]),
TileStatus::from_ldtk_value(rule.pattern[2]),
TileStatus::from_ldtk_value(rule.pattern[3]),
TileStatus::from_ldtk_value(rule.pattern[4]),
TileStatus::from_ldtk_value(rule.pattern[5]),
TileStatus::from_ldtk_value(rule.pattern[6]),
TileStatus::from_ldtk_value(rule.pattern[7]),
TileStatus::from_ldtk_value(rule.pattern[8]),
]);
let rule = AutoTileRule {
chance: rule.chance as f32,
matcher,
output: TileOutput::Random(
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
),
};
Some(rule)
} else {
None
}
}
_ => None,
})
.collect();
AutoRuleSet(set)
}
}
impl From<&Project> for AutoRuleSet {
fn from(value: &Project) -> Self {
let mut base_set = AutoRuleSet::default();
for layers in value.defs.layers.iter() {
for rule_group in layers.auto_rule_groups.iter() {
base_set = base_set + rule_group.into();
}
}
base_set
}
}
}
pub fn load_project() {
const project_data: &[u8] = include_bytes!("./test_data/ver_1_2_5.ldtk");
let project = Project::from_bytes(project_data).expect("Failed to parse project file");
for layer in project.defs.layers.iter() {
for _auto_rule_group in layer.auto_rule_groups.iter() {}