From b509471140c10126e5c6864f98ed7e22e4543b10 Mon Sep 17 00:00:00 2001 From: GRL Date: Tue, 18 May 2021 17:05:16 +0200 Subject: [PATCH] documentation documentation of onPlayerMove documentation of getMap documentation of getGameState --- docs/maps/api-reference.md | 94 ++++++++++++++++++++++++++++++++++++++ front/src/iframe_api.ts | 34 ++++++++------ 2 files changed, 115 insertions(+), 13 deletions(-) diff --git a/docs/maps/api-reference.md b/docs/maps/api-reference.md index 6e98dfb5..8eb00397 100644 --- a/docs/maps/api-reference.md +++ b/docs/maps/api-reference.md @@ -260,7 +260,101 @@ WA.setProperty(layerName : string, propertyName : string, propertyValue : string 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". +Example : + ```javascript WA.setProperty('wikiLayer', 'openWebsite', 'https://www.wikipedia.org/'); ``` +### Listen player movement + +``` +onPlayerMove(callback: HasPlayerMovedEventCallback): void; +``` +Listens to the movement of the current user and calls the callback. Send a event when current user stop moving, change direction and every 200ms when moving in the same direction. + +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. + +Exemple : +```javascript +WA.onPlayerMove(console.log); +``` + +### Getting the map + +``` +getMap(): Promise +``` + +Return a promise of an ITiledMap that contains the JSON file of the map plus the property set by a script. + +Example : +```javascript +WA.getMap().then((data) => console.log(data.layers)); +``` + +### Getting the url of the JSON file map + +``` +getMapUrl(): Promise +``` + +Return a promise of the url of the JSON file map. + +Example : +```javascript +WA.getMapUrl().then((mapUrl) => {console.log(mapUrl)}); +``` + +### Getting the roomID +``` +getRoomId(): Promise +``` +Return a promise of the ID of the current room. + +Example : +```javascript +WA.getRoomId().then((roomId) => console.log(roomId)); +``` + +### Getting the UUID of the current user +``` +getUuid(): Promise +``` +Return a promise of the ID of the current user. + +Example : +```javascript +WA.getUuid().then((uuid) => {console.log(uuid)}); +``` + +### Getting the nickname of the current user +``` +getNickName(): Promise +``` +Return a promise of the nickname of the current user. + +Example : +```javascript +WA.getNickName().then((nickname) => {console.log(nickname)}); +``` + +### Getting the name of the layer where the current user started (if other than start) +``` +getStartLayerName(): Promise +``` +Return a promise of the name of the layer where the current user started if the name is different than "start". + +Example : +```javascript +WA.getStartLayerName().then((starLayerName) => {console.log(starLayerName)}); +``` + + + diff --git a/front/src/iframe_api.ts b/front/src/iframe_api.ts index a2fbb70b..4fdb0a03 100644 --- a/front/src/iframe_api.ts +++ b/front/src/iframe_api.ts @@ -14,6 +14,7 @@ import type { SetPropertyEvent } from "./Api/Events/setPropertyEvent"; import { GameStateEvent, isGameStateEvent } from './Api/Events/GameStateEvent'; import { HasPlayerMovedEvent, HasPlayerMovedEventCallback, isHasPlayerMovedEvent } from './Api/Events/HasPlayerMovedEvent'; import { DataLayerEvent, isDataLayerEvent } from "./Api/Events/DataLayerEvent"; +import type {ITiledMap} from "./Phaser/Map/ITiledMap"; interface WorkAdventureApi { sendChatMessage(message: string, author: string): void; @@ -44,7 +45,7 @@ interface WorkAdventureApi { onPlayerMove(callback: (playerMovedEvent: HasPlayerMovedEvent) => void): void - getDataLayer(): Promise + getMap(): Promise } declare global { @@ -114,6 +115,16 @@ function getGameState(): Promise { } } +function getDataLayer(): Promise { + return new Promise((resolver, thrower) => { + dataLayerResolver.push(resolver); + postToParent({ + type: "getDataLayer", + data: undefined + }) + }) +} + const gameStateResolver: Array<(event: GameStateEvent) => void> = [] const dataLayerResolver: Array<(event: DataLayerEvent) => void> = [] let immutableData: GameStateEvent; @@ -137,41 +148,38 @@ window.WA = { }) }, - getDataLayer(): Promise { - return new Promise((resolver, thrower) => { - dataLayerResolver.push(resolver); - postToParent({ - type: "getDataLayer", - data: undefined - }) + + getMap(): Promise { + return getDataLayer().then((res) => { + return res.data as ITiledMap; }) }, - getNickName() { + getNickName(): Promise { return getGameState().then((res) => { return res.nickname; }) }, - getMapUrl() { + getMapUrl(): Promise { return getGameState().then((res) => { return res.mapUrl; }) }, - getUuid() { + getUuid(): Promise { return getGameState().then((res) => { return res.uuid; }) }, - getRoomId() { + getRoomId(): Promise { return getGameState().then((res) => { return res.roomId; }) }, - getStartLayerName() { + getStartLayerName(): Promise { return getGameState().then((res) => { return res.startLayerName; })