diff --git a/src/main.ts b/src/main.ts index 72aaae4..df20ecd 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,4 +1,4 @@ -import {bootstrapExtra} from '@workadventure/scripting-api-extra'; +import { bootstrapExtra } from '@workadventure/scripting-api-extra'; (async () => { await WA.onInit(); @@ -6,6 +6,14 @@ import {bootstrapExtra} from '@workadventure/scripting-api-extra'; let tileSize = 32; +interface Position { + x: number | undefined; + y: number | undefined; +} + +let lastPositionBreak: Position = {x: undefined, y: undefined}; +let lastPositionCall: Position = {x: undefined, y: undefined}; + function getRandomInt(min: number, max: number): number { return Math.ceil(Math.random() * (max - min + 1)) + min; } @@ -26,6 +34,13 @@ function showOrHideLayer(layerName: string, startDate: Date, endDate: Date) { } } +function clearLastPositions() { + lastPositionBreak.x = undefined; + lastPositionBreak.y = undefined; + lastPositionCall.x = undefined; + lastPositionCall.y = undefined; +} + function showOrHideChristmasLayer() { let today = new Date(); @@ -43,17 +58,30 @@ function addActionButtons() { type: 'action', imageSrc: 'https://github.com/othaldo/workadventure-ds/blob/master/src/assets/ds/pause.png?raw=true', - toolTip: 'Zum Pausenbereich teleportieren', + toolTip: 'Zum Pausenbereich teleportieren und zurück', callback: async () => { - const area = await WA.room.area.get('pauseArea'); - let xStart = area.x; - let xEnd = area.x + area.width - (tileSize / 2); - - let yStart = area.y; - let yEnd = area.y + area.height - (tileSize / 2); + let x; + let y; + if (lastPositionBreak.x === undefined || lastPositionBreak.y === undefined) { + const area = await WA.room.area.get("pauseArea"); + let xStart = area.x; + let xEnd = area.x + area.width - (tileSize / 2); + + let yStart = area.y; + let yEnd = area.y + area.height - (tileSize / 2); + + x = getRandomInt(xStart , xEnd); + y = getRandomInt(yStart , yEnd); + lastPositionBreak.x = (await WA.player.getPosition()).x; + lastPositionBreak.y = (await WA.player.getPosition()).y; + } else { + x = lastPositionBreak.x; + y = lastPositionBreak.y; + lastPositionBreak.x = undefined; + lastPositionBreak.y = undefined; + } - WA.player.teleport( - getRandomInt(xStart, xEnd), getRandomInt(yStart, yEnd)); + WA.player.teleport(x, y); removeButtons(); addActionButtons(); } @@ -65,42 +93,54 @@ function addActionButtons() { 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', + toolTip: 'Zum \'Im Gespräch\'-Bereich teleportieren und zurück', callback: async () => { - const customerCallArea1 = await WA.room.area.get('ccArea1'); - const customerCallArea2 = await WA.room.area.get('ccArea2'); - const position = await WA.player.getPosition(); - - // Berechne die Mittelpunkte der Areas - const midPointArea1 = { - x: customerCallArea1.x + customerCallArea1.width / 2, - y: customerCallArea1.y + customerCallArea1.height / 2 - }; - const midPointArea2 = { - x: customerCallArea2.x + customerCallArea2.width / 2, - y: customerCallArea2.y + customerCallArea2.height / 2 - }; - - // Berechne die Distanzen zur aktuellen Position - const distanceToArea1 = - getDistance(position.x, position.y, midPointArea1.x, midPointArea1.y); - const distanceToArea2 = - 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); - + let x; + let y; + if (lastPositionCall.x === undefined || lastPositionCall.y === undefined) { + const customerCallArea1 = await WA.room.area.get('ccArea1'); + const customerCallArea2 = await WA.room.area.get('ccArea2'); + const position = await WA.player.getPosition(); + + // Berechne die Mittelpunkte der Areas + const midPointArea1 = { + x: customerCallArea1.x + customerCallArea1.width / 2, + y: customerCallArea1.y + customerCallArea1.height / 2 + }; + const midPointArea2 = { + x: customerCallArea2.x + customerCallArea2.width / 2, + y: customerCallArea2.y + customerCallArea2.height / 2 + }; + + // Berechne die Distanzen zur aktuellen Position + const distanceToArea1 = + getDistance(position.x, position.y, midPointArea1.x, midPointArea1.y); + const distanceToArea2 = + 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); + + x = getRandomInt(xStart , xEnd); + y = getRandomInt(yStart , yEnd); + lastPositionCall.x = (await WA.player.getPosition()).x; + lastPositionCall.y = (await WA.player.getPosition()).y; + } else { + x = lastPositionCall.x; + y = lastPositionCall.y; + lastPositionCall.x = undefined; + lastPositionCall.y = undefined; + } // Teleportiere den Spieler - WA.player.teleport( - getRandomInt(xStart, xEnd), getRandomInt(yStart, yEnd)); + WA.player.teleport(x, y); removeButtons(); addActionButtons(); } @@ -112,6 +152,18 @@ 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(() => { @@ -124,6 +176,7 @@ WA.onInit() addActionButtons(); showOrHideChristmasLayer(); + registerAreaOnLeaveHandler(); // The line below bootstraps the Scripting API Extra library that adds a // number of advanced properties/features to WorkAdventure