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 --- README.md | 1 - src/player.rs | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++------ src/state.rs | 6 +++--- 3 files changed, 59 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index d4d212e..f39b3b9 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,6 @@ ### goals * character sprite -* camera centered around it * collision * sprites with movements (tile states) * fat binary (include-bytes!) 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, diff --git a/src/state.rs b/src/state.rs index 299ecc9..4962c39 100644 --- a/src/state.rs +++ b/src/state.rs @@ -34,8 +34,8 @@ impl State { } impl EventHandler for State { - fn update(&mut self, _: &mut Context) -> GameResult { - self.player.update(); + fn update(&mut self, context: &mut Context) -> GameResult { + self.player.update(context); self.camera.give_center(self.player.position); Ok(()) } @@ -44,7 +44,7 @@ impl EventHandler for State { graphics::clear(context, graphics::BLACK); self.map.draw(&mut self.spritebatch, &self.tileset); - self.player.draw(&mut self.spritebatch, &self.tileset); + self.player.draw(&mut self.spritebatch); graphics::draw( context, -- cgit v1.2.3