From bfdaef7850b6ac17bb88f1b314236fb5014aac8e Mon Sep 17 00:00:00 2001 From: tom barrett Date: Fri, 5 Jul 2019 08:59:22 -0500 Subject: now a cells struct whichs layers own, which takes from tileset tiles --- src/animations.rs | 89 ++++++++++++++----------------------------------------- 1 file changed, 22 insertions(+), 67 deletions(-) (limited to 'src/animations.rs') diff --git a/src/animations.rs b/src/animations.rs index e308f1d..5e5740a 100644 --- a/src/animations.rs +++ b/src/animations.rs @@ -1,72 +1,42 @@ -use ggez::graphics::{spritebatch::SpriteBatch, DrawParam, Rect}; +use ggez::graphics::{spritebatch::SpriteBatch, DrawParam}; use ggez::nalgebra::{Point2, Vector2}; use std::collections::HashMap; -use std::f32::consts::PI; use std::time::Instant; use crate::constants; use crate::entity::Action; -use crate::tileset::Tileset; - -#[derive(Debug, Clone, PartialEq)] -pub struct Frame { - pub source: Rect, - pub delay: Option, - pub rotation: f32, -} - -impl Frame { - pub fn new(source: Rect, delay: Option, rotation: f32) -> Frame { - Frame { - source, - delay, - rotation, - } - } -} - -impl Default for Frame { - fn default() -> Frame { - Frame::new(Rect::zero(), None, 0.0) - } -} +use crate::tileset::{Tile, Tileset}; #[derive(Debug, Clone, PartialEq)] pub struct Animation { - pub frames: Vec, + pub frames: Vec, pub timer: Instant, - pub current: Frame, -} - -impl Default for Animation { - fn default() -> Animation { - Animation::new(Frame::default()) - } + pub current: Tile, } impl Animation { - pub fn new(current: Frame) -> Animation { + pub fn new(frames: Vec) -> Animation { Animation { - current, + current: frames[0].clone(), timer: Instant::now(), - frames: Vec::new(), + frames, } } - pub fn give_frames(&mut self, frames: Vec) { + pub fn give_frames(&mut self, frames: Vec) { self.frames = frames; } pub fn update(&mut self) { if let Some(mut i) = self.frames.iter().position(|a| a == &self.current) { - if let Some(delay) = self.current.delay { + if let Some(delay) = self.current.property.delay { if self.timer.elapsed().as_millis() > delay as u128 { i = if i == self.frames.len() - 1 { 0 } else { i + 1 }; self.current = self.frames[i].clone(); self.timer = Instant::now(); } } - } else if !self.frames.is_empty() { + } else { self.current = self.frames[0].clone(); } } @@ -91,38 +61,34 @@ impl Animations { pub fn new(tileset: &Tileset) -> Animations { let mut available = HashMap::new(); - let mut idle = tileset.get_frame_by_entity_keyframe("player-top", 0); - idle.source.h += tileset - .get_frame_by_entity_keyframe("player-bottom", 0) - .source - .h; + let mut idle = tileset.get_tile_by_entity_keyframe("player-top", 0); + idle.source.h *= 2.0; - let mut animation = Animation::new(idle.clone()); - animation.give_frames(vec![idle.clone()]); + let animation = Animation::new(vec![idle.clone()]); available.insert(Action::IdleLeft, animation.clone()); - let mut moving = tileset.get_frame_by_entity_keyframe("player-top", 1); - moving.source.h += tileset - .get_frame_by_entity_keyframe("player-bottom", 1) - .source - .h; + let mut moving = tileset.get_tile_by_entity_keyframe("player-top", 1); + moving.source.h *= 2.0; - animation.give_frames(vec![idle.clone(), moving.clone()]); + let animation = Animation::new(vec![idle.clone(), moving.clone()]); available.insert(Action::MovingLeft, animation.clone()); available.insert(Action::MovingUpLeft, animation.clone()); available.insert(Action::MovingDownLeft, animation.clone()); - animation.give_frames(vec![flip(idle.clone())]); + idle.flip(); + moving.flip(); + + let animation = Animation::new(vec![idle.clone()]); available.insert(Action::IdleRight, animation.clone()); - animation.give_frames(vec![flip(idle.clone()), flip(moving.clone())]); + let animation = Animation::new(vec![idle.clone(), moving.clone()]); available.insert(Action::MovingRight, animation.clone()); available.insert(Action::MovingUpRight, animation.clone()); available.insert(Action::MovingDownRight, animation.clone()); Animations { available, - current: Animation::default(), + current: animation, } } @@ -137,14 +103,3 @@ impl Animations { self.current.draw(spritebatch, position) } } - -pub fn flip(frame: Frame) -> Frame { - let mut f = frame.clone(); - f.source.x *= -1.0; - f.source.x -= frame.source.w; - f -} - -pub fn convert_angle_to_rad(angle: f32) -> f32 { - angle * (PI / 180.0) -} -- cgit v1.2.3