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

WIP: add spawn and move #8

Merged
Merged
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
12 changes: 12 additions & 0 deletions scripts/move.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash
set -euo pipefail
pushd $(dirname "$0")/..

export RPC_URL="http://0.0.0.0:5050";

export WORLD_ADDRESS="$(cat ./manifests/dev/deployment/manifest.json | jq -r '.world.address')"

echo $WORLD_ADDRESS

# sozo execute --world <WORLD_ADDRESS> <CONTRACT> <ENTRYPOINT>
sozo execute --world $WORLD_ADDRESS actions move -c 1,15,15 --wait
12 changes: 12 additions & 0 deletions scripts/spawn.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash
set -euo pipefail
pushd $(dirname "$0")/..

export RPC_URL="http://0.0.0.0:5050";

export WORLD_ADDRESS="$(cat ./manifests/dev/deployment/manifest.json | jq -r '.world.address')"

echo $WORLD_ADDRESS

# sozo execute --world <WORLD_ADDRESS> <CONTRACT> <ENTRYPOINT>
sozo execute --world $WORLD_ADDRESS actions spawn -c 1 --wait
43 changes: 42 additions & 1 deletion src/models.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,20 @@ struct Potion {
pub potion_effect: u32,
}

#[derive(Drop, Copy, Serde)]
#[dojo::model]
struct Position {
#[key]
player: Player,
coordinates: Coordinates,
}

#[derive(Drop, Copy, Serde, Introspect)]
struct Coordinates {
x: u32,
y: u32
}

#[generate_trait]
impl BeastImpl of BeastTrait {
fn exist(self: Beast) -> bool {
Expand All @@ -98,7 +112,7 @@ impl BeastImpl of BeastTrait {

#[cfg(test)]
mod tests {
use super::{Battle, Beast, Player, Mt, Potion, BeastTrait, WorldElements};
use super::{Battle, Beast, Player, Coordinates, Position, Mt, Potion, BeastTrait, WorldElements};

#[test]
fn test_beast_exist() {
Expand Down Expand Up @@ -139,6 +153,33 @@ mod tests {
assert_eq!(player.potions, 5, "Player should have 5 potions");
}

#[test]
fn test_position_initialization() {
let player = Player {
player_id: 1,
player_name: 'Hero',
beast_1: 1,
beast_2: 2,
beast_3: 3,
beast_4: 4,
potions: 5,
};

let coordinates = Coordinates{
x: 10,
y: 10,
};

let position = Position {
player: player,
coordinates: coordinates
};

assert_eq!(position.player.player_id, 1, "Player ID should be 1");
assert_eq!(position.coordinates.x, 10, "Player X coordinate should be 10");
assert_eq!(position.coordinates.y, 10, "Player Y coordinate should be 10");
}

#[test]
fn test_battle_initialization() {
let battle = Battle {
Expand Down
33 changes: 32 additions & 1 deletion src/systems/actions.cairo
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
use bytebeasts::models::Beast;
use bytebeasts::models::Mt;
use bytebeasts::models::Player;
use bytebeasts::models::Coordinates;
use bytebeasts::models::Position;
use bytebeasts::models::Potion;
use bytebeasts::models::WorldElements;

// define the interface
#[dojo::interface]
trait IActions {
fn setWorld(ref world: IWorldDispatcher);
fn spawn(ref world: IWorldDispatcher, player_id: u32);
fn move(ref world: IWorldDispatcher, player_id: u32, new_x: u32, new_y: u32);
}

// dojo decorator
Expand All @@ -16,7 +20,7 @@ mod actions {
use super::{IActions};

use starknet::{ContractAddress, get_caller_address};
use bytebeasts::models::{Beast, Mt, Player, Potion, WorldElements};
use bytebeasts::models::{Beast, Mt, Player, Coordinates, Position, Potion, WorldElements};

#[abi(embed_v0)]
impl ActionsImpl of IActions<ContractState> {
Expand Down Expand Up @@ -195,5 +199,32 @@ mod actions {
})
);
}

fn spawn(ref world: IWorldDispatcher, player_id: u32) {
let player_from_world = get!(world, player_id, (Player));

set!(
world,
(Position {
player: player_from_world,
coordinates: Coordinates { x: 10, y: 10 }
},
)
);
}

fn move(ref world: IWorldDispatcher, player_id: u32, new_x: u32, new_y:u32) {
let player_from_world = get!(world, player_id, (Player));

set!(
world,
(Position {
player: player_from_world,
coordinates: Coordinates { x: new_x, y: new_y }
},
)
);
}

}
}
Loading