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 --- resources/map.tmx | 4 ++-- resources/tileset.png | Bin 10252 -> 10036 bytes resources/tileset.tsx | 6 ++++++ src/lib.rs | 1 + src/math.rs | 23 ++++++++++------------- src/player.rs | 10 +++++++--- src/property.rs | 44 ++++++++++++++++++++++++++++++++++++++++++++ src/tile.rs | 2 +- src/tileset.rs | 39 ++++++++++++++++++--------------------- src/xmlelements.rs | 32 +------------------------------- 10 files changed, 90 insertions(+), 71 deletions(-) create mode 100644 src/property.rs diff --git a/resources/map.tmx b/resources/map.tmx index 766cd89..de8221e 100644 --- a/resources/map.tmx +++ b/resources/map.tmx @@ -80,8 +80,8 @@ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, -0,0,0,0,0,0,0,0,0,2147483683,0,0,35,0,0,0,0,0,0,0, -0,0,0,0,0,0,0,0,0,2147483693,0,0,45,0,0,0,0,0,0,0, +0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, +0,0,0,0,0,0,0,0,0,26,0,0,26,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, diff --git a/resources/tileset.png b/resources/tileset.png index 56f2cce..8a32cbf 100644 Binary files a/resources/tileset.png and b/resources/tileset.png differ diff --git a/resources/tileset.tsx b/resources/tileset.tsx index 67229a4..d5a44dd 100644 --- a/resources/tileset.tsx +++ b/resources/tileset.tsx @@ -15,6 +15,12 @@ + + + + + + diff --git a/src/lib.rs b/src/lib.rs index ac7c4f3..1c63cef 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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>, - 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>, + animation: Vec<(usize, Rect)>, animations: HashMap>, 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, + pub keyframe: Option, + pub delay: Option, + pub spawn: Option, + pub visible: Option, +} + +impl Property { + pub fn new(tile_id: usize, property_elements: Vec) -> 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>, + animation: Vec<(usize, Rect)>, timer: Instant, destination: Point2, 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, - 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() } 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) -> 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, } @@ -143,7 +113,7 @@ impl XMLElements { false } }) - .unwrap(); + .ok_or(())?; if let StartElement { attributes, .. } = element { Ok(attributes -- cgit v1.2.3