From 05856461f35caa0cebf8ba69cbf36132485d5488 Mon Sep 17 00:00:00 2001 From: tom barrett Date: Thu, 20 Jun 2019 04:48:32 -0500 Subject: now goes through animation vectors --- src/player.rs | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 56 insertions(+), 6 deletions(-) (limited to 'src/player.rs') 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, state: PlayerState, + tile: Rect, + animation: Vec<(u32, Rect)>, + animations: HashMap>, } 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, -- cgit v1.2.3