From 83ff2ad49e6d3bf83fd85fcfee68a454372bf22a Mon Sep 17 00:00:00 2001 From: tom barrett Date: Thu, 4 Jul 2019 07:39:51 -0500 Subject: moved animation file, moved player function to enitity, tile now uses animation, move drawing from npc tile player to animation --- src/tile.rs | 66 ++++++++++++++++++++++++++++++------------------------------- 1 file changed, 33 insertions(+), 33 deletions(-) (limited to 'src/tile.rs') 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, - 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); - } } -- cgit v1.2.3