From 0947ce5a918207efeaf2a4f67a1cc410795f057a Mon Sep 17 00:00:00 2001 From: tom barrett Date: Mon, 1 Jul 2019 09:14:17 -0500 Subject: simpilfied interfaces and added new xml properties --- src/tileset.rs | 39 ++++++++++++++++++--------------------- 1 file changed, 18 insertions(+), 21 deletions(-) (limited to 'src/tileset.rs') diff --git a/src/tileset.rs b/src/tileset.rs index 45d68a3..dcd5e9a 100644 --- a/src/tileset.rs +++ b/src/tileset.rs @@ -3,11 +3,12 @@ use ggez::graphics::Rect; use std::collections::HashMap; use crate::constants; -use crate::xmlelements::{Property, XMLElements}; +use crate::property::Property; +use crate::xmlelements::XMLElements; pub struct Tileset { tiles: HashMap, - properties: HashMap, + properties: Vec, } impl Tileset { @@ -42,17 +43,18 @@ impl Tileset { } } - let mut properties = HashMap::new(); + let mut properties = Vec::new(); for tile_element in elements.get_elements("tile") { let tile_id = XMLElements::get_attribute(&tile_element, "id") .unwrap() .parse::() - .unwrap(); + .unwrap() + + 1; let property_elements = elements.get_children(&tile_element, "property"); - properties.insert(tile_id + 1, Property::new(property_elements)); + properties.push(Property::new(tile_id, property_elements)); } Tileset { tiles, properties } @@ -62,22 +64,16 @@ impl Tileset { *self.tiles.get(&id).unwrap() } - pub fn get_animation(&self, id: usize) -> Option> { - if let Some(property) = self.properties.get(&id) { - let entitys_properties: HashMap = self - .properties + pub fn get_animation(&self, tile_id: usize) -> Vec<(usize, Rect)> { + if let Some(property) = self.properties.iter().find(|p| p.tile_id == tile_id) { + 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(), - ) + .filter(|p| p.entity == property.entity && p.entity.is_some()) + .map(|p| (p.delay.unwrap(), self.get(p.tile_id))) + .collect() } else { - None + Vec::new() } } @@ -85,11 +81,12 @@ impl Tileset { *self .tiles .get( - self.properties + &self + .properties .iter() - .find(|(_, p)| p.entity == entity && keyframe == p.keyframe) + .find(|p| p.entity == Some(entity.to_string()) && Some(keyframe) == p.keyframe) .unwrap() - .0, + .tile_id, ) .unwrap() } -- cgit v1.2.3