diff options
-rw-r--r-- | resources/map.tmx | 44 | ||||
-rw-r--r-- | src/constants.rs | 6 | ||||
-rw-r--r-- | src/map.rs | 40 | ||||
-rw-r--r-- | src/tileset.rs | 4 |
4 files changed, 67 insertions, 27 deletions
diff --git a/resources/map.tmx b/resources/map.tmx index d187791..aa9a7b3 100644 --- a/resources/map.tmx +++ b/resources/map.tmx @@ -27,49 +27,49 @@ 3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3 </data> </layer> - <layer id="3" name="Ground-Angled" width="20" height="20"> + <layer id="2" name="Greenery" width="20" height="20"> <data encoding="csv"> 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, +6,7,6,7,6,7,6,7,0,0,0,0,0,6,7,6,7,6,7,6, +11,12,11,12,11,12,11,12,0,0,0,0,0,11,12,11,12,11,12,11, +16,17,16,17,16,17,16,17,0,0,0,0,0,16,17,16,17,16,17,16, +21,22,21,22,21,22,21,22,0,0,0,0,0,21,22,21,22,21,22,21, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, +0,0,0,0,0,0,0,0,9,10,18,19,0,0,0,0,0,0,0,0, +0,0,0,0,0,10,0,0,0,0,23,24,0,0,0,0,0,9,10,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, +0,8,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, +0,0,0,8,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, +0,0,0,0,0,0,0,0,0,8,8,0,0,0,0,8,0,0,0,0, +0,0,0,0,0,0,0,0,0,8,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, -2684354564,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, -0,0,0,2684354564,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, -0,0,0,0,0,2684354564,0,0,0,0,0,0,0,3221225476,2,2,2,0,0,0, -0,0,0,0,0,0,0,2684354564,0,0,0,0,4,0,0,0,0,2684354564,0,0, -0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2684354564,0, -0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, -0,1610612741,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, -0,0,0,0,1610612741,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, -0,0,0,0,0,0,1610612741,0,0,0,0,0,3221225477,0,0,0,0,0,0,1610612741, -0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 </data> </layer> - <layer id="2" name="Greenery" width="20" height="20"> + <layer id="3" name="Ground-Angled" width="20" height="20"> <data encoding="csv"> 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, -6,7,6,7,6,7,6,7,0,0,0,0,0,6,7,6,7,6,7,6, -11,12,11,12,11,12,11,12,0,0,0,0,0,11,12,11,12,11,12,11, -16,17,16,17,16,17,16,17,0,0,0,0,0,16,17,16,17,16,17,16, -21,22,21,22,21,22,21,22,0,0,0,0,0,21,22,21,22,21,22,21, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, -0,0,0,0,0,0,0,0,9,10,18,19,0,0,0,0,0,0,0,0, -0,0,0,0,0,10,0,0,0,0,23,24,0,0,0,0,0,9,10,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, -0,8,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, -0,0,0,8,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, -0,0,0,0,0,0,0,0,0,8,8,0,0,0,0,8,0,0,0,0, -0,0,0,0,0,0,0,0,0,8,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, +2684354564,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, +0,0,0,2684354564,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, +0,0,0,0,0,2684354564,0,0,0,0,0,0,0,3221225476,2,2,2,0,0,0, +0,0,0,0,0,0,0,2684354564,0,0,0,0,4,0,0,0,0,2684354564,0,0, +0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2684354564,0, +0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, +0,1610612741,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, +0,0,0,0,1610612741,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, +0,0,0,0,0,0,1610612741,0,0,0,0,0,3221225477,0,0,0,0,0,0,1610612741, +0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 diff --git a/src/constants.rs b/src/constants.rs index 1af4324..367e595 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -3,3 +3,9 @@ pub const TILE_HEIGHT: f32 = 16.0; pub const TILE_SCALE: f32 = 3.0; pub const PLAYER_SPEED: f32 = 5.0; + +pub const FLIP_HORIZONTALLY_FLAG: u32 = 0x8000_0000; +pub const FLIP_VERTICALLY_FLAG: u32 = 0x4000_0000; +pub const FLIP_DIAGONALLY_FLAG: u32 = 0x2000_0000; +pub const ALL_FLIP_FLAGS: u32 = + FLIP_DIAGONALLY_FLAG | FLIP_HORIZONTALLY_FLAG | FLIP_VERTICALLY_FLAG; @@ -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 { diff --git a/src/tileset.rs b/src/tileset.rs index 17f4f61..ae65097 100644 --- a/src/tileset.rs +++ b/src/tileset.rs @@ -22,12 +22,12 @@ impl Tileset { let mut tiles = Vec::new(); tiles.push(Rect::zero()); + let w = 1.0 / columns as f32; + let h = 1.0 / rows as f32; for r in 0..rows { for c in 0..columns { let x = c as f32 / columns as f32; let y = r as f32 / rows as f32; - let w = (c as f32 + 1.0) / columns as f32; - let h = (r as f32 + 1.0) / rows as f32; tiles.push(Rect::new(x, y, w, h)); } } |