summaryrefslogtreecommitdiff
path: root/src/map.rs
diff options
context:
space:
mode:
authortom barrett <spalf0@gmail.com>2019-06-16 05:46:46 -0500
committertom barrett <spalf0@gmail.com>2019-06-16 05:46:46 -0500
commit4cbbf41696dfc76c5a2f758aaa994466a852a489 (patch)
tree8547d57aa2100a6086f63655233726e35d37acf6 /src/map.rs
parent895dd470ecf26faa2db3ba7faf5d02b2ae220d5c (diff)
created new parser
Diffstat (limited to 'src/map.rs')
-rw-r--r--src/map.rs92
1 files changed, 92 insertions, 0 deletions
diff --git a/src/map.rs b/src/map.rs
new file mode 100644
index 0000000..2028b50
--- /dev/null
+++ b/src/map.rs
@@ -0,0 +1,92 @@
+use ggez::filesystem::File;
+use ggez::graphics::Rect;
+use std::io::BufReader;
+use xml::reader::{EventReader, XmlEvent};
+
+pub struct Map {
+ pub width: usize,
+ pub height: usize,
+ pub data: Vec<usize>,
+}
+
+impl Map {
+ pub fn new(file: File) -> Map {
+ let mut width = None;
+ let mut height = None;
+ let mut data: Option<Vec<usize>> = None;
+
+ 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()),
+ _ => (),
+ }
+ }
+ }
+ } else if let Ok(XmlEvent::Characters(text)) = e {
+ data = Some(
+ text.replace("\n", "")
+ .split(',')
+ .map(|s| s.parse().unwrap())
+ .collect(),
+ );
+ }
+ }
+
+ Map {
+ data: data.unwrap(),
+ width: width.unwrap(),
+ height: height.unwrap(),
+ }
+ }
+}
+
+pub struct Tileset {
+ pub tiles: Vec<Rect>,
+ 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::<usize>().unwrap()),
+ "tilewidth" => tile_width = Some(attribute.value.parse::<f32>().unwrap()),
+ "tileheight" => tile_height = Some(attribute.value.parse::<f32>().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(),
+ }
+ }
+}