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

Feat implement map cairo #3

Open
wants to merge 7 commits into
base: tryphaser1
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
4 changes: 2 additions & 2 deletions Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ sierra-replace-ids = true

[dependencies]
# cubit = {git = "https://github.com/influenceth/cubit.git"}
dojo = {git = "https://github.com/dojoengine/dojo.git", tag = "v0.3.15"}
dojo = { git = "https://github.com/dojoengine/dojo.git", tag = "v0.3.15" }
# dojo = {git = "https://github.com/dojoengine/dojo.git", rev = "d7f46502cba3d6462b68a4dfb07336377bca2678"}

[[target.dojo]]
Expand All @@ -29,6 +29,7 @@ migrate_prod = "sozo -P prod migrate"
rpc_url = "http://localhost:5050"
account_address = "0x517ececd29116499f4a1b64b094da79ba08dfd54a3edaa316134c41f8160973"
private_key = "0x1800000000300000180000000000030000000000003006001800006600"
world_address = "0x3c877dbe4454125201bdc0c5c6dcafc5a9b3a2fc78417702a6cd5ebaf143cc3"

# seed 420
# [profile.staging.tool.dojo.env]
Expand All @@ -48,4 +49,3 @@ website = "https://rollyourown.preview.cartridge.gg/"
icon_uri = "file://assets/icon.png"
cover_uri = "file://assets/cover.png"
socials.x = "https://x.com/TheDopeWars"

8 changes: 8 additions & 0 deletions scripts/default_auth.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export DECIDE_ADDRESS=$(cat ./target/dev/manifest.json | jq -r '.contracts[] | s
export TRADE_ADDRESS=$(cat ./target/dev/manifest.json | jq -r '.contracts[] | select(.name == "trade" ).address')
export SHOP_ADDRESS=$(cat ./target/dev/manifest.json | jq -r '.contracts[] | select(.name == "shop" ).address')
export RYO_ADDRESS=$(cat ./target/dev/manifest.json | jq -r '.contracts[] | select(.name == "ryo" ).address')
export MINI_GAME_ADDRESS=$(cat ./target/dev/manifest.json | jq -r '.contracts[] | select(.name == "minigame" ).address')

echo "---------------------------------------------------------------------------"
echo profile : $PROFILE
Expand All @@ -28,6 +29,7 @@ echo decide: $DECIDE_ADDRESS
echo trade : $TRADE_ADDRESS
echo shop : $SHOP_ADDRESS
echo ryo : $RYO_ADDRESS
echo minigame : $MINI_GAME_ADDRESS
echo "---------------------------------------------------------------------------"

# enable system -> component authorizations
Expand All @@ -37,6 +39,7 @@ DECIDE_COMPONENTS=("Player" "Drug" "Market" "Encounter" "Leaderboard" "RyoMeta")
TRADE_COMPONENTS=("Drug" "Market" "Player")
SHOP_COMPONENTS=("Player" "Item" "Market")
RYO_COMPONENTS=("RyoMeta" "Leaderboard")
MINI_GAME_COMPONENTS=("Player" "Game" "Map" "Cop" "Gangster" "PlayerMiniGame" "Tile" "Encounter")

for component in ${LOBBY_COMPONENTS[@]}; do
sozo -P $PROFILE auth writer $component $LOBBY_ADDRESS --world $WORLD_ADDRESS
Expand Down Expand Up @@ -68,6 +71,11 @@ for component in ${RYO_COMPONENTS[@]}; do
sleep 0.1
done

for component in ${MINI_GAME_COMPONENTS[@]}; do
sozo -P $PROFILE auth writer $component $MINI_GAME_ADDRESS --world $WORLD_ADDRESS
sleep 0.1
done

echo "Default authorizations have been successfully set."

echo "Initializing..."
Expand Down
5 changes: 5 additions & 0 deletions src/constants.cairo
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
const SCALING_FACTOR: u128 = 10_000;
const STREET_TYPE: u8 = 0;
const WALL_TYPE: u8 = 1;
const PLAYER_TYPE: u8 = 2;
const COP_TYPE: u8 = 3;
const GANGSTER_TYPE: u8 = 4;
5 changes: 5 additions & 0 deletions src/models.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,8 @@ mod item;
mod encounter;
mod leaderboard;
mod ryo;
mod tile;
mod map;
mod player_minigame;
mod cop;
mod gangster;
24 changes: 24 additions & 0 deletions src/models/cop.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use rollyourown::models::tile::Tile;

#[derive(Model, Copy, Drop, Serde)]
struct Cop {
#[key]
game_id: u32,
level: u32,
step: u32,
coordinate_x: u32,
coordinate_y: u32,
}


#[generate_trait]
impl CopImpl of CopTrait {
#[inline(always)]
fn new(game_id: u32, level: u32, coordinate_x: u32, coordinate_y: u32) -> Cop {
if (level == 1) {
Cop { game_id: game_id, level: level, step: 1, coordinate_x, coordinate_y }
} else {
Cop { game_id: game_id, level: level, step: 2, coordinate_x, coordinate_y }
}
}
}
28 changes: 28 additions & 0 deletions src/models/gangster.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use rollyourown::models::tile::Tile;

#[derive(Model, Copy, Drop, Serde)]
struct Gangster {
#[key]
game_id: u32,
level: u32,
step: u32,
coordinate_x: u32,
coordinate_y: u32,
}


#[generate_trait]
impl GangsterImpl of GangsterTrait {
#[inline(always)]
fn new(game_id: u32, level: u32, coordinate_x: u32, coordinate_y: u32) -> Gangster {
if (level == 1) {
Gangster { game_id: game_id, level: level, step: 2, coordinate_x, coordinate_y }
} else if (level == 2) {
Gangster { game_id: game_id, level: level, step: 3, coordinate_x, coordinate_y }
} else if (level == 3) {
Gangster { game_id: game_id, level: level, step: 4, coordinate_x, coordinate_y }
} else {
Gangster { game_id: game_id, level: level, step: 5, coordinate_x, coordinate_y }
}
}
}
114 changes: 114 additions & 0 deletions src/models/map.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
use core::traits::IndexView;
use core::clone::Clone;
use core::box::BoxTrait;
use core::traits::Into;
use core::traits::TryInto;
use core::option::OptionTrait;
use rollyourown::models::tile::{Tile, TileTrait};
use array::{ArrayTrait, SpanTrait, Span};
use rollyourown::constants::{STREET_TYPE, WALL_TYPE, PLAYER_TYPE, COP_TYPE, GANGSTER_TYPE};
use rollyourown::models::encounter::EncounterType;


#[derive(Model, Copy, Drop, Serde)]
struct Map {
#[key]
map_id: u32,
level: u32,
}

#[derive(Serde, Copy, Drop, PartialEq)]
enum Type {
Street: (),
Wall: (),
Player: (),
Cop: (),
Gangster: (),
}


#[generate_trait]
impl MapImpl of MapTrait {
fn new(player_position: u32, enemy_type: EncounterType, enemy_position: u32) -> Span<Tile> {
let mut i: u32 = 0;
let mut map = array![];
loop {
if i == 400 {
break;
}
if (i >= 0 || i <= 20) {
map.append(TileTrait::new(i % 20, i / 20, WALL_TYPE));
} else if (i % 20 == 0) {
map.append(TileTrait::new(i % 20, i / 20, WALL_TYPE));
} else {
if (i == player_position.into()) {
map.append(TileTrait::new(i % 20, i / 20, PLAYER_TYPE));
} else if (i == enemy_position) {
match enemy_type {
EncounterType::Gang => {
map.append(TileTrait::new(i % 20, i / 20, GANGSTER_TYPE));
},
EncounterType::Cops => {
map.append(TileTrait::new(i % 20, i / 20, COP_TYPE));
},
}
} else {
map.append(TileTrait::new(i % 20, i / 20, STREET_TYPE));
}
}

i += 1;
};

map.span()
}

fn get_type(tile: Tile, raw_type: u8) -> Type {
if raw_type == STREET_TYPE {
return Type::Street(());
} else if raw_type == WALL_TYPE {
return Type::Wall(());
} else if raw_type == PLAYER_TYPE {
return Type::Player(());
} else if raw_type == COP_TYPE {
return Type::Cop(());
} else if raw_type == GANGSTER_TYPE {
return Type::Gangster(());
}
Type::Street(())
}
// fn replace_tile(ref self: Map, index: u32, _type: u8) {
// let mut new_tiles = array![]; // Assuming this creates a new Array<Tile>
// let mut i: u32 = 0;

// // Copy tiles from the old map to the new one, replacing the specified tile
// loop {
// if i == 400 {
// break;
// }
// if (i >= 1 || i <= 20) {
// new_tiles.append(TileTrait::new(i % 20, i / 20, 1));
// } else if (i % 20 == 0) {
// new_tiles.append(TileTrait::new(i % 20, i / 20, 1));
// } else {
// if (i == index) {
// new_tiles.append(TileTrait::new(i % 20, i / 20, _type));
// } else {
// new_tiles.append(TileTrait::new(i % 20, i / 20, 0));
// }
// }

// i += 1;
// };

// self.map = new_tiles.span(); // Update the map with the new Span<Tile>
// }

// fn get_coordinate(ref self: Map, index: u32) -> (u32, u32) {
// let mut point = self.map.at(index);
// let point_x = point.x.clone();
// let point_y = point.y.clone();
// (point_x, point_y)
// }
}

15 changes: 15 additions & 0 deletions src/models/player_minigame.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use starknet::ContractAddress;
use rollyourown::models::player::Player;

#[derive(Model, Copy, Drop, Serde)]
struct PlayerMiniGame {
#[key]
game_id: u32,
#[key]
player_id: ContractAddress,
_player: Player,
position: u32,
}

#[generate_trait]
impl PlayerMiniGameImpl of PlayerMiniGameTrait {}
103 changes: 103 additions & 0 deletions src/models/tile.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
use rollyourown::constants::{STREET_TYPE, WALL_TYPE, PLAYER_TYPE, COP_TYPE, GANGSTER_TYPE};

#[derive(Model, Copy, Drop, Serde)]
struct Tile {
#[key]
game_id: u32,
#[key]
index: u32,
_type: u8,
x: u32,
y: u32
}


trait TileTrait {
fn new(x: u32, y: u32, _type: u8) -> Tile;
fn get_tile(self: Tile) -> u8;
fn is_street(self: Tile) -> bool;
fn is_wall(self: Tile) -> bool;
fn is_player(self: Tile) -> bool;
fn is_cop(self: Tile) -> bool;
fn is_gangster(self: Tile) -> bool;
fn is_zero(self: Tile) -> bool;
fn is_equal(self: Tile, b: Tile) -> bool;
fn is_close(self: Tile, b: Tile) -> bool;
fn distance(self: Tile, b: Tile) -> u32;
fn calculate_index(self: Tile) -> u32;
}

impl TileImpl of TileTrait {
fn new(x: u32, y: u32, _type: u8) -> Tile {
Tile { game_id: 0, index: 0, x: x, y: y, _type: _type }
}

fn get_tile(self: Tile) -> u8 {
if self._type == STREET_TYPE {
return STREET_TYPE;
} else if self._type == WALL_TYPE {
return WALL_TYPE;
} else if self._type == PLAYER_TYPE {
return PLAYER_TYPE;
} else if self._type == COP_TYPE {
return COP_TYPE;
} else if self._type == GANGSTER_TYPE {
return GANGSTER_TYPE;
} else {
return STREET_TYPE;
}
}

fn is_street(self: Tile) -> bool {
self._type == STREET_TYPE
}

fn is_wall(self: Tile) -> bool {
self._type == WALL_TYPE
}

fn is_player(self: Tile) -> bool {
self._type == PLAYER_TYPE
}

fn is_cop(self: Tile) -> bool {
self._type == COP_TYPE
}

fn is_gangster(self: Tile) -> bool {
self._type == GANGSTER_TYPE
}

fn is_zero(self: Tile) -> bool {
self.x == 0 && self.y == 0
}

fn is_equal(self: Tile, b: Tile) -> bool {
self.x == b.x && self.y == b.y
}

fn is_close(self: Tile, b: Tile) -> bool {
self.distance(b) <= 1
}

fn distance(self: Tile, b: Tile) -> u32 {
let mut dx = 0;
if self.x > b.x {
dx = self.x - b.x;
} else {
dx = b.x - self.x;
};

let mut dy = 0;
if self.y > b.y {
dy = self.y - b.y;
} else {
dy = b.y - self.y;
};
dx * dx + dy * dy
}

fn calculate_index(self: Tile) -> u32 {
self.y * 20 + self.x
}
}
3 changes: 2 additions & 1 deletion src/systems.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod travel;
mod decide;
mod shop;
mod ryo;

mod minigame;
// mod devtools;


Loading