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 From 15b4ade5a357a791cf3ffa86cfe78b5f7ae270c9 Mon Sep 17 00:00:00 2001 From: tom barrett Date: Mon, 24 Jun 2019 09:31:24 -0500 Subject: now draws accurately depending on flip --- src/tile.rs | 38 ++++++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 14 deletions(-) (limited to 'src/tile.rs') diff --git a/src/tile.rs b/src/tile.rs index 21b6a79..5667b92 100644 --- a/src/tile.rs +++ b/src/tile.rs @@ -6,32 +6,36 @@ use crate::math::convert_angle_to_rad; use crate::tileset::Tileset; pub struct Tile { - //id: u32, source: Rect, //animation: Option>, - rotate: f32, destination: Point2, + rotation: f32, } 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_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 { + let id = if flip_h | flip_v | flip_d { 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 (source, rotation) = match (flip_d, flip_h, flip_v) { + (true, true, true) => (Tile::flip(tileset.tiles[id]), convert_angle_to_rad(90.0)), + (true, true, false) => (tileset.tiles[id], convert_angle_to_rad(90.0)), + (true, false, true) => (tileset.tiles[id], convert_angle_to_rad(270.0)), + //(true, false, false) => (), + (false, true, true) => (tileset.tiles[id], convert_angle_to_rad(180.0)), + (false, true, false) => (Tile::flip(tileset.tiles[id]), 0.0), + //(false, false, true) => (), + //(false, false, false) => (), + _ => (tileset.tiles[id], 0.0), }; let x = i as f32 % width as f32; @@ -44,20 +48,26 @@ impl Tile { ); Tile { - //id, - source: tileset.tiles[id as usize], + source, //animation: None, - rotate, destination, + rotation, } } + fn flip(rect: Rect) -> Rect { + let mut r = rect; + r.x *= -1.0; + r.x -= rect.w; + r + } + 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) + .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 From 53b4ccdcc6cdac3b02480a97a2ec30439114ff6c Mon Sep 17 00:00:00 2001 From: tom barrett Date: Wed, 26 Jun 2019 05:50:34 -0500 Subject: now can parse tile properties --- src/tile.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'src/tile.rs') diff --git a/src/tile.rs b/src/tile.rs index 5667b92..dc9987c 100644 --- a/src/tile.rs +++ b/src/tile.rs @@ -27,15 +27,15 @@ impl Tile { }; let (source, rotation) = match (flip_d, flip_h, flip_v) { - (true, true, true) => (Tile::flip(tileset.tiles[id]), convert_angle_to_rad(90.0)), - (true, true, false) => (tileset.tiles[id], convert_angle_to_rad(90.0)), - (true, false, true) => (tileset.tiles[id], convert_angle_to_rad(270.0)), + (true, true, true) => (Tile::flip(tileset.get(id)), convert_angle_to_rad(90.0)), + (true, true, false) => (tileset.get(id), convert_angle_to_rad(90.0)), + (true, false, true) => (tileset.get(id), convert_angle_to_rad(270.0)), //(true, false, false) => (), - (false, true, true) => (tileset.tiles[id], convert_angle_to_rad(180.0)), - (false, true, false) => (Tile::flip(tileset.tiles[id]), 0.0), + (false, true, true) => (tileset.get(id), convert_angle_to_rad(180.0)), + (false, true, false) => (Tile::flip(tileset.get(id)), 0.0), //(false, false, true) => (), //(false, false, false) => (), - _ => (tileset.tiles[id], 0.0), + _ => (tileset.get(id), 0.0), }; let x = i as f32 % width as f32; -- cgit v1.2.3 From ee8d055be8326eb1561900bcca6acd9e38071f4a Mon Sep 17 00:00:00 2001 From: tom barrett Date: Thu, 27 Jun 2019 03:31:33 -0500 Subject: tiles can now animate --- src/tile.rs | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) (limited to 'src/tile.rs') diff --git a/src/tile.rs b/src/tile.rs index dc9987c..198be10 100644 --- a/src/tile.rs +++ b/src/tile.rs @@ -1,5 +1,6 @@ use ggez::graphics::{spritebatch::SpriteBatch, DrawParam, Rect}; use ggez::nalgebra::{Point2, Vector2}; +use std::time::Instant; use crate::constants; use crate::math::convert_angle_to_rad; @@ -7,7 +8,8 @@ use crate::tileset::Tileset; pub struct Tile { source: Rect, - //animation: Option>, + animations: Option>, + timer: Instant, destination: Point2, rotation: f32, } @@ -49,7 +51,8 @@ impl Tile { Tile { source, - //animation: None, + animations: tileset.get_animations(id), + timer: Instant::now(), destination, rotation, } @@ -62,6 +65,17 @@ impl Tile { r } + pub fn update(&mut self) { + if let Some(animations) = &self.animations { + let mut i = animations.iter().position(|a| a.1 == self.source).unwrap(); + 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() + } + } + } + pub fn draw(&self, spritebatch: &mut SpriteBatch) { let draw_param = DrawParam::default() .src(self.source) -- cgit v1.2.3