diff options
Diffstat (limited to 'src/tileset.rs')
-rw-r--r-- | src/tileset.rs | 39 |
1 files changed, 14 insertions, 25 deletions
diff --git a/src/tileset.rs b/src/tileset.rs index 21e31d1..d4e652f 100644 --- a/src/tileset.rs +++ b/src/tileset.rs @@ -1,7 +1,7 @@ use ggez::filesystem::File; use ggez::graphics::Rect; -use std::io::BufReader; -use xml::reader::{EventReader, XmlEvent}; + +use crate::xmlelements::XMLElements; pub struct Tileset { pub tiles: Vec<Rect>, @@ -11,29 +11,18 @@ pub struct Tileset { impl Tileset { pub fn new(file: File) -> Tileset { - let mut tile_width = None; - let mut tile_height = None; - let mut columns = None; - let mut height = None; - - for e in EventReader::new(BufReader::new(file)) { - if let Ok(XmlEvent::StartElement { attributes, .. }) = e { - for attribute in attributes { - match attribute.name.local_name.as_str() { - "columns" => columns = Some(attribute.value.parse::<usize>().unwrap()), - "tilewidth" => tile_width = Some(attribute.value.parse::<f32>().unwrap()), - "tileheight" => tile_height = Some(attribute.value.parse::<f32>().unwrap()), - "height" => height = Some(attribute.value.parse::<usize>().unwrap()), - _ => (), - } - } - } - } - - let columns = columns.unwrap(); - let tile_height = tile_height.unwrap(); - let tile_width = tile_width.unwrap(); - let height = height.unwrap(); + let elements = XMLElements::new(file); + + let columns = elements + .get_element_attribute("tileset", "columns") + .unwrap(); + let height = elements.get_element_attribute("image", "height").unwrap(); + let tile_width = elements + .get_element_attribute("tileset", "tilewidth") + .unwrap() as f32; + let tile_height = elements + .get_element_attribute("tileset", "tileheight") + .unwrap() as f32; let rows = height / (tile_height as usize); |