Skip to content

Commit

Permalink
working with layout recursive
Browse files Browse the repository at this point in the history
  • Loading branch information
chudkowsky committed Aug 22, 2024
1 parent c1e2a58 commit ee0aa6d
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 42 deletions.
2 changes: 2 additions & 0 deletions dojo_bin/Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ edition = "2024_07"

[dev-dependencies]
cairo_test = "2.7.0"
[cairo]
enable-gas = false
31 changes: 13 additions & 18 deletions dojo_bin/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
mod systems {
mod models;
pub mod systems {
pub mod actions;
}

mod models;
use systems::actions::actions::WorldState;
use systems::actions::actions::ActionsImpl;
use starknet::contract_address_const;
use starknet::ContractAddress;
use models::Direction;
use systems::actions::actions::ActionsImpl;
use systems::actions::actions::WorldState;

#[derive(Drop)]
enum TransactionType {
Spawn: ContractAddress,
Move: (ContractAddress, models::Direction)
}

fn main() -> WorldState {
let mut world = WorldState { positions: Default::default(), moves: Default::default(), };
let player = contract_address_const::<0>();
let mut world = WorldState { positions: Default::default(), moves: Default::default(), };

let txns = array![
TransactionType::Spawn(player),
Expand All @@ -24,21 +25,15 @@ fn main() -> WorldState {
];
for txn in txns {
match txn {
TransactionType::Spawn(player) => { ActionsImpl::spawn(ref world, player); },
TransactionType::Spawn(player) => { ActionsImpl::spawn(ref world, player) },
TransactionType::Move((
player, direction
)) => { ActionsImpl::move(ref world, player, direction); },
)) => { ActionsImpl::move(ref world, player, direction) },
}
};
let position = world.positions.get(player.into());
let moves = world.moves.get(player.into());
if position.is_null() {
panic!("Position is null");
}
println!("{:?}", position);
if moves.is_null() {
panic!("Moves is null");
}
println!("{:?}", moves);
world
}
#[inline(never)]
fn identity<T>(t: T) -> T {
t
}
9 changes: 4 additions & 5 deletions dojo_bin/src/models.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use starknet::ContractAddress;
#[derive(Copy, Drop, Serde,Debug)]
#[derive(Copy, Drop, Serde, Debug)]
pub struct Moves {
pub player: ContractAddress,
pub remaining: u8,
Expand All @@ -13,15 +13,14 @@ pub struct DirectionsAvailable {
pub directions: Array<Direction>,
}

#[derive(Copy, Drop, Serde,Debug)]
#[derive(Copy, Drop, Serde, Debug)]
pub struct Position {
pub player: ContractAddress,
pub vec: Vec2,
}



#[derive(Serde, Copy, Drop,PartialEq,Debug)]
#[derive(Serde, Copy, Drop, PartialEq, Debug)]
pub enum Direction {
None,
Left,
Expand All @@ -31,7 +30,7 @@ pub enum Direction {
}


#[derive(Copy, Drop, Serde,Debug)]
#[derive(Copy, Drop, Serde, Debug)]
pub struct Vec2 {
pub x: u32,
pub y: u32
Expand Down
71 changes: 52 additions & 19 deletions dojo_bin/src/systems/actions.cairo
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
pub mod actions {
use starknet::contract_address_const;
use starknet::ContractAddress;
use core::dict::Felt252Dict;
use binary_dojo_example::models::Position;
use binary_dojo_example::models::Moves;
use binary_dojo_example::models::Direction;
use binary_dojo_example::models::Vec2;
use starknet::ContractAddress;
use core::nullable::NullableTrait;

use core::nullable::{match_nullable, FromNullableResult, Nullable, NullableTrait};
#[derive(Destruct)]
pub struct WorldState {
pub positions: Felt252Dict<Nullable<Position>>,
Expand All @@ -19,32 +19,61 @@ pub mod actions {
}
pub impl ActionsImpl of IActions {
fn spawn(ref world: WorldState, player: ContractAddress) {
let position = Position { player: player, vec: Vec2 { x: 10, y: 10, }, };
world.positions.insert(player.into(), NullableTrait::new(position));
let moves = Moves {
player: player, remaining: 100, last_direction: Direction::None, can_move: true,
};
world.moves.insert(player.into(), NullableTrait::new(moves.into()));
let box_position = NullableTrait::new(
identity(Position { player: player, vec: Vec2 { x: 10, y: 10, }, })
);

world.positions.insert(player.into(), box_position);
let box_moves = NullableTrait::new(
identity(
Moves {
player: player,
remaining: 100,
last_direction: Direction::None,
can_move: true,
}
)
);
world.moves.insert(player.into(), box_moves);
}
fn move(ref world: WorldState, player: ContractAddress, direction: Direction) {
let position = world.positions.get(player.into());
let position = match match_nullable(position) {
FromNullableResult::Null => { return; },
FromNullableResult::NotNull(value) => value.unbox(),
};
let moves = world.moves.get(player.into());
let moves = match match_nullable(moves) {
FromNullableResult::Null => { return; },
FromNullableResult::NotNull(value) => value.unbox(),
};
if moves.remaining == 0 {
let box_moves = NullableTrait::new(
identity(
Moves {
player: player,
remaining: 0,
last_direction: direction,
can_move: false,
}
)
);
world.moves.insert(player.into(), box_moves);
return;
} else {
let box_position = NullableTrait::new(identity(position));
world.positions.insert(player.into(), box_position);
let moves = Moves {
player: player, remaining: 0, last_direction: direction, can_move: false,
player: player,
remaining: moves.remaining - 1,
last_direction: direction,
can_move: true,
};
world.moves.insert(player.into(), NullableTrait::new(moves.into()));
return;
let box_moves = NullableTrait::new(identity(moves));
world.moves.insert(player.into(), box_moves);
}
let next_position = next_position(position.deref(), direction);
world.positions.insert(player.into(), NullableTrait::new(next_position));
let moves = Moves {
player: player, remaining: moves.remaining - 1, last_direction: direction, can_move: true,
};
world.moves.insert(player.into(), NullableTrait::new(moves.into()));
}
}

fn next_position(mut position: Position, direction: Direction) -> Position {
match direction {
Direction::None => { return position; },
Expand All @@ -55,5 +84,9 @@ pub mod actions {
};
position
}
#[inline(never)]
fn identity<T>(t: T) -> T {
t
}
}

4 changes: 4 additions & 0 deletions rust-toolchain
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[toolchain]
channel = "1.76.0"
components = ["rustfmt", "clippy"]
profile = "minimal"

0 comments on commit ee0aa6d

Please sign in to comment.