Skip to content

Commit d7873b2

Browse files
committed
feat: bind dojo to phaser
1 parent f1c4860 commit d7873b2

File tree

8 files changed

+1780
-145
lines changed

8 files changed

+1780
-145
lines changed

examples/vanilla/phaser/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
"preview": "vite preview"
1010
},
1111
"devDependencies": {
12-
"typescript": "^5.2.2",
13-
"vite": "^5.3.4",
12+
"typescript": "^5.5.4",
13+
"vite": "^5.3.5",
1414
"vite-plugin-wasm": "^3.3.0"
1515
},
1616
"dependencies": {

examples/vanilla/phaser/src/dojo/setup.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import { setupWorld } from "./defineContractSystems.ts";
1010

1111
import { DojoProvider } from "@dojoengine/core";
1212
import { BurnerManager } from "@dojoengine/create-burner";
13-
import { Account, RpcProvider } from "starknet";
13+
import { Account, RpcProvider, wallet } from "starknet";
14+
import { createClientComponents } from "./createClientComponent.ts";
1415

1516
export type SetupResult = Awaited<ReturnType<typeof setup>>;
1617
export type IDojo = Awaited<ReturnType<typeof setup>>;
@@ -25,10 +26,12 @@ export async function setup({ ...config }: Config) {
2526
});
2627

2728
// create contract components
28-
const contractModels = defineContractComponents(world);
29+
const contractModels = createClientComponents({
30+
contractComponents: defineContractComponents(world),
31+
});
2932

3033
// create client components
31-
const clientModels = models({ contractModels });
34+
const { models: clientModels } = models({ contractModels });
3235

3336
// fetch all existing entities from torii
3437
const sync = await getSyncEntities(
@@ -67,16 +70,26 @@ export async function setup({ ...config }: Config) {
6770
} catch (e) {
6871
console.error(e);
6972
}
73+
const actions = systems({
74+
client,
75+
clientModels,
76+
contractComponents: contractModels,
77+
});
78+
const account = burnerManager.getActiveAccount();
79+
if (null === account || undefined === account) {
80+
throw new Error("failed to get active account");
81+
}
7082

7183
return {
7284
client,
7385
clientModels,
7486
contractComponents: clientModels,
75-
systemCalls: systems({ client, clientModels }),
87+
systemCalls: actions.actions,
7688
config,
7789
world,
7890
burnerManager,
7991
rpcProvider,
8092
sync,
93+
account,
8194
};
8295
}

examples/vanilla/phaser/src/dojo/systems.ts

Lines changed: 96 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -8,115 +8,123 @@ import {
88
getEvents,
99
setComponentsFromEvents,
1010
} from "@dojoengine/utils";
11-
import { ContractComponents } from "./defineContractComponents";
1211
import type { IWorld } from "./defineContractSystems";
12+
import { ContractComponents } from "./defineContractComponents";
1313

1414
export type SystemCalls = ReturnType<typeof systems>;
1515

1616
export function systems({
1717
client,
1818
clientModels: { Position, Moves },
19+
contractComponents,
1920
}: {
2021
client: IWorld;
2122
clientModels: ClientComponents;
23+
contractComponents: ContractComponents;
2224
}) {
23-
const spawn = async (account: AccountInterface) => {
24-
const entityId = getEntityIdFromKeys([
25-
BigInt(account.address),
26-
]) as Entity;
27-
28-
const positionId = uuid();
29-
Position.addOverride(positionId, {
30-
entity: entityId,
31-
value: { player: BigInt(entityId), vec: { x: 10, y: 10 } },
32-
});
25+
function actions() {
26+
const spawn = async (account: AccountInterface) => {
27+
const entityId = getEntityIdFromKeys([
28+
BigInt(account.address),
29+
]) as Entity;
3330

34-
const movesId = uuid();
35-
Moves.addOverride(movesId, {
36-
entity: entityId,
37-
value: {
38-
player: BigInt(entityId),
39-
remaining: 100,
40-
last_direction: 0,
41-
},
42-
});
31+
const positionId = uuid();
32+
Position.addOverride(positionId, {
33+
entity: entityId,
34+
value: { player: BigInt(entityId), vec: { x: 10, y: 10 } },
35+
});
4336

44-
try {
45-
const { transaction_hash } = await client.actions.spawn({
46-
account,
37+
const movesId = uuid();
38+
Moves.addOverride(movesId, {
39+
entity: entityId,
40+
value: {
41+
player: BigInt(entityId),
42+
remaining: 100,
43+
last_direction: 0,
44+
},
4745
});
4846

49-
setComponentsFromEvents(
50-
contractComponents,
51-
getEvents(
52-
await account.waitForTransaction(transaction_hash, {
53-
retryInterval: 100,
54-
})
55-
)
56-
);
57-
} catch (e) {
58-
console.log(e);
59-
Position.removeOverride(positionId);
60-
Moves.removeOverride(movesId);
61-
} finally {
62-
Position.removeOverride(positionId);
63-
Moves.removeOverride(movesId);
64-
}
65-
};
47+
try {
48+
const { transaction_hash } = await client.actions.spawn({
49+
account,
50+
});
51+
// setComponentsFromEvents(
52+
// contractComponents,
53+
// getEvents(
54+
// await account.waitForTransaction(transaction_hash, {
55+
// retryInterval: 100,
56+
// })
57+
// )
58+
// );
59+
} catch (e) {
60+
console.log(e);
61+
Position.removeOverride(positionId);
62+
Moves.removeOverride(movesId);
63+
} finally {
64+
Position.removeOverride(positionId);
65+
Moves.removeOverride(movesId);
66+
}
67+
};
6668

67-
const move = async (account: AccountInterface, direction: Direction) => {
68-
const entityId = getEntityIdFromKeys([
69-
BigInt(account.address),
70-
]) as Entity;
69+
const move = async (
70+
account: AccountInterface,
71+
direction: Direction
72+
) => {
73+
const entityId = getEntityIdFromKeys([
74+
BigInt(account.address),
75+
]) as Entity;
7176

72-
const positionId = uuid();
73-
Position.addOverride(positionId, {
74-
entity: entityId,
75-
value: {
76-
player: BigInt(entityId),
77-
vec: updatePositionWithDirection(
78-
direction,
79-
getComponentValue(Position, entityId) as any
80-
).vec,
81-
},
82-
});
83-
84-
const movesId = uuid();
85-
Moves.addOverride(movesId, {
86-
entity: entityId,
87-
value: {
88-
player: BigInt(entityId),
89-
remaining:
90-
(getComponentValue(Moves, entityId)?.remaining || 0) - 1,
91-
},
92-
});
77+
console.log(direction, getComponentValue(Position, entityId));
78+
const positionId = uuid();
79+
Position.addOverride(positionId, {
80+
entity: entityId,
81+
value: {
82+
player: BigInt(entityId),
83+
vec: updatePositionWithDirection(
84+
direction,
85+
getComponentValue(Position, entityId) as any
86+
).vec,
87+
},
88+
});
9389

94-
try {
95-
const { transaction_hash } = await client.actions.move({
96-
account,
97-
direction,
90+
const movesId = uuid();
91+
Moves.addOverride(movesId, {
92+
entity: entityId,
93+
value: {
94+
player: BigInt(entityId),
95+
remaining:
96+
(getComponentValue(Moves, entityId)?.remaining || 0) -
97+
1,
98+
},
9899
});
99100

100-
setComponentsFromEvents(
101-
contractComponents,
102-
getEvents(
103-
await account.waitForTransaction(transaction_hash, {
104-
retryInterval: 100,
105-
})
106-
)
107-
);
108-
} catch (e) {
109-
console.log(e);
110-
Position.removeOverride(positionId);
111-
Moves.removeOverride(movesId);
112-
} finally {
113-
Position.removeOverride(positionId);
114-
Moves.removeOverride(movesId);
115-
}
116-
};
101+
try {
102+
const { transaction_hash } = await client.actions.move({
103+
account,
104+
direction,
105+
});
106+
107+
setComponentsFromEvents(
108+
contractComponents,
109+
getEvents(
110+
await account.waitForTransaction(transaction_hash, {
111+
retryInterval: 100,
112+
})
113+
)
114+
);
115+
} catch (e) {
116+
console.error(e);
117+
Position.removeOverride(positionId);
118+
Moves.removeOverride(movesId);
119+
} finally {
120+
Position.removeOverride(positionId);
121+
Moves.removeOverride(movesId);
122+
}
123+
};
124+
return { spawn, move };
125+
}
117126

118127
return {
119-
spawn,
120-
move,
128+
actions: actions(),
121129
};
122130
}

examples/vanilla/phaser/src/dojo/utils.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ export function updatePositionWithDirection(
99
direction: Direction,
1010
value: { vec: { x: number; y: number } }
1111
) {
12+
console.log(value);
13+
debugger;
1214
switch (direction) {
1315
case Direction.Left:
1416
value.vec.x--;

examples/vanilla/phaser/src/scenes/scene-main.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Scene } from "phaser";
22
import { Chunk } from "../entities";
33
import { IDojo } from "../dojo/setup";
4+
import { Direction } from "../dojo/utils";
45
export default class SceneMain extends Scene {
56
dojo: IDojo;
67
chunkSize: number;
@@ -69,6 +70,8 @@ export default class SceneMain extends Scene {
6970
this.keyD = this.input.keyboard.addKey(
7071
Phaser.Input.Keyboard.KeyCodes.D
7172
);
73+
74+
this.dojo.systemCalls.spawn(this.dojo.account);
7275
}
7376

7477
getChunk(x: number, y: number) {
@@ -138,21 +141,25 @@ export default class SceneMain extends Scene {
138141
if (null !== this.keyW && this.keyW.isDown) {
139142
this.followPoint.y -= this.cameraSpeed;
140143
this.cameras.main.centerOn(this.followPoint.x, this.followPoint.y);
144+
this.dojo.systemCalls.move(this.dojo.account, Direction.Up);
141145
return;
142146
}
143147
if (null !== this.keyS && this.keyS.isDown) {
144148
this.followPoint.y += this.cameraSpeed;
145149
this.cameras.main.centerOn(this.followPoint.x, this.followPoint.y);
150+
this.dojo.systemCalls.move(this.dojo.account, Direction.Down);
146151
return;
147152
}
148153
if (null !== this.keyA && this.keyA.isDown) {
149154
this.followPoint.x -= this.cameraSpeed;
150155
this.cameras.main.centerOn(this.followPoint.x, this.followPoint.y);
156+
this.dojo.systemCalls.move(this.dojo.account, Direction.Left);
151157
return;
152158
}
153159
if (null !== this.keyD && this.keyD.isDown) {
154160
this.followPoint.x += this.cameraSpeed;
155161
this.cameras.main.centerOn(this.followPoint.x, this.followPoint.y);
162+
this.dojo.systemCalls.move(this.dojo.account, Direction.Right);
156163
return;
157164
}
158165

0 commit comments

Comments
 (0)