2021-05-10 12:08:52 +02:00
{.section-title.accent.text-primary}
# API Reference
2021-06-17 18:05:16 +02:00
- [Navigation functions ](api-nav.md )
- [Chat functions ](api-chat.md )
2021-06-18 11:44:54 +02:00
- [Room functions ](api-room.md )
- [UI functions ](api-ui.md )
- [Sound functions ](api-sound.md )
- [Controls functions ](api-controls.md )
2021-06-21 12:13:40 +02:00
- [List of deprecated functions ](api-deprecated.md )
2021-05-10 15:10:11 +02:00
2021-05-26 10:41:33 +02:00
### Show / Hide a layer
2021-05-10 15:10:11 +02:00
```
WA.showLayer(layerName : string): void
WA.hideLayer(layerName : string) : void
```
These 2 methods can be used to show and hide a layer.
Example :
```javascript
2021-05-10 17:29:50 +02:00
WA.showLayer('bottom');
//...
WA.hideLayer('bottom');
2021-05-10 15:10:11 +02:00
```
2021-05-17 10:13:48 +02:00
### Set/Create properties in a layer
```
WA.setProperty(layerName : string, propertyName : string, propertyValue : string | number | boolean | undefined) : void;
```
2021-05-25 11:02:25 +02:00
Set the value of the `propertyName` property of the layer `layerName` at `propertyValue` . If the property doesn't exist, create the property `propertyName` and set the value of the property at `propertyValue` .
2021-05-17 10:13:48 +02:00
2021-05-26 10:41:33 +02:00
Example :
2021-05-17 10:13:48 +02:00
```javascript
WA.setProperty('wikiLayer', 'openWebsite', 'https://www.wikipedia.org/');
```
2021-05-10 15:10:11 +02:00
2021-05-25 11:02:25 +02:00
### Listen to player movement
2021-05-18 17:05:16 +02:00
```
onPlayerMove(callback: HasPlayerMovedEventCallback): void;
```
2021-05-25 11:02:25 +02:00
Listens to the movement of the current user and calls the callback. Sends an event when the user stops moving, changes direction and every 200ms when moving in the same direction.
2021-05-18 17:05:16 +02:00
The event has the following attributes :
* **moving (boolean):** **true** when the current player is moving, **false** otherwise.
* **direction (string):** ** "right"** | ** "left"** | ** "down"** | ** "top"** the direction where the current player is moving.
* **x (number):** coordinate X of the current player.
* **y (number):** coordinate Y of the current player.
**callback:** the function that will be called when the current player is moving. It contains the event.
2021-05-25 11:02:25 +02:00
Example :
2021-05-18 17:05:16 +02:00
```javascript
WA.onPlayerMove(console.log);
```
2021-05-26 10:41:33 +02:00
### Getting informations on the current user
2021-05-18 17:05:16 +02:00
```
2021-05-26 10:41:33 +02:00
getCurrentUser(): Promise< User >
2021-05-18 17:05:16 +02:00
```
2021-05-26 10:41:33 +02:00
Return a promise that resolves to a `User` object with the following attributes :
* **id (string) :** ID of the current user
* **nickName (string) :** name displayed above the current user
* **tags (string[]) :** list of all the tags of the current user
2021-05-18 17:05:16 +02:00
Example :
```javascript
2021-05-26 10:41:33 +02:00
WA.getCurrentUser().then((user) => {
if (user.nickName === 'ABC') {
console.log(user.tags);
}
})
2021-05-18 17:05:16 +02:00
```
2021-05-26 10:41:33 +02:00
### Getting informations on the current room
2021-05-18 17:05:16 +02:00
```
2021-05-26 10:41:33 +02:00
getCurrentRoom(): Promise< Room >
2021-05-18 17:05:16 +02:00
```
2021-05-26 10:41:33 +02:00
Return a promise that resolves to a `Room` object with the following attributes :
* **id (string) :** ID of the current room
* **map (ITiledMap) :** contains the JSON map file with the properties that were setted by the script if `setProperty` was called.
* **mapUrl (string) :** Url of the JSON map file
* **startLayer (string | null) :** Name of the layer where the current user started, only if different from `start` layer
2021-05-18 17:05:16 +02:00
Example :
```javascript
2021-05-26 10:41:33 +02:00
WA.getCurrentRoom((room) => {
if (room.id === '42') {
console.log(room.map);
window.open(room.mapUrl, '_blank');
}
})
2021-05-18 17:05:16 +02:00
```
2021-05-20 08:58:05 +02:00
### Add a custom menu
```
2021-05-28 12:13:10 +02:00
registerMenuCommand(commandDescriptor: string, callback: (commandDescriptor: string) => void): void
2021-05-20 08:58:05 +02:00
```
2021-05-25 11:02:25 +02:00
Add a custom menu item containing the text `commandDescriptor` . A click on the menu will trigger the `callback` .
2021-05-18 17:05:16 +02:00
2021-05-20 08:58:05 +02:00
Example :
```javascript
2021-05-25 11:02:25 +02:00
WA.registerMenuCommand('About', () => {
console.log("The About menu was clicked");
2021-05-20 08:58:05 +02:00
});
2021-05-20 10:57:36 +02:00
```
2021-05-26 10:41:33 +02:00
### Working with group layers
If you use group layers in your map, to reference a layer in a group you will need to use a `/` to join layer names together.
2021-05-20 10:57:36 +02:00
Example :
2021-05-26 10:41:33 +02:00
< div class = "row" >
< div class = "col" >
< img src = "https://workadventu.re/img/docs/groupLayer.png" class = "figure-img img-fluid rounded" alt = "" / >
< / div >
< / div >
The name of the layers of this map are :
* `entries/start`
* `bottom/ground/under`
* `bottom/build/carpet`
* `wall`