diff options
author | tom barrett <spalf0@gmail.com> | 2019-07-01 09:14:17 -0500 |
---|---|---|
committer | tom barrett <spalf0@gmail.com> | 2019-07-01 09:14:17 -0500 |
commit | 0947ce5a918207efeaf2a4f67a1cc410795f057a (patch) | |
tree | 30d601261754e236fc064faa9138eec9d1314d7c /src | |
parent | 4b01b87d8aca041304c927560095af92feb406da (diff) |
simpilfied interfaces and added new xml properties
Diffstat (limited to 'src')
-rw-r--r-- | src/lib.rs | 1 | ||||
-rw-r--r-- | src/math.rs | 23 | ||||
-rw-r--r-- | src/player.rs | 10 | ||||
-rw-r--r-- | src/property.rs | 44 | ||||
-rw-r--r-- | src/tile.rs | 2 | ||||
-rw-r--r-- | src/tileset.rs | 39 | ||||
-rw-r--r-- | src/xmlelements.rs | 32 |
7 files changed, 82 insertions, 69 deletions
@@ -4,6 +4,7 @@ pub mod layer; pub mod map; pub mod math; pub mod player; +pub mod property; pub mod state; pub mod tile; pub mod tileset; diff --git a/src/math.rs b/src/math.rs index 7599f87..c747cce 100644 --- a/src/math.rs +++ b/src/math.rs @@ -13,20 +13,17 @@ pub fn flip(rect: Rect) -> Rect { r } -pub fn next_source( - source: Rect, - animation: &Option<Vec<(usize, Rect)>>, - timer: Instant, -) -> (Rect, Instant) { - if let Some(animation) = animation { - if let Some(mut i) = animation.iter().position(|a| a.1 == source) { - if timer.elapsed().as_millis() > animation[i].0 as u128 { - i = if i == animation.len() - 1 { 0 } else { i + 1 }; - return (animation[i].1, Instant::now()); - } +pub fn next_source(source: Rect, animation: &[(usize, Rect)], timer: Instant) -> (Rect, Instant) { + if let Some(mut i) = animation.iter().position(|a| a.1 == source) { + if timer.elapsed().as_millis() > animation[i].0 as u128 { + i = if i == animation.len() - 1 { 0 } else { i + 1 }; + (animation[i].1, Instant::now()) } else { - return (animation[0].1, Instant::now()); + (source, timer) } + } else if !animation.is_empty() { + (animation[0].1, timer) + } else { + (source, timer) } - (source, timer) } diff --git a/src/player.rs b/src/player.rs index d30288b..1f941be 100644 --- a/src/player.rs +++ b/src/player.rs @@ -13,7 +13,7 @@ pub struct Player { state: PlayerState, source: Rect, timer: Instant, - animation: Option<Vec<(usize, Rect)>>, + animation: Vec<(usize, Rect)>, animations: HashMap<PlayerState, Vec<(usize, Rect)>>, map_height: f32, map_width: f32, @@ -26,7 +26,7 @@ impl Player { state: PlayerState::IdleLeft, source: Rect::zero(), timer: Instant::now(), - animation: None, + animation: Vec::new(), animations: Player::build_animations(tileset), map_width: dimensions.0, map_height: dimensions.1, @@ -83,7 +83,11 @@ impl Player { pub fn update(&mut self) { self.move_position(); - self.animation = self.animations.get(&self.state).cloned(); + self.animation = self + .animations + .get(&self.state) + .cloned() + .unwrap_or_default(); let (source, timer) = next_source(self.source, &self.animation, self.timer); self.source = source; self.timer = timer; diff --git a/src/property.rs b/src/property.rs new file mode 100644 index 0000000..fb4e912 --- /dev/null +++ b/src/property.rs @@ -0,0 +1,44 @@ +use xml::reader::XmlEvent; + +use crate::xmlelements::XMLElements; + +#[derive(Debug, Clone)] +pub struct Property { + pub tile_id: usize, + pub entity: Option<String>, + pub keyframe: Option<usize>, + pub delay: Option<usize>, + pub spawn: Option<String>, + pub visible: Option<bool>, +} + +impl Property { + pub fn new(tile_id: usize, property_elements: Vec<XmlEvent>) -> Property { + let entity = match XMLElements::get_attribute_value(&property_elements, "entity") { + Ok(entity) => entity.parse().ok(), + Err(_) => None, + }; + let keyframe = match XMLElements::get_attribute_value(&property_elements, "keyframe") { + Ok(keyframe) => keyframe.parse().ok(), + Err(_) => None, + }; + let delay = match XMLElements::get_attribute_value(&property_elements, "delay") { + Ok(delay) => delay.parse().ok(), + Err(_) => None, + }; + let spawn = XMLElements::get_attribute_value(&property_elements, "spawn").ok(); + let visible = match XMLElements::get_attribute_value(&property_elements, "visible") { + Ok(visible) => visible.parse().ok(), + Err(_) => None, + }; + + Property { + tile_id, + entity, + keyframe, + delay, + spawn, + visible, + } + } +} diff --git a/src/tile.rs b/src/tile.rs index c8c2ae7..45b8b3e 100644 --- a/src/tile.rs +++ b/src/tile.rs @@ -8,7 +8,7 @@ use crate::tileset::Tileset; pub struct Tile { source: Rect, - animation: Option<Vec<(usize, Rect)>>, + animation: Vec<(usize, Rect)>, timer: Instant, destination: Point2<f32>, rotation: f32, 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<usize, Rect>, - properties: HashMap<usize, Property>, + properties: Vec<Property>, } 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::<usize>() - .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<Vec<(usize, Rect)>> { - if let Some(property) = self.properties.get(&id) { - let entitys_properties: HashMap<usize, Property> = 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() } diff --git a/src/xmlelements.rs b/src/xmlelements.rs index ea880c8..23dafc4 100644 --- a/src/xmlelements.rs +++ b/src/xmlelements.rs @@ -5,36 +5,6 @@ use xml::reader::{ XmlEvent::{self, EndElement, StartElement}, }; -#[derive(Debug, Clone)] -pub struct Property { - pub entity: String, - pub keyframe: usize, - pub delay: usize, -} - -impl Property { - pub fn new(property_elements: Vec<XmlEvent>) -> Property { - let entity = XMLElements::get_attribute_value(&property_elements, "entity") - .unwrap() - .parse() - .unwrap(); - let keyframe = XMLElements::get_attribute_value(&property_elements, "keyframe") - .unwrap() - .parse() - .unwrap(); - let delay = XMLElements::get_attribute_value(&property_elements, "delay") - .unwrap() - .parse() - .unwrap(); - - Property { - entity, - keyframe, - delay, - } - } -} - pub struct XMLElements { pub events: Vec<XmlEvent>, } @@ -143,7 +113,7 @@ impl XMLElements { false } }) - .unwrap(); + .ok_or(())?; if let StartElement { attributes, .. } = element { Ok(attributes |