diff options
author | tom barrett <spalf0@gmail.com> | 2019-06-28 10:05:13 -0500 |
---|---|---|
committer | tom barrett <spalf0@gmail.com> | 2019-06-28 10:05:13 -0500 |
commit | 7d59cd2510ac926edd0790de4bffb0fbb9c60e7c (patch) | |
tree | 5b17c70031f57de3ba0f663018fdc41bad31f953 | |
parent | 39ea81bea4a61baf8c95a33bcc979b0e65ce660e (diff) |
animation working
-rw-r--r-- | src/math.rs | 9 | ||||
-rw-r--r-- | src/player.rs | 32 | ||||
-rw-r--r-- | src/tile.rs | 13 | ||||
-rw-r--r-- | src/tileset.rs | 4 |
4 files changed, 43 insertions, 15 deletions
diff --git a/src/math.rs b/src/math.rs index b6f53ed..7599f87 100644 --- a/src/math.rs +++ b/src/math.rs @@ -6,6 +6,13 @@ pub fn convert_angle_to_rad(angle: f32) -> f32 { angle * (PI / 180.0) } +pub fn flip(rect: Rect) -> Rect { + let mut r = rect; + r.x *= -1.0; + r.x -= rect.w; + r +} + pub fn next_source( source: Rect, animation: &Option<Vec<(usize, Rect)>>, @@ -17,6 +24,8 @@ pub fn next_source( i = if i == animation.len() - 1 { 0 } else { i + 1 }; return (animation[i].1, Instant::now()); } + } else { + return (animation[0].1, Instant::now()); } } (source, timer) diff --git a/src/player.rs b/src/player.rs index 6e2bf06..f7db814 100644 --- a/src/player.rs +++ b/src/player.rs @@ -5,7 +5,7 @@ use std::collections::HashMap; use std::time::Instant; use crate::constants; -use crate::math::next_source; +use crate::math::{flip, next_source}; use crate::tileset::Tileset; pub struct Player { @@ -23,10 +23,36 @@ impl Player { pub fn new(tileset: &Tileset, dimensions: (f32, f32)) -> Player { let mut animations = HashMap::new(); - let mut source = tileset.get_rect_by_entity("player-top"); - source.h += tileset.get_rect_by_entity("player-bottom").h; + let mut source = tileset.get_tile_by_entity_keyframe("player-top", 0); + source.h += tileset.get_tile_by_entity_keyframe("player-bottom", 0).h; animations.insert(PlayerState::Idle, vec![(1, source)]); + let mut moving = tileset.get_tile_by_entity_keyframe("player-top", 1); + moving.h += tileset.get_tile_by_entity_keyframe("player-bottom", 1).h; + + animations.insert(PlayerState::MovingLeft, vec![(100, source), (100, moving)]); + animations.insert( + PlayerState::MovingUpLeft, + vec![(100, source), (100, moving)], + ); + animations.insert( + PlayerState::MovingDownLeft, + vec![(100, source), (100, moving)], + ); + + source = flip(source); + moving = flip(moving); + + animations.insert(PlayerState::MovingRight, vec![(100, source), (100, moving)]); + animations.insert( + PlayerState::MovingUpRight, + vec![(100, source), (100, moving)], + ); + animations.insert( + PlayerState::MovingDownRight, + vec![(100, source), (100, moving)], + ); + Player { position: Point2::new(0.0, 0.0), state: PlayerState::Idle, diff --git a/src/tile.rs b/src/tile.rs index a367592..c8c2ae7 100644 --- a/src/tile.rs +++ b/src/tile.rs @@ -3,7 +3,7 @@ use ggez::nalgebra::{Point2, Vector2}; use std::time::Instant; use crate::constants; -use crate::math::{convert_angle_to_rad, next_source}; +use crate::math::{convert_angle_to_rad, flip, next_source}; use crate::tileset::Tileset; pub struct Tile { @@ -29,12 +29,12 @@ impl Tile { }; let (source, rotation) = match (flip_d, flip_h, flip_v) { - (true, true, true) => (Tile::flip(tileset.get(id)), convert_angle_to_rad(90.0)), + (true, true, true) => (flip(tileset.get(id)), convert_angle_to_rad(90.0)), (true, true, false) => (tileset.get(id), convert_angle_to_rad(90.0)), (true, false, true) => (tileset.get(id), convert_angle_to_rad(270.0)), //(true, false, false) => (), (false, true, true) => (tileset.get(id), convert_angle_to_rad(180.0)), - (false, true, false) => (Tile::flip(tileset.get(id)), 0.0), + (false, true, false) => (flip(tileset.get(id)), 0.0), //(false, false, true) => (), //(false, false, false) => (), _ => (tileset.get(id), 0.0), @@ -58,13 +58,6 @@ impl Tile { } } - fn flip(rect: Rect) -> Rect { - let mut r = rect; - r.x *= -1.0; - r.x -= rect.w; - r - } - pub fn update(&mut self) { let (source, timer) = next_source(self.source, &self.animation, self.timer); self.source = source; diff --git a/src/tileset.rs b/src/tileset.rs index eb097e5..45d68a3 100644 --- a/src/tileset.rs +++ b/src/tileset.rs @@ -81,13 +81,13 @@ impl Tileset { } } - pub fn get_rect_by_entity(&self, entity: &str) -> Rect { + pub fn get_tile_by_entity_keyframe(&self, entity: &str, keyframe: usize) -> Rect { *self .tiles .get( self.properties .iter() - .find(|(_, p)| p.entity == entity && 0 == p.keyframe) + .find(|(_, p)| p.entity == entity && keyframe == p.keyframe) .unwrap() .0, ) |