-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2b7fef1
commit 74d3e55
Showing
10 changed files
with
132 additions
and
131 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,6 +1,6 @@ | ||
[package] | ||
cairo-version = "2.4.0" | ||
name = "dojo_examples" | ||
name = "dojo_starter" | ||
version = "0.4.4" | ||
|
||
[cairo] | ||
|
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,4 +1,12 @@ | ||
mod actions; | ||
mod models; | ||
mod utils; | ||
mod systems { | ||
mod actions; | ||
} | ||
|
||
mod models { | ||
mod moves; | ||
mod position; | ||
} | ||
|
||
mod tests { | ||
mod test_world; | ||
} |
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,31 @@ | ||
use starknet::ContractAddress; | ||
|
||
#[derive(Model, Drop, Serde)] | ||
struct Moves { | ||
#[key] | ||
player: ContractAddress, | ||
remaining: u8, | ||
last_direction: Direction | ||
} | ||
|
||
#[derive(Serde, Copy, Drop, Introspect)] | ||
enum Direction { | ||
None, | ||
Left, | ||
Right, | ||
Up, | ||
Down, | ||
} | ||
|
||
impl DirectionIntoFelt252 of Into<Direction, felt252> { | ||
fn into(self: Direction) -> felt252 { | ||
match self { | ||
Direction::None => 0, | ||
Direction::Left => 1, | ||
Direction::Right => 2, | ||
Direction::Up => 3, | ||
Direction::Down => 4, | ||
} | ||
} | ||
} | ||
|
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 |
---|---|---|
@@ -0,0 +1,62 @@ | ||
#[cfg(test)] | ||
mod tests { | ||
use starknet::class_hash::Felt252TryIntoClassHash; | ||
|
||
// import world dispatcher | ||
use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait}; | ||
|
||
// import test utils | ||
use dojo::test_utils::{spawn_test_world, deploy_contract}; | ||
|
||
// import test utils | ||
use dojo_starter::{ | ||
systems::{actions::{actions, IActionsDispatcher, IActionsDispatcherTrait}}, | ||
models::{position::{Position, Vec2, position}, moves::{Moves, Direction, moves}} | ||
}; | ||
|
||
|
||
#[test] | ||
#[available_gas(30000000)] | ||
fn test_move() { | ||
// caller | ||
let caller = starknet::contract_address_const::<0x0>(); | ||
|
||
// models | ||
let mut models = array![position::TEST_CLASS_HASH, moves::TEST_CLASS_HASH]; | ||
|
||
// deploy world with models | ||
let world = spawn_test_world(models); | ||
|
||
// deploy systems contract | ||
let contract_address = world | ||
.deploy_contract('salt', actions::TEST_CLASS_HASH.try_into().unwrap()); | ||
let actions_system = IActionsDispatcher { contract_address }; | ||
|
||
// call spawn() | ||
actions_system.spawn(); | ||
|
||
// call move with direction right | ||
actions_system.move(Direction::Right); | ||
|
||
// Check world state | ||
let moves = get!(world, caller, Moves); | ||
|
||
// casting right direction | ||
let right_dir_felt: felt252 = Direction::Right.into(); | ||
|
||
// check moves | ||
assert(moves.remaining == 99, 'moves is wrong'); | ||
|
||
// check last direction | ||
assert(moves.last_direction.into() == right_dir_felt, 'last direction is wrong'); | ||
|
||
// get new_position | ||
let new_position = get!(world, caller, Position); | ||
|
||
// check new position x | ||
assert(new_position.vec.x == 11, 'position x is wrong'); | ||
|
||
// check new position y | ||
assert(new_position.vec.y == 10, 'position y is wrong'); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.