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

View File

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

View File

@ -120,7 +120,7 @@
{ {
"name":"openWebsite", "name":"openWebsite",
"type":"string", "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, "rotation":0,
"type":"area", "type":"area",