Skip to content

Commit f047fe3

Browse files
fix: merge
2 parents bf1b654 + 5f81899 commit f047fe3

33 files changed

+16033
-16094
lines changed

examples/vanilla/phaser/.gitignore

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
pnpm-debug.log*
8+
lerna-debug.log*
9+
10+
node_modules
11+
dist
12+
dist-ssr
13+
*.local
14+
15+
# Editor directories and files
16+
.vscode/*
17+
!.vscode/extensions.json
18+
.idea
19+
.DS_Store
20+
*.suo
21+
*.ntvs*
22+
*.njsproj
23+
*.sln
24+
*.sw?
206 Bytes
Loading
206 Bytes
Loading
253 Bytes
Loading

examples/vanilla/phaser/dojoConfig.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import manifest from "../../dojo-starter/manifests/dev/deployment/manifest.json";
2+
import { createDojoConfig } from "@dojoengine/core";
3+
4+
export const dojoConfig = createDojoConfig({
5+
manifest,
6+
});
7+
export type Config = typeof dojoConfig;

examples/vanilla/phaser/index.html

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<link rel="icon" type="image/x-icon" href="/favicon.ico" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<title>Dojo.js + Vanilla + Phaser example</title>
8+
</head>
9+
<body>
10+
<div id="app"></div>
11+
<script type="module" src="/src/main.ts"></script>
12+
</body>
13+
</html>

examples/vanilla/phaser/package.json

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "phaser",
3+
"private": true,
4+
"version": "0.0.0",
5+
"type": "module",
6+
"scripts": {
7+
"dev": "vite",
8+
"build": "tsc && vite build",
9+
"preview": "vite preview"
10+
},
11+
"devDependencies": {
12+
"typescript": "^5.5.4",
13+
"vite": "^5.3.5",
14+
"vite-plugin-top-level-await": "^1.4.2",
15+
"vite-plugin-wasm": "^3.3.0"
16+
},
17+
"dependencies": {
18+
"@dojoengine/core": "workspace:*",
19+
"@dojoengine/create-burner": "workspace:*",
20+
"@dojoengine/recs": "2.0.13",
21+
"@dojoengine/state": "workspace:*",
22+
"@dojoengine/torii-client": "workspace:*",
23+
"@dojoengine/utils": "workspace:*",
24+
"@latticexyz/utils": "^2.0.12",
25+
"noise": "^0.0.0",
26+
"phaser": "3.60.0-beta.14",
27+
"starknet": "6.11.0"
28+
}
29+
}
15 KB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { overridableComponent } from "@dojoengine/recs";
2+
import { ContractComponents } from "./defineContractComponents";
3+
4+
export type ClientComponents = ReturnType<typeof createClientComponents>;
5+
6+
export function createClientComponents({
7+
contractComponents,
8+
}: {
9+
contractComponents: ContractComponents;
10+
}) {
11+
return {
12+
...contractComponents,
13+
Position: overridableComponent(contractComponents.Position),
14+
Moves: overridableComponent(contractComponents.Moves),
15+
};
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/* Autogenerated file. Do not edit manually. */
2+
3+
import { defineComponent, Type as RecsType, World } from "@dojoengine/recs";
4+
5+
export type ContractComponents = Awaited<
6+
ReturnType<typeof defineContractComponents>
7+
>;
8+
9+
export function defineContractComponents(world: World) {
10+
return {
11+
DirectionsAvailable: (() => {
12+
return defineComponent(
13+
world,
14+
{ player: RecsType.BigInt, directions: RecsType.StringArray },
15+
{
16+
metadata: {
17+
name: "dojo_starter-DirectionsAvailable",
18+
types: ["contractaddress"],
19+
customTypes: [],
20+
},
21+
}
22+
);
23+
})(),
24+
Moved: (() => {
25+
return defineComponent(
26+
world,
27+
{ player: RecsType.BigInt, direction: RecsType.Number },
28+
{
29+
metadata: {
30+
name: "dojo_starter-Moved",
31+
types: ["contractaddress", "enum"],
32+
customTypes: ["Direction"],
33+
},
34+
}
35+
);
36+
})(),
37+
Moves: (() => {
38+
return defineComponent(
39+
world,
40+
{
41+
player: RecsType.BigInt,
42+
remaining: RecsType.Number,
43+
last_direction: RecsType.Number,
44+
can_move: RecsType.Boolean,
45+
},
46+
{
47+
metadata: {
48+
name: "dojo_starter-Moves",
49+
types: ["contractaddress", "u8", "enum", "bool"],
50+
customTypes: ["Direction"],
51+
},
52+
}
53+
);
54+
})(),
55+
Position: (() => {
56+
return defineComponent(
57+
world,
58+
{
59+
player: RecsType.BigInt,
60+
vec: { x: RecsType.Number, y: RecsType.Number },
61+
},
62+
{
63+
metadata: {
64+
name: "dojo_starter-Position",
65+
types: ["contractaddress", "u32", "u32"],
66+
customTypes: ["Vec2"],
67+
},
68+
}
69+
);
70+
})(),
71+
};
72+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { Account, AccountInterface } from "starknet";
2+
import { DojoProvider } from "@dojoengine/core";
3+
import { Config } from "../../dojoConfig.ts";
4+
import { Direction } from "./utils.ts";
5+
const NAMESPACE = "dojo_starter";
6+
7+
export interface MoveProps {
8+
account: Account | AccountInterface;
9+
direction: Direction;
10+
}
11+
12+
const handleError = (action: string, error: unknown) => {
13+
console.error(`Error executing ${action}:`, error);
14+
throw error;
15+
};
16+
17+
export type IWorld = Awaited<ReturnType<typeof setupWorld>>;
18+
19+
export async function setupWorld(provider: DojoProvider, _config: Config) {
20+
const actions = () => ({
21+
spawn: async ({ account }: { account: AccountInterface }) => {
22+
try {
23+
return await provider.execute(
24+
account,
25+
{
26+
contractName: "actions",
27+
entrypoint: "spawn",
28+
calldata: [],
29+
},
30+
NAMESPACE
31+
);
32+
} catch (error) {
33+
handleError("spawn", error);
34+
}
35+
},
36+
37+
move: async ({ account, direction }: MoveProps) => {
38+
try {
39+
return await provider.execute(
40+
account,
41+
{
42+
contractName: "actions",
43+
entrypoint: "move",
44+
calldata: [direction],
45+
},
46+
NAMESPACE
47+
);
48+
} catch (error) {
49+
handleError("move", error);
50+
}
51+
},
52+
});
53+
54+
return { actions: actions() };
55+
}
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { ContractComponents } from "./defineContractComponents";
2+
3+
export type ClientModels = ReturnType<typeof models>;
4+
5+
export function models({
6+
contractModels,
7+
}: {
8+
contractModels: ContractComponents;
9+
}) {
10+
return {
11+
models: {
12+
...contractModels,
13+
},
14+
};
15+
}
+128
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
import { getSyncEntities } from "@dojoengine/state";
2+
import * as torii from "@dojoengine/torii-client";
3+
4+
import { models } from "./models.ts";
5+
import { systems } from "./systems.ts";
6+
import { defineContractComponents } from "./defineContractComponents.ts";
7+
import { world } from "./world.ts";
8+
import { Config } from "../../dojoConfig.ts";
9+
import { setupWorld } from "./defineContractSystems.ts";
10+
11+
import { DojoProvider } from "@dojoengine/core";
12+
import { BurnerManager } from "@dojoengine/create-burner";
13+
import { Account, RpcProvider } from "starknet";
14+
import {
15+
ClientComponents,
16+
createClientComponents,
17+
} from "./createClientComponent.ts";
18+
19+
export type SetupResult = Awaited<ReturnType<typeof setup>>;
20+
export type IDojo = Awaited<ReturnType<typeof setup>>;
21+
22+
export async function setup({ ...config }: Config) {
23+
// torii client
24+
let toriiClient = null;
25+
try {
26+
toriiClient = await torii.createClient({
27+
rpcUrl: config.rpcUrl,
28+
toriiUrl: config.toriiUrl,
29+
relayUrl: "",
30+
worldAddress: config.manifest.world.address || "",
31+
});
32+
} catch (e) {
33+
console.error("Failed to create torii client:", e);
34+
throw e;
35+
}
36+
37+
// create contract components
38+
let contractModels = null;
39+
try {
40+
contractModels = createClientComponents({
41+
contractComponents: defineContractComponents(world),
42+
});
43+
} catch (e) {
44+
console.error("Failed to create contract components:", e);
45+
throw e;
46+
}
47+
48+
// create client components
49+
const { models: clientModels } = models({ contractModels });
50+
51+
// fetch all existing entities from torii
52+
let sync = null;
53+
try {
54+
sync = await getSyncEntities(
55+
toriiClient,
56+
contractModels as any,
57+
[],
58+
1000
59+
);
60+
} catch (e) {
61+
console.error("Failed to fetch sync:", e);
62+
throw e;
63+
}
64+
65+
let client = null;
66+
try {
67+
client = await setupWorld(
68+
new DojoProvider(config.manifest, config.rpcUrl),
69+
config
70+
);
71+
} catch (e) {
72+
console.error("Failed to create client:", e);
73+
throw e;
74+
}
75+
76+
const rpcProvider = new RpcProvider({
77+
nodeUrl: config.rpcUrl,
78+
});
79+
80+
let burnerManager = null;
81+
try {
82+
burnerManager = new BurnerManager({
83+
masterAccount: new Account(
84+
rpcProvider,
85+
config.masterAddress,
86+
config.masterPrivateKey
87+
),
88+
feeTokenAddress: config.feeTokenAddress,
89+
accountClassHash: config.accountClassHash,
90+
91+
rpcProvider,
92+
});
93+
} catch (e) {
94+
console.log("Failed to create burner manager:", e);
95+
throw e;
96+
}
97+
98+
try {
99+
await burnerManager.init();
100+
if (burnerManager.list().length === 0) {
101+
await burnerManager.create();
102+
}
103+
} catch (e) {
104+
console.error(e);
105+
}
106+
const actions = systems({
107+
client,
108+
clientModels: clientModels as ClientComponents,
109+
contractComponents: contractModels,
110+
});
111+
const account = burnerManager.getActiveAccount();
112+
if (null === account || undefined === account) {
113+
throw new Error("failed to get active account");
114+
}
115+
116+
return {
117+
client,
118+
clientModels,
119+
contractComponents: clientModels,
120+
systemCalls: actions.actions,
121+
config,
122+
world,
123+
burnerManager,
124+
rpcProvider,
125+
sync,
126+
account,
127+
};
128+
}

0 commit comments

Comments
 (0)