Skip to content

Commit

Permalink
Add Map C&C
Browse files Browse the repository at this point in the history
  • Loading branch information
dpinones committed Dec 24, 2023
1 parent 5679d26 commit f157be4
Show file tree
Hide file tree
Showing 19 changed files with 240 additions and 296 deletions.
14 changes: 14 additions & 0 deletions Scarb.lock
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ name = "alexandria_sorting"
version = "0.1.0"
source = "git+https://github.com/keep-starknet-strange/alexandria.git?rev=ecc881e2aee668332441bdfa32336e3404cf8eb1#ecc881e2aee668332441bdfa32336e3404cf8eb1"

[[package]]
name = "cc_starknet"
version = "0.1.1"
source = "git+https://github.com/CheDAOLabs/cryptsandcaverns.git?tag=v0.1.1-rc0#0ad6a6443106e176ddc1cfd4fa0ff3e0da3b82e1"
dependencies = [
"openzeppelin",
]

[[package]]
name = "dojo"
version = "0.4.1"
Expand All @@ -52,11 +60,17 @@ name = "dojo_plugin"
version = "0.3.11"
source = "git+https://github.com/dojoengine/dojo?tag=v0.3.11#1e651b5d4d3b79b14a7d8aa29a92062fcb9e6659"

[[package]]
name = "openzeppelin"
version = "0.8.0-beta.0"
source = "git+https://github.com/OpenZeppelin/cairo-contracts.git?tag=v0.8.0-beta.0#4c95981a7178e43730f152c85a92336dc6a22d62"

[[package]]
name = "starkane"
version = "0.4.0"
dependencies = [
"alexandria_data_structures",
"alexandria_sorting",
"cc_starknet",
"dojo",
]
1 change: 1 addition & 0 deletions Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ sierra-replace-ids = true
dojo = { git = "https://github.com/dojoengine/dojo", version = "0.4.0" }
alexandria_sorting = { git = "https://github.com/keep-starknet-strange/alexandria.git", rev = "ecc881e2aee668332441bdfa32336e3404cf8eb1"}
alexandria_data_structures = { git = "https://github.com/keep-starknet-strange/alexandria.git", rev = "ecc881e2aee668332441bdfa32336e3404cf8eb1"}
cc_starknet = { git = "https://github.com/CheDAOLabs/cryptsandcaverns.git", tag = "v0.1.1-rc0" }

[[target.dojo]]

Expand Down
6 changes: 3 additions & 3 deletions src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ mod store;
mod models {
mod entities {
mod character;
mod map;
mod map_cc;
mod skill;
}
mod data {
Expand All @@ -20,7 +20,7 @@ mod systems {
mod action_system;
mod character_system;
mod match_system;
mod map_system;
mod map_cc_system;
mod move_system;
mod turn_system;
mod ranking_system;
Expand All @@ -36,7 +36,7 @@ mod utils {
mod tests {
mod setup;
mod test_character_system;
// mod test_map_system;
mod test_map_cc_system;
mod test_match_system;
mod test_move_system;
mod test_skill_system;
Expand Down
133 changes: 0 additions & 133 deletions src/models/entities/map.cairo

This file was deleted.

50 changes: 50 additions & 0 deletions src/models/entities/map_cc.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use cc_starknet::utils::pack::{Pack, PackTrait};
use starknet::ContractAddress;

#[derive(Model, Copy, Drop, Serde)]
struct MapCC {
#[key]
id: u32,
token_id: u64,
size: u8,
obstacles_1: felt252,
obstacles_2: felt252,
obstacles_3: felt252,
owner: felt252,
width: u64,
height: u64,
}

trait MapCCTrait {
fn is_inside(self: MapCC, position: (u64, u64)) -> bool;
fn is_walkable(self: MapCC, position: (u64, u64)) -> bool;
fn get_value(self: MapCC, position: (u64, u64)) -> ByteArray;
}

impl MapCCImpl of MapCCTrait {
fn is_inside(self: MapCC, position: (u64, u64)) -> bool {
let (x, y) = position;
x >= 0 && x < self.width && y >= 0 && y < self.height
}

fn is_walkable(self: MapCC, position: (u64, u64)) -> bool {
let mut pack = Pack {
first: self.obstacles_1, second: self.obstacles_2, third: self.obstacles_3
};
let (x, y) = position;
!PackTrait::get_bit(ref pack, (y * 25 + x).into())
}

fn get_value(self: MapCC, position: (u64, u64)) -> ByteArray {
let mut pack = Pack {
first: self.obstacles_1, second: self.obstacles_2, third: self.obstacles_3
};
let (x, y) = position;
if PackTrait::get_bit(ref pack, (y * 25 + x).into()) {
"X"
} else {
"_"
}
}
}

2 changes: 1 addition & 1 deletion src/models/states/match_state.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait};
use starkane::models::entities::map::{Map, MapTrait};
use starkane::models::entities::map_cc::{MapCC, MapCCTrait};

#[derive(Model, Copy, Drop, Serde)]
struct MatchState {
Expand Down
23 changes: 6 additions & 17 deletions src/store.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use starkane::models::states::match_state::{
use starkane::models::states::character_state::{CharacterState, ActionState};
use starkane::models::entities::character::Character;
use starkane::models::entities::skill::Skill;
use starkane::models::entities::map::{Map, Tile};
use starkane::models::entities::map_cc::MapCC;
use starkane::models::data::starkane::{
CharacterPlayerProgress, MatchCount, PlayerStadistics, Ranking, RankingCount
};
Expand Down Expand Up @@ -57,10 +57,8 @@ trait StoreTrait {
fn set_character(ref self: Store, character: Character);
fn get_skill(ref self: Store, skill_id: u32, character_id: u32, level: u32) -> Skill;
fn set_skill(ref self: Store, skill: Skill);
fn get_tile(ref self: Store, map_id: u32, tile_id: u32) -> Tile;
fn set_tile(ref self: Store, tile: Tile);
fn get_map(ref self: Store, map_id: u32) -> Map;
fn set_map(ref self: Store, map: Map);
fn get_map_cc(ref self: Store, map_id: u32) -> MapCC;
fn set_map_cc(ref self: Store, map: MapCC);
// Data
fn get_match_count(ref self: Store, id: felt252) -> MatchCount;
fn set_match_count(ref self: Store, match_count: MatchCount);
Expand Down Expand Up @@ -231,20 +229,11 @@ impl StoreImpl of StoreTrait {
set!(self.world, (skill));
}

fn get_tile(ref self: Store, map_id: u32, tile_id: u32) -> Tile {
let tile_key = (map_id, tile_id);
get!(self.world, tile_key.into(), (Tile))
fn get_map_cc(ref self: Store, map_id: u32) -> MapCC {
get!(self.world, map_id, (MapCC))
}

fn set_tile(ref self: Store, tile: Tile) {
set!(self.world, (tile));
}

fn get_map(ref self: Store, map_id: u32) -> Map {
get!(self.world, map_id, (Map))
}

fn set_map(ref self: Store, map: Map) {
fn set_map_cc(ref self: Store, map: MapCC) {
set!(self.world, (map));
}

Expand Down
60 changes: 60 additions & 0 deletions src/systems/map_cc_system.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait};

#[starknet::interface]
trait IMapCCSystem<TContractState> {
fn init(ref self: TContractState);
}

#[dojo::contract]
mod map_cc_system {
use super::IMapCCSystem;
use starknet::{get_caller_address, ContractAddress};
use starkane::models::entities::map_cc::MapCC;
use starkane::store::{Store, StoreTrait};
use cc_starknet::{Dungeons, utils::pack::Pack};

#[storage]
struct Storage {}

#[external(v0)]
impl MapCCSystem of IMapCCSystem<ContractState> {
fn init(ref self: ContractState) {
// [Setup] Datastore
let world = self.world();
let mut store: Store = StoreTrait::new(world);

let mut state = Dungeons::unsafe_new_contract_state();
Dungeons::mint(ref state);
// let dungeon = Dungeons::generate_dungeon_dojo(@state, 1);
let player = get_caller_address();

let (pack, size) = Dungeons::get_layout(
@state,
2736342820117253318139212381411529799466697035823955982763560346397174457569,
26
);
// let (pack, size) = Dungeons::get_layout(@state, 5472685640234506636278424762823059598933394071647911965527120692794348915138, 25);
// println!("first: {}", pack.first);
// println!("second: {}", pack.second);
// println!("third: {}", pack.third);

set!(
world,
MapCC {
id: 1,
token_id: 1,
size: 25,
obstacles_1: pack.first,
obstacles_2: pack.second,
obstacles_3: pack.third,
// obstacles_1: dungeon.layout.first,
// obstacles_2: dungeon.layout.second,
// obstacles_3: dungeon.layout.third,
owner: Dungeons::ERC721Impl::owner_of(@state, 1).into(),
width: 25,
height: 25,
}
);
}
}
}
Loading

0 comments on commit f157be4

Please sign in to comment.