From 39ea81bea4a61baf8c95a33bcc979b0e65ce660e Mon Sep 17 00:00:00 2001 From: tom barrett Date: Fri, 28 Jun 2019 08:37:07 -0500 Subject: unified interfaces --- src/math.rs | 18 +++++++++++++++++ src/player.rs | 64 ++++++++++++++++++---------------------------------------- src/state.rs | 6 +++--- src/tile.rs | 18 ++++++----------- src/tileset.rs | 15 +++++++++++--- 5 files changed, 59 insertions(+), 62 deletions(-) diff --git a/src/math.rs b/src/math.rs index c7ea7f6..b6f53ed 100644 --- a/src/math.rs +++ b/src/math.rs @@ -1,5 +1,23 @@ +use ggez::graphics::Rect; use std::f32::consts::PI; +use std::time::Instant; pub fn convert_angle_to_rad(angle: f32) -> f32 { angle * (PI / 180.0) } + +pub fn next_source( + source: Rect, + animation: &Option>, + timer: Instant, +) -> (Rect, Instant) { + if let Some(animation) = animation { + if let Some(mut i) = animation.iter().position(|a| a.1 == source) { + if timer.elapsed().as_millis() > animation[i].0 as u128 { + i = if i == animation.len() - 1 { 0 } else { i + 1 }; + return (animation[i].1, Instant::now()); + } + } + } + (source, timer) +} diff --git a/src/player.rs b/src/player.rs index e9c3a75..6e2bf06 100644 --- a/src/player.rs +++ b/src/player.rs @@ -1,42 +1,38 @@ use ggez::event::KeyCode; 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 std::time::Instant; use crate::constants; +use crate::math::next_source; +use crate::tileset::Tileset; pub struct Player { pub position: Point2, state: PlayerState, - tile: Rect, - animation: Vec<(usize, Rect)>, + source: Rect, + timer: Instant, + animation: Option>, animations: HashMap>, map_height: f32, map_width: f32, } impl Player { - pub fn new(dimensions: (f32, f32)) -> Player { + pub fn new(tileset: &Tileset, dimensions: (f32, f32)) -> 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); + let mut source = tileset.get_rect_by_entity("player-top"); + source.h += tileset.get_rect_by_entity("player-bottom").h; + animations.insert(PlayerState::Idle, vec![(1, source)]); Player { position: Point2::new(0.0, 0.0), state: PlayerState::Idle, - tile: Rect::zero(), - animation: Vec::new(), + source, + timer: Instant::now(), + animation: None, animations, map_width: dimensions.0, map_height: dimensions.1, @@ -45,16 +41,20 @@ impl Player { pub fn draw(&self, spritebatch: &mut SpriteBatch) { let draw_param = DrawParam::default() - .src(self.tile) + .src(self.source) .dest(self.position) .scale(Vector2::new(constants::TILE_SCALE, constants::TILE_SCALE)); spritebatch.add(draw_param); } - pub fn update(&mut self, context: &mut Context) { + pub fn update(&mut self) { self.move_position(); - self.find_tile(context); + + self.animation = self.animations.get(&self.state).cloned(); + let (source, timer) = next_source(self.source, &self.animation, self.timer); + self.source = source; + self.timer = timer; } fn move_position(&mut self) { @@ -98,30 +98,6 @@ 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 as u32) { - 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(); diff --git a/src/state.rs b/src/state.rs index a981924..077917b 100644 --- a/src/state.rs +++ b/src/state.rs @@ -29,15 +29,15 @@ impl State { map, spritebatch: SpriteBatch::new(image), camera: Camera::new(context, map_dimensions), - player: Player::new(map_dimensions), + player: Player::new(&tileset, map_dimensions), }) } } impl EventHandler for State { - fn update(&mut self, context: &mut Context) -> GameResult { + fn update(&mut self, _context: &mut Context) -> GameResult { self.map.update(); - self.player.update(context); + self.player.update(); self.camera.give_center(self.player.position); Ok(()) } diff --git a/src/tile.rs b/src/tile.rs index cb2aa83..a367592 100644 --- a/src/tile.rs +++ b/src/tile.rs @@ -3,12 +3,12 @@ use ggez::nalgebra::{Point2, Vector2}; use std::time::Instant; use crate::constants; -use crate::math::convert_angle_to_rad; +use crate::math::{convert_angle_to_rad, next_source}; use crate::tileset::Tileset; pub struct Tile { source: Rect, - animations: Option>, + animation: Option>, timer: Instant, destination: Point2, rotation: f32, @@ -51,7 +51,7 @@ impl Tile { Tile { source, - animations: tileset.get_animations(id), + animation: tileset.get_animation(id), timer: Instant::now(), destination, rotation, @@ -66,15 +66,9 @@ impl Tile { } pub fn update(&mut self) { - if let Some(animations) = &self.animations { - if let Some(mut i) = animations.iter().position(|a| a.1 == self.source) { - if self.timer.elapsed().as_millis() > animations[i].0 as u128 { - i = if i == animations.len() - 1 { 0 } else { i + 1 }; - self.source = animations[i].1; - self.timer = Instant::now() - } - } - } + let (source, timer) = next_source(self.source, &self.animation, self.timer); + self.source = source; + self.timer = timer; } pub fn draw(&self, spritebatch: &mut SpriteBatch) { diff --git a/src/tileset.rs b/src/tileset.rs index 548cde6..eb097e5 100644 --- a/src/tileset.rs +++ b/src/tileset.rs @@ -62,7 +62,7 @@ impl Tileset { *self.tiles.get(&id).unwrap() } - pub fn get_animations(&self, id: usize) -> Option> { + pub fn get_animation(&self, id: usize) -> Option> { if let Some(property) = self.properties.get(&id) { let entitys_properties: HashMap = self .properties @@ -81,7 +81,16 @@ impl Tileset { } } - pub fn get_property(&self, id: usize) -> Option<&Property> { - self.properties.get(&id) + pub fn get_rect_by_entity(&self, entity: &str) -> Rect { + *self + .tiles + .get( + self.properties + .iter() + .find(|(_, p)| p.entity == entity && 0 == p.keyframe) + .unwrap() + .0, + ) + .unwrap() } } -- cgit v1.2.3