1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 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 184 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 216 217 218 219 220 221 222 223 224 225 226 227 228 229 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 256 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 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
//! Functions and types for parsing Ogmo levels. use std::fs; use std::path::{Path, PathBuf}; use hashbrown::HashMap; use serde::Deserialize; use crate::{Error, Vec2}; /// A dynamically typed value. /// /// As Ogmo's level format does not store the type alongside the value, /// it is not possible for this enum to specify the exact type of the /// original value template. #[derive(Clone, Debug, Deserialize)] #[serde(untagged)] pub enum Value { /// A boolean value. Boolean(bool), /// A string value. String(String), /// A numeric value. /// /// This may have originally been an integer when set, but the Ogmo /// format does not provide enough information to figure that out /// without cross-referencing the project. Number(f32), } /// An Ogmo level. #[derive(Clone, Debug, Deserialize)] #[serde(rename_all = "camelCase")] pub struct Level { /// The width of the level. pub width: f32, /// The height of the level. pub height: f32, /// The offset of the level on the X axis. Useful for loading multiple chunked levels. pub offset_x: f32, /// The offset of the level on the Y axis. Useful for loading multiple chunked levels. pub offset_y: f32, /// The layers that make up the level. pub layers: Vec<Layer>, /// The level's custom values. pub values: HashMap<String, Value>, } impl Level { /// Parses an Ogmo level from a JSON string. /// /// # Errors /// /// * `Error::Json` will be returned if deserialization fails. pub fn from_json(s: &str) -> Result<Level, Error> { serde_json::from_str(s).map_err(Error::Json) } /// Parses an Ogmo level from a file. /// /// # Errors /// /// * `Error::Io` will be returned if the file cannot be read. /// * `Error::Json` will be returned if deserialization fails. pub fn from_file(path: impl AsRef<Path>) -> Result<Level, Error> { let json = fs::read_to_string(path).map_err(Error::Io)?; Level::from_json(&json) } } /// An entity instance. #[derive(Clone, Debug, Deserialize)] #[serde(rename_all = "camelCase")] pub struct Entity { /// The entity's name. pub name: String, /// The entity's ID. pub id: i32, /// The unique export ID of the entity. #[serde(rename = "_eid")] pub export_id: String, /// The X position of the entity. pub x: f32, /// The Y position of the entity. pub y: f32, /// The width of the entity. /// Will only be present if the entity template was defined as resizable. pub width: Option<f32>, /// The width of the entity. /// Will only be present if the entity template was defined as resizable. pub height: Option<f32>, /// The X origin of the entity. /// Will only be present if the entity template defined an origin. pub origin_x: Option<f32>, /// The Y origin of the entity. /// Will only be present if the entity template defined an origin. pub origin_y: Option<f32>, /// The rotation of the entity. /// Will only be present if the entity template was defined as rotatable. pub rotation: Option<f32>, /// Whether the entity is flipped on the X axis. /// Will only be present if the entity template was defined as X-flippable. pub flipped_x: Option<bool>, /// Whether the entity is flipped on the Y axis. /// Will only be present if the entity template was defined as Y-flippable. pub flipped_y: Option<bool>, /// The entity's nodes. /// Will only be present if the entity template was defined as having nodes. pub nodes: Option<Vec<Vec2<f32>>>, /// The entity's custom values. /// Will only be present if the entity template was defined as having custom values. pub values: Option<HashMap<String, Value>>, } /// A decal instance. #[derive(Clone, Debug, Deserialize)] #[serde(rename_all = "camelCase")] pub struct Decal { /// The X position of the decal. pub x: f32, /// The Y position of the decal. pub y: f32, /// The name of the decal's texture. pub texture: String, /// The rotation of the decal. /// Will only be present if the decal template was defined as rotatable. pub rotation: Option<f32>, /// The scale of the decal on the X axis. /// Will only be present if the decal template was defined as scalable. pub scale_x: Option<f32>, /// The scale of the decal on the Y axis. /// Will only be present if the decal template was defined as scalable. pub scale_y: Option<f32>, } /// A layer instance. #[derive(Clone, Debug, Deserialize)] #[serde(rename_all = "camelCase")] pub struct Layer { /// The name of the layer. pub name: String, /// The unique export ID of the entity. #[serde(rename = "_eid")] pub export_id: String, /// The layer's offset on the X axis. pub offset_x: f32, /// The layer's offset on the Y axis. pub offset_y: f32, /// The width of the layer's grid cells. pub grid_cell_width: i32, /// The height of the layer's grid cells. pub grid_cell_height: i32, /// The number of grid cells on the X axis. pub grid_cells_x: i32, /// The number of grid cells on the Y axis. pub grid_cells_y: i32, /// Data specific to certain layer types. #[serde(flatten)] pub data: LayerData, } /// Data specific to certain layer types. #[derive(Clone, Debug, Deserialize)] #[serde(untagged)] pub enum LayerData { /// Data specific to tile layers with 1D storage. Tile(LayerTileData), /// Data specific to tile layers with 2D storage. Tile2D(LayerTile2DData), /// Data specific to tile co-ord layers with 1D storage. TileCoords(LayerTileCoordsData), /// Data specific to tile co-ord layers with 2D storage. TileCoords2D(LayerTileCoords2DData), /// Data specific to grid layers with 1D storage. Grid(LayerGridData), /// Data specific to grid layers with 2D storage. Grid2D(LayerGrid2DData), /// Data specific to entity layers. Entity(LayerEntityData), /// Data specific to decal layers. Decal(LayerDecalData), } /// Data specific to tile layers with 1D storage. #[derive(Clone, Debug, Deserialize)] #[serde(rename_all = "camelCase")] pub struct LayerTileData { /// The tile data, stored as a flat list. /// Each value corresponds to an index of a tileset tile (counting left to right, top to bottom). /// Empty tiles are represented by a `-1`. pub data: Vec<i32>, /// The name of the tileset used for this layer. pub tileset: String, } /// Data specific to tile layers with 2D storage. #[derive(Clone, Debug, Deserialize)] #[serde(rename_all = "camelCase")] pub struct LayerTile2DData { /// The tile data, stored as a 2D list. /// Each value corresponds to an index of a tileset tile (counting left to right, top to bottom). /// Empty tiles are represented by a `-1`. #[serde(rename = "data2D")] pub data_2d: Vec<Vec<i32>>, /// The name of the tileset used for this layer. pub tileset: String, } /// Data specific to tile co-ord layers with 1D storage. #[derive(Clone, Debug, Deserialize)] #[serde(rename_all = "camelCase")] pub struct LayerTileCoordsData { /// The tile data, stored as a flat list. /// Each value corresponds to an X and Y co-ordinate of a tileset tile. /// Empty tiles are represented by a `[-1]`. pub data_coords: Vec<Vec<i32>>, /// The name of the tileset used for this layer. pub tileset: String, } /// Data specific to tile co-ord layers with 2D storage. #[derive(Clone, Debug, Deserialize)] #[serde(rename_all = "camelCase")] pub struct LayerTileCoords2DData { /// The tile data, stored as a 2D list. /// Each value corresponds to an X and Y co-ordinate of a tileset tile. /// Empty tiles are represented by a `[-1]`. #[serde(rename = "dataCoords2D")] pub data_coords_2d: Vec<Vec<Vec<i32>>>, /// The name of the tileset used for this layer. pub tileset: String, } /// Data specific to grid layers with 1D storage. #[derive(Clone, Debug, Deserialize)] #[serde(rename_all = "camelCase")] pub struct LayerGridData { /// The grid data, stored as a flat list. pub grid: Vec<String>, } /// Data specific to grid layers with 2D storage. #[derive(Clone, Debug, Deserialize)] #[serde(rename_all = "camelCase")] pub struct LayerGrid2DData { /// The grid data, stored as a 2D list. #[serde(rename = "grid2D")] pub grid_2d: Vec<Vec<String>>, } /// Data specific to entity layers. #[derive(Clone, Debug, Deserialize)] #[serde(rename_all = "camelCase")] pub struct LayerEntityData { /// The entity data. pub entities: Vec<Entity>, } /// Data specific to decal layers. #[derive(Clone, Debug, Deserialize)] #[serde(rename_all = "camelCase")] pub struct LayerDecalData { /// The entity data. pub decals: Vec<Decal>, /// The path containing the decal images, relative to the project. pub folder: PathBuf, }