Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve direction type + add check on 'can_move' #98

Merged
merged 1 commit into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
sozo-test:
runs-on: ubuntu-latest
env:
DOJO_VERSION: v1.0.8
DOJO_VERSION: v1.0.9
steps:
- uses: actions/checkout@v3
- run: curl -L https://install.dojoengine.org | bash
Expand Down
27 changes: 16 additions & 11 deletions manifest_dev.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"world": {
"class_hash": "0x79d9ce84b97bcc2a631996c3100d57966fc2f5b061fb1ec4dfd0040976bcac6",
"address": "0xe2ea9b5dd9804d13903edf712998943b7d5d606c139dd0f13eeb8f5b84da8d",
"class_hash": "0x45575a88cc5cef1e444c77ce60b7b4c9e73a01cbbe20926d5a4c72a94011410",
"address": "0x525177c8afe8680d7ad1da30ca183e482cfcd6404c1e09d83fd3fa2994fd4b8",
"seed": "dojo_starter",
"name": "Dojo starter",
"entrypoints": [
Expand Down Expand Up @@ -92,6 +92,10 @@
{
"name": "metadata_uri",
"type": "core::byte_array::ByteArray"
},
{
"name": "metadata_hash",
"type": "core::felt252"
}
]
},
Expand Down Expand Up @@ -1002,6 +1006,11 @@
"name": "uri",
"type": "core::byte_array::ByteArray",
"kind": "data"
},
{
"name": "hash",
"type": "core::felt252",
"kind": "data"
}
]
},
Expand Down Expand Up @@ -1243,8 +1252,8 @@
},
"contracts": [
{
"address": "0x58b83bea84766c5725c12e239c1ec9e1fde679ddf709b772fe7a8fdfd3cda27",
"class_hash": "0x3f87eccb39c9dc7bb70d372350f4360ae1ff9da43bd72c89a96e336402a569b",
"address": "0x73a20bf1f5ebe9caf16bc08fb07434618300a55a764395a22f4fdde8237a547",
"class_hash": "0x6cbb5ab40ad7b48dff70edeacc4902a9eeae63a9e36456f9f908aedb904fbd2",
"abi": [
{
"type": "impl",
Expand Down Expand Up @@ -1305,10 +1314,6 @@
"type": "enum",
"name": "dojo_starter::models::Direction",
"variants": [
{
"name": "None",
"type": "()"
},
{
"name": "Left",
"type": "()"
Expand Down Expand Up @@ -1480,13 +1485,13 @@
"models": [
{
"members": [],
"class_hash": "0x7deb48ccf95cc441a0489cfefdae54aeb6f8ec462ba13ff25e23f080e66cc2f",
"class_hash": "0x3f52c6cf17db224f4e7f88629adf7e907ff110baa0616c7e8ef17c5aecf1b1b",
"tag": "dojo_starter-DirectionsAvailable",
"selector": "0x77844f1facb51e60e546a9832d56c6bd04fa23be4fd5b57290caae5e9a3c1e4"
},
{
"members": [],
"class_hash": "0x70edf8f3be0b118e78f856f3ea9ebb652cba3684abaf7f299bfa6f93bf907c9",
"class_hash": "0x5f79e21312b142fc289d3bd1e0f4c65ac3c4e532252c6fb1ad48b718d892dc",
"tag": "dojo_starter-Moves",
"selector": "0x2a29373f1af8348bd366a990eb3a342ef2cbe5e85160539eaca3441a673f468"
},
Expand All @@ -1500,7 +1505,7 @@
"events": [
{
"members": [],
"class_hash": "0x5be0a05a5df3bd3b4fc17f8b1feb395cb463ced20ea41d4fbb9b86a4d7efc66",
"class_hash": "0x3dca5564f84050b20e66a2e5e7619201502d241b223ea73cfc05f29e6e20ea1",
"tag": "dojo_starter-Moved",
"selector": "0x504403e5c02b6442527721fc464d9ea5fc8f1ee600ab5ccd5c0845d36fd45f1"
}
Expand Down
14 changes: 10 additions & 4 deletions src/models.cairo
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use starknet::ContractAddress;
use starknet::{ContractAddress};

#[derive(Copy, Drop, Serde, Debug)]
#[dojo::model]
pub struct Moves {
#[key]
pub player: ContractAddress,
pub remaining: u8,
pub last_direction: Direction,
pub last_direction: Option<Direction>,
pub can_move: bool,
}

Expand All @@ -29,7 +29,6 @@ pub struct Position {

#[derive(Serde, Copy, Drop, Introspect, PartialEq, Debug)]
pub enum Direction {
None,
Left,
Right,
Up,
Expand All @@ -47,7 +46,6 @@ pub struct Vec2 {
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,
Expand All @@ -56,6 +54,14 @@ impl DirectionIntoFelt252 of Into<Direction, felt252> {
}
}

impl OptionDirectionIntoFelt252 of Into<Option<Direction>, felt252> {
fn into(self: Option<Direction>) -> felt252 {
match self {
Option::None => 0,
Option::Some(d) => d.into(),
}
}
}

#[generate_trait]
impl Vec2Impl of Vec2Trait {
Expand Down
25 changes: 16 additions & 9 deletions src/systems/actions.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub mod actions {

// 2. Set the player's remaining moves to 100.
let moves = Moves {
player, remaining: 100, last_direction: Direction::None(()), can_move: true
player, remaining: 100, last_direction: Option::None, can_move: true
};

// Write the new moves to the world.
Expand All @@ -66,15 +66,20 @@ pub mod actions {
// Retrieve the player's current position and moves data from the world.
let position: Position = world.read_model(player);
let mut moves: Moves = world.read_model(player);
// if player hasn't spawn, read returns model default values. This leads to sub overflow afterwards.
// Plus it's generally considered as a good pratice to fast-return on matching conditions.
if !moves.can_move {
return;
}

// Deduct one from the player's remaining moves.
moves.remaining -= 1;

// Update the last direction the player moved in.
moves.last_direction = direction;
moves.last_direction = Option::Some(direction);

// Calculate the player's next position based on the provided direction.
let next = next_position(position, direction);
let next = next_position(position, moves.last_direction);

// Write the new position to the world.
world.write_model(@next);
Expand All @@ -98,13 +103,15 @@ pub mod actions {
}

// Define function like this:
fn next_position(mut position: Position, direction: Direction) -> Position {
fn next_position(mut position: Position, direction: Option<Direction>) -> Position {
match direction {
Direction::None => { return position; },
Direction::Left => { position.vec.x -= 1; },
Direction::Right => { position.vec.x += 1; },
Direction::Up => { position.vec.y -= 1; },
Direction::Down => { position.vec.y += 1; },
Option::None => { return position; },
Option::Some(d) => match d {
Direction::Left => { position.vec.x -= 1; },
Direction::Right => { position.vec.x += 1; },
Direction::Up => { position.vec.y -= 1; },
Direction::Down => { position.vec.y += 1; },
}
};
position
}
6 changes: 3 additions & 3 deletions src/tests/test_world.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,16 @@ use dojo::model::{ModelStorage, ModelValueStorage, ModelStorageTest};
initial_position.vec.x == 10 && initial_position.vec.y == 10, 'wrong initial position'
);

actions_system.move(Direction::Right(()));
actions_system.move(Direction::Right(()).into());

let moves: Moves = world.read_model(caller);
let right_dir_felt: felt252 = Direction::Right(()).into();

assert(moves.remaining == initial_moves.remaining - 1, 'moves is wrong');
assert(moves.last_direction.into() == right_dir_felt, 'last direction is wrong');
assert(moves.last_direction.unwrap().into() == right_dir_felt, 'last direction is wrong');

let new_position: Position = world.read_model(caller);
assert(new_position.vec.x == initial_position.vec.x + 1, 'position x is wrong');
assert(new_position.vec.y == initial_position.vec.y, 'position y is wrong');
}
}
}
Loading