diff options
author | tom barrett <spalf0@gmail.com> | 2019-06-27 03:31:33 -0500 |
---|---|---|
committer | tom barrett <spalf0@gmail.com> | 2019-06-27 03:31:33 -0500 |
commit | ee8d055be8326eb1561900bcca6acd9e38071f4a (patch) | |
tree | adc6da1976866de4b3bf436f99e6eac673af2df3 | |
parent | f7bbf5646c8220bca2fbd9451d5c234049cf9225 (diff) |
tiles can now animate
-rw-r--r-- | src/layer.rs | 6 | ||||
-rw-r--r-- | src/map.rs | 6 | ||||
-rw-r--r-- | src/state.rs | 1 | ||||
-rw-r--r-- | src/tile.rs | 18 | ||||
-rw-r--r-- | src/tileset.rs | 27 | ||||
-rw-r--r-- | src/xmlelements.rs | 8 |
6 files changed, 58 insertions, 8 deletions
diff --git a/src/layer.rs b/src/layer.rs index efecd89..c9f32e8 100644 --- a/src/layer.rs +++ b/src/layer.rs @@ -23,6 +23,12 @@ impl Layer { } } + pub fn update(&mut self) { + for tile in self.tiles.iter_mut() { + tile.update(); + } + } + pub fn draw(&self, spritebatch: &mut SpriteBatch) { for tile in self.tiles.iter() { tile.draw(spritebatch); @@ -53,6 +53,12 @@ impl Map { } } + pub fn update(&mut self) { + for layer in self.layers.iter_mut() { + layer.update(); + } + } + pub fn get_dimensions(&self) -> (f32, f32) { ( (constants::TILE_WIDTH * constants::TILE_SCALE) * self.width as f32, diff --git a/src/state.rs b/src/state.rs index b06d535..a981924 100644 --- a/src/state.rs +++ b/src/state.rs @@ -36,6 +36,7 @@ impl State { impl EventHandler for State { fn update(&mut self, context: &mut Context) -> GameResult { + self.map.update(); self.player.update(context); self.camera.give_center(self.player.position); Ok(()) diff --git a/src/tile.rs b/src/tile.rs index dc9987c..198be10 100644 --- a/src/tile.rs +++ b/src/tile.rs @@ -1,5 +1,6 @@ use ggez::graphics::{spritebatch::SpriteBatch, DrawParam, Rect}; use ggez::nalgebra::{Point2, Vector2}; +use std::time::Instant; use crate::constants; use crate::math::convert_angle_to_rad; @@ -7,7 +8,8 @@ use crate::tileset::Tileset; pub struct Tile { source: Rect, - //animation: Option<Vec<(u32, Rect)>>, + animations: Option<Vec<(usize, Rect)>>, + timer: Instant, destination: Point2<f32>, rotation: f32, } @@ -49,7 +51,8 @@ impl Tile { Tile { source, - //animation: None, + animations: tileset.get_animations(id), + timer: Instant::now(), destination, rotation, } @@ -62,6 +65,17 @@ impl Tile { r } + pub fn update(&mut self) { + if let Some(animations) = &self.animations { + let mut i = animations.iter().position(|a| a.1 == self.source).unwrap(); + if self.timer.elapsed().as_millis() > animations[i].0 as u128 { + i = if i == animations.len() - 1 { 0 } else { i + 1 }; + self.source = animations[i].1; + self.timer = Instant::now() + } + } + } + pub fn draw(&self, spritebatch: &mut SpriteBatch) { let draw_param = DrawParam::default() .src(self.source) 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::<usize>() .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<Vec<(usize, Rect)>> { + if let Some(property) = self.properties.get(&id) { + let entitys_properties: HashMap<usize, Property> = 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) + } } diff --git a/src/xmlelements.rs b/src/xmlelements.rs index d05e9ff..ea880c8 100644 --- a/src/xmlelements.rs +++ b/src/xmlelements.rs @@ -5,11 +5,11 @@ use xml::reader::{ XmlEvent::{self, EndElement, StartElement}, }; -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct Property { - entity: String, - keyframe: usize, - delay: usize, + pub entity: String, + pub keyframe: usize, + pub delay: usize, } impl Property { |