-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding documentation related to utility functions
- Loading branch information
Showing
9 changed files
with
261 additions
and
169 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
{.section-title.accent.text-primary} | ||
# Layers utility functions | ||
|
||
{.alert.alert-info} | ||
**Important!** To use these functions, you need to [import the "Scripting API Extra"](utils.md#importing-the-utility-functions) library. | ||
|
||
### Return a Map of all layers | ||
|
||
Layers can be nested in group layers. | ||
|
||
The `getLayersMap()` function returns a map of all layers in a uni-dimensional map. | ||
|
||
Layers are **renamed**: if they are in a group layer, the name of the group layer is prepended with a "/" as a separator. | ||
|
||
Layers are indexed by name. | ||
|
||
```typescript | ||
import {getLayersMap} from '@workadventure/scripting-api-extra'; | ||
|
||
const layers = await getLayersMap(); | ||
|
||
// Access a layer directly by name | ||
const mylayer = layers.get('my_layer'); | ||
|
||
// Iterate over all layers | ||
for (const layer of layers.values()) { | ||
// ... | ||
} | ||
``` | ||
|
||
### Get boundaries of a layer | ||
|
||
`findLayerBoundaries` returns the boundaries of a given layer as an object with properties: { top: number, left: number, right: number, bottom: number } | ||
|
||
Numbers are expressed in "tiles", not pixels. | ||
|
||
``` | ||
findLayerBoundaries(layer: ITiledMapTileLayer): { | ||
top: number; | ||
left: number; | ||
right: number; | ||
bottom: number; | ||
} | ||
``` | ||
|
||
Example: | ||
|
||
```typescript | ||
import {getLayersMap, findLayerBoundaries} from '@workadventure/scripting-api-extra'; | ||
import {ITiledMapTileLayer} from "@workadventure/tiled-map-type-guard/dist/ITiledMapTileLayer"; | ||
|
||
const layers = await getLayersMap(); | ||
|
||
const layer = layers.get("my_layer") as ITiledMapTileLayer; | ||
|
||
const boundaries = findLayerBoundaries(layer); | ||
console.log('Top:' , boundaries.top); | ||
console.log('Left:' , boundaries.left); | ||
console.log('Bottom:' , boundaries.bottom); | ||
console.log('Right:' , boundaries.right); | ||
``` | ||
|
||
### Get boundaries of several layers | ||
|
||
If you are looking for the boundaries of several layers at once, you can use the `findLayersBoundaries` variant. | ||
|
||
``` | ||
findLayersBoundaries(layers: ITiledMapTileLayer[]): { | ||
top: number; | ||
left: number; | ||
right: number; | ||
bottom: number; | ||
} | ||
``` | ||
|
||
It will return a square containing all the tiles of all the layers passed in parameters. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
{.section-title.accent.text-primary} | ||
# Tiled properties | ||
|
||
{.alert.alert-info} | ||
**Important!** To use these functions, you need to [import the "Scripting API Extra"](utils.md#importing-the-utility-functions) library. | ||
|
||
In your Tiled map, a number of items can have "properties" (the map itself, layers, tiles, tilesets, objects...). | ||
|
||
The JSON map can be fetched using `WA.room.getTiledMap`. But when it comes to analyzing the JSON map, you are on your own. | ||
|
||
The Scripting API Extra package comes with a useful `Properties` class to analyze these properties. | ||
|
||
Usage: | ||
|
||
```typescript | ||
const map = await WA.room.getTiledMap(); | ||
|
||
const mapProperties = new Properties(map.properties); | ||
|
||
// getOne fetches the value of the property passed in parameter. | ||
const name = mapProperties.get('name') as string; | ||
|
||
// getString is the same as get except it ensures the value is a string (and throws an exception if it is not) | ||
const name = mapProperties.getString('name'); | ||
``` | ||
|
||
Methods available: | ||
|
||
```typescript | ||
class Properties { | ||
get(name: string): string | boolean | number | undefined; // returns the property | ||
getString(name: string): string | undefined; // returns the property (and checks it is a string) | ||
getNumber(name: string): number | undefined; // returns the property (and checks it is a number) | ||
getBoolean(name: string): boolean | undefined; // returns the property (and checks it is a boolean) | ||
mustGetString(name: string): string; // returns the property as a string (throws an Error if not found) | ||
mustGetNumber(name: string): string; // returns the property as a number (throws an Error if not found) | ||
mustGetBoolean(name: string): string; // returns the property as a boolean (throws an Error if not found) | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
{.section-title.accent.text-primary} | ||
# Variables metadata | ||
|
||
{.alert.alert-info} | ||
**Important!** To use these functions, you need to [import the "Scripting API Extra"](utils.md#importing-the-utility-functions) library. | ||
|
||
## Return a list of all variables defined in the map | ||
|
||
``` | ||
// returns a list of all the variables defined in the map. | ||
getAllVariables(): Promise<Map<string, VariableDescriptor>> | ||
``` | ||
|
||
Variables are returned as a Map. The key is the name of the variable, the value if an object representing the variable. | ||
You can fetch individual properties defined in Tiled for this variable using this object's `properties` attribute. | ||
|
||
For instance: | ||
|
||
```typescript | ||
import { getAllVariables, VariableDescriptor } from '@workadventure/scripting-api-extra'; | ||
|
||
const variables = await getAllVariables(); | ||
console.log(variables['my_variable'].properties.getOne('persist')); | ||
``` | ||
|
||
Note: the `VariableDescriptor` class returned does not contain the value of the variable itself. It represents the | ||
variable object as defined in the Tiled map. This can be useful to access additional metadata that can be stored | ||
in special properties of the variable, or to access the variable "position" in the map (since a variable is represented | ||
by a "Point" object in a map). | ||
|
||
```typescript | ||
class VariableDescriptor { | ||
// the name of the variable | ||
name: string | ||
// an object representing the properties of the variable | ||
properties: Properties | ||
// The position of the variable | ||
x: number | ||
y: number | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
{.section-title.accent.text-primary} | ||
# Action zones | ||
|
||
{.alert.alert-info} | ||
**Important!** To use these action zones, you need to [import the "Scripting API Extra" script in your map](about.md#importing-the-extended-features) | ||
|
||
You can define special zones that will **alter the value of a ["variable"](https://workadventu.re/map-building/api-state.md)** | ||
when walked upon. | ||
|
||
To define such a zone, create a new "tile layer" in Tiled. | ||
|
||
In this layer, add these properties: | ||
|
||
- `zone`: A unique name for this zone | ||
- `bindVariable`: The name of the variable that will be altered when one enters/leaves the zone | ||
- `enterValue`: (optional) The value the variable will be set to when entering the zone | ||
- `leaveValue`: (optional) The value the variable will be set to when leaving the zone | ||
|
||
## User interaction | ||
|
||
By default, as soon as anyone enters or leaves the zone, the value of the variable will be changed. | ||
|
||
Optionally, you can request a user interaction to trigger the change of the value. Use the `triggerMessage` property | ||
to display a message to the player. When the player presses "SPACE", the value of the variable will be changed to `enterValue`. | ||
|
||
## Managing rights | ||
|
||
You can restrict who will trigger an action zone using user **tags**. | ||
|
||
{.alert.alert-info} | ||
User tags is a feature of the "pro" accounts. | ||
|
||
Add a `tag` property on the layer. The variable will only change value if the player walking on the layer has the specified tag. |
Oops, something went wrong.