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, 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}")]
}
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 Project {
pub fn from_bytes(bytes: &[u8]) -> Result<Self, ParseError> {
serde_json::from_slice(bytes).map_err(|e| ParseError::SerdeError(format!("{}", e)))
}
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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)
}
98
99
100
101
102
103
104
105
106
107
108
109
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
}
pub type LdtkProject = Project;
impl<'a> From<&'a [u8]> for Project {
fn from(value: &'a [u8]) -> Self {
#[cfg(feature = "no_panic")]
{
match Project::from_bytes(value) {
Ok(val) => val,
Err(e) => {
log::error!("{}", e);
std::process::abort();
}
}
}
#[cfg(not(feature = "no_panic"))]
{
Project::from_bytes(value).expect("Failed to parse ldtk project file")
}
}
}
#[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 {
load_context.set_default_asset(LoadedAsset::new(Project::from_bytes(bytes)?));
Ok(())
})
}
fn extensions(&self) -> &[&str] {
&["ldtk"]
}
}
#[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(
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
),
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(
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
),
};
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
}
}
}
#[cfg(test)]
mod test {
use crate::ldtk::Project;
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() {}