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

update #172

Merged
merged 1 commit into from
Jan 5, 2024
Merged
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
78 changes: 51 additions & 27 deletions src/cairo/hello-dojo.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,22 @@ Inspect the contents of the `dojo-starter` project, and you'll notice the follow

```bash
src
- actions.cairo
- lib.cairo
- models.cairo
- utils.cairo
- systems.cairo
- actions.cairo
- models.cairo
- position.cairo
- moves.cairo
- tests.cairo
- test_world.cairo
Scarb.toml
```

Dojo projects bear a strong resemblance to typical Cairo projects. The primary difference is the inclusion of a special attribute tag used to define your data models. In this context, we'll refer to these models as components.

As we're crafting an ECS, we'll adhere to the specific terminology associated with Entity Component Systems.

Open the `src/models.cairo` file to continue.
Open the `src/models/moves.cairo` file to continue.

```rust,ignore
#[derive(Model, Drop, Serde)]
Expand All @@ -41,40 +45,51 @@ struct Moves {
remaining: u8,
last_direction: Direction
}
...rest of code
```

#[derive(Copy, Drop, Serde, Introspect)]
struct Vec2 {
x: u32,
y: u32
}
Notice the `#[derive(Model, Drop, Serde)]` attributes. For a model to be recognized, we _must_ include `Model`. This signals to the Dojo compiler that this struct should be treated as a model.

Our `Moves` model houses a `player` field. At the same time, we have the `#[key]` attribute, it informs Dojo that this model is indexed by the `player` field. If this is unfamiliar to you, we'll clarify its importance later in the chapter. Essentially, it implies that you can query this model using the `player` field. Our `Moves` model also contains the `remaining` and `last_direction` fields

Open the `src/models/position.cairo` file to continue.

```rust,ignore
#[derive(Model, Copy, Drop, Serde)]
struct Position {
#[key]
player: ContractAddress,
vec: Vec2,
}

#[derive(Copy, Drop, Serde, Introspect)]
struct Vec2 {
x: u32,
y: u32
}
...rest of code
```

Notice the `#[derive(Model, Drop, Serde)]` attributes. For a model to be recognized, we _must_ include `Model`. This signals to the Dojo compiler that this struct should be treated as a model.

Our `Moves` model houses a `player` field. At the same time, we have the `#[key]` attribute, it informs Dojo that this model is indexed by the `player` field. If this is unfamiliar to you, we'll clarify its importance later in the chapter. Essentially, it implies that you can query this model using the `player` field. Our `Moves` model also contains the `remaining` and `last_direction` fields

In a similar vein, we have a `Position` model that have a Vec2 data structure. Vec holds `x` and `y` values. Once again, this model is indexed by the `player` field.

Now, let's examine the `src/actions.cairo` file:
Now, let's examine the `src/systems/actions.cairo` file:

```rust,ignore
// define the interface
#[starknet::interface]
trait IActions<TContractState> {
fn spawn(self: @TContractState);
fn move(self: @TContractState, direction: dojo_starter::models::moves::Direction);
}

// dojo decorator
#[dojo::contract]
mod actions {
use starknet::{ContractAddress, get_caller_address};
use dojo_examples::models::{Position, Moves, Direction, Vec2};
use dojo_examples::utils::next_position;
use super::IActions;

use starknet::{ContractAddress, get_caller_address};
use dojo_starter::models::{position::{Position, Vec2}, moves::{Moves, Direction}};

// declaring custom event struct
#[event]
#[derive(Drop, starknet::Event)]
Expand All @@ -89,6 +104,19 @@ mod actions {
direction: Direction
}

// define functions in your contracts like this:
fn next_position(mut position: Position, direction: 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; },
};
position
}


// impl: implement functions specified in trait
#[external(v0)]
impl ActionsImpl of IActions<ContractState> {
Expand All @@ -107,17 +135,13 @@ mod actions {
let moves = get!(world, player, (Moves));

// Update the world state with the new data.
// 1. Increase the player's remaining moves by 10.
// 2. Move the player's position 10 units in both the x and y direction.
// 1. Set players moves to 10
// 2. Move the player's position 100 units in both the x and y direction.
set!(
world,
(
Moves {
player, remaining: moves.remaining + 10, last_direction: Direction::None
},
Position {
player, vec: Vec2 { x: position.vec.x + 10, y: position.vec.y + 10 }
},
Moves { player, remaining: 100, last_direction: Direction::None },
Position { player, vec: Vec2 { x: 10, y: 10 } },
)
);
}
Expand Down Expand Up @@ -262,11 +286,11 @@ Your 🌎 is now deployed at `0x5010c31f127114c6198df8a5239e2b7a5151e1156fb43791

This establishes the world address for your project.

Let's discuss the `Scarb.toml` file in the project. This file contains environment variables that make running CLI commands in your project a breeze (read more about it [here](./config.md)). Make sure your file specifies the version of Dojo you have installed! In this case version `0.3.14`.
Let's discuss the `Scarb.toml` file in the project. This file contains environment variables that make running CLI commands in your project a breeze (read more about it [here](./config.md)). Make sure your file specifies the version of Dojo you have installed! In this case version `0.4.4`.

```toml
[dependencies]
dojo = { git = "https://github.com/dojoengine/dojo", version = "0.3.14" }
dojo = { git = "https://github.com/dojoengine/dojo", version = "0.4.4" }
```

### Indexing
Expand Down