From 159d76dcff98b2b2e38d88201b1ffed5516914ed Mon Sep 17 00:00:00 2001 From: democritus Date: Wed, 8 Nov 2023 14:33:05 +0100 Subject: [PATCH] init --- .vscode/settings.json | 4 +- README.md | 48 ++++--------- Scarb.toml | 2 +- src/actions.cairo | 151 ---------------------------------------- src/lib.cairo | 2 - src/models.cairo | 80 +-------------------- src/models/dna.cairo | 39 +++++++++++ src/systems/spawn.cairo | 9 +++ src/utils.cairo | 23 ------ 9 files changed, 64 insertions(+), 294 deletions(-) delete mode 100644 src/actions.cairo create mode 100644 src/models/dna.cairo create mode 100644 src/systems/spawn.cairo delete mode 100644 src/utils.cairo diff --git a/.vscode/settings.json b/.vscode/settings.json index 47c48a3..c4414f5 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,5 +1,5 @@ { - "cairo1.languageServerPath": "$HOME/.dojo/bin/dojo-language-server", + // "cairo1.languageServerPath": "~/.dojo/bin/dojo-language-server", "cairo1.enableLanguageServer": true, - "cairo1.enableScarb": false + "cairo1.enableScarb": true } \ No newline at end of file diff --git a/README.md b/README.md index fba13f5..e63e0d1 100644 --- a/README.md +++ b/README.md @@ -1,44 +1,20 @@ - - - Dojo logo - +## Realms World Adventurers - - - - - - +> WIP: This is heavy work in progress -[![discord](https://img.shields.io/badge/join-dojo-green?logo=discord&logoColor=white)](https://discord.gg/PwDa2mKhR4) -[![Telegram Chat][tg-badge]][tg-url] +Milestone: Deploy into Realms Testnet. -[tg-badge]: https://img.shields.io/endpoint?color=neon&logo=telegram&label=chat&style=flat-square&url=https%3A%2F%2Ftg.sumanjay.workers.dev%2Fdojoengine -[tg-url]: https://t.me/dojoengine +## Table of Contents -# Dojo Starter: Official Guide +### Adventurers -The official Dojo Starter guide, the quickest and most streamlined way to get your Dojo Autonomous World up and running. This guide will assist you with the initial setup, from cloning the repository to deploying your world. +Adventurers are the base layer Character in the world. They are the simplest possible Character with the idea they will be extended by other modules via new components. -Read the full tutorial [here](https://book.dojoengine.org/cairo/hello-dojo.html) +### Adventurers DNA ---- +Adventurers have a minimum of these: -## Contribution - -This starter project is a constant work in progress and contributions are greatly appreciated! - -1. **Report a Bug** - - - If you think you have encountered a bug, and we should know about it, feel free to report it [here](https://github.com/dojoengine/dojo-starter/issues) and we will take care of it. - -2. **Request a Feature** - - - You can also request for a feature [here](https://github.com/dojoengine/dojo-starter/issues), and if it's viable, it will be picked for development. - -3. **Create a Pull Request** - - It can't get better then this, your pull request will be appreciated by the community. - -For any other questions, feel free to reach out to us [here](https://dojoengine.org/contact). - -Happy coding! +- Health: The is the base health of the Adventurer. This is the amount of damage they can take before they die. +- Energy: This is the base energy of the Adventurer. This is the amount of energy they have to perform actions. +- AdventurerId: This is a unique human readable identifier for the Adventurer. +- Position: This is the position of the Adventurer in the world. diff --git a/Scarb.toml b/Scarb.toml index 66c54ca..236b934 100644 --- a/Scarb.toml +++ b/Scarb.toml @@ -1,6 +1,6 @@ [package] cairo-version = "2.2.0" -name = "dojo_examples" +name = "realms_adventurers" version = "0.3.5" [cairo] diff --git a/src/actions.cairo b/src/actions.cairo deleted file mode 100644 index 2695618..0000000 --- a/src/actions.cairo +++ /dev/null @@ -1,151 +0,0 @@ -use dojo_examples::models::{Direction}; - -// define the interface -#[starknet::interface] -trait IActions { - fn spawn(self: @TContractState); - fn move(self: @TContractState, direction: 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; - - // declaring custom event struct - #[event] - #[derive(Drop, starknet::Event)] - enum Event { - Moved: Moved, - } - - // declaring custom event struct - #[derive(Drop, starknet::Event)] - struct Moved { - player: ContractAddress, - direction: Direction - } - - // impl: implement functions specified in trait - #[external(v0)] - impl ActionsImpl of IActions { - // ContractState is defined by system decorator expansion - fn spawn(self: @ContractState) { - // Access the world dispatcher for reading. - let world = self.world_dispatcher.read(); - - // Get the address of the current caller, possibly the player's address. - let player = get_caller_address(); - - // Retrieve the player's current position from the world. - let position = get!(world, player, (Position)); - - // Retrieve the player's move data, e.g., how many moves they have left. - let moves = get!(world, player, (Moves)); - - // Update the world state with the new data. - // 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: 100, last_direction: Direction::None(()) }, - Position { player, vec: Vec2 { x: 10, y: 10 } }, - ) - ); - } - - // Implementation of the move function for the ContractState struct. - fn move(self: @ContractState, direction: Direction) { - // Access the world dispatcher for reading. - let world = self.world_dispatcher.read(); - - // Get the address of the current caller, possibly the player's address. - let player = get_caller_address(); - - // Retrieve the player's current position and moves data from the world. - let (mut position, mut moves) = get!(world, player, (Position, Moves)); - - // Deduct one from the player's remaining moves. - moves.remaining -= 1; - - // Update the last direction the player moved in. - moves.last_direction = direction; - - // Calculate the player's next position based on the provided direction. - let next = next_position(position, direction); - - // Update the world state with the new moves data and position. - set!(world, (moves, next)); - - // Emit an event to the world to notify about the player's move. - emit!(world, Moved { player, direction }); - } - } -} - -#[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 models - use dojo_examples::models::{position, moves}; - use dojo_examples::models::{Position, Moves, Direction, Vec2}; - - // import actions - use super::{actions, IActionsDispatcher, IActionsDispatcherTrait}; - - #[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'); - } -} diff --git a/src/lib.cairo b/src/lib.cairo index 9f927ca..d99ad44 100644 --- a/src/lib.cairo +++ b/src/lib.cairo @@ -1,4 +1,2 @@ -mod actions; mod models; -mod utils; diff --git a/src/models.cairo b/src/models.cairo index cc00446..a312eb5 100644 --- a/src/models.cairo +++ b/src/models.cairo @@ -1,79 +1 @@ -use starknet::ContractAddress; - -#[derive(Serde, Copy, Drop, Introspect)] -enum Direction { - None: (), - Left: (), - Right: (), - Up: (), - Down: (), -} - -impl DirectionIntoFelt252 of Into { - fn into(self: Direction) -> felt252 { - match self { - Direction::None(()) => 0, - Direction::Left(()) => 1, - Direction::Right(()) => 2, - Direction::Up(()) => 3, - Direction::Down(()) => 4, - } - } -} - -#[derive(Model, Drop, Serde)] -struct Moves { - #[key] - player: ContractAddress, - remaining: u8, - last_direction: Direction -} - -#[derive(Copy, Drop, Serde, Introspect)] -struct Vec2 { - x: u32, - y: u32 -} - -#[derive(Model, Copy, Drop, Serde)] -struct Position { - #[key] - player: ContractAddress, - vec: Vec2, -} - -trait Vec2Trait { - fn is_zero(self: Vec2) -> bool; - fn is_equal(self: Vec2, b: Vec2) -> bool; -} - -impl Vec2Impl of Vec2Trait { - fn is_zero(self: Vec2) -> bool { - if self.x - self.y == 0 { - return true; - } - false - } - - fn is_equal(self: Vec2, b: Vec2) -> bool { - self.x == b.x && self.y == b.y - } -} - -#[cfg(test)] -mod tests { - use super::{Position, Vec2, Vec2Trait}; - - #[test] - #[available_gas(100000)] - fn test_vec_is_zero() { - assert(Vec2Trait::is_zero(Vec2 { x: 0, y: 0 }), 'not zero'); - } - - #[test] - #[available_gas(100000)] - fn test_vec_is_equal() { - let position = Vec2 { x: 420, y: 0 }; - assert(position.is_equal(Vec2 { x: 420, y: 0 }), 'not equal'); - } -} +mod dna; diff --git a/src/models/dna.cairo b/src/models/dna.cairo new file mode 100644 index 0000000..4ca178f --- /dev/null +++ b/src/models/dna.cairo @@ -0,0 +1,39 @@ +// the minimum DNA of an Adventurer + +#[derive(Copy, Drop, Serde, Introspect)] +struct Vec2 { + x: u32, + y: u32 +} + +// Health is a component that can be attached to an entity +#[derive(Model, Copy, Drop, Serde, SerdeLen)] +struct Health { + #[key] + entity_id: u64, + health: u32, +} + +// This can be inherited from the World, but leaving here for testing +#[derive(Model, Copy, Drop, Serde)] +struct Position { + #[key] + entity_id: u64, + vec: Vec2, +} + +// ID is a unique identifier for an entity +#[derive(Model, Copy, Drop, Serde)] +struct AdventurerId { + #[key] + entity_id: u64, + id: u64, +} + +// ID is a unique identifier for an entity +#[derive(Model, Copy, Drop, Serde)] +struct Energy { + #[key] + entity_id: u64, + value: u64, +} diff --git a/src/systems/spawn.cairo b/src/systems/spawn.cairo new file mode 100644 index 0000000..f5400f2 --- /dev/null +++ b/src/systems/spawn.cairo @@ -0,0 +1,9 @@ +#[starknet::interface] +trait IDNA { + fn spawn(self: @TContractState, location: u64); + fn move(self: @TContractState, adventurer_id: u64); + fn get_adventurer(self: @TContractState, adventurer_id: u64) -> u64; +} + +#[dojo::contract] +mod spawn {} diff --git a/src/utils.cairo b/src/utils.cairo deleted file mode 100644 index ca5c571..0000000 --- a/src/utils.cairo +++ /dev/null @@ -1,23 +0,0 @@ -use dojo_examples::models::{Position, Direction}; - -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 -}