Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Teleport back to last position #1

Merged
merged 2 commits into from
Dec 22, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 97 additions & 44 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
import {bootstrapExtra} from '@workadventure/scripting-api-extra';
import { bootstrapExtra } from '@workadventure/scripting-api-extra';

(async () => {
await WA.onInit();
})();

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;
}
Expand All @@ -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();

Expand All @@ -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();
}
Expand All @@ -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();
}
Expand All @@ -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(() => {
Expand All @@ -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
Expand Down