From cccfb66c7c58bf464252e942ef2b742b41ece19e Mon Sep 17 00:00:00 2001 From: tom barrett Date: Sat, 22 Jun 2019 10:11:18 -0500 Subject: now draw at the tile level --- src/tile.rs | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 src/tile.rs (limited to 'src/tile.rs') diff --git a/src/tile.rs b/src/tile.rs new file mode 100644 index 0000000..21b6a79 --- /dev/null +++ b/src/tile.rs @@ -0,0 +1,65 @@ +use ggez::graphics::{spritebatch::SpriteBatch, DrawParam, Rect}; +use ggez::nalgebra::{Point2, Vector2}; + +use crate::constants; +use crate::math::convert_angle_to_rad; +use crate::tileset::Tileset; + +pub struct Tile { + //id: u32, + source: Rect, + //animation: Option>, + rotate: f32, + destination: Point2, +} + +impl Tile { + pub fn new(text: &str, i: usize, tileset: &Tileset, width: usize, height: usize) -> Tile { + let id = text.parse::().unwrap(); + + //let flip_d = (id & constants::FLIP_DIAGONAL_FLAG) == constants::FLIP_DIAGONAL_FLAG; + let flip_h = (id & constants::FLIP_HORIZONTAL_FLAG) == constants::FLIP_HORIZONTAL_FLAG; + let flip_v = (id & constants::FLIP_VERTICAL_FLAG) == constants::FLIP_VERTICAL_FLAG; + + let id = if flip_h | flip_v { + id & !constants::ALL_FLIP_FLAGS + } else { + id + }; + + let rotate = match (flip_h, flip_v) { + (true, false) => convert_angle_to_rad(90.0), + (true, true) => convert_angle_to_rad(180.0), + (false, true) => convert_angle_to_rad(270.0), + (false, false) => 0.0, + }; + + 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 destination = Point2::new( + (constants::TILE_WIDTH * constants::TILE_SCALE * x) + offset, + (constants::TILE_HEIGHT * constants::TILE_SCALE * y) + offset, + ); + + Tile { + //id, + source: tileset.tiles[id as usize], + //animation: None, + rotate, + destination, + } + } + + pub fn draw(&self, spritebatch: &mut SpriteBatch) { + let draw_param = DrawParam::default() + .src(self.source) + .rotation(self.rotate) + .offset(Point2::new(0.5, 0.5)) + .dest(self.destination) + .scale(Vector2::new(constants::TILE_SCALE, constants::TILE_SCALE)); + + spritebatch.add(draw_param); + } +} -- cgit v1.2.3