Skip to content

Instantly share code, notes, and snippets.

@Turupawn
Created December 2, 2025 02:36
Show Gist options
  • Select an option

  • Save Turupawn/b12931c42d69b92856f7bd0cd2c92d71 to your computer and use it in GitHub Desktop.

Select an option

Save Turupawn/b12931c42d69b92856f7bd0cd2c92d71 to your computer and use it in GitHub Desktop.
import { defineWorld } from "@latticexyz/world";
export default defineWorld({
namespace: "app",
enums: {
Direction: ["North", "East", "South", "West"],
},
tables: {
Position: {
schema: { player: "address", x: "int32", y: "int32" },
key: ["player"],
},
},
});
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.24;
/* Autogenerated file. Do not edit manually. */
// Import store internals
import { IStore } from "@latticexyz/store/src/IStore.sol";
import { StoreSwitch } from "@latticexyz/store/src/StoreSwitch.sol";
import { StoreCore } from "@latticexyz/store/src/StoreCore.sol";
import { Bytes } from "@latticexyz/store/src/Bytes.sol";
import { Memory } from "@latticexyz/store/src/Memory.sol";
import { SliceLib } from "@latticexyz/store/src/Slice.sol";
import { EncodeArray } from "@latticexyz/store/src/tightcoder/EncodeArray.sol";
import { FieldLayout } from "@latticexyz/store/src/FieldLayout.sol";
import { Schema } from "@latticexyz/store/src/Schema.sol";
import { EncodedLengths, EncodedLengthsLib } from "@latticexyz/store/src/EncodedLengths.sol";
import { ResourceId } from "@latticexyz/store/src/ResourceId.sol";
struct PositionData {
int32 x;
int32 y;
}
library Position {
// Hex below is the result of `WorldResourceIdLib.encode({ namespace: "app", name: "Position", typeId: RESOURCE_TABLE });`
ResourceId constant _tableId = ResourceId.wrap(0x74626170700000000000000000000000506f736974696f6e0000000000000000);
FieldLayout constant _fieldLayout =
FieldLayout.wrap(0x0008020004040000000000000000000000000000000000000000000000000000);
// Hex-encoded key schema of (address)
Schema constant _keySchema = Schema.wrap(0x0014010061000000000000000000000000000000000000000000000000000000);
// Hex-encoded value schema of (int32, int32)
Schema constant _valueSchema = Schema.wrap(0x0008020023230000000000000000000000000000000000000000000000000000);
/**
* @notice Get the table's key field names.
* @return keyNames An array of strings with the names of key fields.
*/
function getKeyNames() internal pure returns (string[] memory keyNames) {
keyNames = new string[](1);
keyNames[0] = "player";
}
/**
* @notice Get the table's value field names.
* @return fieldNames An array of strings with the names of value fields.
*/
function getFieldNames() internal pure returns (string[] memory fieldNames) {
fieldNames = new string[](2);
fieldNames[0] = "x";
fieldNames[1] = "y";
}
/**
* @notice Register the table with its config.
*/
function register() internal {
StoreSwitch.registerTable(_tableId, _fieldLayout, _keySchema, _valueSchema, getKeyNames(), getFieldNames());
}
/**
* @notice Register the table with its config.
*/
function _register() internal {
StoreCore.registerTable(_tableId, _fieldLayout, _keySchema, _valueSchema, getKeyNames(), getFieldNames());
}
/**
* @notice Get x.
*/
function getX(address player) internal view returns (int32 x) {
bytes32[] memory _keyTuple = new bytes32[](1);
_keyTuple[0] = bytes32(uint256(uint160(player)));
bytes32 _blob = StoreSwitch.getStaticField(_tableId, _keyTuple, 0, _fieldLayout);
return (int32(uint32(bytes4(_blob))));
}
/**
* @notice Get x.
*/
function _getX(address player) internal view returns (int32 x) {
bytes32[] memory _keyTuple = new bytes32[](1);
_keyTuple[0] = bytes32(uint256(uint160(player)));
bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 0, _fieldLayout);
return (int32(uint32(bytes4(_blob))));
}
/**
* @notice Set x.
*/
function setX(address player, int32 x) internal {
bytes32[] memory _keyTuple = new bytes32[](1);
_keyTuple[0] = bytes32(uint256(uint160(player)));
StoreSwitch.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((x)), _fieldLayout);
}
/**
* @notice Set x.
*/
function _setX(address player, int32 x) internal {
bytes32[] memory _keyTuple = new bytes32[](1);
_keyTuple[0] = bytes32(uint256(uint160(player)));
StoreCore.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((x)), _fieldLayout);
}
/**
* @notice Get y.
*/
function getY(address player) internal view returns (int32 y) {
bytes32[] memory _keyTuple = new bytes32[](1);
_keyTuple[0] = bytes32(uint256(uint160(player)));
bytes32 _blob = StoreSwitch.getStaticField(_tableId, _keyTuple, 1, _fieldLayout);
return (int32(uint32(bytes4(_blob))));
}
/**
* @notice Get y.
*/
function _getY(address player) internal view returns (int32 y) {
bytes32[] memory _keyTuple = new bytes32[](1);
_keyTuple[0] = bytes32(uint256(uint160(player)));
bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 1, _fieldLayout);
return (int32(uint32(bytes4(_blob))));
}
/**
* @notice Set y.
*/
function setY(address player, int32 y) internal {
bytes32[] memory _keyTuple = new bytes32[](1);
_keyTuple[0] = bytes32(uint256(uint160(player)));
StoreSwitch.setStaticField(_tableId, _keyTuple, 1, abi.encodePacked((y)), _fieldLayout);
}
/**
* @notice Set y.
*/
function _setY(address player, int32 y) internal {
bytes32[] memory _keyTuple = new bytes32[](1);
_keyTuple[0] = bytes32(uint256(uint160(player)));
StoreCore.setStaticField(_tableId, _keyTuple, 1, abi.encodePacked((y)), _fieldLayout);
}
/**
* @notice Get the full data.
*/
function get(address player) internal view returns (PositionData memory _table) {
bytes32[] memory _keyTuple = new bytes32[](1);
_keyTuple[0] = bytes32(uint256(uint160(player)));
(bytes memory _staticData, EncodedLengths _encodedLengths, bytes memory _dynamicData) = StoreSwitch.getRecord(
_tableId,
_keyTuple,
_fieldLayout
);
return decode(_staticData, _encodedLengths, _dynamicData);
}
/**
* @notice Get the full data.
*/
function _get(address player) internal view returns (PositionData memory _table) {
bytes32[] memory _keyTuple = new bytes32[](1);
_keyTuple[0] = bytes32(uint256(uint160(player)));
(bytes memory _staticData, EncodedLengths _encodedLengths, bytes memory _dynamicData) = StoreCore.getRecord(
_tableId,
_keyTuple,
_fieldLayout
);
return decode(_staticData, _encodedLengths, _dynamicData);
}
/**
* @notice Set the full data using individual values.
*/
function set(address player, int32 x, int32 y) internal {
bytes memory _staticData = encodeStatic(x, y);
EncodedLengths _encodedLengths;
bytes memory _dynamicData;
bytes32[] memory _keyTuple = new bytes32[](1);
_keyTuple[0] = bytes32(uint256(uint160(player)));
StoreSwitch.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData);
}
/**
* @notice Set the full data using individual values.
*/
function _set(address player, int32 x, int32 y) internal {
bytes memory _staticData = encodeStatic(x, y);
EncodedLengths _encodedLengths;
bytes memory _dynamicData;
bytes32[] memory _keyTuple = new bytes32[](1);
_keyTuple[0] = bytes32(uint256(uint160(player)));
StoreCore.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, _fieldLayout);
}
/**
* @notice Set the full data using the data struct.
*/
function set(address player, PositionData memory _table) internal {
bytes memory _staticData = encodeStatic(_table.x, _table.y);
EncodedLengths _encodedLengths;
bytes memory _dynamicData;
bytes32[] memory _keyTuple = new bytes32[](1);
_keyTuple[0] = bytes32(uint256(uint160(player)));
StoreSwitch.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData);
}
/**
* @notice Set the full data using the data struct.
*/
function _set(address player, PositionData memory _table) internal {
bytes memory _staticData = encodeStatic(_table.x, _table.y);
EncodedLengths _encodedLengths;
bytes memory _dynamicData;
bytes32[] memory _keyTuple = new bytes32[](1);
_keyTuple[0] = bytes32(uint256(uint160(player)));
StoreCore.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, _fieldLayout);
}
/**
* @notice Decode the tightly packed blob of static data using this table's field layout.
*/
function decodeStatic(bytes memory _blob) internal pure returns (int32 x, int32 y) {
x = (int32(uint32(Bytes.getBytes4(_blob, 0))));
y = (int32(uint32(Bytes.getBytes4(_blob, 4))));
}
/**
* @notice Decode the tightly packed blobs using this table's field layout.
* @param _staticData Tightly packed static fields.
*
*
*/
function decode(
bytes memory _staticData,
EncodedLengths,
bytes memory
) internal pure returns (PositionData memory _table) {
(_table.x, _table.y) = decodeStatic(_staticData);
}
/**
* @notice Delete all data for given keys.
*/
function deleteRecord(address player) internal {
bytes32[] memory _keyTuple = new bytes32[](1);
_keyTuple[0] = bytes32(uint256(uint160(player)));
StoreSwitch.deleteRecord(_tableId, _keyTuple);
}
/**
* @notice Delete all data for given keys.
*/
function _deleteRecord(address player) internal {
bytes32[] memory _keyTuple = new bytes32[](1);
_keyTuple[0] = bytes32(uint256(uint160(player)));
StoreCore.deleteRecord(_tableId, _keyTuple, _fieldLayout);
}
/**
* @notice Tightly pack static (fixed length) data using this table's schema.
* @return The static data, encoded into a sequence of bytes.
*/
function encodeStatic(int32 x, int32 y) internal pure returns (bytes memory) {
return abi.encodePacked(x, y);
}
/**
* @notice Encode all of a record's fields.
* @return The static (fixed length) data, encoded into a sequence of bytes.
* @return The lengths of the dynamic fields (packed into a single bytes32 value).
* @return The dynamic (variable length) data, encoded into a sequence of bytes.
*/
function encode(int32 x, int32 y) internal pure returns (bytes memory, EncodedLengths, bytes memory) {
bytes memory _staticData = encodeStatic(x, y);
EncodedLengths _encodedLengths;
bytes memory _dynamicData;
return (_staticData, _encodedLengths, _dynamicData);
}
/**
* @notice Encode keys as a bytes32 array using this table's field layout.
*/
function encodeKeyTuple(address player) internal pure returns (bytes32[] memory) {
bytes32[] memory _keyTuple = new bytes32[](1);
_keyTuple[0] = bytes32(uint256(uint160(player)));
return _keyTuple;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment