diff options
author | tom barrett <spalf0@gmail.com> | 2019-06-20 04:48:32 -0500 |
---|---|---|
committer | tom barrett <spalf0@gmail.com> | 2019-06-20 04:48:32 -0500 |
commit | 05856461f35caa0cebf8ba69cbf36132485d5488 (patch) | |
tree | 24bdea79e0e5242a549bc48d9c100db809c41fc3 /src/player.rs | |
parent | b4072b110df8886089d807bcd78891430ea7f974 (diff) |
now goes through animation vectors
Diffstat (limited to 'src/player.rs')
-rw-r--r-- | src/player.rs | 62 |
1 files changed, 56 insertions, 6 deletions
diff --git a/src/player.rs b/src/player.rs index 976ab05..0350656 100644 --- a/src/player.rs +++ b/src/player.rs @@ -1,33 +1,59 @@ use ggez::event::KeyCode; -use ggez::graphics::{spritebatch::SpriteBatch, DrawParam}; +use ggez::graphics::{spritebatch::SpriteBatch, DrawParam, Rect}; use ggez::nalgebra::{Point2, Vector2}; +use ggez::timer::check_update_time; +use ggez::Context; +use std::collections::HashMap; use crate::constants; -use crate::tileset::Tileset; pub struct Player { pub position: Point2<f32>, state: PlayerState, + tile: Rect, + animation: Vec<(u32, Rect)>, + animations: HashMap<PlayerState, Vec<(u32, Rect)>>, } impl Player { pub fn new() -> Player { + let mut animations = HashMap::new(); + + let mut idle = Vec::new(); + idle.push((1, Rect::new(0.0, 0.0, 0.2, 0.2))); + idle.push((1, Rect::new(0.2, 0.0, 0.2, 0.2))); + + let mut moving_right = Vec::new(); + moving_right.push((1, Rect::new(0.4, 0.0, 0.2, 0.2))); + moving_right.push((1, Rect::new(0.6, 0.0, 0.2, 0.2))); + + animations.insert(PlayerState::Idle, idle); + animations.insert(PlayerState::MovingRight, moving_right); + Player { position: Point2::new(0.0, 0.0), state: PlayerState::Idle, + tile: Rect::zero(), + animation: Vec::new(), + animations, } } - pub fn draw(&self, spritebatch: &mut SpriteBatch, tileset: &Tileset) { + pub fn draw(&self, spritebatch: &mut SpriteBatch) { let draw_param = DrawParam::default() - .src(tileset.tiles[1]) + .src(self.tile) .dest(self.position) .scale(Vector2::new(constants::TILE_SCALE, constants::TILE_SCALE)); spritebatch.add(draw_param); } - pub fn update(&mut self) { + pub fn update(&mut self, context: &mut Context) { + self.move_position(); + self.find_tile(context); + } + + fn move_position(&mut self) { match self.state { PlayerState::MovingUp => self.position.y -= constants::PLAYER_SPEED, PlayerState::MovingUpLeft => { @@ -53,6 +79,30 @@ impl Player { } } + fn find_tile(&mut self, context: &mut Context) { + self.animation = match self.animations.get(&self.state) { + Some(animation) => animation.to_vec(), + None => self.animations.get(&PlayerState::Idle).unwrap().to_vec(), + }; + + let index = match self.animation.iter().position(|a| a.1 == self.tile) { + Some(index) => { + if check_update_time(context, self.animation[index].0) { + index + 1 + } else { + index + } + } + None => 0, + }; + + if index == self.animation.len() { + self.tile = self.animation[0].1; + } else { + self.tile = self.animation[index].1; + } + } + pub fn give_key_down(&mut self, keycode: KeyCode) { let original_state = self.state.clone(); @@ -116,7 +166,7 @@ impl Default for Player { } } -#[derive(Clone)] +#[derive(Clone, Hash, Eq, PartialEq)] enum PlayerState { Idle, MovingUp, |