From 49b07a29ab42b2d550becfd0de8da4231341b706 Mon Sep 17 00:00:00 2001 From: tom barrett Date: Sun, 16 Jun 2019 10:33:29 -0500 Subject: layers now supported --- src/map.rs | 75 ++++++++++++++++++-------------------------------------------- 1 file changed, 22 insertions(+), 53 deletions(-) (limited to 'src/map.rs') diff --git a/src/map.rs b/src/map.rs index 2028b50..db79deb 100644 --- a/src/map.rs +++ b/src/map.rs @@ -1,19 +1,36 @@ use ggez::filesystem::File; -use ggez::graphics::Rect; use std::io::BufReader; use xml::reader::{EventReader, XmlEvent}; +pub struct Layer { + pub id: usize, + pub data: Vec, +} + +impl Layer { + pub fn new(text: String, id: usize) -> Layer { + Layer { + id, + data: text + .replace("\n", "") + .split(',') + .map(|s| s.parse().unwrap()) + .collect(), + } + } +} + pub struct Map { pub width: usize, pub height: usize, - pub data: Vec, + pub layers: Vec, } impl Map { pub fn new(file: File) -> Map { let mut width = None; let mut height = None; - let mut data: Option> = None; + let mut layers = Vec::new(); for e in EventReader::new(BufReader::new(file)) { if let Ok(XmlEvent::StartElement { @@ -30,63 +47,15 @@ impl Map { } } } else if let Ok(XmlEvent::Characters(text)) = e { - data = Some( - text.replace("\n", "") - .split(',') - .map(|s| s.parse().unwrap()) - .collect(), - ); + layers.push(Layer::new(text, layers.len() + 1)); } } Map { - data: data.unwrap(), + layers, width: width.unwrap(), height: height.unwrap(), } } } -pub struct Tileset { - pub tiles: Vec, - pub tile_width: f32, - pub tile_height: f32, -} - -impl Tileset { - pub fn new(file: File) -> Tileset { - let mut tile_width = None; - let mut tile_height = None; - let mut columns = 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::().unwrap()), - "tilewidth" => tile_width = Some(attribute.value.parse::().unwrap()), - "tileheight" => tile_height = Some(attribute.value.parse::().unwrap()), - _ => (), - } - } - } - } - - let columns = columns.unwrap(); - - let mut tiles = Vec::new(); - tiles.push(Rect::zero()); - - for c in 0..columns { - let x = c as f32 / columns as f32; - let w = (c as f32 + 1.0) / columns as f32; - tiles.push(Rect::new(x, 0.0, w, 1.0)); - } - - Tileset { - tiles, - tile_height: tile_height.unwrap(), - tile_width: tile_width.unwrap(), - } - } -} -- cgit v1.2.3