From 53b4ccdcc6cdac3b02480a97a2ec30439114ff6c Mon Sep 17 00:00:00 2001 From: tom barrett Date: Wed, 26 Jun 2019 05:50:34 -0500 Subject: now can parse tile properties --- src/tileset.rs | 38 +++++++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 7 deletions(-) (limited to 'src/tileset.rs') diff --git a/src/tileset.rs b/src/tileset.rs index ae65097..a5c00af 100644 --- a/src/tileset.rs +++ b/src/tileset.rs @@ -1,37 +1,61 @@ use ggez::filesystem::File; use ggez::graphics::Rect; +use std::collections::HashMap; use crate::constants; -use crate::xmlelements::XMLElements; +use crate::xmlelements::{XMLElements, Property}; pub struct Tileset { - pub tiles: Vec, + tiles: HashMap, + properties: HashMap, } impl Tileset { pub fn new(file: File) -> Tileset { let elements = XMLElements::new(file); - let height = elements.get_element_attribute("image", "height").unwrap(); + let height = elements + .get_element_attribute("image", "height") + .unwrap() + .parse::() + .unwrap(); let columns = elements .get_element_attribute("tileset", "columns") + .unwrap() + .parse::() .unwrap(); let rows = height / (constants::TILE_HEIGHT as usize); - let mut tiles = Vec::new(); - tiles.push(Rect::zero()); + let mut tiles = HashMap::new(); + tiles.insert(0, Rect::zero()); let w = 1.0 / columns as f32; let h = 1.0 / rows as f32; + let mut key = 1; for r in 0..rows { for c in 0..columns { let x = c as f32 / columns as f32; let y = r as f32 / rows as f32; - tiles.push(Rect::new(x, y, w, h)); + tiles.insert(key, Rect::new(x, y, w, h)); + key += 1; } } - Tileset { tiles } + let mut properties = HashMap::new(); + + for tile_element in elements.get_elements("tile") { + let tile_id = XMLElements::get_attribute(&tile_element, "id").unwrap().parse().unwrap(); + + let property_elements = elements.get_children(&tile_element, "property"); + + properties.insert(tile_id, Property::new(property_elements)); + } + + Tileset { tiles, properties } + } + + pub fn get(&self, id: usize) -> Rect { + *self.tiles.get(&id).unwrap() } } -- cgit v1.2.3 From f7bbf5646c8220bca2fbd9451d5c234049cf9225 Mon Sep 17 00:00:00 2001 From: tom barrett Date: Wed, 26 Jun 2019 06:01:12 -0500 Subject: clippy and fmt --- src/tileset.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'src/tileset.rs') diff --git a/src/tileset.rs b/src/tileset.rs index a5c00af..44941df 100644 --- a/src/tileset.rs +++ b/src/tileset.rs @@ -3,7 +3,7 @@ use ggez::graphics::Rect; use std::collections::HashMap; use crate::constants; -use crate::xmlelements::{XMLElements, Property}; +use crate::xmlelements::{Property, XMLElements}; pub struct Tileset { tiles: HashMap, @@ -45,7 +45,10 @@ impl Tileset { let mut properties = HashMap::new(); for tile_element in elements.get_elements("tile") { - let tile_id = XMLElements::get_attribute(&tile_element, "id").unwrap().parse().unwrap(); + let tile_id = XMLElements::get_attribute(&tile_element, "id") + .unwrap() + .parse() + .unwrap(); let property_elements = elements.get_children(&tile_element, "property"); -- cgit v1.2.3 From ee8d055be8326eb1561900bcca6acd9e38071f4a Mon Sep 17 00:00:00 2001 From: tom barrett Date: Thu, 27 Jun 2019 03:31:33 -0500 Subject: tiles can now animate --- src/tileset.rs | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) (limited to 'src/tileset.rs') diff --git a/src/tileset.rs b/src/tileset.rs index 44941df..548cde6 100644 --- a/src/tileset.rs +++ b/src/tileset.rs @@ -47,12 +47,12 @@ impl Tileset { for tile_element in elements.get_elements("tile") { let tile_id = XMLElements::get_attribute(&tile_element, "id") .unwrap() - .parse() + .parse::() .unwrap(); let property_elements = elements.get_children(&tile_element, "property"); - properties.insert(tile_id, Property::new(property_elements)); + properties.insert(tile_id + 1, Property::new(property_elements)); } Tileset { tiles, properties } @@ -61,4 +61,27 @@ impl Tileset { pub fn get(&self, id: usize) -> Rect { *self.tiles.get(&id).unwrap() } + + pub fn get_animations(&self, id: usize) -> Option> { + if let Some(property) = self.properties.get(&id) { + let entitys_properties: HashMap = self + .properties + .clone() + .into_iter() + .filter(|(_, p)| p.entity == property.entity) + .collect(); + Some( + entitys_properties + .iter() + .map(|(id, p)| (p.delay, self.get(*id))) + .collect(), + ) + } else { + None + } + } + + pub fn get_property(&self, id: usize) -> Option<&Property> { + self.properties.get(&id) + } } -- cgit v1.2.3