diff options
author | LawnCable <git@lawncable.net> | 2022-04-15 08:37:05 +0200 |
---|---|---|
committer | LawnCable <git@lawncable.net> | 2022-04-15 08:37:05 +0200 |
commit | a595ed8d9fbe6ff4a7a6f13c279b5412aeb41ab4 (patch) | |
tree | 843069077a21fe5598a1a52f6bf4bab4183d10a2 /src/lowlevel/layer_tile.rs | |
parent | d775eb1e9c6be67e5c07cabde79145b4ed3136e1 (diff) | |
download | fast-tiled.rs-a595ed8d9fbe6ff4a7a6f13c279b5412aeb41ab4.tar.gz fast-tiled.rs-a595ed8d9fbe6ff4a7a6f13c279b5412aeb41ab4.tar.bz2 fast-tiled.rs-a595ed8d9fbe6ff4a7a6f13c279b5412aeb41ab4.tar.xz fast-tiled.rs-a595ed8d9fbe6ff4a7a6f13c279b5412aeb41ab4.zip |
layer tiles, draft for object parsing and more
Diffstat (limited to 'src/lowlevel/layer_tile.rs')
-rw-r--r-- | src/lowlevel/layer_tile.rs | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/src/lowlevel/layer_tile.rs b/src/lowlevel/layer_tile.rs new file mode 100644 index 0000000..bca5daf --- /dev/null +++ b/src/lowlevel/layer_tile.rs @@ -0,0 +1,73 @@ +// Bits on the far end of the 32-bit global tile ID are used for tile flags +const FLIPPED_HORIZONTALLY_FLAG: u32 = 0x80000000; +const FLIPPED_VERTICALLY_FLAG: u32 = 0x40000000; +const FLIPPED_DIAGONALLY_FLAG: u32 = 0x20000000; + +#[derive(Debug, Default, PartialEq)] +pub struct TileFlip { + // whether tile is flipped diagonally (should be the first operation) + pub diagonally: bool, + // whether tile is flipped horizontally (should be the second operation) + pub horizontally: bool, + // whether tile is flipped vertically (should be the third operation) + pub vertically: bool, +} + +impl TileFlip { + fn new(diagonally: bool, horizontally: bool, vertically: bool) -> Self { + Self { + diagonally, + horizontally, + vertically, + } + } +} + +#[derive(Debug)] +pub struct LayerTile(u32); + +impl LayerTile { + /// Global Tile Id + pub fn gid(&self) -> u32 { + // Clear the flags + self.0 & !(FLIPPED_HORIZONTALLY_FLAG | FLIPPED_VERTICALLY_FLAG | FLIPPED_DIAGONALLY_FLAG) + } + + pub fn get_flip(&self) -> TileFlip { + // Read out the flags + TileFlip { + diagonally: (self.0 & FLIPPED_DIAGONALLY_FLAG) > 0, + horizontally: (self.0 & FLIPPED_HORIZONTALLY_FLAG) > 0, + vertically: (self.0 & FLIPPED_VERTICALLY_FLAG) > 0, + } + } +} + +#[cfg(test)] +mod test_layer_tile { + use crate::lowlevel::{layer_tile::*}; + + #[test] + fn test_get_flip() { + assert_eq!(LayerTile(13).get_flip(), TileFlip::default()); + assert_eq!( + LayerTile(3451 | FLIPPED_DIAGONALLY_FLAG).get_flip(), + TileFlip::new(true, false, false) + ); + } + + #[test] + fn test_gid() { + assert_eq!(LayerTile(5).gid(), 5); + assert_eq!( + LayerTile( + 52 | (FLIPPED_HORIZONTALLY_FLAG + | FLIPPED_VERTICALLY_FLAG + | FLIPPED_DIAGONALLY_FLAG) + ) + .gid(), + 52 + ); + assert_eq!(LayerTile(798 | (FLIPPED_DIAGONALLY_FLAG)).gid(), 798); + } +} |