Map and terrain
In this section, we will accomplish the following:
- Configure the map as a singleton table and initialize it in the client.
- Add terrain (tall grass and boulders) to the map.
- Prevent movement into boulders.
Use a singleton table for the map config
At this point we have the concept of a 2D grid but there is no official "map" and there is no terrain. To do so in the ECS model we will now implement the map as a singleton table and initialize it in the client. Singleton tables are tables with a single record. This kind of table is useful for storing top-level state.
Go ahead and add the MapConfig
as a singleton table in the MUD config (mud.config.ts
).
import { defineWorld } from "@latticexyz/world";
export default defineWorld({
enums: {
Direction: ["North", "East", "South", "West"],
TerrainType: ["None", "TallGrass", "Boulder"],
},
tables: {
MapConfig: {
schema: {
width: "uint32",
height: "uint32",
terrain: "bytes",
},
key: [],
codegen: {
dataStruct: false,
},
},
Movable: "bool",
Player: "bool",
Position: {
schema: {
id: "bytes32",
x: "int32",
y: "int32",
},
key: ["id"],
codegen: {
dataStruct: false,
},
},
},
});
Explanation
Similar to the included Direction
enum, we add one for terrain types.
We also add a singleton table (a table with only one value) to hold our map configuration by using an empty key
. The terrain is a bytes
blob for ease of storage, which we'll encode and store in the next step.
Add terrain
There are two features we have yet to implement—boulders to obstruct movement and tall grass to generate encounters. Before we set up the components and systems necessary to do so, we must first add the terrain itself and render it in the client.
First, we’ll use the PostDeploy.s.sol
to initialize the terrain in the MapConfig
.
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.24;
import { Script } from "forge-std/Script.sol";
import { console } from "forge-std/console.sol";
import { StoreSwitch } from "@latticexyz/store/src/StoreSwitch.sol";
import { IWorld } from "../src/codegen/world/IWorld.sol";
import { MapConfig, Position } from "../src/codegen/index.sol";
import { TerrainType } from "../src/codegen/common.sol";
contract PostDeploy is Script {
function run(address worldAddress) external {
// Specify a store so that you can use tables directly in PostDeploy
StoreSwitch.setStoreAddress(worldAddress);
// Load the private key from the `PRIVATE_KEY` environment variable (in .env)
uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY");
vm.startBroadcast(deployerPrivateKey);
TerrainType O = TerrainType.None;
TerrainType T = TerrainType.TallGrass;
TerrainType B = TerrainType.Boulder;
TerrainType[20][20] memory map = [
[O, O, O, O, O, O, T, O, O, O, O, O, O, O, O, O, O, O, O, O],
[O, O, T, O, O, O, O, O, T, O, O, O, O, B, O, O, O, O, O, O],
[O, T, T, T, T, O, O, O, O, O, O, O, O, O, O, T, T, O, O, O],
[O, O, T, T, T, T, O, O, O, O, B, O, O, O, O, O, T, O, O, O],
[O, O, O, O, T, T, O, O, O, O, O, O, O, O, O, O, O, T, O, O],
[O, O, O, B, B, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O],
[O, T, O, O, O, B, B, O, O, O, O, T, O, O, O, O, O, B, O, O],
[O, O, T, T, O, O, O, O, O, T, O, B, O, O, T, O, B, O, O, O],
[O, O, T, O, O, O, O, T, T, T, O, B, B, O, O, O, O, O, O, O],
[O, O, O, O, O, O, O, T, T, T, O, B, T, O, T, T, O, O, O, O],
[O, B, O, O, O, B, O, O, T, T, O, B, O, O, T, T, O, O, O, O],
[O, O, B, O, O, O, T, O, T, T, O, O, B, T, T, T, O, O, O, O],
[O, O, B, B, O, O, O, O, T, O, O, O, B, O, T, O, O, O, O, O],
[O, O, O, B, B, O, O, O, O, O, O, O, O, B, O, T, O, O, O, O],
[O, O, O, O, B, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O],
[O, O, O, O, O, O, O, O, O, O, B, B, O, O, T, O, O, O, O, O],
[O, O, O, O, T, O, O, O, T, B, O, O, O, T, T, O, B, O, O, O],
[O, O, O, T, O, T, T, T, O, O, O, O, O, T, O, O, O, O, O, O],
[O, O, O, T, T, T, T, O, O, O, O, T, O, O, O, T, O, O, O, O],
[O, O, O, O, O, T, O, O, O, O, O, O, O, O, O, O, O, O, O, O]
];
uint32 height = uint32(map.length);
uint32 width = uint32(map[0].length);
bytes memory terrain = new bytes(width * height);
for (uint32 y = 0; y < height; y++) {
for (uint32 x = 0; x < width; x++) {
TerrainType terrainType = map[y][x];
if (terrainType == TerrainType.None) continue;
terrain[(y * width) + x] = bytes1(uint8(terrainType));
}
}
MapConfig.set(width, height, terrain);
vm.stopBroadcast();
}
}
Note that PostDeploy.s.sol
will only run once per world, so you will need to restart your pnpm run dev
script to redeploy the world.
Now let’s render the terrain in the client via GameBoard.tsx
.
import { useComponentValue } from "@latticexyz/react";
import { GameMap } from "./GameMap";
import { useMUD } from "./MUDContext";
import { useKeyboardMovement } from "./useKeyboardMovement";
import { hexToArray } from "@latticexyz/utils";
import { TerrainType, terrainTypes } from "./terrainTypes";
import { singletonEntity } from "@latticexyz/store-sync/recs";
export const GameBoard = () => {
useKeyboardMovement();
const {
components: { MapConfig, Player, Position },
network: { playerEntity },
systemCalls: { spawn },
} = useMUD();
const canSpawn = useComponentValue(Player, playerEntity)?.value !== true;
const playerPosition = useComponentValue(Position, playerEntity);
const player =
playerEntity && playerPosition
? {
x: playerPosition.x,
y: playerPosition.y,
emoji: "🤠",
entity: playerEntity,
}
: null;
const mapConfig = useComponentValue(MapConfig, singletonEntity);
if (mapConfig == null) {
throw new Error("map config not set or not ready, only use this hook after loading state === LIVE");
}
const { width, height, terrain: terrainData } = mapConfig;
const terrain = Array.from(hexToArray(terrainData)).map((value, index) => {
const { emoji } = value in TerrainType ? terrainTypes[value as TerrainType] : { emoji: "" };
return {
x: index % width,
y: Math.floor(index / width),
emoji,
};
});
return (
<GameMap
width={width}
height={height}
terrain={terrain}
onTileClick={canSpawn ? spawn : undefined}
players={player ? [player] : []}
/>
);
};
Turn boulders into obstructions
Although boulders are rendering on the map at this point, they do not yet prevent movement in the way we want them to.
To accomplish this we will add an Obstruction
table and query for entities with that table in our move method.
Let's start by adding the table to the MUD config:
import { defineWorld } from "@latticexyz/world";
export default defineWorld({
enums: {
Direction: ["North", "East", "South", "West"],
TerrainType: ["None", "TallGrass", "Boulder"],
},
tables: {
MapConfig: {
schema: {
width: "uint32",
height: "uint32",
terrain: "bytes",
},
key: [],
codegen: {
dataStruct: false,
},
},
Movable: "bool",
Obstruction: "bool",
Player: "bool",
Position: {
schema: {
id: "bytes32",
x: "int32",
y: "int32",
},
key: ["id"],
codegen: {
dataStruct: false,
},
},
},
});
We'll then make sure PostDeploy.s.sol
initializes the boulders properly (with the obstruction and position component) so we can query them later.
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.24;
import { Script } from "forge-std/Script.sol";
import { console } from "forge-std/console.sol";
import { StoreSwitch } from "@latticexyz/store/src/StoreSwitch.sol";
import { IWorld } from "../src/codegen/world/IWorld.sol";
import { MapConfig, Obstruction, Position } from "../src/codegen/index.sol";
import { TerrainType } from "../src/codegen/common.sol";
import { positionToEntityKey } from "../src/positionToEntityKey.sol";
contract PostDeploy is Script {
function run(address worldAddress) external {
// Specify a store so that you can use tables directly in PostDeploy
StoreSwitch.setStoreAddress(worldAddress);
// Load the private key from the `PRIVATE_KEY` environment variable (in .env)
uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY");
vm.startBroadcast(deployerPrivateKey);
TerrainType O = TerrainType.None;
TerrainType T = TerrainType.TallGrass;
TerrainType B = TerrainType.Boulder;
TerrainType[20][20] memory map = [
[O, O, O, O, O, O, T, O, O, O, O, O, O, O, O, O, O, O, O, O],
[O, O, T, O, O, O, O, O, T, O, O, O, O, B, O, O, O, O, O, O],
[O, T, T, T, T, O, O, O, O, O, O, O, O, O, O, T, T, O, O, O],
[O, O, T, T, T, T, O, O, O, O, B, O, O, O, O, O, T, O, O, O],
[O, O, O, O, T, T, O, O, O, O, O, O, O, O, O, O, O, T, O, O],
[O, O, O, B, B, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O],
[O, T, O, O, O, B, B, O, O, O, O, T, O, O, O, O, O, B, O, O],
[O, O, T, T, O, O, O, O, O, T, O, B, O, O, T, O, B, O, O, O],
[O, O, T, O, O, O, O, T, T, T, O, B, B, O, O, O, O, O, O, O],
[O, O, O, O, O, O, O, T, T, T, O, B, T, O, T, T, O, O, O, O],
[O, B, O, O, O, B, O, O, T, T, O, B, O, O, T, T, O, O, O, O],
[O, O, B, O, O, O, T, O, T, T, O, O, B, T, T, T, O, O, O, O],
[O, O, B, B, O, O, O, O, T, O, O, O, B, O, T, O, O, O, O, O],
[O, O, O, B, B, O, O, O, O, O, O, O, O, B, O, T, O, O, O, O],
[O, O, O, O, B, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O],
[O, O, O, O, O, O, O, O, O, O, B, B, O, O, T, O, O, O, O, O],
[O, O, O, O, T, O, O, O, T, B, O, O, O, T, T, O, B, O, O, O],
[O, O, O, T, O, T, T, T, O, O, O, O, O, T, O, O, O, O, O, O],
[O, O, O, T, T, T, T, O, O, O, O, T, O, O, O, T, O, O, O, O],
[O, O, O, O, O, T, O, O, O, O, O, O, O, O, O, O, O, O, O, O]
];
uint32 height = uint32(map.length);
uint32 width = uint32(map[0].length);
bytes memory terrain = new bytes(width * height);
for (uint32 y = 0; y < height; y++) {
for (uint32 x = 0; x < width; x++) {
TerrainType terrainType = map[y][x];
if (terrainType == TerrainType.None) continue;
terrain[(y * width) + x] = bytes1(uint8(terrainType));
bytes32 entity = positionToEntityKey(int32(x), int32(y));
if (terrainType == TerrainType.Boulder) {
Position.set(entity, int32(x), int32(y));
Obstruction.set(entity, true);
}
}
}
MapConfig.set(width, height, terrain);
vm.stopBroadcast();
}
}
Explanation
Because boulders affect game play, they are entities. The changes here implement that fact.
import { positionToEntityKey } from "../src/positionToEntityKey.sol";
This function (opens in a new tab) gives us an entity ID for a location that is consistent, and different for every location.
bytes32 entity = positionToEntityKey(x, y);
if (terrainType == TerrainType.Boulder) {
Position.set(world, entity, x, y);
Obstruction.set(world, entity, true);
}
If the terrain is a boulder, specify the location and that it is an Obstruction
.
Then let's use this table in the move
and spawn
methods in MapSystem.sol
.
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.24;
import { System } from "@latticexyz/world/src/System.sol";
import { Movable, Obstruction, Player, Position } from "../codegen/index.sol";
import { Direction } from "../codegen/common.sol";
import { addressToEntityKey } from "../addressToEntityKey.sol";
import { positionToEntityKey } from "../positionToEntityKey.sol";
contract MapSystem is System {
function spawn(int32 x, int32 y) public {
bytes32 player = addressToEntityKey(address(_msgSender()));
require(!Player.get(player), "already spawned");
bytes32 position = positionToEntityKey(x, y);
require(!Obstruction.get(position), "this space is obstructed");
Player.set(player, true);
Position.set(player, x, y);
Movable.set(player, true);
}
function move(Direction direction) public {
bytes32 player = addressToEntityKey(_msgSender());
require(Movable.get(player), "cannot move");
(int32 x, int32 y) = Position.get(player);
if (direction == Direction.North) {
y -= 1;
} else if (direction == Direction.East) {
x += 1;
} else if (direction == Direction.South) {
y += 1;
} else if (direction == Direction.West) {
x -= 1;
}
bytes32 position = positionToEntityKey(x, y);
require(!Obstruction.get(position), "this space is obstructed");
Position.set(player, x, y);
}
}
Lastly, let's ensure these interactions are optimistically rendering in the createSystemCalls.ts
method.
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 { SetupNetworkResult } from "./setupNetwork";
import { Direction } from "../direction";
export type SystemCalls = ReturnType<typeof createSystemCalls>;
export function createSystemCalls(
{ playerEntity, worldContract, waitForTransaction }: SetupNetworkResult,
{ Obstruction, Player, Position }: ClientComponents,
) {
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;
}
let { x, y } = position;
if (direction === Direction.North) {
y -= 1;
} else if (direction === Direction.East) {
x += 1;
} else if (direction === Direction.South) {
y += 1;
} else if (direction === Direction.West) {
x -= 1;
}
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 (x: number, y: number) => {
if (!playerEntity) {
throw new Error("no player");
}
const canSpawn = getComponentValue(Player, playerEntity)?.value !== true;
if (!canSpawn) {
throw new Error("already spawned");
}
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 () => {
// TODO
return null as any;
};
const fleeEncounter = async () => {
// TODO
return null as any;
};
return {
move,
spawn,
throwBall,
fleeEncounter,
};
}
Explanation
const isObstructed = (x: number, y: number) => {
return runQuery([Has(Obstruction), HasValue(Position, { x, y })]).size > 0;
};
Here we run a query that looks for all the entities that have Obstruction
set to true
, and whose entity ID is the one for the terrain at (x,y)
.
If there are any then this space is obstructed.
This function is not the most efficient possibility.
In theory we could have created a TypeScript version of positionToEntityKey
(opens in a new tab) and used that to check if the specific entity for the location where the player wishes to go is obstructed.
While this could have been more efficient, it would have made future development harder.
In the future we might have mobile obstructions, for example trolls that walk around the map randomly.
If we use a query, then as soon as we provide those entites with Obstruction
and Position
they'll start obstructing the player.
Processing power on the client is usually cheap and abundant, because most client devices spend most of their time waiting for input to act upon. The expensive resource we need to optimize for is processing on the blockchain, a.k.a. gas (opens in a new tab).
Now if you try moving onto a tile with a boulder you’ll see that you can’t!
Wrap map boundary
Currently, players can move off of the bounds of the map.
We'll address this by updating the spawn
and move
methods in MapSystem.sol
to wrap the player coordinate around the map size.
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.24;
import { System } from "@latticexyz/world/src/System.sol";
import { MapConfig, Movable, Obstruction, Player, Position } from "../codegen/index.sol";
import { Direction } from "../codegen/common.sol";
import { addressToEntityKey } from "../addressToEntityKey.sol";
import { positionToEntityKey } from "../positionToEntityKey.sol";
contract MapSystem is System {
function spawn(int32 x, int32 y) public {
bytes32 player = addressToEntityKey(address(_msgSender()));
require(!Player.get(player), "already spawned");
// Constrain position to map size, wrapping around if necessary
(uint32 width, uint32 height, ) = MapConfig.get();
x = (x + int32(width)) % int32(width);
y = (y + int32(height)) % int32(height);
bytes32 position = positionToEntityKey(x, y);
require(!Obstruction.get(position), "this space is obstructed");
Player.set(player, true);
Position.set(player, x, y);
Movable.set(player, true);
}
function move(Direction direction) public {
bytes32 player = addressToEntityKey(_msgSender());
require(Movable.get(player), "cannot move");
(int32 x, int32 y) = Position.get(player);
if (direction == Direction.North) {
y -= 1;
} else if (direction == Direction.East) {
x += 1;
} else if (direction == Direction.South) {
y += 1;
} else if (direction == Direction.West) {
x -= 1;
}
// Constrain position to map size, wrapping around if necessary
(uint32 width, uint32 height, ) = MapConfig.get();
x = (x + int32(width)) % int32(width);
y = (y + int32(height)) % int32(height);
bytes32 position = positionToEntityKey(x, y);
require(!Obstruction.get(position), "this space is obstructed");
Position.set(player, x, y);
}
}
We'll also need to update our client-side optimistic rendering.
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 { SetupNetworkResult } from "./setupNetwork";
import { Direction } from "../direction";
export type SystemCalls = ReturnType<typeof createSystemCalls>;
export function createSystemCalls(
{ playerEntity, worldContract, waitForTransaction }: SetupNetworkResult,
{ MapConfig, Obstruction, Player, Position }: ClientComponents,
) {
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;
}
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 () => {
// TODO
return null as any;
};
const fleeEncounter = async () => {
// TODO
return null as any;
};
return {
move,
spawn,
throwBall,
fleeEncounter,
};
}
You can run this command to update all the files to this point in the game's development.
git reset --hard origin/step-2