diff options
author | tom barrett <spalf0@gmail.com> | 2019-06-18 07:23:10 -0500 |
---|---|---|
committer | tom barrett <spalf0@gmail.com> | 2019-06-18 07:23:10 -0500 |
commit | c510d4a406218c38e1146503473e9e10c282a56d (patch) | |
tree | 07ac460954a6919bb040b70ee791a9fe5473264a /src/map.rs | |
parent | 2ad96b7f882ff962f67a13c7c56a46df86cfe6e3 (diff) |
much better way of receiving xml
Diffstat (limited to 'src/map.rs')
-rw-r--r-- | src/map.rs | 43 |
1 files changed, 19 insertions, 24 deletions
@@ -1,11 +1,11 @@ use ggez::filesystem::File; use ggez::graphics::{spritebatch::SpriteBatch, DrawParam}; use ggez::nalgebra::{Point2, Vector2}; -use std::io::BufReader; -use xml::reader::{EventReader, XmlEvent}; +use xml::reader::XmlEvent::Characters; use crate::constants; use crate::tileset::Tileset; +use crate::xmlelements::XMLElements; pub struct Map { pub width: usize, @@ -15,33 +15,28 @@ pub struct Map { impl Map { pub fn new(file: File) -> Map { - let mut width = None; - let mut height = None; - let mut layers = Vec::new(); + let elements = XMLElements::new(file); - for e in EventReader::new(BufReader::new(file)) { - if let Ok(XmlEvent::StartElement { - name, attributes, .. - }) = e - { - if name.local_name == "map" { - for attribute in attributes { - match attribute.name.local_name.as_str() { - "width" => width = Some(attribute.value.parse::<usize>().unwrap()), - "height" => height = Some(attribute.value.parse::<usize>().unwrap()), - _ => (), - } - } + let width = elements.get_element_attribute("map", "width").unwrap(); + + let height = elements.get_element_attribute("map", "height").unwrap(); + + let layers = elements + .events + .iter() + .filter_map(|e| { + if let Characters(text) = e { + Some(Layer::new(text.to_string())) + } else { + None } - } else if let Ok(XmlEvent::Characters(text)) = e { - layers.push(Layer::new(text)); - } - } + }) + .collect(); Map { layers, - width: width.unwrap(), - height: height.unwrap(), + width, + height, } } |