diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/lib.rs | 1 | ||||
-rw-r--r-- | src/map.rs | 43 | ||||
-rw-r--r-- | src/tileset.rs | 39 | ||||
-rw-r--r-- | src/xmlelements.rs | 54 |
4 files changed, 88 insertions, 49 deletions
@@ -3,3 +3,4 @@ pub mod constants; pub mod map; pub mod state; pub mod tileset; +pub mod xmlelements; @@ -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, } } 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); diff --git a/src/xmlelements.rs b/src/xmlelements.rs new file mode 100644 index 0000000..86d7c77 --- /dev/null +++ b/src/xmlelements.rs @@ -0,0 +1,54 @@ +use ggez::filesystem::File; +use std::io::BufReader; +use xml::reader::{ + EventReader, + XmlEvent::{self, StartElement}, +}; + +pub struct XMLElements { + pub events: Vec<XmlEvent>, +} + +impl XMLElements { + pub fn new(file: File) -> XMLElements { + XMLElements { + events: EventReader::new(BufReader::new(file)) + .into_iter() + .map(Result::unwrap) + .collect(), + } + } + + pub fn get_element(&self, element_name: &str) -> XmlEvent { + self.events + .clone() + .into_iter() + .find(|e| { + if let StartElement { name, .. } = e { + name.local_name == element_name + } else { + false + } + }) + .unwrap() + } + + pub fn get_element_attribute( + &self, + element_name: &str, + attribute_name: &str, + ) -> Result<usize, ()> { + let element = self.get_element(element_name); + if let StartElement { attributes, .. } = element { + Ok(attributes + .iter() + .find(|a| a.name.local_name == attribute_name) + .unwrap() + .value + .parse() + .unwrap()) + } else { + Err(()) + } + } +} |