Skip to content

Commit

Permalink
update spawn-and-move example
Browse files Browse the repository at this point in the history
  • Loading branch information
remybar committed Mar 6, 2024
1 parent ad5cc68 commit ce78cf1
Showing 1 changed file with 12 additions and 14 deletions.
26 changes: 12 additions & 14 deletions examples/spawn-and-move/src/actions.cairo
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use dojo_examples::models::{Position, Vec2};

#[starknet::interface]
trait IActions<TContractState> {
fn spawn(self: @TContractState);
fn move(self: @TContractState, direction: dojo_examples::models::Direction);
#[dojo::interface]
trait IActions {
fn spawn();
fn move(direction: dojo_examples::models::Direction);
}

#[starknet::interface]
trait IActionsComputed<TContractState> {
fn tile_terrain(self: @TContractState, vec: Vec2) -> felt252 ;
fn quadrant(self: @TContractState, pos: Position) -> u8;
#[dojo::interface]
trait IActionsComputed {
fn tile_terrain(vec: Vec2) -> felt252;
fn quadrant(pos: Position) -> u8;
}

#[dojo::contract]
Expand All @@ -36,12 +36,12 @@ mod actions {
#[abi(embed_v0)]
impl ActionsComputedImpl of IActionsComputed<ContractState> {
#[computed]
fn tile_terrain(self: @ContractState, vec: Vec2) -> felt252 {
fn tile_terrain(vec: Vec2) -> felt252 {
'land'
}

#[computed(Position)]
fn quadrant(self: @ContractState, pos: Position) -> u8 {
fn quadrant(pos: Position) -> u8 {
// 10 is zero
if pos.vec.x < 10 {
if pos.vec.y < 10 {
Expand All @@ -64,8 +64,7 @@ mod actions {
#[abi(embed_v0)]
impl ActionsImpl of IActions<ContractState> {
// ContractState is defined by system decorator expansion
fn spawn(self: @ContractState) {
let world = self.world_dispatcher.read();
fn spawn(world: IWorldDispatcher) {
let player = get_caller_address();
let position = get!(world, player, (Position));
let moves = get!(world, player, (Moves));
Expand All @@ -83,8 +82,7 @@ mod actions {
);
}

fn move(self: @ContractState, direction: Direction) {
let world = self.world_dispatcher.read();
fn move(direction: Direction, world: IWorldDispatcher) {
let player = get_caller_address();
let (mut position, mut moves) = get!(world, player, (Position, Moves));
moves.remaining -= 1;
Expand Down

0 comments on commit ce78cf1

Please sign in to comment.