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

Base adventure #1

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -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"
}
14 changes: 7 additions & 7 deletions Scarb.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
4 changes: 2 additions & 2 deletions Scarb.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
[package]
cairo-version = "2.2.0"
cairo-version = "2.3.1"
name = "realms_adventurers"
version = "0.3.5"

[cairo]
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]]

Expand Down
2 changes: 1 addition & 1 deletion src/lib.cairo
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
mod models;

mod systems;
76 changes: 58 additions & 18 deletions src/models/dna.cairo
Original file line number Diff line number Diff line change
@@ -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
}
1 change: 1 addition & 0 deletions src/systems.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mod spawn;
90 changes: 86 additions & 4 deletions src/systems/spawn.cairo
Original file line number Diff line number Diff line change
@@ -1,9 +1,91 @@
use realms_adventurers::models::dna::{Vec3};
use starknet::ContractAddress;

#[starknet::interface]
trait IDNA<TContractState> {
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<ContractState> {}

#[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<ContractState> {
//
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
}
}
}