diff --git a/.vscode/settings.json b/.vscode/settings.json index c4414f5..2c8c984 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,5 +1,6 @@ { // "cairo1.languageServerPath": "~/.dojo/bin/dojo-language-server", "cairo1.enableLanguageServer": true, - "cairo1.enableScarb": true + "cairo1.enableScarb": true, + "cairo1.languageServerPath": "/Users/xolo/.dojo/bin/dojo-language-server" } \ No newline at end of file diff --git a/Scarb.lock b/Scarb.lock index 88a2125..be4f9c1 100644 --- a/Scarb.lock +++ b/Scarb.lock @@ -3,19 +3,19 @@ version = 1 [[package]] name = "dojo" -version = "0.3.5" -source = "git+https://github.com/dojoengine/dojo#008b9e55da44c14e839ec8e1f62f0c2fef568f77" +version = "0.3.11" +source = "git+https://github.com/dojoengine/dojo?tag=v0.3.11#1e651b5d4d3b79b14a7d8aa29a92062fcb9e6659" dependencies = [ "dojo_plugin", ] [[package]] -name = "dojo_examples" +name = "dojo_plugin" +version = "0.3.11" + +[[package]] +name = "realms_adventurers" version = "0.3.5" dependencies = [ "dojo", ] - -[[package]] -name = "dojo_plugin" -version = "0.3.6" diff --git a/Scarb.toml b/Scarb.toml index 236b934..a422925 100644 --- a/Scarb.toml +++ b/Scarb.toml @@ -1,5 +1,5 @@ [package] -cairo-version = "2.2.0" +cairo-version = "2.3.1" name = "realms_adventurers" version = "0.3.5" @@ -7,7 +7,7 @@ version = "0.3.5" sierra-replace-ids = true [dependencies] -dojo = { git = "https://github.com/dojoengine/dojo", version = "0.3.5" } +dojo = { git = "https://github.com/dojoengine/dojo", tag = "v0.3.11" } [[target.dojo]] diff --git a/src/lib.cairo b/src/lib.cairo index d99ad44..224a04b 100644 --- a/src/lib.cairo +++ b/src/lib.cairo @@ -1,2 +1,2 @@ mod models; - +mod systems; diff --git a/src/models/dna.cairo b/src/models/dna.cairo index 4ca178f..874a1bf 100644 --- a/src/models/dna.cairo +++ b/src/models/dna.cairo @@ -1,39 +1,79 @@ // the minimum DNA of an Adventurer +use starknet::ContractAddress; -#[derive(Copy, Drop, Serde, Introspect)] -struct Vec2 { - x: u32, - y: u32 + +const GAMEKEY: u128 = 0; +// record the auto-increment id +#[derive(Model, Copy, Drop, Serde)] +struct GameData { + #[key] + game: u128, + adventurer_id: u128, } -// Health is a component that can be attached to an entity -#[derive(Model, Copy, Drop, Serde, SerdeLen)] -struct Health { + +// ID is a unique identifier for an entity +#[derive(Model, Copy, Drop, Serde)] +struct AdventurerId { #[key] - entity_id: u64, - health: u32, + player: ContractAddress, + adventurer_id: u128, +// +// think about the situation where player has a few adventurers +// model can't store array yet, so hard code +// second: u64, +// third: u64, +// } -// This can be inherited from the World, but leaving here for testing #[derive(Model, Copy, Drop, Serde)] -struct Position { +struct PlayerAddress { #[key] - entity_id: u64, - vec: Vec2, + adventurer_id: u128, + player: ContractAddress, } -// ID is a unique identifier for an entity +// Name is a component that can be attached to an entity #[derive(Model, Copy, Drop, Serde)] -struct AdventurerId { +struct Name { #[key] - entity_id: u64, - id: u64, + adventurer_id: u128, + name: felt252, } + // ID is a unique identifier for an entity #[derive(Model, Copy, Drop, Serde)] struct Energy { #[key] - entity_id: u64, + adventurer_id: u128, value: u64, } + +// Health is a component that can be attached to an entity +#[derive(Model, Copy, Drop, Serde, SerdeLen)] +struct Health { + #[key] + adventurer_id: u128, + health: u32, +} + + +// This can be inherited from the World, but leaving here for testing +#[derive(Model, Copy, Drop, Serde)] +struct Position { + #[key] + adventurer_id: u128, + // which map is this adventurer in + location: u128, + // where is this adventurer in the map + vec: Vec3, +} + +#[derive(Copy, Drop, Serde, Introspect)] +struct Vec3 { + x: u32, + y: u32, + // in case of 3d + z: u32 +} diff --git a/src/systems.cairo b/src/systems.cairo new file mode 100644 index 0000000..f5384ed --- /dev/null +++ b/src/systems.cairo @@ -0,0 +1 @@ +mod spawn; \ No newline at end of file diff --git a/src/systems/spawn.cairo b/src/systems/spawn.cairo index f5400f2..d889c6b 100644 --- a/src/systems/spawn.cairo +++ b/src/systems/spawn.cairo @@ -1,9 +1,91 @@ +use realms_adventurers::models::dna::{Vec3}; +use starknet::ContractAddress; + #[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; + fn spawn(self: @TContractState, location: u128, vec: Vec3, name: felt252); + fn move(self: @TContractState, location: u128, adventurer_id: u128); + fn get_adventurer(self: @TContractState, player: ContractAddress) -> u128; +} + +mod error { + const AdventurerAlreadyExists: felt252 = 'adventurer already exists'; } #[dojo::contract] -mod spawn {} +mod spawn { + use super::{IDNA, error}; + use realms_adventurers::models::dna::{ + GameData, GAMEKEY, AdventurerId, PlayerAddress, Name, Energy, Health, Position, Vec3 + }; + use starknet::{ContractAddress, get_caller_address}; + + #[event] + #[derive(Drop, starknet::Event)] + enum Event { + Spawn: Spawn + } + + #[derive(Drop, starknet::Event)] + struct Spawn { + #[key] + player: ContractAddress, + #[key] + adventurer_id: u128 + } + + // #[generate_trait] + // impl InternalImpl of InternalTrait {} + + #[external(v0)] + fn test(self: @ContractState) { + // let world = self.world_dispatcher.read(); + // let input: felt252 = get_caller_address().into(); + // world.delete_entity('AdventurerId', array![input].span()); + + DNAImpl::spawn(self, 1, Vec3 { x: 0, y: 0, z: 0 }, '1'); + } + + #[external(v0)] + impl DNAImpl of IDNA { + // + fn spawn(self: @ContractState, location: u128, vec: Vec3, name: felt252) { + let world = self.world_dispatcher.read(); + let player = get_caller_address(); + + assert( + get!(world, player, (AdventurerId)).adventurer_id == 0, + error::AdventurerAlreadyExists + ); + + let adventurer_id = get!(world, GAMEKEY, (GameData)).adventurer_id + 1; + + set!( + world, + ( + GameData { game: GAMEKEY, adventurer_id: adventurer_id }, + AdventurerId { player: player, adventurer_id: adventurer_id }, + PlayerAddress { adventurer_id: adventurer_id, player: player }, + Name { adventurer_id: adventurer_id, name: name }, + Energy { adventurer_id: adventurer_id, value: 100 }, + Health { adventurer_id: adventurer_id, health: 100 }, + Position { + adventurer_id: adventurer_id, + location: location, + vec: Vec3 { x: vec.x, y: vec.y, z: vec.z } + }, + ) + ); + + // emit events + emit!(world, Spawn { player: player, adventurer_id: adventurer_id }); + } + + fn move(self: @ContractState, location: u128, adventurer_id: u128) {} + + fn get_adventurer(self: @ContractState, player: ContractAddress) -> u128 { + 0 + } + } +} +