Skip to content

feat: add vanillajs + phaser example #249

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

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
24 changes: 24 additions & 0 deletions examples/vanilla/phaser/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
Binary file added examples/vanilla/phaser/assets/sprGrass.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/vanilla/phaser/assets/sprSand.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/vanilla/phaser/assets/sprWater.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions examples/vanilla/phaser/dojoConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import manifest from "../../dojo-starter/manifests/dev/deployment/manifest.json";
import { createDojoConfig } from "@dojoengine/core";

export const dojoConfig = createDojoConfig({
manifest,
});
export type Config = typeof dojoConfig;
13 changes: 13 additions & 0 deletions examples/vanilla/phaser/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/x-icon" href="/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Dojo.js + Vanilla + Phaser example</title>
</head>
<body>
<div id="app"></div>
<script type="module" src="/src/main.ts"></script>
</body>
</html>
29 changes: 29 additions & 0 deletions examples/vanilla/phaser/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "phaser",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
"preview": "vite preview"
},
"devDependencies": {
"typescript": "^5.5.4",
"vite": "^5.3.5",
"vite-plugin-top-level-await": "^1.4.2",
"vite-plugin-wasm": "^3.3.0"
},
"dependencies": {
"@dojoengine/core": "workspace:*",
"@dojoengine/create-burner": "workspace:*",
"@dojoengine/recs": "2.0.13",
"@dojoengine/state": "workspace:*",
"@dojoengine/torii-client": "workspace:*",
"@dojoengine/utils": "workspace:*",
"@latticexyz/utils": "^2.0.12",
"noise": "^0.0.0",
"phaser": "3.60.0-beta.14",
"starknet": "6.11.0"
}
}
Binary file added examples/vanilla/phaser/public/favicon.ico
Binary file not shown.
16 changes: 16 additions & 0 deletions examples/vanilla/phaser/src/dojo/createClientComponent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { overridableComponent } from "@dojoengine/recs";
import { ContractComponents } from "./defineContractComponents";

export type ClientComponents = ReturnType<typeof createClientComponents>;

export function createClientComponents({
contractComponents,
}: {
contractComponents: ContractComponents;
}) {
return {
...contractComponents,
Position: overridableComponent(contractComponents.Position),
Moves: overridableComponent(contractComponents.Moves),
};
}
72 changes: 72 additions & 0 deletions examples/vanilla/phaser/src/dojo/defineContractComponents.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/* Autogenerated file. Do not edit manually. */

import { defineComponent, Type as RecsType, World } from "@dojoengine/recs";

export type ContractComponents = Awaited<
ReturnType<typeof defineContractComponents>
>;

export function defineContractComponents(world: World) {
return {
DirectionsAvailable: (() => {
return defineComponent(
world,
{ player: RecsType.BigInt, directions: RecsType.StringArray },
{
metadata: {
name: "dojo_starter-DirectionsAvailable",
types: ["contractaddress"],
customTypes: [],
},
}
);
})(),
Moved: (() => {
return defineComponent(
world,
{ player: RecsType.BigInt, direction: RecsType.Number },
{
metadata: {
name: "dojo_starter-Moved",
types: ["contractaddress", "enum"],
customTypes: ["Direction"],
},
}
);
})(),
Moves: (() => {
return defineComponent(
world,
{
player: RecsType.BigInt,
remaining: RecsType.Number,
last_direction: RecsType.Number,
can_move: RecsType.Boolean,
},
{
metadata: {
name: "dojo_starter-Moves",
types: ["contractaddress", "u8", "enum", "bool"],
customTypes: ["Direction"],
},
}
);
})(),
Position: (() => {
return defineComponent(
world,
{
player: RecsType.BigInt,
vec: { x: RecsType.Number, y: RecsType.Number },
},
{
metadata: {
name: "dojo_starter-Position",
types: ["contractaddress", "u32", "u32"],
customTypes: ["Vec2"],
},
}
);
})(),
};
}
55 changes: 55 additions & 0 deletions examples/vanilla/phaser/src/dojo/defineContractSystems.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { Account, AccountInterface } from "starknet";
import { DojoProvider } from "@dojoengine/core";
import { Config } from "../../dojoConfig.ts";
import { Direction } from "./utils.ts";
const NAMESPACE = "dojo_starter";

export interface MoveProps {
account: Account | AccountInterface;
direction: Direction;
}

const handleError = (action: string, error: unknown) => {
console.error(`Error executing ${action}:`, error);
throw error;
};

export type IWorld = Awaited<ReturnType<typeof setupWorld>>;

export async function setupWorld(provider: DojoProvider, _config: Config) {
const actions = () => ({
spawn: async ({ account }: { account: AccountInterface }) => {
try {
return await provider.execute(
account,
{
contractName: "actions",
entrypoint: "spawn",
calldata: [],
},
NAMESPACE
);
} catch (error) {
handleError("spawn", error);
}
},

move: async ({ account, direction }: MoveProps) => {
try {
return await provider.execute(
account,
{
contractName: "actions",
entrypoint: "move",
calldata: [direction],
},
NAMESPACE
);
} catch (error) {
handleError("move", error);
}
},
});

return { actions: actions() };
}
15 changes: 15 additions & 0 deletions examples/vanilla/phaser/src/dojo/models.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { ContractComponents } from "./defineContractComponents";

export type ClientModels = ReturnType<typeof models>;

export function models({
contractModels,
}: {
contractModels: ContractComponents;
}) {
return {
models: {
...contractModels,
},
};
}
128 changes: 128 additions & 0 deletions examples/vanilla/phaser/src/dojo/setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import { getSyncEntities } from "@dojoengine/state";
import * as torii from "@dojoengine/torii-client";

import { models } from "./models.ts";
import { systems } from "./systems.ts";
import { defineContractComponents } from "./defineContractComponents.ts";
import { world } from "./world.ts";
import { Config } from "../../dojoConfig.ts";
import { setupWorld } from "./defineContractSystems.ts";

import { DojoProvider } from "@dojoengine/core";
import { BurnerManager } from "@dojoengine/create-burner";
import { Account, RpcProvider } from "starknet";
import {
ClientComponents,
createClientComponents,
} from "./createClientComponent.ts";

export type SetupResult = Awaited<ReturnType<typeof setup>>;
export type IDojo = Awaited<ReturnType<typeof setup>>;

export async function setup({ ...config }: Config) {
// torii client
let toriiClient = null;
try {
toriiClient = await torii.createClient({
rpcUrl: config.rpcUrl,
toriiUrl: config.toriiUrl,
relayUrl: "",
worldAddress: config.manifest.world.address || "",
});
} catch (e) {
console.error("Failed to create torii client:", e);
throw e;
}

// create contract components
let contractModels = null;
try {
contractModels = createClientComponents({
contractComponents: defineContractComponents(world),
});
} catch (e) {
console.error("Failed to create contract components:", e);
throw e;
}

// create client components
const { models: clientModels } = models({ contractModels });

// fetch all existing entities from torii
let sync = null;
try {
sync = await getSyncEntities(
toriiClient,
contractModels as any,
[],
1000
);
} catch (e) {
console.error("Failed to fetch sync:", e);
throw e;
}

let client = null;
try {
client = await setupWorld(
new DojoProvider(config.manifest, config.rpcUrl),
config
);
} catch (e) {
console.error("Failed to create client:", e);
throw e;
}

const rpcProvider = new RpcProvider({
nodeUrl: config.rpcUrl,
});

let burnerManager = null;
try {
burnerManager = new BurnerManager({
masterAccount: new Account(
rpcProvider,
config.masterAddress,
config.masterPrivateKey
),
feeTokenAddress: config.feeTokenAddress,
accountClassHash: config.accountClassHash,

rpcProvider,
});
} catch (e) {
console.log("Failed to create burner manager:", e);
throw e;
}

try {
await burnerManager.init();
if (burnerManager.list().length === 0) {
await burnerManager.create();
}
} catch (e) {
console.error(e);
}
const actions = systems({
client,
clientModels: clientModels as ClientComponents,
contractComponents: contractModels,
});
const account = burnerManager.getActiveAccount();
if (null === account || undefined === account) {
throw new Error("failed to get active account");
}

return {
client,
clientModels,
contractComponents: clientModels,
systemCalls: actions.actions,
config,
world,
burnerManager,
rpcProvider,
sync,
account,
};
}
Loading
Loading