diff --git a/map.tmj b/map.tmj index 3eaf13d..f3fb467 100644 --- a/map.tmj +++ b/map.tmj @@ -3166,15 +3166,15 @@ "y":959.440052700922 }, { - "height":190, + "height":192, "id":59, "name":"pauseArea", "rotation":0, "type":"area", "visible":true, - "width":286, - "x":1185, - "y":705 + "width":288, + "x":1184, + "y":704 }, { "height":118, diff --git a/src/main.ts b/src/main.ts index 72aaae4..c83f577 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,13 +1,29 @@ -import {bootstrapExtra} from '@workadventure/scripting-api-extra'; +import { Area } from '@workadventure/iframe-api-typings/front/Api/Iframe/Area/Area'; +import { bootstrapExtra } from '@workadventure/scripting-api-extra'; (async () => { await WA.onInit(); })(); -let tileSize = 32; +const tileSize = 32; + +enum PositionType { + LastPositionBreak, + LastPositionCall, +} + +interface Position { + x: number | undefined; + y: number | undefined; +} + +const positions: Record = { + [PositionType.LastPositionBreak]: { x: undefined, y: undefined }, + [PositionType.LastPositionCall]: { x: undefined, y: undefined }, +}; function getRandomInt(min: number, max: number): number { - return Math.ceil(Math.random() * (max - min + 1)) + min; + return Math.floor(Math.random() * (max - min + 1)) + min; } function getDistance(x1: number, y1: number, x2: number, y2: number) { @@ -26,47 +42,86 @@ function showOrHideLayer(layerName: string, startDate: Date, endDate: Date) { } } +function clearLastPositions() { + for (let position of Object.values(positions)) { + position.x = undefined; + position.y = undefined; + } +} + function showOrHideChristmasLayer() { - let today = new Date(); + const today = new Date(); // December 1st (current year) to January 6th (next year) - let startDate = new Date(today.getFullYear(), 11, 1); - let endDate = new Date(today.getFullYear() + 1, 0, 6); + const startDate = new Date(today.getFullYear(), 11, 1); + const endDate = new Date(today.getFullYear() + 1, 0, 6); showOrHideLayer('Christmas', startDate, endDate); } -function addActionButtons() { +async function teleportPlayerToArea(area: Area | undefined, positionType: PositionType) { + let x = positions[positionType].x; + let y = positions[positionType].y; + + if (area !== undefined) { + const xStart = area.x; + const xEnd = area.x + area.width - (tileSize / 2); + + const yStart = area.y; + const yEnd = area.y + area.height - (tileSize / 2); + + x = getRandomInt(xStart, xEnd); + y = getRandomInt(yStart, yEnd); + + const position = await WA.player.getPosition(); + if (position) { + Object.assign(positions[positionType], position); + } + } else { + Object.assign(positions[positionType], { x: undefined, y: undefined }); + } + + if (x !== undefined && y !== undefined) { + WA.player.teleport(x, y); + } + + removeButtons(); + addActionButtons(); +} + +function addTeleportButton(id: string, imageSrc: string, toolTip: string, positionType: PositionType, getArea: () => Promise) { WA.ui.actionBar.addButton({ - id: 'pause-btn', - // @ts-ignore + id, type: 'action', - imageSrc: - 'https://github.com/othaldo/workadventure-ds/blob/master/src/assets/ds/pause.png?raw=true', - toolTip: 'Zum Pausenbereich teleportieren', + imageSrc, + toolTip, callback: async () => { - const area = await WA.room.area.get('pauseArea'); - let xStart = area.x; - let xEnd = area.x + area.width - (tileSize / 2); + const position = positions[positionType]; + let area; - let yStart = area.y; - let yEnd = area.y + area.height - (tileSize / 2); + if (position.x === undefined || position.y === undefined) { + area = await getArea(); + } - WA.player.teleport( - getRandomInt(xStart, xEnd), getRandomInt(yStart, yEnd)); - removeButtons(); - addActionButtons(); + teleportPlayerToArea(area, positionType); } }); +} - WA.ui.actionBar.addButton({ - id: 'customer-call-btn', - // @ts-ignore - type: 'action', - imageSrc: - 'https://github.com/othaldo/workadventure-ds/blob/master/src/assets/ds/call.png?raw=true', - toolTip: 'Zum \'Im Gespräch\'-Bereich teleportieren', - callback: async () => { +function addPauseButton() { + addTeleportButton('pause-btn', + 'https://github.com/othaldo/workadventure-ds/blob/master/src/assets/ds/pause.png?raw=true', + 'Zum Pausenbereich teleportieren und zurück', + PositionType.LastPositionBreak, + async () => await WA.room.area.get("pauseArea")); +} + +function addCustomerCallButton() { + addTeleportButton('customer-call-btn', + 'https://github.com/othaldo/workadventure-ds/blob/master/src/assets/ds/call.png?raw=true', + 'Zum \'Im Gespräch\'-Bereich teleportieren und zurück', + PositionType.LastPositionCall, + async () => { const customerCallArea1 = await WA.room.area.get('ccArea1'); const customerCallArea2 = await WA.room.area.get('ccArea2'); const position = await WA.player.getPosition(); @@ -83,28 +138,20 @@ function addActionButtons() { // Berechne die Distanzen zur aktuellen Position const distanceToArea1 = - getDistance(position.x, position.y, midPointArea1.x, midPointArea1.y); + getDistance(position.x, position.y, midPointArea1.x, midPointArea1.y); const distanceToArea2 = - getDistance(position.x, position.y, midPointArea2.x, midPointArea2.y); + getDistance(position.x, position.y, midPointArea2.x, midPointArea2.y); // Bestimme die nächstgelegene Area - const nearestArea = distanceToArea1 < distanceToArea2 ? - customerCallArea1 : - customerCallArea2; - - // Berechne zufällige Position innerhalb der nächstgelegenen Area - let xStart = nearestArea.x; - let xEnd = nearestArea.x + nearestArea.width - (tileSize / 2); - let yStart = nearestArea.y; - let yEnd = nearestArea.y + nearestArea.height - (tileSize / 2); - - // Teleportiere den Spieler - WA.player.teleport( - getRandomInt(xStart, xEnd), getRandomInt(yStart, yEnd)); - removeButtons(); - addActionButtons(); - } - }); + return distanceToArea1 < distanceToArea2 ? + customerCallArea1 : + customerCallArea2; + }); +} + +function addActionButtons() { + addPauseButton(); + addCustomerCallButton(); } function removeButtons() { @@ -112,27 +159,40 @@ function removeButtons() { WA.ui.actionBar.removeButton('customer-call-btn'); } +function registerAreaOnLeaveHandler() { + WA.room.area.onLeave('pauseArea').subscribe(() => { + clearLastPositions(); + }); + WA.room.area.onLeave('ccArea1').subscribe(() => { + clearLastPositions(); + }); + WA.room.area.onLeave('ccArea2').subscribe(() => { + clearLastPositions(); + }); +} + // Waiting for the API to be ready WA.onInit() - .then(() => { - const userTag = WA.player.tags; - - // If user is admin, name it with a dark blue border - if (userTag.includes('admin')) { - WA.player.setOutlineColor(27, 42, 65); - } + .then(() => { + const userTag = WA.player.tags; - addActionButtons(); - showOrHideChristmasLayer(); - - // The line below bootstraps the Scripting API Extra library that adds a - // number of advanced properties/features to WorkAdventure - bootstrapExtra() - .then(() => { - console.log('Scripting API Extra ready'); - }) - .catch(e => console.error(e)); - }) - .catch(e => console.error(e)); + // If user is admin, name it with a dark blue border + if (userTag.includes('admin')) { + WA.player.setOutlineColor(27, 42, 65); + } -export {}; + addActionButtons(); + showOrHideChristmasLayer(); + registerAreaOnLeaveHandler(); + + // The line below bootstraps the Scripting API Extra library that adds a + // number of advanced properties/features to WorkAdventure + bootstrapExtra() + .then(() => { + console.log('Scripting API Extra ready'); + }) + .catch(e => console.error(e)); + }) + .catch(e => console.error(e)); + +export { };