diff options
author | tom barrett <spalf0@gmail.com> | 2019-07-04 07:39:51 -0500 |
---|---|---|
committer | tom barrett <spalf0@gmail.com> | 2019-07-04 07:39:51 -0500 |
commit | 83ff2ad49e6d3bf83fd85fcfee68a454372bf22a (patch) | |
tree | 029ab8bd9d73652f1fabd9e34e970f16f93116b5 /src/tile.rs | |
parent | c9ce92e1830505269b631ba1fdd864a173bacf48 (diff) |
moved animation file, moved player function to enitity, tile now uses animation, move drawing from npc tile player to animation
Diffstat (limited to 'src/tile.rs')
-rw-r--r-- | src/tile.rs | 66 |
1 files changed, 33 insertions, 33 deletions
diff --git a/src/tile.rs b/src/tile.rs index 1f3c2f7..144e764 100644 --- a/src/tile.rs +++ b/src/tile.rs @@ -1,19 +1,36 @@ -use ggez::graphics::{spritebatch::SpriteBatch, DrawParam, Rect}; -use ggez::nalgebra::{Point2, Vector2}; -use std::time::Instant; +use ggez::graphics::spritebatch::SpriteBatch; +use ggez::nalgebra::Point2; +use crate::animations::{Animation, Frame}; use crate::constants; -use crate::math::{convert_angle_to_rad, flip, next_source}; +use crate::entity::Operable; +use crate::math::{convert_angle_to_rad, flip}; use crate::tileset::Tileset; -#[derive(Clone, Debug)] +#[derive(Debug, Clone)] pub struct Tile { pub id: usize, - source: Rect, - animation: Vec<(usize, Rect)>, - timer: Instant, + pub animation: Animation, pub destination: Point2<f32>, - rotation: f32, +} + +impl Operable for Tile { + fn update(&mut self) { + self.animation.update(); + } + + fn draw(&self, spritebatch: &mut SpriteBatch) { + self.animation.draw(spritebatch, self.destination); + /* + DrawParam::default() + .src(self.animation.current.source) + .dest(self.destination) + .offset(Point2::new(0.5, 0.5)) + //.rotation(self.rotation) + .scale(Vector2::new(constants::TILE_SCALE, constants::TILE_SCALE)), + ); + */ + } } impl Tile { @@ -44,37 +61,20 @@ impl Tile { let x = i as f32 % width as f32; let y = (i as f32 / height as f32).floor(); - let offset = (constants::TILE_WIDTH / 2.0) * constants::TILE_SCALE; + //let offset = (constants::TILE_WIDTH / 2.0) * constants::TILE_SCALE; let destination = Point2::new( - (constants::TILE_WIDTH * constants::TILE_SCALE * x) + offset, - (constants::TILE_HEIGHT * constants::TILE_SCALE * y) + offset, + constants::TILE_WIDTH * constants::TILE_SCALE * x, //+ offset, + constants::TILE_HEIGHT * constants::TILE_SCALE * y, //+ offset, ); + let mut animation = Animation::new(Frame::new(source, None, rotation)); + animation.give_frames(tileset.get_frames(id)); + Tile { id, - source, - animation: tileset.get_animation(id), - timer: Instant::now(), + animation, destination, - rotation, } } - - pub fn update(&mut self) { - let (source, timer) = next_source(self.source, &self.animation, self.timer); - self.source = source; - self.timer = timer; - } - - pub fn draw(&self, spritebatch: &mut SpriteBatch) { - let draw_param = DrawParam::default() - .src(self.source) - .dest(self.destination) - .offset(Point2::new(0.5, 0.5)) - .rotation(self.rotation) - .scale(Vector2::new(constants::TILE_SCALE, constants::TILE_SCALE)); - - spritebatch.add(draw_param); - } } |