generated from latticexyz/mud-template-react
-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathcreateSystemCalls.ts
More file actions
160 lines (136 loc) · 4.14 KB
/
createSystemCalls.ts
File metadata and controls
160 lines (136 loc) · 4.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import { Has, HasValue, getComponentValue, runQuery } from "@latticexyz/recs";
import { singletonEntity } from "@latticexyz/store-sync/recs";
import { uuid } from "@latticexyz/utils";
import { ClientComponents } from "./createClientComponents";
import { type Network, WorldContractWrite } from "./setupNetwork"
import { Direction } from "../direction";
import { MonsterCatchResult } from "../monsterCatchResult";
export type SystemCalls = ReturnType<typeof createSystemCalls>;
export function createSystemCalls(
{ playerEntity, worldContract: _worldContract, waitForTransaction }: Network,
{
Encounter,
MapConfig,
MonsterCatchAttempt,
Obstruction,
Player,
Position,
}: ClientComponents
) {
const worldContract = _worldContract as WorldContractWrite;
const wrapPosition = (x: number, y: number) => {
const mapConfig = getComponentValue(MapConfig, singletonEntity);
if (!mapConfig) {
throw new Error("mapConfig no yet loaded or initialized");
}
return [
(x + mapConfig.width) % mapConfig.width,
(y + mapConfig.height) % mapConfig.height,
];
};
const isObstructed = (x: number, y: number) => {
return runQuery([Has(Obstruction), HasValue(Position, { x, y })]).size > 0;
};
const move = async (direction: Direction) => {
if (!playerEntity) {
throw new Error("no player");
}
const position = getComponentValue(Position, playerEntity);
if (!position) {
console.warn("cannot move without a player position, not yet spawned?");
return;
}
const inEncounter = !!getComponentValue(Encounter, playerEntity);
if (inEncounter) {
console.warn("cannot move while in encounter");
return;
}
let { x: inputX, y: inputY } = position;
if (direction === Direction.North) {
inputY -= 1;
} else if (direction === Direction.East) {
inputX += 1;
} else if (direction === Direction.South) {
inputY += 1;
} else if (direction === Direction.West) {
inputX -= 1;
}
const [x, y] = wrapPosition(inputX, inputY);
if (isObstructed(x, y)) {
console.warn("cannot move to obstructed space");
return;
}
const positionId = uuid();
Position.addOverride(positionId, {
entity: playerEntity,
value: { x, y },
});
try {
const tx = await worldContract.write.move([direction]);
await waitForTransaction(tx);
} finally {
Position.removeOverride(positionId);
}
};
const spawn = async (inputX: number, inputY: number) => {
if (!playerEntity) {
throw new Error("no player");
}
const canSpawn = getComponentValue(Player, playerEntity)?.value !== true;
if (!canSpawn) {
throw new Error("already spawned");
}
const [x, y] = wrapPosition(inputX, inputY);
if (isObstructed(x, y)) {
console.warn("cannot spawn on obstructed space");
return;
}
const positionId = uuid();
Position.addOverride(positionId, {
entity: playerEntity,
value: { x, y },
});
const playerId = uuid();
Player.addOverride(playerId, {
entity: playerEntity,
value: { value: true },
});
try {
const tx = await worldContract.write.spawn([x, y]);
await waitForTransaction(tx);
} finally {
Position.removeOverride(positionId);
Player.removeOverride(playerId);
}
};
const throwBall = async () => {
const player = playerEntity;
if (!player) {
throw new Error("no player");
}
const encounter = getComponentValue(Encounter, player);
if (!encounter) {
throw new Error("no encounter");
}
const tx = await worldContract.write.throwBall();
await waitForTransaction(tx);
const catchAttempt = getComponentValue(MonsterCatchAttempt, player);
if (!catchAttempt) {
throw new Error("no catch attempt found");
}
return catchAttempt.result as MonsterCatchResult;
};
const fleeEncounter = async () => {
if (!playerEntity) {
throw new Error("no player");
}
const tx = await worldContract.write.flee();
await waitForTransaction(tx);
};
return {
move,
spawn,
throwBall,
fleeEncounter,
};
}