open website linked from Tiled Object

This commit is contained in:
Piotr 'pwh' Hanusiak 2022-04-12 10:57:38 +02:00
parent 255f4375da
commit 1ca393f3db
3 changed files with 191 additions and 177 deletions

View File

@ -6,7 +6,7 @@ import { layoutManagerActionStore } from "../../Stores/LayoutManagerStore";
import { localUserStore } from "../../Connexion/LocalUserStore";
import { get } from "svelte/store";
import { ON_ACTION_TRIGGER_BUTTON, ON_ICON_TRIGGER_BUTTON } from "../../WebRtc/LayoutManager";
import type { ITiledMapLayer } from "../Map/ITiledMap";
import type { ITiledMapLayer, ITiledMapProperty } from "../Map/ITiledMap";
import { GameMapProperties } from "./GameMapProperties";
import type { CoWebsite } from "../../WebRtc/CoWebsite/CoWesbite";
import { SimpleCoWebsite } from "../../WebRtc/CoWebsite/SimpleCoWebsite";
@ -23,9 +23,17 @@ interface OpenCoWebsite {
coWebsite?: CoWebsite;
}
/**
* Either Layer or Object within Objects Layer in Tiled
*/
export interface ITiledPlace {
name: string;
properties?: ITiledMapProperty[];
}
export class GameMapPropertiesListener {
private coWebsitesOpenByLayer = new Map<ITiledMapLayer, OpenCoWebsite>();
private coWebsitesActionTriggerByLayer = new Map<ITiledMapLayer, string>();
private coWebsitesOpenByPlace = new Map<ITiledPlace, OpenCoWebsite>();
private coWebsitesActionTriggerByPlace = new Map<ITiledPlace, string>();
constructor(private scene: GameScene, private gameMap: GameMap) {}
@ -179,11 +187,26 @@ export class GameMapPropertiesListener {
}
});
// Open a new co-website by the property.
this.gameMap.onEnterLayer((newLayers) => {
const handler = () => {
newLayers.forEach((layer) => {
if (!layer.properties) {
this.onEnterPlaceHandler(newLayers);
});
this.gameMap.onLeaveLayer((oldLayers) => {
this.onLeavePlaceHandler(oldLayers);
});
this.gameMap.onEnterArea((newAreas) => {
this.onEnterPlaceHandler(newAreas);
});
this.gameMap.onLeaveArea((oldAreas) => {
this.onLeavePlaceHandler(oldAreas);
});
}
private onEnterPlaceHandler(places: ITiledPlace[]): void {
places.forEach((place) => {
if (!place.properties) {
return;
}
@ -195,7 +218,7 @@ export class GameMapPropertiesListener {
let websiteTriggerProperty: string | undefined;
let websiteTriggerMessageProperty: string | undefined;
layer.properties.forEach((property) => {
place.properties.forEach((property) => {
switch (property.name) {
case GameMapProperties.OPEN_WEBSITE:
openWebsiteProperty = property.value as string | undefined;
@ -227,7 +250,7 @@ export class GameMapPropertiesListener {
const actionId = "openWebsite-" + (Math.random() + 1).toString(36).substring(7);
if (this.coWebsitesOpenByLayer.has(layer)) {
if (this.coWebsitesOpenByPlace.has(place)) {
return;
}
@ -235,7 +258,7 @@ export class GameMapPropertiesListener {
actionId: actionId,
};
this.coWebsitesOpenByLayer.set(layer, coWebsiteOpen);
this.coWebsitesOpenByPlace.set(place, coWebsiteOpen);
const loadCoWebsiteFunction = (coWebsite: CoWebsite) => {
coWebsiteManager.loadCoWebsite(coWebsite).catch(() => {
@ -261,15 +284,12 @@ export class GameMapPropertiesListener {
loadCoWebsiteFunction(coWebsite);
};
if (
localUserStore.getForceCowebsiteTrigger() ||
websiteTriggerProperty === ON_ACTION_TRIGGER_BUTTON
) {
if (localUserStore.getForceCowebsiteTrigger() || websiteTriggerProperty === ON_ACTION_TRIGGER_BUTTON) {
if (!websiteTriggerMessageProperty) {
websiteTriggerMessageProperty = get(LL).trigger.cowebsite();
}
this.coWebsitesActionTriggerByLayer.set(layer, actionId);
this.coWebsitesActionTriggerByPlace.set(place, actionId);
layoutManagerActionStore.addAction({
uuid: actionId,
@ -296,23 +316,18 @@ export class GameMapPropertiesListener {
openCoWebsiteFunction();
}
});
};
}
handler();
});
// Close opened co-websites on leave the layer who contain the property.
this.gameMap.onLeaveLayer((oldLayers) => {
const handler = () => {
oldLayers.forEach((layer) => {
if (!layer.properties) {
private onLeavePlaceHandler(places: ITiledPlace[]): void {
places.forEach((place) => {
if (!place.properties) {
return;
}
let openWebsiteProperty: string | undefined;
let websiteTriggerProperty: string | undefined;
layer.properties.forEach((property) => {
place.properties.forEach((property) => {
switch (property.name) {
case GameMapProperties.OPEN_WEBSITE:
openWebsiteProperty = property.value as string | undefined;
@ -327,7 +342,7 @@ export class GameMapPropertiesListener {
return;
}
const coWebsiteOpen = this.coWebsitesOpenByLayer.get(layer);
const coWebsiteOpen = this.coWebsitesOpenByPlace.get(place);
if (!coWebsiteOpen) {
return;
@ -339,14 +354,14 @@ export class GameMapPropertiesListener {
coWebsiteManager.closeCoWebsite(coWebsite);
}
this.coWebsitesOpenByLayer.delete(layer);
this.coWebsitesOpenByPlace.delete(place);
if (!websiteTriggerProperty) {
return;
}
const actionStore = get(layoutManagerActionStore);
const actionTriggerUuid = this.coWebsitesActionTriggerByLayer.get(layer);
const actionTriggerUuid = this.coWebsitesActionTriggerByPlace.get(place);
if (!actionTriggerUuid) {
return;
@ -361,11 +376,7 @@ export class GameMapPropertiesListener {
layoutManagerActionStore.removeAction(actionTriggerUuid);
}
this.coWebsitesActionTriggerByLayer.delete(layer);
});
};
handler();
this.coWebsitesActionTriggerByPlace.delete(place);
});
}
}

View File

@ -892,6 +892,7 @@ export class GameScene extends DirtyScene {
});
});
// TODO: Move to GameMapPropertiesListener?
this.gameMap.onEnterArea((areas) => {
for (const area of areas) {
const focusable = area.properties?.find(
@ -920,7 +921,9 @@ export class GameScene extends DirtyScene {
this.gameMap.onLeaveArea((areas) => {
for (const area of areas) {
const focusable = area.properties?.find((property) => property.name === "focusable");
const focusable = area.properties?.find(
(property) => property.name === GameMapProperties.FOCUSABLE
);
if (focusable && focusable.value === true) {
this.cameraManager.leaveFocusMode(this.CurrentPlayer, 1000);
break;

View File

@ -120,7 +120,7 @@
{
"name":"openWebsite",
"type":"string",
"value":"https:\/\/youtu.be\/iF-ucIgP0OE?list=RDGMEMWO-g6DgCWEqKlDtKbJA1GwVMiF-ucIgP0OE"
"value":"https:\/\/www.youtube.com\/embed\/CvXUGIm_hkA?list=RDCvXUGIm_hkA"
}],
"rotation":0,
"type":"area",