Skip to content

Commit

Permalink
add moves
Browse files Browse the repository at this point in the history
  • Loading branch information
ponderingdemocritus committed Nov 9, 2023
1 parent b7b8344 commit f8fcb89
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 47 deletions.
2 changes: 1 addition & 1 deletion examples/emojiman/scripts/default_auth.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ echo actions : $ACTIONS_ADDRESS
echo "---------------------------------------------------------------------------"

# enable system -> component authorizations
COMPONENTS=("Position" "MovesQueue" "RPSType" "GameData" "PlayerID" )
COMPONENTS=("Position" "MovesQueue" "RPSType" "GameData" "PlayerID" "Energy" )

for component in ${COMPONENTS[@]}; do
sozo auth writer $component $ACTIONS_ADDRESS --world $WORLD_ADDRESS --rpc-url $RPC_URL
Expand Down
23 changes: 9 additions & 14 deletions examples/react-phaser-example/src/dojo/contractComponents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,30 @@ import { defineComponent, Type as RecsType, World } from "@dojoengine/recs";

export function defineContractComponents(world: World) {
return {
GameData: (() => {
Energy: (() => {
return defineComponent(
world,
{
game: RecsType.Number,
number_of_players: RecsType.Number,
available_ids: RecsType.NumberArray,
},
{ id: RecsType.Number, amt: RecsType.Number },
{
metadata: {
name: "GameData",
name: "Energy",
types: [],
},
}
);
})(),
MovesQueue: (() => {
GameData: (() => {
return defineComponent(
world,
{
player: RecsType.String,
m1: RecsType.Number,
m2: RecsType.Number,
m3: RecsType.Number,
game: RecsType.Number,
number_of_players: RecsType.Number,
available_ids: RecsType.NumberArray,
},
{
metadata: {
name: "MovesQueue",
types: ["Direction", "Direction", "Direction"],
name: "GameData",
types: [],
},
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ export function createClientComponents({
}: SetupNetworkResult) {
return {
...contractComponents,
Position: overridableComponent(contractComponents.Position),
};
}
55 changes: 28 additions & 27 deletions examples/react-phaser-example/src/dojo/createSystemCalls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { uuid } from "@latticexyz/utils";
import { ClientComponents } from "./createClientComponents";
import { Direction, updatePositionWithDirection } from "./utils";
import { getEvents, setComponentsFromEvents } from "@dojoengine/utils";
import { get } from "http";

export type SystemCalls = ReturnType<typeof createSystemCalls>;

Expand All @@ -18,7 +19,7 @@ export interface MoveSystemProps extends SystemSigner {

export function createSystemCalls(
{ execute, contractComponents }: SetupNetworkResult,
{ Position }: ClientComponents
{ Position, PlayerID }: ClientComponents
) {
const spawn = async (props: SystemSigner) => {
const signer = props.signer;
Expand Down Expand Up @@ -72,30 +73,30 @@ export function createSystemCalls(

console.log("spawn", signer);

const entityId = signer.address.toString() as Entity;

// const positionId = uuid();
// Position.addOverride(positionId, {
// entity: entityId,
// value: {
// player: BigInt(entityId),
// vec: updatePositionWithDirection(
// direction,
// // currently recs does not support nested values so we use any here
// getComponentValue(Position, entityId) as any
// ).vec,
// },
// });

const movesId = uuid();
// Moves.addOverride(movesId, {
// entity: entityId,
// value: {
// player: BigInt(entityId),
// remaining:
// (getComponentValue(Moves, entityId)?.remaining || 0) - 1,
// },
// });
const playerId = getComponentValue(
PlayerID,
signer.address.toString() as Entity
);

const currentPosition = getComponentValue(
Position,
playerId?.id.toString() as Entity
) || { x: 0, y: 0 };

const newPosition = updatePositionWithDirection(direction, {
x: currentPosition["x"],
y: currentPosition["y"],
});

const positionId = uuid();
Position.addOverride(positionId, {
entity: playerId?.id.toString() as Entity,
value: {
id: 1,
x: newPosition["x"],
y: newPosition["y"],
},
});

try {
const { transaction_hash } = await execute(
Expand All @@ -115,10 +116,10 @@ export function createSystemCalls(
);
} catch (e) {
console.log(e);
// Position.removeOverride(positionId);
Position.removeOverride(positionId);
// Moves.removeOverride(movesId);
} finally {
// Position.removeOverride(positionId);
Position.removeOverride(positionId);
// Moves.removeOverride(movesId);
}
};
Expand Down
10 changes: 5 additions & 5 deletions examples/react-phaser-example/src/dojo/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@ export enum Direction {

export function updatePositionWithDirection(
direction: Direction,
value: { vec: { x: number; y: number } }
value: { x: number; y: number }
) {
switch (direction) {
case Direction.Left:
value.vec.x--;
value.x--;
break;
case Direction.Right:
value.vec.x++;
value.x++;
break;
case Direction.Up:
value.vec.y--;
value.y--;
break;
case Direction.Down:
value.vec.y++;
value.y++;
break;
default:
throw new Error("Invalid direction provided");
Expand Down

0 comments on commit f8fcb89

Please sign in to comment.