-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from dojoengine/hex
Hex
- Loading branch information
Showing
16 changed files
with
632 additions
and
70 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,13 @@ | ||
[workspace] | ||
members = [ | ||
"crates", | ||
"examples/*", | ||
] | ||
members = ["crates", "examples/*"] | ||
|
||
[workspace.package] | ||
version = "0.0.0" | ||
version = "0.1.0" | ||
description = "Community-maintained libraries for Cairo" | ||
homepage = "https://github.com/dojoengine/origami" | ||
authors = [ | ||
"[email protected]", | ||
] | ||
authors = ["[email protected]"] | ||
|
||
[workspace.dependencies] | ||
cubit = { git = "https://github.com/influenceth/cubit", rev = "b459053" } | ||
dojo = { git = "https://github.com/dojoengine/dojo", tag = "v0.3.10" } | ||
dojo = { git = "https://github.com/dojoengine/dojo", tag = "v0.3.12" } | ||
origami = { path = "crates" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,3 +20,10 @@ mod random { | |
mod security { | ||
mod commitment; | ||
} | ||
|
||
mod map { | ||
mod hex { | ||
mod hex; | ||
mod types; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
use origami::map::hex::{types::{HexTile, Direction, DirectionIntoFelt252}}; | ||
|
||
trait IHexTile { | ||
fn new(col: u32, row: u32) -> HexTile; | ||
fn neighbor(self: HexTile, direction: Direction) -> HexTile; | ||
fn neighbors(self: HexTile) -> Array<HexTile>; | ||
fn is_neighbor(self: HexTile, other: HexTile) -> bool; | ||
} | ||
|
||
impl ImplHexTile of IHexTile { | ||
fn new(col: u32, row: u32) -> HexTile { | ||
HexTile { col, row } | ||
} | ||
|
||
fn neighbor(self: HexTile, direction: Direction) -> HexTile { | ||
match direction { | ||
Direction::East(()) => HexTile { col: self.col + 1, row: self.row }, | ||
Direction::NorthEast(()) => HexTile { col: self.col + 1, row: self.row - 1 }, | ||
Direction::NorthWest(()) => HexTile { col: self.col, row: self.row - 1 }, | ||
Direction::West(()) => HexTile { col: self.col - 1, row: self.row }, | ||
Direction::SouthWest(()) => HexTile { col: self.col, row: self.row + 1 }, | ||
Direction::SouthEast(()) => HexTile { col: self.col + 1, row: self.row + 1 }, | ||
} | ||
} | ||
|
||
fn neighbors(self: HexTile) -> Array<HexTile> { | ||
array![ | ||
self.neighbor(Direction::East(())), | ||
self.neighbor(Direction::NorthEast(())), | ||
self.neighbor(Direction::NorthWest(())), | ||
self.neighbor(Direction::West(())), | ||
self.neighbor(Direction::SouthWest(())), | ||
self.neighbor(Direction::SouthEast(())) | ||
] | ||
} | ||
|
||
fn is_neighbor(self: HexTile, other: HexTile) -> bool { | ||
let mut neighbors = self.neighbors(); | ||
|
||
loop { | ||
if (neighbors.len() == 0) { | ||
break false; | ||
} | ||
|
||
let curent_neighbor = neighbors.pop_front().unwrap(); | ||
|
||
if (curent_neighbor.col == other.col) { | ||
if (curent_neighbor.row == other.row) { | ||
break true; | ||
} | ||
}; | ||
} | ||
} | ||
} | ||
|
||
|
||
// tests ----------------------------------------------------------------------- // | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::{IHexTile, ImplHexTile, Direction, HexTile}; | ||
#[test] | ||
#[available_gas(500000)] | ||
fn test_row_col() { | ||
let mut hex_tile = ImplHexTile::new(5, 5); | ||
|
||
assert(hex_tile.col == 5, 'col should be 5'); | ||
assert(hex_tile.row == 5, 'row should be 5'); | ||
} | ||
|
||
|
||
#[test] | ||
#[available_gas(500000)] | ||
fn test_hex_tile_neighbors() { | ||
let mut hex_tile = ImplHexTile::new(5, 5); | ||
|
||
let east_neighbor = hex_tile.neighbor(Direction::East(())); | ||
|
||
assert(east_neighbor.col == 6, 'col should be 7'); | ||
assert(east_neighbor.row == 5, 'row should be 5'); | ||
|
||
let north_east_neighbor = hex_tile.neighbor(Direction::NorthEast(())); | ||
|
||
assert(north_east_neighbor.col == 6, 'col should be 6'); | ||
assert(north_east_neighbor.row == 4, 'row should be 4'); | ||
|
||
let north_west_neighbor = hex_tile.neighbor(Direction::NorthWest(())); | ||
|
||
assert(north_west_neighbor.col == 5, 'col should be 5'); | ||
assert(north_west_neighbor.row == 4, 'row should be 4'); | ||
|
||
let west_neighbor = hex_tile.neighbor(Direction::West(())); | ||
|
||
assert(west_neighbor.col == 4, 'col should be 3'); | ||
assert(west_neighbor.row == 5, 'row should be 5'); | ||
|
||
let south_west_neighbor = hex_tile.neighbor(Direction::SouthWest(())); | ||
|
||
assert(south_west_neighbor.col == 5, 'col should be 4'); | ||
assert(south_west_neighbor.row == 6, 'row should be 6'); | ||
|
||
let south_east_neighbor = hex_tile.neighbor(Direction::SouthEast(())); | ||
|
||
assert(south_east_neighbor.col == 6, 'col should be 5'); | ||
assert(south_east_neighbor.row == 6, 'row should be 6'); | ||
} | ||
|
||
#[test] | ||
#[available_gas(501230000)] | ||
fn test_is_neighbor() { | ||
let mut hex_tile = ImplHexTile::new(5, 5); | ||
|
||
assert(hex_tile.is_neighbor(HexTile { col: hex_tile.col + 1, row: hex_tile.row }), 'east'); | ||
|
||
assert( | ||
hex_tile.is_neighbor(HexTile { col: hex_tile.col, row: hex_tile.row + 1 }), 'north east' | ||
); | ||
|
||
assert( | ||
hex_tile.is_neighbor(HexTile { col: hex_tile.col, row: hex_tile.row - 1 }), 'north west' | ||
); | ||
|
||
assert(hex_tile.is_neighbor(HexTile { col: hex_tile.col - 1, row: hex_tile.row }), 'west'); | ||
|
||
assert( | ||
hex_tile.is_neighbor(HexTile { col: hex_tile.col, row: hex_tile.row - 1 }), 'south west' | ||
); | ||
|
||
assert( | ||
hex_tile.is_neighbor(HexTile { col: hex_tile.col + 1, row: hex_tile.row - 1 }), | ||
'south east' | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#[derive(Drop, Copy, Serde)] | ||
struct HexTile { | ||
col: u32, | ||
row: u32, | ||
} | ||
|
||
#[derive(Drop, Copy, Serde)] | ||
enum Direction { | ||
East: (), | ||
NorthEast: (), | ||
NorthWest: (), | ||
West: (), | ||
SouthWest: (), | ||
SouthEast: (), | ||
} | ||
|
||
impl DirectionIntoFelt252 of Into<Direction, felt252> { | ||
fn into(self: Direction) -> felt252 { | ||
match self { | ||
Direction::East => 0, | ||
Direction::NorthEast => 1, | ||
Direction::NorthWest => 2, | ||
Direction::West => 3, | ||
Direction::SouthWest => 4, | ||
Direction::SouthEast => 5, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[package] | ||
name = "hex_map" | ||
version = "0.0.0" | ||
description = "Example Hex Map with noise" | ||
homepage = "https://github.com/dojoengine/origami/tree/examples/hex_map" | ||
|
||
[dependencies] | ||
cubit.workspace = true | ||
dojo.workspace = true | ||
origami.workspace = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
## Hex map with noise | ||
|
||
This example uses the hex grid origami component along with cubits simplex noise. | ||
|
||
You can replicate the noise in a client to mirror the onchain noise with the offchain noise to provide users with a rich map to explore. |
Oops, something went wrong.