diff --git a/.gitignore b/.gitignore index 20f7bb4..cf34ca4 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,5 @@ coverage # VSCode .vscode +.DS_Store +**/.DS_Store \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index 2367bba..a1d96ca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,40 @@ and this project adheres to [Conventional Commits](https://www.conventionalcommi ## [Unreleased] +## [0.6.1] - 2025-02-11 + +### Added +- Added `Cell::to_bits` helper API for developer experience returns the `Cell::bits` representation. + +### Updated +- Improved examples `bevy_pathfinding` and `bevy_ecs_tilemap` to use `Cell::to_bits` instead of `to_bits_str`. +- Cargo update + +### Breaking +- `to_bits_str` now returns `&'static str` whereas `to_bits_string` now returns a `String`. + +## [0.6.0] - 2025-02-8 + +### Added +- Introduced feature `pathfinding` as default feature. +- Added A*Pathfinding for `OrthogonalMaze`. +- Added `Start` and `Goal` components for `OrthogonalMaze`. Support for Pathfinding. +- Added extra functions to `CoordsComponent`: + - `new` creates new `CoordsComponent` from `x` and `y` type usize + - `xy` returns `(usize, usize)` of `CoordsComponent`. Where `(x, y)` +- Added maze `CellSize` Resource as type f32. +- Added examples: + - bevy_ecs_tilemap + - bevy_pathfinding + +### Updated +- Implemented `fmt::Display` for `CoordsComponent`. + +## [0.5.2] - 2025-02-7 + +### Updated +- Cargo update + ## [0.5.1] - 2025-01-31 ### Added diff --git a/Cargo.lock b/Cargo.lock index 0e270ac..a22c5c7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1015,7 +1015,7 @@ dependencies = [ [[package]] name = "bevy_knossos" -version = "0.6.0" +version = "0.6.1" dependencies = [ "assert_cmd", "assert_fs", @@ -1646,9 +1646,9 @@ dependencies = [ [[package]] name = "built" -version = "0.7.5" +version = "0.7.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c360505aed52b7ec96a3636c3f039d99103c37d1d9b4f7a8c743d3ea9ffcd03b" +checksum = "73848a43c5d63a1251d17adf6c2bf78aa94830e60a335a95eeea45d6ba9e1e4d" [[package]] name = "bumpalo" @@ -1728,9 +1728,9 @@ checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5" [[package]] name = "cc" -version = "1.2.12" +version = "1.2.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "755717a7de9ec452bf7f3f1a3099085deabd7f2962b861dae91ecd7a365903d2" +checksum = "c7777341816418c02e033934a09f20dc0ccaf65a5201ef8a450ae0105a573fda" dependencies = [ "jobserver", "libc", @@ -1820,9 +1820,9 @@ dependencies = [ [[package]] name = "clap" -version = "4.5.28" +version = "4.5.29" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3e77c3243bd94243c03672cb5154667347c457ca271254724f9f393aee1c05ff" +checksum = "8acebd8ad879283633b343856142139f2da2317c96b05b4dd6181c61e2480184" dependencies = [ "clap_builder", "clap_derive", @@ -1830,9 +1830,9 @@ dependencies = [ [[package]] name = "clap_builder" -version = "4.5.27" +version = "4.5.29" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1b26884eb4b57140e4d2d93652abfa49498b938b3c9179f9fc487b0acc3edad7" +checksum = "f6ba32cbda51c7e1dfd49acc1457ba1a7dec5b64fe360e828acb13ca8dc9c2f9" dependencies = [ "anstream", "anstyle", @@ -2196,9 +2196,9 @@ checksum = "0c87e182de0887fd5361989c677c4e8f5000cd9491d6d563161a8f3a5519fc7f" [[package]] name = "data-encoding" -version = "2.7.0" +version = "2.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0e60eed09d8c01d3cee5b7d30acb059b76614c918fa0f992e0dd6eeb10daad6f" +checksum = "575f75dfd25738df5b91b8e43e14d44bda14637a58fae779fd2b064f8bf3e010" [[package]] name = "deprecate-until" @@ -2674,9 +2674,9 @@ dependencies = [ [[package]] name = "gilrs-core" -version = "0.6.1" +version = "0.6.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0ed2326d21aa97752d41b2c195aee1d99cd84456ff4d5a7f5e6e1cdbd3dcb0b8" +checksum = "b4ed3920aa2e2a5b02fb67182e269b7c988ffbba86e1278bb9f9bbe1815e3ae1" dependencies = [ "core-foundation 0.10.0", "inotify", @@ -3369,9 +3369,9 @@ checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" [[package]] name = "miniz_oxide" -version = "0.8.3" +version = "0.8.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b8402cab7aefae129c6977bb0ff1b8fd9a04eb5b51efc50a70bea51cda0c7924" +checksum = "b3b1c9bd4fe1f0f8b387f6eb9eb3b4a1aa26185e5750efb9140301703f62cd1b" dependencies = [ "adler2", "simd-adler32", @@ -5021,9 +5021,9 @@ dependencies = [ [[package]] name = "toml_edit" -version = "0.22.23" +version = "0.22.24" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "02a8b472d1a3d7c18e2d61a489aee3453fd9031c33e4f55bd533f4a7adca1bee" +checksum = "17b4795ff5edd201c7cd6dca065ae59972ce77d1b80fa0a84d94950ece7d1474" dependencies = [ "indexmap", "serde", @@ -6173,9 +6173,9 @@ dependencies = [ [[package]] name = "winnow" -version = "0.7.1" +version = "0.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "86e376c75f4f43f44db463cf729e0d3acbf954d13e22c51e26e4c264b4ab545f" +checksum = "59690dea168f2198d1a3b0cac23b8063efcd11012f10ae4698f284808c8ef603" dependencies = [ "memchr", ] diff --git a/Cargo.toml b/Cargo.toml index ac43b2f..343b857 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "bevy_knossos" -version = "0.6.0" +version = "0.6.1" authors = [ "Julia Naomi ", "unrenamed ", diff --git a/README.md b/README.md index 9e928dd..802b80a 100644 --- a/README.md +++ b/README.md @@ -72,9 +72,9 @@ For information on knossos CLI usage, check the original repository [#Cli](https ## Features -| name | description | default| -| ------------- | ----------- | ------ | -| `pathfinding` | Enables bevy to pathfind in the Maze (banner image is a demo) | false | +| name | description | default| dependencies | +| ------------- | ----------- | ------ | ------------ | +| `pathfinding` | Enables bevy to pathfind in the Maze (banner image is a demo) | true | `pathfinding = "4.14"` | ### Examples: diff --git a/examples/bevy_ecs_tilemap.rs b/examples/bevy_ecs_tilemap.rs index aaf3886..6d19405 100644 --- a/examples/bevy_ecs_tilemap.rs +++ b/examples/bevy_ecs_tilemap.rs @@ -59,7 +59,7 @@ fn setup(mut commands: Commands, maze: Res, asset_server: let maze_cache: HashMap<(usize, usize), &maze::Cell> = maze.iter().collect(); for ((x, y), cell) in maze.iter() { - let index = cell_to_index(cell.to_bits_str().as_str(), (x, y), &maze_cache); + let index = cell_to_index(cell.to_bits(), (x, y), &maze_cache); let tile_pos = TilePos { x: x as u32, @@ -132,19 +132,19 @@ fn check_corner( } fn cell_to_index( - cell: &str, + cell: u8, position: (usize, usize), cache: &HashMap<(usize, usize), &maze::Cell>, ) -> TileTextureIndex { TileTextureIndex( // wesn match cell { - "0000" => 358, - "0001" => 286, - "0010" => 312, - "0011" => 309, - "0100" => 313, - "0101" => { + 0b0000 => 358, + 0b0001 => 286, + 0b0010 => 312, + 0b0011 => 309, + 0b0100 => 313, + 0b0101 => { let has_ne_corner = check_corner(position, IVec2::new(1, -1), cache); if has_ne_corner { 307 @@ -152,7 +152,7 @@ fn cell_to_index( 314 } } - "0110" => { + 0b0110 => { let has_se_corner = check_corner(position, IVec2::new(1, 1), cache); if has_se_corner { 280 @@ -160,7 +160,7 @@ fn cell_to_index( 287 } } - "0111" => { + 0b0111 => { let has_ne_corner = check_corner(position, IVec2::new(1, -1), cache); let has_se_corner = check_corner(position, IVec2::new(1, 1), cache); match (has_ne_corner, has_se_corner) { @@ -170,8 +170,8 @@ fn cell_to_index( (false, false) => 338, } } - "1000" => 285, - "1001" => { + 0b1000 => 285, + 0b1001 => { let has_nw_corner = check_corner(position, IVec2::new(-1, -1), cache); if has_nw_corner { 308 @@ -179,7 +179,7 @@ fn cell_to_index( 315 } } - "1010" => { + 0b1010 => { let has_sw_corner = check_corner(position, IVec2::new(-1, 1), cache); if has_sw_corner { 281 @@ -187,7 +187,7 @@ fn cell_to_index( 288 } } - "1011" => { + 0b1011 => { let has_nw_corner = check_corner(position, IVec2::new(-1, -1), cache); let has_sw_corner = check_corner(position, IVec2::new(-1, 1), cache); match (has_nw_corner, has_sw_corner) { @@ -197,8 +197,8 @@ fn cell_to_index( (false, false) => 339, } } - "1100" => 282, - "1101" => { + 0b1100 => 282, + 0b1101 => { let has_ne_corner = check_corner(position, IVec2::new(1, -1), cache); let has_nw_corner = check_corner(position, IVec2::new(-1, -1), cache); match (has_ne_corner, has_nw_corner) { @@ -208,7 +208,7 @@ fn cell_to_index( (false, false) => 366, } } - "1110" => { + 0b1110 => { let has_se_corner = check_corner(position, IVec2::new(1, 1), cache); let has_sw_corner = check_corner(position, IVec2::new(-1, 1), cache); match (has_se_corner, has_sw_corner) { @@ -218,7 +218,7 @@ fn cell_to_index( (false, false) => 365, } } - "1111" => { + 0b1111 => { let has_ne_corner = check_corner(position, IVec2::new(1, -1), cache); let has_nw_corner = check_corner(position, IVec2::new(-1, -1), cache); let has_se_corner = check_corner(position, IVec2::new(1, 1), cache); diff --git a/examples/bevy_pathfinding.rs b/examples/bevy_pathfinding.rs index ce0bece..16f8046 100644 --- a/examples/bevy_pathfinding.rs +++ b/examples/bevy_pathfinding.rs @@ -62,7 +62,7 @@ fn setup(mut commands: Commands, maze: Res, asset_server: let maze_cache: HashMap<(usize, usize), &maze::Cell> = maze.iter().collect(); for ((x, y), cell) in maze.iter() { - let index = cell_to_index(cell.to_bits_str().as_str(), (x, y), &maze_cache); + let index = cell_to_index(cell.to_bits(), (x, y), &maze_cache); let tile_pos = TilePos { x: x as u32, @@ -150,19 +150,19 @@ fn check_corner( } fn cell_to_index( - cell: &str, + cell: u8, position: (usize, usize), cache: &HashMap<(usize, usize), &maze::Cell>, ) -> TileTextureIndex { TileTextureIndex( // wesn match cell { - "0000" => 358, - "0001" => 286, - "0010" => 312, - "0011" => 309, - "0100" => 313, - "0101" => { + 0b0000 => 358, + 0b0001 => 286, + 0b0010 => 312, + 0b0011 => 309, + 0b0100 => 313, + 0b0101 => { let has_ne_corner = check_corner(position, IVec2::new(1, -1), cache); if has_ne_corner { 307 @@ -170,7 +170,7 @@ fn cell_to_index( 314 } } - "0110" => { + 0b0110 => { let has_se_corner = check_corner(position, IVec2::new(1, 1), cache); if has_se_corner { 280 @@ -178,7 +178,7 @@ fn cell_to_index( 287 } } - "0111" => { + 0b0111 => { let has_ne_corner = check_corner(position, IVec2::new(1, -1), cache); let has_se_corner = check_corner(position, IVec2::new(1, 1), cache); match (has_ne_corner, has_se_corner) { @@ -188,8 +188,8 @@ fn cell_to_index( (false, false) => 338, } } - "1000" => 285, - "1001" => { + 0b1000 => 285, + 0b1001 => { let has_nw_corner = check_corner(position, IVec2::new(-1, -1), cache); if has_nw_corner { 308 @@ -197,7 +197,7 @@ fn cell_to_index( 315 } } - "1010" => { + 0b1010 => { let has_sw_corner = check_corner(position, IVec2::new(-1, 1), cache); if has_sw_corner { 281 @@ -205,7 +205,7 @@ fn cell_to_index( 288 } } - "1011" => { + 0b1011 => { let has_nw_corner = check_corner(position, IVec2::new(-1, -1), cache); let has_sw_corner = check_corner(position, IVec2::new(-1, 1), cache); match (has_nw_corner, has_sw_corner) { @@ -215,8 +215,8 @@ fn cell_to_index( (false, false) => 339, } } - "1100" => 282, - "1101" => { + 0b1100 => 282, + 0b1101 => { let has_ne_corner = check_corner(position, IVec2::new(1, -1), cache); let has_nw_corner = check_corner(position, IVec2::new(-1, -1), cache); match (has_ne_corner, has_nw_corner) { @@ -226,7 +226,7 @@ fn cell_to_index( (false, false) => 366, } } - "1110" => { + 0b1110 => { let has_se_corner = check_corner(position, IVec2::new(1, 1), cache); let has_sw_corner = check_corner(position, IVec2::new(-1, 1), cache); match (has_se_corner, has_sw_corner) { @@ -236,7 +236,7 @@ fn cell_to_index( (false, false) => 365, } } - "1111" => { + 0b1111 => { let has_ne_corner = check_corner(position, IVec2::new(1, -1), cache); let has_nw_corner = check_corner(position, IVec2::new(-1, -1), cache); let has_se_corner = check_corner(position, IVec2::new(1, 1), cache); diff --git a/src/maze/grid/cell.rs b/src/maze/grid/cell.rs index 8084391..576a2fb 100644 --- a/src/maze/grid/cell.rs +++ b/src/maze/grid/cell.rs @@ -20,10 +20,23 @@ bitflags! { } impl Cell { - /// Returns bit string representation - pub fn to_bits_str(&self) -> String { + /// Returns bits &str representation. + /// > use `to_bits_string` for string value + pub fn to_bits_str(&self) -> &'static str { + let bits = format!("{:0>4b}", self.bits()); + bits.leak() + } + + /// Returns bits string representation. + pub fn to_bits_string(&self) -> String { format!("{:0>4b}", self.bits()) } + + /// Returns bits u8 representation. + /// > analogous to `bits`. + pub const fn to_bits(&self) -> u8 { + self.bits() + } } impl fmt::Display for Cell { @@ -71,47 +84,131 @@ mod tests { use super::Cell; #[test] - fn empty_is_0000() { + fn empty_is_0000_str() { let zero = Cell::empty(); assert_eq!(zero.to_bits_str(), "0000") } #[test] - fn all_is_1111() { + fn all_is_1111_str() { let zero = Cell::all(); assert_eq!(zero.to_bits_str(), "1111") } #[test] - fn north_is_0001() { + fn north_is_0001_str() { let zero = Cell::NORTH; assert_eq!(zero.to_bits_str(), "0001") } #[test] - fn south_is_0010() { + fn south_is_0010_str() { let zero = Cell::SOUTH; assert_eq!(zero.to_bits_str(), "0010") } #[test] - fn east_is_0100() { + fn east_is_0100_str() { let zero = Cell::EAST; assert_eq!(zero.to_bits_str(), "0100") } #[test] - fn west_is_1000() { + fn west_is_1000_str() { let zero = Cell::WEST; assert_eq!(zero.to_bits_str(), "1000") } + #[test] + fn empty_is_0000_bits() { + let zero = Cell::empty(); + + assert_eq!(zero.to_bits(), 0b0000) + } + + #[test] + fn all_is_1111_bits() { + let zero = Cell::all(); + + assert_eq!(zero.to_bits(), 0b1111) + } + + #[test] + fn north_is_0001_bits() { + let zero = Cell::NORTH; + + assert_eq!(zero.to_bits(), 0b0001) + } + + #[test] + fn south_is_0010_bits() { + let zero = Cell::SOUTH; + + assert_eq!(zero.to_bits(), 0b0010) + } + + #[test] + fn east_is_0100_bits() { + let zero = Cell::EAST; + + assert_eq!(zero.to_bits(), 0b0100) + } + + #[test] + fn west_is_1000_bits() { + let zero = Cell::WEST; + + assert_eq!(zero.to_bits(), 0b1000) + } + + #[test] + fn empty_is_0000() { + let zero = Cell::empty(); + + assert_eq!(zero.to_bits_string(), "0000") + } + + #[test] + fn all_is_1111() { + let zero = Cell::all(); + + assert_eq!(zero.to_bits_string(), "1111") + } + + #[test] + fn north_is_0001() { + let zero = Cell::NORTH; + + assert_eq!(zero.to_bits_string(), "0001") + } + + #[test] + fn south_is_0010() { + let zero = Cell::SOUTH; + + assert_eq!(zero.to_bits_string(), "0010") + } + + #[test] + fn east_is_0100() { + let zero = Cell::EAST; + + assert_eq!(zero.to_bits_string(), "0100") + } + + #[test] + fn west_is_1000() { + let zero = Cell::WEST; + + assert_eq!(zero.to_bits_string(), "1000") + } + #[test] fn empty_cell_name() { let cell = Cell::empty();