Skip to main content

Cheatsheet

Hooks tell when a code should be executed, there are different hooks which are useful depending on the situation.


Create = The code will be executed if the player enters a room

Destroy = The code will be executed if the asset is destroyed

Interact = The code will be executed if the player interacts with the object

Over = The code will be executed if the player is over the object

Next = The code will be executed if the player is next to the object

Functions

imGameMaster

// checks if the current player is the game master
imGameMaster(): boolean

Messages

displaySpeechBubble

// displays a speech bubble on the map position (default: current asset name)
displaySpeechBubble(text: string, name?: string, targetName?: string)

displayMessage

// displays a common message box ( default: asset image)
displayMessage(text: string, img?: string)

Movement / Warping

movePlayerTo (Player)

// moves the player to the given position
movePlayerTo(x: number, y: number, isRelocate?: boolean, playerId?: string)

moveTo (Asset)

// moves the asset to the given x,y position, if isRelocate is true, the asset will be relocated to the position, otherwise it will be moved to the position (DEFAULT : current asset)
moveTo(x, y, isRelocate? :booleab, assetId?:string)

warp

// warps the current player to the given world and position
warp(world: string, x: number, y: number)

State handling

setWorldState

// set the world state, can be used to save data in the world, must be a json object
setWorldState(data: any)

//example :
setState({
raining:true // if its raining
})

updateWorldState

// update the world state with a key and value like "goatCount", 10
updateWorldState(keyname: string, value: any)

getWorldState

// get the world state as object
getWorldState():Object

setState

// set the temporary world state, must be a json object, will be resetted after the player left the world
setState(data: any)

//example :
setState({
isImposter:true // if xy is imposter
})

updateState

// update the temporary world state
updateState(property: string, value: any)

getState

// get the temporary world state
getState(): Object

Quest

addQuest

QuestData[]

// adds a quest to the player's quest log (default: current player)
addQuest(questData: QuestData, playerId?: string)

removeQuest

// removes a quest from the player's quest log (default: current player)
removeQuest(questId: string, playerId?: string)

hasQuest

// checks if the player has a specific quest in their quest log (default: current player)
hasQuest(questId: string, playerId: string = player.id): boolean

Items

addItem

AssetData[]

// adds an item to the player's inventory (default: current player)
addItem(assetData: AssetData, playerId?: string)

removeItem

// removes an item from the player's inventory (default: current player)
removeItem(itemId: string, playerId?: string)

hasItem

// checks if the player has a specific item in their inventory (default: current player)
hasItem(assetId: string, playerId?: string): boolean

Other functions

sleep

// waits for X seconds
sleep(ms: number): Promise<void>

setCanBuild

// grants the player build permissions
setCanBuild(canBuild: boolean)

playSound

// plays a sound, with absolute https path
playSound(path:string, loop :boolean, volume : number)

stopAllSounds

// stops all sounds
stopAllSounds()

addCss

// adds css style classes to the game
addCss(css:string)

// example
add(".text-white{ color :white } ")

addScript

// adds a 3rd party js script to the game
addScript(url: string)

displayEffect

interface Data{
from: { x: number, y: number }
to: { x: number, y: number }
size: { width: number, height: number }
speed: number
}

// displays an effect with the given https path on the map position (default: current player)
displayEffect(effectPath: string, position?: {x, y}, duration?:number, data?: Data)

//example:
displayEffect("https://myImage.com/img.png");

placeAsset

// places an asset on the map, builderAssetId or https source
placeAsset(builderAssetIdOrSrc: string, position: { x: number, y: number }, data?: AssetData, id?: string)

// example:
placeAsset("https://myImage.com/img.png", {12,12})

destroyAsset

// destroys the asset with the given id (default: current asset)
destroyAsset(assetId?: string)

hitPlayer

// hits a player
hitPlayer(playerId: string)
// opens an external link in a dialog
showLink(link: string)

showDialog

// shows a global dialog box
showDialog(title: string, body: string, buttons: Array<{ text: string, onClick: Function }>)

// example :
showDialog({
title: "Hi",
body : "<h1>Grettings</hi>",
buttons : [{
text : "ok",
onclick : function(){
moveTo(1,1)
}
}]
})

Listeners

isPlayerInRadius

    // checks if the current player is in the given radius
isAnyPlayerInRadius(radius:number)

isPlayerInRadius

// checks if any player is in the given radius
isPlayerInRadius(radius:number)

executeIfPlayerInRadius

// executes the given function if the player is in the given radius
executeIfPlayerInRadius(radius: number, func: () => void): Listener

executeIfPlayerInRadius

// moves to the player if they are in the given radius
moveToPlayerIfInRadius(radius: number)

playerMoved

// listener for current player movement events
playerMoved(callback: (mapPos: {x,y}) => void): Listener

anyoneTalked

// listener for speech events
anyoneTalked(callback: (text: {name:stirng, text:string}) => void): Listener

assetMoved

// listener for asset movement events
assetMoved(callback: (mapPos: {x,y}) => void): Listener

anyAssetGotHit

// listener for any asset getting hit events
anyAssetGotHit(callback: (data: { attackerId:string, receiverId:string }) => void): Listener

assetGotHit

// listener for current asset getting hit events
assetGotHit(callback: (data: { attackerId:string }) => void): Listener

Web3

getTokenBalance

// gets the token balance of the given token address (DEFAULT : current player primaryWalletAddress OR walletAddress)
getTokenBalance(tokenAddress: string, http_provider:string, walletAddress?:string)

getNFTCount

// gets the nft count of the given nft token address (DEFAULT : current player)
getNFTCount(tokenAddress: string, http_provider:string, walletAddress?:string)

transferToken

// transfers a specific token to the player's wallet (DEFAULT : current player)
// E.g ewwLockerAddress = matic 0xD40C94BF410537dC1C71C6Cab3900B14B23BFF67
transferToken(tokenAddress: string, amount:number, http_provider:string, ewwLockerAddress:string, walletAddress?:string)

Ask for further help here we TODO on how to

Interfaces

Asset

interface Asset {
id: string; // Asset ID
worldId: string; // World ID that the asset belongs to
builderAssetId: string; // ID of the related asset builder pack
isTile: boolean; // Boolean indicating if the asset is a tile or not
source: string; // Source string of the asset, fetched from assetService
data?: AssetData; // Dynamic object containing data for the asset
getPosition() // get the {x,y} position of the asset
}

AssetData

interface AssetData {
respawn?: RespawnData; // Data for asset's respawn
direction?: Direction; // Asset's direction on the map
moveable?: boolean; // Indicating if players can move over the asset
underlining?: boolean; // Indicating if the asset is underlining like a floor tile
hasCode?: boolean; // Indicating if the asset has code or not
life?: number; // Life of the asset
maxLife?: number; // Maximum life of the asset
width?: number; // Asset's width on the map
height?: number; // Asset's height on the map
name?:string; // assets name
}

Player

interface Player {
id: string; // Player ID
name: string; // Player's name
worldId: string; // World ID that the player belongs to
source: string; // Image src string of the player
direction: Direction; // Player's direction on the map
walletAddress: string; // Player's wallet address
data: PlayerData; // Dynamic object containing data for the player
getPosition() // get the {x,y} position of the player
}

QuestData

interface QuestData {
id: string;
name: string;
description: string;
image?: string; // absolute https image path
}

PlayerData

export interface PlayerData {
life: number;
maxLife: number;
assets: PlayerAsset[];
quests: PlayerQuest[];
wearables: PlayerWearable[];
}

export interface PlayerAsset {
id: string;
name?: string;
description?: string;
image: string;
action: () => void; // executes code e.g () => {moveTo(1,2)}
}
export interface PlayerQuest {
id: string;
name: string;
description: string;
image?: string;
action: () => void; // executes code e.g () => {moveTo(1,2)}
}

export interface PlayerWearable {
source: string; // absolute https image path
x: number;
y: number;
}

Direction

export enum DIRECTION {
SW = 2,
NW = 4,
NE = 6,
SE = 8
};

RespawnData

export interface RespawnData {
lastDeathTime?: number;
respawnSeconds: number;
cPos: number;
rPos: number;
}

Variables

NameTypeDescription
$thisAssetCurrent asset object
$thisObjectPixi.js ContainerThe asset container pixijs object
destroyedByPlayerIdstringPlayer id that destroyed the asset
playerPlayerCurrent player
playerObjectPixi.js ContainerCurrent player container object object
playersPlayer[]List of players in the game
assetsAsset[]List of assets in the game
gamefieldHtmlElement (readonly)The gamefield panel