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 /src/tile.rs | |
parent | f7bbf5646c8220bca2fbd9451d5c234049cf9225 (diff) |
tiles can now animate
Diffstat (limited to 'src/tile.rs')
-rw-r--r-- | src/tile.rs | 18 |
1 files changed, 16 insertions, 2 deletions
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) |