micro_ldtk/
map_query_neutral.rs1use std::fmt::Debug;
2use std::ops::Index;
3use std::str::FromStr;
4
5use crate::ldtk::EntityInstance;
6use crate::{LdtkLayer, LdtkLevel};
7
8pub struct MapQuery {}
9
10#[derive(Clone)]
11pub struct InstanceRef<'a> {
12 pub entity: &'a EntityInstance,
13}
14
15impl<'a> InstanceRef<'a> {
16 pub fn x(&self) -> i64 {
18 self.entity.px[0]
19 }
20 pub fn y(&self) -> i64 {
22 self.entity.px[1]
23 }
24 pub fn width(&self) -> i64 {
26 self.entity.width
27 }
28 pub fn height(&self) -> i64 {
30 self.entity.height
31 }
32
33 pub fn get_type(&self) -> &'a String {
36 &self.entity.identifier
37 }
38 pub fn try_get_typed_id<T: FromStr>(&self) -> Result<T, T::Err> {
93 T::from_str(self.get_type().as_str())
94 }
95
96 pub fn property(&self, name: impl ToString) -> serde_json::Value {
99 self[name].clone()
100 }
101
102 pub fn instance_ref(&self) -> &EntityInstance {
104 self.entity
105 }
106}
107
108impl<T: ToString> Index<T> for InstanceRef<'_> {
109 type Output = serde_json::Value;
110
111 fn index(&self, index: T) -> &Self::Output {
112 let name = index.to_string();
113 for field in &self.entity.field_instances {
114 if field.identifier == name {
115 return field.value.as_ref().unwrap_or(&serde_json::Value::Null);
116 }
117 }
118
119 &serde_json::Value::Null
120 }
121}
122
123#[derive(Copy, Clone, Debug)]
124pub struct CameraBounds {
125 pub left: f32,
126 pub top: f32,
127 pub bottom: f32,
128 pub right: f32,
129}
130
131impl CameraBounds {
132 pub fn get_min_x(&self, camera_width: f32) -> f32 {
133 self.left + (camera_width / 2.0) }
135 pub fn get_max_x(&self, camera_width: f32) -> f32 {
136 self.right - (camera_width / 2.0) }
138 pub fn get_min_y(&self, camera_height: f32) -> f32 {
139 self.bottom + (camera_height / 2.0) }
141 pub fn get_max_y(&self, camera_height: f32) -> f32 {
142 self.top - (camera_height / 2.0) }
144}
145
146impl MapQuery {
147 pub fn for_each_layer_of(level: &LdtkLevel, mut cb: impl FnMut(&LdtkLayer)) {
153 for layer in level.layers() {
154 cb(layer);
155 }
156 }
157
158 pub fn get_layers_of(level: &LdtkLevel) -> impl DoubleEndedIterator<Item = &LdtkLayer> {
160 level.layers()
161 }
162
163 pub fn get_entities_of(level: &LdtkLevel) -> Vec<&EntityInstance> {
165 level
166 .layers()
167 .flat_map(|layer| layer.as_ref().entity_instances.iter())
168 .collect()
169 }
170
171 pub fn get_instance_refs_of(level: &'_ LdtkLevel) -> Vec<InstanceRef<'_>> {
173 level
174 .layers()
175 .flat_map(|layer| {
176 layer
177 .as_ref()
178 .entity_instances
179 .iter()
180 .map(|inst| InstanceRef { entity: inst })
181 })
182 .collect()
183 }
184
185 pub fn get_filtered_entities_of(
188 level: &LdtkLevel,
189 entity_type: impl ToString,
190 ) -> Vec<&EntityInstance> {
191 let e_type = entity_type.to_string();
192 level
193 .layers()
194 .flat_map(|layer| layer.as_ref().entity_instances.iter())
195 .filter(|inst| inst.identifier == e_type)
196 .collect()
197 }
198
199 pub fn get_filtered_instance_refs_of(
202 level: &'_ LdtkLevel,
203 entity_type: impl ToString,
204 ) -> Vec<InstanceRef<'_>> {
205 let e_type = entity_type.to_string();
206 level
207 .layers()
208 .flat_map(|layer| {
209 layer
210 .as_ref()
211 .entity_instances
212 .iter()
213 .map(|inst| InstanceRef { entity: inst })
214 })
215 .filter(|inst| inst.entity.identifier == e_type)
216 .collect()
217 }
218
219 pub fn get_owned_entities_of(level: &LdtkLevel) -> Vec<EntityInstance> {
221 level
222 .layers()
223 .flat_map(|layer| layer.as_ref().entity_instances.iter().cloned())
224 .collect()
225 }
226
227 pub fn get_camera_bounds_of(level: &LdtkLevel) -> CameraBounds {
230 let level = level.level_ref();
231 CameraBounds {
232 left: 0.0,
233 top: level.px_hei as f32,
234 bottom: 0.0,
235 right: level.px_wid as f32,
236 }
237 }
238}