summaryrefslogtreecommitdiff
path: root/src/map.rs
diff options
context:
space:
mode:
authortom barrett <spalf0@gmail.com>2019-06-22 06:36:40 -0500
committertom barrett <spalf0@gmail.com>2019-06-22 06:36:40 -0500
commit324fce116cc8daacf64ba49ecc8a2b833aaf4a40 (patch)
treea1f9fe8b33311d1207408d158b33a346e29d84b2 /src/map.rs
parentc182662e06b3134e51db938371a0b313d2948d7c (diff)
reordered layers, now added rotation of tiles, fixed tileset mapping bug
Diffstat (limited to 'src/map.rs')
-rw-r--r--src/map.rs40
1 files changed, 37 insertions, 3 deletions
diff --git a/src/map.rs b/src/map.rs
index 57b39f5..bb5f694 100644
--- a/src/map.rs
+++ b/src/map.rs
@@ -1,6 +1,7 @@
use ggez::filesystem::File;
use ggez::graphics::{spritebatch::SpriteBatch, DrawParam};
use ggez::nalgebra::{Point2, Vector2};
+use std::f32::consts::PI;
use xml::reader::XmlEvent::Characters;
use crate::constants;
@@ -42,11 +43,18 @@ impl Map {
for layer in self.layers.iter() {
for x in 0..self.width {
for y in 0..self.height {
+ let tile_id = layer.data[x + (y * self.height)];
+ let (tile_id, rotate) = self.decode(tile_id as u32);
+
+ let offset = (constants::TILE_WIDTH / 2.0) * constants::TILE_SCALE;
+
let draw_param = DrawParam::default()
- .src(tileset.tiles[layer.data[x + (y * self.height)]])
+ .src(tileset.tiles[tile_id as usize])
+ .rotation(rotate)
+ .offset(Point2::new(0.5, 0.5))
.dest(Point2::new(
- constants::TILE_WIDTH * constants::TILE_SCALE * x as f32,
- constants::TILE_HEIGHT * constants::TILE_SCALE * y as f32,
+ (constants::TILE_WIDTH * constants::TILE_SCALE * x as f32) + offset,
+ (constants::TILE_HEIGHT * constants::TILE_SCALE * y as f32) + offset,
))
.scale(Vector2::new(constants::TILE_SCALE, constants::TILE_SCALE));
@@ -62,6 +70,32 @@ impl Map {
(constants::TILE_HEIGHT * constants::TILE_SCALE) * self.height as f32,
)
}
+
+ fn decode(&self, tile_id: u32) -> (u32, f32) {
+ //let flip_d = (tile_id & constants::FLIP_DIAGONALLY_FLAG) == constants::FLIP_DIAGONALLY_FLAG;
+ let flip_h =
+ (tile_id & constants::FLIP_HORIZONTALLY_FLAG) == constants::FLIP_HORIZONTALLY_FLAG;
+ let flip_v = (tile_id & constants::FLIP_VERTICALLY_FLAG) == constants::FLIP_VERTICALLY_FLAG;
+
+ let new_tile_id = if flip_h | flip_v {
+ tile_id & !constants::ALL_FLIP_FLAGS
+ } else {
+ tile_id
+ };
+
+ let rotate = match (flip_h, flip_v) {
+ (true, false) => self.convert_angle_to_rad(90.0),
+ (true, true) => self.convert_angle_to_rad(180.0),
+ (false, true) => self.convert_angle_to_rad(270.0),
+ (false, false) => 0.0,
+ };
+
+ (new_tile_id, rotate)
+ }
+
+ fn convert_angle_to_rad(&self, angle: f32) -> f32 {
+ angle * (PI / 180.0)
+ }
}
pub struct Layer {