partey_workadventure/front/src/Phaser/Game/GameScene.ts

1718 lines
68 KiB
TypeScript
Raw Normal View History

2021-07-02 19:03:34 +02:00
import type { Subscription } from 'rxjs';
import { GlobalMessageManager } from '../../Administration/GlobalMessageManager';
import { userMessageManager } from '../../Administration/UserMessageManager';
import { iframeListener } from '../../Api/IframeListener';
import { connectionManager } from '../../Connexion/ConnectionManager';
import type {
GroupCreatedUpdatedMessageInterface,
MessageUserJoined,
MessageUserMovedInterface,
MessageUserPositionInterface,
OnConnectInterface,
PointInterface,
PositionInterface,
RoomJoinedMessageInterface,
2021-07-02 19:03:34 +02:00
} from '../../Connexion/ConnexionModels';
import { DEBUG_MODE, JITSI_PRIVATE_MODE, MAX_PER_GROUP, POSITION_DELAY } from '../../Enum/EnvironmentVariable';
2021-07-02 19:03:34 +02:00
import { Queue } from 'queue-typescript';
2020-11-23 20:34:05 +01:00
import {
AUDIO_LOOP_PROPERTY,
AUDIO_VOLUME_PROPERTY,
Box,
JITSI_MESSAGE_PROPERTIES,
2020-11-23 20:34:05 +01:00
layoutManager,
ON_ACTION_TRIGGER_BUTTON,
TRIGGER_JITSI_PROPERTIES,
TRIGGER_WEBSITE_PROPERTIES,
WEBSITE_MESSAGE_PROPERTIES,
2021-07-02 19:03:34 +02:00
} from '../../WebRtc/LayoutManager';
import { coWebsiteManager } from '../../WebRtc/CoWebsiteManager';
import type { UserMovedMessage } from '../../Messages/generated/messages_pb';
import { ProtobufClientUtils } from '../../Network/ProtobufClientUtils';
import type { RoomConnection } from '../../Connexion/RoomConnection';
import { Room } from '../../Connexion/Room';
import { jitsiFactory } from '../../WebRtc/JitsiFactory';
import { urlManager } from '../../Url/UrlManager';
import { audioManager } from '../../WebRtc/AudioManager';
import { TextureError } from '../../Exception/TextureError';
import { localUserStore } from '../../Connexion/LocalUserStore';
import { HtmlUtils } from '../../WebRtc/HtmlUtils';
import { mediaManager } from '../../WebRtc/MediaManager';
import { SimplePeer } from '../../WebRtc/SimplePeer';
import { addLoader } from '../Components/Loader';
import { OpenChatIcon, openChatIconName } from '../Components/OpenChatIcon';
import { lazyLoadPlayerCharacterTextures, loadCustomTexture } from '../Entity/PlayerTexturesLoadingManager';
import { RemotePlayer } from '../Entity/RemotePlayer';
import type { ActionableItem } from '../Items/ActionableItem';
import type { ItemFactoryInterface } from '../Items/ItemFactoryInterface';
import { SelectCharacterScene, SelectCharacterSceneName } from '../Login/SelectCharacterScene';
2021-06-21 18:22:31 +02:00
import type {
ITiledMap,
ITiledMapLayer,
ITiledMapLayerProperty,
ITiledMapObject,
ITiledTileSet,
2021-07-02 19:03:34 +02:00
} from '../Map/ITiledMap';
import { MenuScene, MenuSceneName } from '../Menu/MenuScene';
import { PlayerAnimationDirections } from '../Player/Animation';
import { hasMovedEventName, Player, requestEmoteEventName } from '../Player/Player';
import { ErrorSceneName } from '../Reconnecting/ErrorScene';
import { ReconnectingSceneName } from '../Reconnecting/ReconnectingScene';
import { UserInputManager } from '../UserInput/UserInputManager';
import type { AddPlayerInterface } from './AddPlayerInterface';
import { gameManager } from './GameManager';
import { GameMap } from './GameMap';
import { PlayerMovement } from './PlayerMovement';
import { PlayersPositionInterpolator } from './PlayersPositionInterpolator';
import Texture = Phaser.Textures.Texture;
import Sprite = Phaser.GameObjects.Sprite;
import CanvasTexture = Phaser.Textures.CanvasTexture;
import GameObject = Phaser.GameObjects.GameObject;
import FILE_LOAD_ERROR = Phaser.Loader.Events.FILE_LOAD_ERROR;
import DOMElement = Phaser.GameObjects.DOMElement;
2021-07-02 19:03:34 +02:00
import { worldFullMessageStream } from '../../Connexion/WorldFullMessageStream';
import { lazyLoadCompanionResource } from '../Companion/CompanionTexturesLoadingManager';
import { DirtyScene } from './DirtyScene';
import { TextUtils } from '../Components/TextUtils';
import { touchScreenManager } from '../../Touch/TouchScreenManager';
import { PinchManager } from '../UserInput/PinchManager';
import { joystickBaseImg, joystickBaseKey, joystickThumbImg, joystickThumbKey } from '../Components/MobileJoystick';
import { waScaleManager } from '../Services/WaScaleManager';
import { EmoteManager } from './EmoteManager';
2021-06-29 18:39:43 +02:00
import EVENT_TYPE = Phaser.Scenes.Events;
import RenderTexture = Phaser.GameObjects.RenderTexture;
import Tilemap = Phaser.Tilemaps.Tilemap;
2021-07-02 19:03:34 +02:00
import type { HasPlayerMovedEvent } from '../../Api/Events/HasPlayerMovedEvent';
2020-04-11 18:17:36 +02:00
2021-07-02 19:03:34 +02:00
import AnimatedTiles from 'phaser-animated-tiles';
import { StartPositionCalculator } from './StartPositionCalculator';
import { soundManager } from './SoundManager';
import { peerStore, screenSharingPeerStore } from '../../Stores/PeerStore';
import { videoFocusStore } from '../../Stores/VideoFocusStore';
import { biggestAvailableAreaStore } from '../../Stores/BiggestAvailableAreaStore';
import { isMessageReferenceEvent, isTriggerMessageEvent } from '../../Api/Events/ui/TriggerMessageEvent';
2021-01-03 12:23:13 +01:00
export interface GameSceneInitInterface {
initPosition: PointInterface | null;
reconnecting: boolean;
}
interface InitUserPositionEventInterface {
2021-07-02 19:03:34 +02:00
type: 'InitUserPositionEvent';
event: MessageUserPositionInterface[];
}
interface AddPlayerEventInterface {
2021-07-02 19:03:34 +02:00
type: 'AddPlayerEvent';
event: AddPlayerInterface;
}
interface RemovePlayerEventInterface {
2021-07-02 19:03:34 +02:00
type: 'RemovePlayerEvent';
userId: number;
}
interface UserMovedEventInterface {
2021-07-02 19:03:34 +02:00
type: 'UserMovedEvent';
event: MessageUserMovedInterface;
}
interface GroupCreatedUpdatedEventInterface {
2021-07-02 19:03:34 +02:00
type: 'GroupCreatedUpdatedEvent';
event: GroupCreatedUpdatedMessageInterface;
}
interface DeleteGroupEventInterface {
2021-07-02 19:03:34 +02:00
type: 'DeleteGroupEvent';
groupId: number;
}
2021-06-17 18:48:56 +02:00
export class GameScene extends DirtyScene {
Terrains: Array<Phaser.Tilemaps.Tileset>;
2021-05-10 17:10:41 +02:00
CurrentPlayer!: Player;
MapPlayers!: Phaser.Physics.Arcade.Group;
MapPlayersByKey: Map<number, RemotePlayer> = new Map<number, RemotePlayer>();
Map!: Phaser.Tilemaps.Tilemap;
Objects!: Array<Phaser.Physics.Arcade.Sprite>;
mapFile!: ITiledMap;
2021-01-03 12:23:13 +01:00
animatedTiles!: AnimatedTiles;
2020-09-21 11:24:03 +02:00
groups: Map<number, Sprite>;
circleTexture!: CanvasTexture;
circleRedTexture!: CanvasTexture;
pendingEvents: Queue<
| InitUserPositionEventInterface
| AddPlayerEventInterface
| RemovePlayerEventInterface
| UserMovedEventInterface
| GroupCreatedUpdatedEventInterface
| DeleteGroupEventInterface
> = new Queue<
| InitUserPositionEventInterface
| AddPlayerEventInterface
| RemovePlayerEventInterface
| UserMovedEventInterface
| GroupCreatedUpdatedEventInterface
| DeleteGroupEventInterface
>();
private initPosition: PositionInterface | null = null;
private playersPositionInterpolator = new PlayersPositionInterpolator();
public connection: RoomConnection | undefined;
private simplePeer!: SimplePeer;
private GlobalMessageManager!: GlobalMessageManager;
2020-07-27 22:36:07 +02:00
private connectionAnswerPromise: Promise<RoomJoinedMessageInterface>;
private connectionAnswerPromiseResolve!: (
value: RoomJoinedMessageInterface | PromiseLike<RoomJoinedMessageInterface>
) => void;
// A promise that will resolve when the "create" method is called (signaling loading is ended)
private createPromise: Promise<void>;
private createPromiseResolve!: (value?: void | PromiseLike<void>) => void;
private iframeSubscriptionList!: Array<Subscription>;
private peerStoreUnsubscribe!: () => void;
private biggestAvailableAreaStoreUnsubscribe!: () => void;
MapUrlFile: string;
RoomId: string;
instance: string;
currentTick!: number;
lastSentTick!: number; // The last tick at which a position was sent.
lastMoveEventSent: HasPlayerMovedEvent = {
2021-07-02 19:03:34 +02:00
direction: '',
moving: false,
x: -1000,
y: -1000,
};
private gameMap!: GameMap;
2020-07-27 22:36:07 +02:00
private actionableItems: Map<number, ActionableItem> = new Map<number, ActionableItem>();
// The item that can be selected by pressing the space key.
private outlinedItem: ActionableItem | null = null;
public userInputManager!: UserInputManager;
private isReconnecting: boolean | undefined = undefined;
private openChatIcon!: OpenChatIcon;
private playerName!: string;
private characterLayers!: string[];
private companion!: string | null;
private messageSubscription: Subscription | null = null;
2021-06-21 18:22:31 +02:00
private popUpElements: Map<number, DOMElement> = new Map<number, Phaser.GameObjects.DOMElement>();
private originalMapUrl: string | undefined;
private pinchManager: PinchManager | undefined;
private mapTransitioning: boolean = false; //used to prevent transitions happenning at the same time.
private emoteManager!: EmoteManager;
2021-06-29 18:39:43 +02:00
private preloading: boolean = true;
2021-06-25 17:57:09 +02:00
startPositionCalculator!: StartPositionCalculator;
constructor(private room: Room, MapUrlFile: string, customKey?: string | undefined) {
super({
key: customKey ?? room.id,
});
this.Terrains = [];
2020-09-21 11:24:03 +02:00
this.groups = new Map<number, Sprite>();
2020-10-13 18:44:50 +02:00
this.instance = room.getInstance();
2020-12-18 15:58:49 +01:00
this.MapUrlFile = MapUrlFile;
2020-10-13 18:44:50 +02:00
this.RoomId = room.id;
this.createPromise = new Promise<void>((resolve, reject): void => {
this.createPromiseResolve = resolve;
});
2020-07-27 22:36:07 +02:00
this.connectionAnswerPromise = new Promise<RoomJoinedMessageInterface>((resolve, reject): void => {
this.connectionAnswerPromiseResolve = resolve;
2021-04-21 18:01:26 +02:00
});
}
//hook preload scene
preload(): void {
2021-01-24 15:57:47 +01:00
const localUser = localUserStore.getLocalUser();
const textures = localUser?.textures;
if (textures) {
for (const texture of textures) {
loadCustomTexture(this.load, texture);
}
}
2021-01-06 17:08:48 +01:00
2021-07-02 19:03:34 +02:00
this.load.image(openChatIconName, 'resources/objects/talk.png');
2021-04-20 11:40:39 +02:00
if (touchScreenManager.supportTouchScreen) {
this.load.image(joystickBaseKey, joystickBaseImg);
this.load.image(joystickThumbKey, joystickThumbImg);
}
2021-07-02 19:03:34 +02:00
this.load.audio('audio-webrtc-in', '/resources/objects/webrtc-in.mp3');
this.load.audio('audio-webrtc-out', '/resources/objects/webrtc-out.mp3');
//this.load.audio('audio-report-message', '/resources/objects/report-message.mp3');
this.sound.pauseOnBlur = false;
this.load.on(FILE_LOAD_ERROR, (file: { src: string }) => {
// If we happen to be in HTTP and we are trying to load a URL in HTTPS only... (this happens only in dev environments)
if (
2021-07-02 19:03:34 +02:00
window.location.protocol === 'http:' &&
file.src === this.MapUrlFile &&
2021-07-02 19:03:34 +02:00
file.src.startsWith('http:') &&
this.originalMapUrl === undefined
) {
this.originalMapUrl = this.MapUrlFile;
2021-07-02 19:03:34 +02:00
this.MapUrlFile = this.MapUrlFile.replace('http://', 'https://');
this.load.tilemapTiledJSON(this.MapUrlFile, this.MapUrlFile);
this.load.on(
2021-07-02 19:03:34 +02:00
'filecomplete-tilemapJSON-' + this.MapUrlFile,
(key: string, type: string, data: unknown) => {
this.onMapLoad(data);
}
);
return;
}
// 127.0.0.1, localhost and *.localhost are considered secure, even on HTTP.
// So if we are in https, we can still try to load a HTTP local resource (can be useful for testing purposes)
// See https://developer.mozilla.org/en-US/docs/Web/Security/Secure_Contexts#when_is_a_context_considered_secure
const url = new URL(file.src);
2021-07-02 19:03:34 +02:00
const host = url.host.split(':')[0];
if (
2021-07-02 19:03:34 +02:00
window.location.protocol === 'https:' &&
file.src === this.MapUrlFile &&
2021-07-02 19:03:34 +02:00
(host === '127.0.0.1' || host === 'localhost' || host.endsWith('.localhost')) &&
this.originalMapUrl === undefined
) {
this.originalMapUrl = this.MapUrlFile;
2021-07-02 19:03:34 +02:00
this.MapUrlFile = this.MapUrlFile.replace('https://', 'http://');
this.load.tilemapTiledJSON(this.MapUrlFile, this.MapUrlFile);
this.load.on(
2021-07-02 19:03:34 +02:00
'filecomplete-tilemapJSON-' + this.MapUrlFile,
(key: string, type: string, data: unknown) => {
this.onMapLoad(data);
}
);
return;
}
2021-06-29 18:39:43 +02:00
//once preloading is over, we don't want loading errors to crash the game, so we need to disable this behavior after preloading.
2021-07-02 19:03:34 +02:00
console.error('Error when loading: ', file);
2021-06-29 18:39:43 +02:00
if (this.preloading) {
this.scene.start(ErrorSceneName, {
2021-07-02 19:03:34 +02:00
title: 'Network error',
subTitle: 'An error occurred while loading resource:',
2021-06-29 18:39:43 +02:00
message: this.originalMapUrl ?? file.src,
});
}
});
2021-07-02 19:03:34 +02:00
this.load.scenePlugin('AnimatedTiles', AnimatedTiles, 'animatedTiles', 'animatedTiles');
this.load.on('filecomplete-tilemapJSON-' + this.MapUrlFile, (key: string, type: string, data: unknown) => {
this.onMapLoad(data);
});
//TODO strategy to add access token
this.load.tilemapTiledJSON(this.MapUrlFile, this.MapUrlFile);
// If the map has already been loaded as part of another GameScene, the "on load" event will not be triggered.
// In this case, we check in the cache to see if the map is here and trigger the event manually.
if (this.cache.tilemap.exists(this.MapUrlFile)) {
const data = this.cache.tilemap.get(this.MapUrlFile);
this.onMapLoad(data);
}
2021-07-02 19:03:34 +02:00
this.load.bitmapFont('main_font', 'resources/fonts/arcade.png', 'resources/fonts/arcade.xml');
//eslint-disable-next-line @typescript-eslint/no-explicit-any
2021-06-02 16:46:28 +02:00
(this.load as any).rexWebFont({
custom: {
2021-07-02 19:03:34 +02:00
families: ['Press Start 2P'],
urls: ['/resources/fonts/fonts.css'],
testString: 'abcdefg',
},
});
2021-05-04 15:47:45 +02:00
//this function must stay at the end of preload function
addLoader(this);
}
// FIXME: we need to put a "unknown" instead of a "any" and validate the structure of the JSON we are receiving.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
private async onMapLoad(data: any): Promise<void> {
// Triggered when the map is loaded
// Load tiles attached to the map recursively
this.mapFile = data.data;
2021-07-02 19:03:34 +02:00
const url = this.MapUrlFile.substr(0, this.MapUrlFile.lastIndexOf('/'));
this.mapFile.tilesets.forEach((tileset) => {
2021-07-02 19:03:34 +02:00
if (typeof tileset.name === 'undefined' || typeof tileset.image === 'undefined') {
console.warn("Don't know how to handle tileset ", tileset);
return;
}
//TODO strategy to add access token
this.load.image(`${url}/${tileset.image}`, `${url}/${tileset.image}`);
});
// Scan the object layers for objects to load and load them.
2020-07-23 18:47:28 +02:00
const objects = new Map<string, ITiledMapObject[]>();
2020-07-23 18:47:28 +02:00
for (const layer of this.mapFile.layers) {
2021-07-02 19:03:34 +02:00
if (layer.type === 'objectgroup') {
2020-07-23 18:47:28 +02:00
for (const object of layer.objects) {
let objectsOfType: ITiledMapObject[] | undefined;
if (!objects.has(object.type)) {
objectsOfType = new Array<ITiledMapObject>();
} else {
objectsOfType = objects.get(object.type);
if (objectsOfType === undefined) {
2021-07-02 19:03:34 +02:00
throw new Error('Unexpected object type not found');
}
}
objectsOfType.push(object);
objects.set(object.type, objectsOfType);
}
}
}
2020-07-23 18:47:28 +02:00
for (const [itemType, objectsOfType] of objects) {
// FIXME: we would ideally need for the loader to WAIT for the import to be performed, which means writing our own loader plugin.
let itemFactory: ItemFactoryInterface;
switch (itemType) {
2021-07-02 19:03:34 +02:00
case 'computer': {
const module = await import('../Items/Computer/computer');
2020-07-23 18:47:28 +02:00
itemFactory = module.default;
break;
2020-07-23 18:47:28 +02:00
}
default:
2021-03-09 18:05:07 +01:00
continue;
//throw new Error('Unsupported object type: "'+ itemType +'"');
}
itemFactory.preload(this.load);
this.load.start(); // Let's manually start the loader because the import might be over AFTER the loading ends.
2021-07-02 19:03:34 +02:00
this.load.on('complete', () => {
// FIXME: the factory might fail because the resources might not be loaded yet...
// We would need to add a loader ended event in addition to the createPromise
2020-07-27 22:36:07 +02:00
this.createPromise.then(async () => {
itemFactory.create(this);
2020-07-27 22:36:07 +02:00
const roomJoinedAnswer = await this.connectionAnswerPromise;
2020-07-23 18:47:28 +02:00
for (const object of objectsOfType) {
// TODO: we should pass here a factory to create sprites (maybe?)
2020-07-27 22:36:07 +02:00
// Do we have a state for this object?
const state = roomJoinedAnswer.items[object.id];
const actionableItem = itemFactory.factory(this, object, state);
this.actionableItems.set(actionableItem.getId(), actionableItem);
}
});
});
}
// Now, let's load the script, if any
const scripts = this.getScriptUrls(this.mapFile);
for (const script of scripts) {
iframeListener.registerScript(script);
}
}
//hook initialisation
init(initData: GameSceneInitInterface) {
2020-06-04 18:11:07 +02:00
if (initData.initPosition !== undefined) {
this.initPosition = initData.initPosition; //todo: still used?
2020-06-04 18:11:07 +02:00
}
if (initData.initPosition !== undefined) {
this.isReconnecting = initData.reconnecting;
}
}
//hook create scene
create(): void {
2021-06-29 18:39:43 +02:00
this.preloading = false;
this.trackDirtyAnims();
gameManager.gameSceneIsCreated(this);
2020-11-25 17:17:48 +01:00
urlManager.pushRoomIdToUrl(this.room);
if (touchScreenManager.supportTouchScreen) {
this.pinchManager = new PinchManager(this);
}
this.messageSubscription = worldFullMessageStream.stream.subscribe((message) =>
this.showWorldFullError(message)
);
const playerName = gameManager.getPlayerName();
if (!playerName) {
2021-07-02 19:03:34 +02:00
throw 'playerName is not set';
}
this.playerName = playerName;
this.characterLayers = gameManager.getCharacterLayers();
2021-04-02 21:21:11 +02:00
this.companion = gameManager.getCompanion();
2020-04-13 15:34:09 +02:00
//initalise map
this.Map = this.add.tilemap(this.MapUrlFile);
2021-07-02 19:03:34 +02:00
const mapDirUrl = this.MapUrlFile.substr(0, this.MapUrlFile.lastIndexOf('/'));
this.mapFile.tilesets.forEach((tileset: ITiledTileSet) => {
this.Terrains.push(
this.Map.addTilesetImage(
tileset.name,
`${mapDirUrl}/${tileset.image}`,
tileset.tilewidth,
tileset.tileheight,
tileset.margin,
tileset.spacing /*, tileset.firstgid*/
)
);
});
2020-04-13 15:34:09 +02:00
//permit to set bound collision
this.physics.world.setBounds(0, 0, this.Map.widthInPixels, this.Map.heightInPixels);
2020-04-13 15:34:09 +02:00
//add layer on map
this.gameMap = new GameMap(this.mapFile, this.Map, this.Terrains);
for (const layer of this.gameMap.flatLayers) {
2021-07-02 19:03:34 +02:00
if (layer.type === 'tilelayer') {
const exitSceneUrl = this.getExitSceneUrl(layer);
if (exitSceneUrl !== undefined) {
this.loadNextGame(exitSceneUrl);
}
const exitUrl = this.getExitUrl(layer);
if (exitUrl !== undefined) {
this.loadNextGame(exitUrl);
}
}
2021-07-02 19:03:34 +02:00
if (layer.type === 'objectgroup') {
for (const object of layer.objects) {
if (object.text) {
TextUtils.createTextFromITiledMapObject(this, object);
}
}
}
2020-06-07 23:00:05 +02:00
}
2020-04-13 15:34:09 +02:00
this.gameMap.exitUrls.forEach((exitUrl) => {
this.loadNextGame(exitUrl);
});
this.startPositionCalculator = new StartPositionCalculator(
this.gameMap,
this.mapFile,
this.initPosition,
urlManager.getStartLayerNameFromUrl()
);
2020-04-13 15:34:09 +02:00
//add entities
this.Objects = new Array<Phaser.Physics.Arcade.Sprite>();
//initialise list of other player
this.MapPlayers = this.physics.add.group({ immovable: true });
2020-04-13 15:34:09 +02:00
2020-07-27 22:36:07 +02:00
//create input to move
this.userInputManager = new UserInputManager(this);
2020-11-10 12:38:32 +01:00
mediaManager.setUserInputManager(this.userInputManager);
if (localUserStore.getFullscreen()) {
2021-07-02 19:03:34 +02:00
document.querySelector('body')?.requestFullscreen();
}
//notify game manager can to create currentUser in map
this.createCurrentPlayer();
this.removeAllRemotePlayers(); //cleanup the list of remote players in case the scene was rebooted
this.initCamera();
2021-01-03 12:23:13 +01:00
this.animatedTiles.init(this.Map);
2021-07-02 19:03:34 +02:00
this.events.on('tileanimationupdate', () => (this.dirty = true));
this.initCirclesCanvas();
// Let's pause the scene if the connection is not established yet
if (!this.room.isDisconnected()) {
if (this.isReconnecting) {
setTimeout(() => {
this.scene.sleep();
this.scene.launch(ReconnectingSceneName);
}, 0);
} else if (this.connection === undefined) {
// Let's wait 1 second before printing the "connecting" screen to avoid blinking
setTimeout(() => {
if (this.connection === undefined) {
this.scene.sleep();
this.scene.launch(ReconnectingSceneName);
}
}, 1000);
}
}
this.createPromiseResolve();
2020-07-27 22:36:07 +02:00
this.userInputManager.spaceEvent(() => {
2020-07-27 22:36:07 +02:00
this.outlinedItem?.activate();
});
this.openChatIcon = new OpenChatIcon(this, 2, this.game.renderer.height - 2);
2020-08-17 15:20:03 +02:00
this.reposition();
// From now, this game scene will be notified of reposition events
this.biggestAvailableAreaStoreUnsubscribe = biggestAvailableAreaStore.subscribe((box) =>
this.updateCameraOffset(box)
);
this.triggerOnMapLayerPropertyChange();
this.listenToIframeEvents();
2020-10-12 11:22:41 +02:00
if (!this.room.isDisconnected()) {
this.connect();
}
this.emoteManager = new EmoteManager(this);
let oldPeerNumber = 0;
this.peerStoreUnsubscribe = peerStore.subscribe((peers) => {
const newPeerNumber = peers.size;
if (newPeerNumber > oldPeerNumber) {
2021-07-02 19:03:34 +02:00
this.sound.play('audio-webrtc-in', {
volume: 0.2,
});
} else if (newPeerNumber < oldPeerNumber) {
2021-07-02 19:03:34 +02:00
this.sound.play('audio-webrtc-out', {
volume: 0.2,
});
}
oldPeerNumber = newPeerNumber;
});
}
/**
* Initializes the connection to Pusher.
*/
private connect(): void {
const camera = this.cameras.main;
connectionManager
.connectToRoomSocket(
this.RoomId,
this.playerName,
this.characterLayers,
{
...this.startPositionCalculator.startPosition,
},
{
left: camera.scrollX,
top: camera.scrollY,
right: camera.scrollX + camera.width,
bottom: camera.scrollY + camera.height,
},
this.companion
)
.then((onConnect: OnConnectInterface) => {
this.connection = onConnect.connection;
this.connection.onUserJoins((message: MessageUserJoined) => {
const userMessage: AddPlayerInterface = {
userId: message.userId,
characterLayers: message.characterLayers,
name: message.name,
position: message.position,
visitCardUrl: message.visitCardUrl,
companion: message.companion,
};
this.addPlayer(userMessage);
});
this.connection.onUserMoved((message: UserMovedMessage) => {
const position = message.getPosition();
if (position === undefined) {
2021-07-02 19:03:34 +02:00
throw new Error('Position missing from UserMovedMessage');
}
const messageUserMoved: MessageUserMovedInterface = {
userId: message.getUserid(),
position: ProtobufClientUtils.toPointInterface(position),
};
this.updatePlayerPosition(messageUserMoved);
});
this.connection.onUserLeft((userId: number) => {
this.removePlayer(userId);
});
this.connection.onGroupUpdatedOrCreated((groupPositionMessage: GroupCreatedUpdatedMessageInterface) => {
this.shareGroupPosition(groupPositionMessage);
});
this.connection.onGroupDeleted((groupId: number) => {
try {
this.deleteGroup(groupId);
} catch (e) {
console.error(e);
}
});
this.connection.onServerDisconnected(() => {
2021-07-02 19:03:34 +02:00
console.log('Player disconnected from server. Reloading scene.');
this.cleanupClosingScene();
2021-07-02 19:03:34 +02:00
const gameSceneKey = 'somekey' + Math.round(Math.random() * 10000);
const game: Phaser.Scene = new GameScene(this.room, this.MapUrlFile, gameSceneKey);
this.scene.add(gameSceneKey, game, true, {
initPosition: {
x: this.CurrentPlayer.x,
y: this.CurrentPlayer.y,
},
reconnecting: true,
});
this.scene.stop(this.scene.key);
this.scene.remove(this.scene.key);
});
2020-10-16 19:13:26 +02:00
this.connection.onActionableEvent((message) => {
const item = this.actionableItems.get(message.itemId);
if (item === undefined) {
console.warn(
'Received an event about object "' +
message.itemId +
'" but cannot find this item on the map.'
);
return;
}
item.fire(message.event, message.state, message.parameters);
});
/**
* Triggered when we receive the JWT token to connect to Jitsi
*/
this.connection.onStartJitsiRoom((jwt, room) => {
this.startJitsi(room, jwt);
});
// When connection is performed, let's connect SimplePeer
this.simplePeer = new SimplePeer(this.connection, !this.room.isPublic, this.playerName);
peerStore.connectToSimplePeer(this.simplePeer);
screenSharingPeerStore.connectToSimplePeer(this.simplePeer);
videoFocusStore.connectToSimplePeer(this.simplePeer);
this.GlobalMessageManager = new GlobalMessageManager(this.connection);
userMessageManager.setReceiveBanListener(this.bannedUser.bind(this));
const self = this;
this.simplePeer.registerPeerConnectionListener({
onConnect(peer) {
self.openChatIcon.setVisible(true);
audioManager.decreaseVolume();
},
onDisconnect(userId: number) {
if (self.simplePeer.getNbConnections() === 0) {
self.openChatIcon.setVisible(false);
audioManager.restoreVolume();
}
},
});
//listen event to share position of user
this.CurrentPlayer.on(hasMovedEventName, this.pushPlayerPosition.bind(this));
this.CurrentPlayer.on(hasMovedEventName, this.outlineItem.bind(this));
this.CurrentPlayer.on(hasMovedEventName, (event: HasPlayerMovedEvent) => {
this.gameMap.setPosition(event.x, event.y);
});
//this.initUsersPosition(roomJoinedMessage.users);
this.connectionAnswerPromiseResolve(onConnect.room);
// Analyze tags to find if we are admin. If yes, show console.
this.scene.wake();
this.scene.stop(ReconnectingSceneName);
//init user position and play trigger to check layers properties
this.gameMap.setPosition(this.CurrentPlayer.x, this.CurrentPlayer.y);
});
2020-08-17 15:20:03 +02:00
}
//todo: into dedicated classes
private initCirclesCanvas(): void {
// Let's generate the circle for the group delimiter
let circleElement = Object.values(this.textures.list).find(
2021-07-02 19:03:34 +02:00
(object: Texture) => object.key === 'circleSprite-white'
);
if (circleElement) {
2021-07-02 19:03:34 +02:00
this.textures.remove('circleSprite-white');
}
2021-07-02 19:03:34 +02:00
circleElement = Object.values(this.textures.list).find((object: Texture) => object.key === 'circleSprite-red');
if (circleElement) {
2021-07-02 19:03:34 +02:00
this.textures.remove('circleSprite-red');
}
//create white circle canvas use to create sprite
2021-07-02 19:03:34 +02:00
this.circleTexture = this.textures.createCanvas('circleSprite-white', 96, 96);
const context = this.circleTexture.context;
context.beginPath();
context.arc(48, 48, 48, 0, 2 * Math.PI, false);
// context.lineWidth = 5;
2021-07-02 19:03:34 +02:00
context.strokeStyle = '#ffffff';
context.stroke();
this.circleTexture.refresh();
//create red circle canvas use to create sprite
2021-07-02 19:03:34 +02:00
this.circleRedTexture = this.textures.createCanvas('circleSprite-red', 96, 96);
const contextRed = this.circleRedTexture.context;
contextRed.beginPath();
contextRed.arc(48, 48, 48, 0, 2 * Math.PI, false);
//context.lineWidth = 5;
2021-07-02 19:03:34 +02:00
contextRed.strokeStyle = '#ff0000';
contextRed.stroke();
this.circleRedTexture.refresh();
}
private safeParseJSONstring(jsonString: string | undefined, propertyName: string) {
2021-02-09 20:31:49 +01:00
try {
return jsonString ? JSON.parse(jsonString) : {};
} catch (e) {
console.warn('Invalid JSON found in property "' + propertyName + '" of the map:' + jsonString, e);
return {};
2021-02-09 20:31:49 +01:00
}
}
private triggerOnMapLayerPropertyChange() {
2021-07-02 19:03:34 +02:00
this.gameMap.onPropertyChange('exitSceneUrl', (newValue, oldValue) => {
if (newValue) this.onMapExit(newValue as string);
2021-02-17 19:29:59 +01:00
});
2021-07-02 19:03:34 +02:00
this.gameMap.onPropertyChange('exitUrl', (newValue, oldValue) => {
2021-01-25 13:18:57 +01:00
if (newValue) this.onMapExit(newValue as string);
});
2021-07-02 19:03:34 +02:00
this.gameMap.onPropertyChange('openWebsite', (newValue, oldValue, allProps) => {
if (newValue === undefined) {
2021-07-02 19:03:34 +02:00
layoutManager.removeActionButton('openWebsite', this.userInputManager);
coWebsiteManager.closeCoWebsite();
} else {
const openWebsiteFunction = () => {
coWebsiteManager.loadCoWebsite(
newValue as string,
this.MapUrlFile,
2021-07-02 19:03:34 +02:00
allProps.get('openWebsiteAllowApi') as boolean | undefined,
allProps.get('openWebsitePolicy') as string | undefined
);
2021-07-02 19:03:34 +02:00
layoutManager.removeActionButton('openWebsite', this.userInputManager);
};
2020-11-23 20:34:05 +01:00
const openWebsiteTriggerValue = allProps.get(TRIGGER_WEBSITE_PROPERTIES);
if (openWebsiteTriggerValue && openWebsiteTriggerValue === ON_ACTION_TRIGGER_BUTTON) {
let message = allProps.get(WEBSITE_MESSAGE_PROPERTIES);
if (message === undefined) {
2021-07-02 19:03:34 +02:00
message = 'Press SPACE or touch here to open web site';
}
layoutManager.addActionButton(
2021-07-02 19:03:34 +02:00
'openWebsite',
message.toString(),
() => {
openWebsiteFunction();
},
this.userInputManager
);
} else {
openWebsiteFunction();
}
}
});
2021-07-02 19:03:34 +02:00
this.gameMap.onPropertyChange('jitsiRoom', (newValue, oldValue, allProps) => {
if (newValue === undefined) {
2021-07-02 19:03:34 +02:00
layoutManager.removeActionButton('jitsiRoom', this.userInputManager);
this.stopJitsi();
} else {
const openJitsiRoomFunction = () => {
const roomName = jitsiFactory.getRoomName(newValue.toString(), this.instance);
2021-07-02 19:03:34 +02:00
const jitsiUrl = allProps.get('jitsiUrl') as string | undefined;
if (JITSI_PRIVATE_MODE && !jitsiUrl) {
2021-07-02 19:03:34 +02:00
const adminTag = allProps.get('jitsiRoomAdminTag') as string | undefined;
this.connection?.emitQueryJitsiJwtMessage(roomName, adminTag);
} else {
this.startJitsi(roomName, undefined);
}
2021-07-02 19:03:34 +02:00
layoutManager.removeActionButton('jitsiRoom', this.userInputManager);
};
2020-11-23 20:34:05 +01:00
const jitsiTriggerValue = allProps.get(TRIGGER_JITSI_PROPERTIES);
if (jitsiTriggerValue && jitsiTriggerValue === ON_ACTION_TRIGGER_BUTTON) {
let message = allProps.get(JITSI_MESSAGE_PROPERTIES);
if (message === undefined) {
2021-07-02 19:03:34 +02:00
message = 'Press SPACE or touch here to enter Jitsi Meet room';
}
layoutManager.addActionButton(
2021-07-02 19:03:34 +02:00
'jitsiRoom',
message.toString(),
() => {
openJitsiRoomFunction();
},
this.userInputManager
);
} else {
openJitsiRoomFunction();
}
}
});
2021-07-02 19:03:34 +02:00
this.gameMap.onPropertyChange('silent', (newValue, oldValue) => {
if (newValue === undefined || newValue === false || newValue === '') {
this.connection?.setSilent(false);
} else {
this.connection?.setSilent(true);
}
});
2021-07-02 19:03:34 +02:00
this.gameMap.onPropertyChange('playAudio', (newValue, oldValue, allProps) => {
const volume = allProps.get(AUDIO_VOLUME_PROPERTY) as number | undefined;
const loop = allProps.get(AUDIO_LOOP_PROPERTY) as boolean | undefined;
newValue === undefined
? audioManager.unloadAudio()
: audioManager.playAudio(newValue, this.getMapDirUrl(), volume, loop);
2021-01-03 12:45:18 +01:00
});
// TODO: This legacy property should be removed at some point
2021-07-02 19:03:34 +02:00
this.gameMap.onPropertyChange('playAudioLoop', (newValue, oldValue) => {
newValue === undefined
? audioManager.unloadAudio()
: audioManager.playAudio(newValue, this.getMapDirUrl(), undefined, true);
2021-01-03 12:45:18 +01:00
});
2021-07-02 19:03:34 +02:00
this.gameMap.onPropertyChange('zone', (newValue, oldValue) => {
if (newValue === undefined || newValue === false || newValue === '') {
iframeListener.sendLeaveEvent(oldValue as string);
} else {
iframeListener.sendEnterEvent(newValue as string);
}
});
}
private listenToIframeEvents(): void {
this.iframeSubscriptionList = [];
this.iframeSubscriptionList.push(
iframeListener.openPopupStream.subscribe((openPopupEvent) => {
let objectLayerSquare: ITiledMapObject;
const targetObjectData = this.getObjectLayerData(openPopupEvent.targetObject);
if (targetObjectData !== undefined) {
objectLayerSquare = targetObjectData;
} else {
console.error(
"Error while opening a popup. Cannot find an object on the map with name '" +
openPopupEvent.targetObject +
"'. The first parameter of WA.openPopup() must be the name of a rectangle object in your map."
);
return;
}
const escapedMessage = HtmlUtils.escapeHtml(openPopupEvent.message);
let html = `<div id="container" hidden><div class="nes-container with-title is-centered">
${escapedMessage}
2021-04-09 14:35:15 +02:00
</div> `;
2021-07-02 19:03:34 +02:00
const buttonContainer = '<div class="buttonContainer"</div>';
html += buttonContainer;
let id = 0;
for (const button of openPopupEvent.buttons) {
html += `<button type="button" class="nes-btn is-${HtmlUtils.escapeHtml(
2021-07-02 19:03:34 +02:00
button.className ?? ''
)}" id="popup-${openPopupEvent.popupId}-${id}">${HtmlUtils.escapeHtml(button.label)}</button>`;
id++;
2021-03-09 18:05:07 +01:00
}
2021-07-02 19:03:34 +02:00
html += '</div>';
const domElement = this.add.dom(objectLayerSquare.x, objectLayerSquare.y).createFromHTML(html);
2021-07-02 19:03:34 +02:00
const container: HTMLDivElement = domElement.getChildByID('container') as HTMLDivElement;
container.style.width = objectLayerSquare.width + 'px';
domElement.scale = 0;
2021-07-02 19:03:34 +02:00
domElement.setClassName('popUpElement');
setTimeout(() => {
container.hidden = false;
}, 100);
id = 0;
for (const button of openPopupEvent.buttons) {
const button = HtmlUtils.getElementByIdOrFail<HTMLButtonElement>(
`popup-${openPopupEvent.popupId}-${id}`
);
const btnId = id;
button.onclick = () => {
iframeListener.sendButtonClickedEvent(openPopupEvent.popupId, btnId);
button.disabled = true;
};
id++;
}
this.tweens.add({
targets: domElement,
scale: 1,
2021-07-02 19:03:34 +02:00
ease: 'EaseOut',
duration: 400,
});
this.popUpElements.set(openPopupEvent.popupId, domElement);
2021-04-21 11:20:17 +02:00
})
);
this.iframeSubscriptionList.push(
iframeListener.closePopupStream.subscribe((closePopupEvent) => {
const popUpElement = this.popUpElements.get(closePopupEvent.popupId);
if (popUpElement === undefined) {
console.error(
2021-07-02 19:03:34 +02:00
'Could not close popup with ID ',
closePopupEvent.popupId,
2021-07-02 19:03:34 +02:00
'. Maybe it has already been closed?'
);
}
this.tweens.add({
targets: popUpElement,
scale: 0,
2021-07-02 19:03:34 +02:00
ease: 'EaseOut',
duration: 400,
onComplete: () => {
popUpElement?.destroy();
this.popUpElements.delete(closePopupEvent.popupId);
},
});
})
);
this.iframeSubscriptionList.push(
iframeListener.disablePlayerControlStream.subscribe(() => {
this.userInputManager.disableControls();
})
);
this.iframeSubscriptionList.push(
iframeListener.playSoundStream.subscribe((playSoundEvent) => {
const url = new URL(playSoundEvent.url, this.MapUrlFile);
soundManager.playSound(this.load, this.sound, url.toString(), playSoundEvent.config);
})
);
this.iframeSubscriptionList.push(
iframeListener.stopSoundStream.subscribe((stopSoundEvent) => {
const url = new URL(stopSoundEvent.url, this.MapUrlFile);
soundManager.stopSound(this.sound, url.toString());
})
);
this.iframeSubscriptionList.push(
iframeListener.loadSoundStream.subscribe((loadSoundEvent) => {
const url = new URL(loadSoundEvent.url, this.MapUrlFile);
soundManager.loadSound(this.load, this.sound, url.toString());
2021-04-21 11:20:17 +02:00
})
);
this.iframeSubscriptionList.push(
iframeListener.enablePlayerControlStream.subscribe(() => {
this.userInputManager.restoreControls();
2021-04-21 11:20:17 +02:00
})
);
this.iframeSubscriptionList.push(
iframeListener.loadPageStream.subscribe((url: string) => {
this.loadNextGame(url).then(() => {
this.events.once(EVENT_TYPE.POST_UPDATE, () => {
this.onMapExit(url);
});
});
})
);
2021-06-21 18:22:31 +02:00
let scriptedBubbleSprite: Sprite;
this.iframeSubscriptionList.push(
iframeListener.displayBubbleStream.subscribe(() => {
scriptedBubbleSprite = new Sprite(
this,
this.CurrentPlayer.x + 25,
this.CurrentPlayer.y,
2021-07-02 19:03:34 +02:00
'circleSprite-white'
);
scriptedBubbleSprite.setDisplayOrigin(48, 48);
this.add.existing(scriptedBubbleSprite);
})
);
this.iframeSubscriptionList.push(
iframeListener.removeBubbleStream.subscribe(() => {
scriptedBubbleSprite.destroy();
})
);
this.iframeSubscriptionList.push(
iframeListener.showLayerStream.subscribe((layerEvent) => {
this.setLayerVisibility(layerEvent.name, true);
})
);
this.iframeSubscriptionList.push(
iframeListener.hideLayerStream.subscribe((layerEvent) => {
this.setLayerVisibility(layerEvent.name, false);
})
);
this.iframeSubscriptionList.push(
iframeListener.setPropertyStream.subscribe((setProperty) => {
this.setPropertyLayer(setProperty.layerName, setProperty.propertyName, setProperty.propertyValue);
2021-05-25 17:21:02 +02:00
})
);
this.iframeSubscriptionList.push(
iframeListener.dataLayerChangeStream.subscribe(() => {
iframeListener.sendDataLayerEvent({ data: this.gameMap.getMap() });
})
);
2021-07-02 19:03:34 +02:00
iframeListener.registerAnswerer('getState', () => {
return {
2021-05-25 17:21:02 +02:00
mapUrl: this.MapUrlFile,
startLayerName: this.startPositionCalculator.startLayerName,
2021-05-25 17:21:02 +02:00
uuid: localUserStore.getLocalUser()?.uuid,
nickname: localUserStore.getName(),
roomId: this.RoomId,
tags: this.connection ? this.connection.getAllTags() : [],
};
});
this.iframeSubscriptionList.push(
iframeListener.setTilesStream.subscribe((eventTiles) => {
for (const eventTile of eventTiles) {
this.gameMap.putTile(eventTile.tile, eventTile.x, eventTile.y, eventTile.layer);
}
2021-05-25 17:21:02 +02:00
})
);
iframeListener.registerAnswerer(
2021-07-02 19:03:34 +02:00
'triggerMessage',
(message) =>
new Promise((resolver) => {
layoutManager.addActionButton(
message.uuid,
message.message,
() => {
layoutManager.removeActionButton(message.uuid, this.userInputManager);
resolver();
},
this.userInputManager
);
}),
isTriggerMessageEvent
);
2021-06-23 17:32:32 +02:00
iframeListener.registerAnswerer(
2021-07-02 19:03:34 +02:00
'removeTriggerMessage',
(message) => {
2021-06-23 17:32:32 +02:00
layoutManager.removeActionButton(message.uuid, this.userInputManager);
},
isMessageReferenceEvent
);
}
private setPropertyLayer(
layerName: string,
propertyName: string,
propertyValue: string | number | boolean | undefined
): void {
const layer = this.gameMap.findLayer(layerName);
2021-06-23 17:32:32 +02:00
if (layer === undefined) {
console.warn('Could not find layer "' + layerName + '" when calling setProperty');
return;
}
if (layer.properties === undefined) {
2021-06-23 17:32:32 +02:00
layer.properties = [];
}
2021-07-02 14:45:27 +02:00
const property = layer.properties.find((property) => property.name === propertyName);
if (property === undefined) {
2021-06-23 17:32:32 +02:00
layer.properties.push({ name: propertyName, type: typeof propertyValue, value: propertyValue });
return;
}
property.value = propertyValue;
}
2021-05-10 11:20:07 +02:00
private setLayerVisibility(layerName: string, visible: boolean): void {
const phaserLayer = this.gameMap.findPhaserLayer(layerName);
if (phaserLayer === undefined) {
2021-05-10 11:20:07 +02:00
console.warn('Could not find layer "' + layerName + '" when calling WA.hideLayer / WA.showLayer');
return;
}
phaserLayer.setVisible(visible);
2021-05-10 11:20:07 +02:00
this.dirty = true;
}
private getMapDirUrl(): string {
2021-07-02 19:03:34 +02:00
return this.MapUrlFile.substr(0, this.MapUrlFile.lastIndexOf('/'));
}
private onMapExit(exitKey: string) {
if (this.mapTransitioning) return;
this.mapTransitioning = true;
2021-06-21 18:22:31 +02:00
const { roomId, hash } = Room.getIdFromIdentifier(exitKey, this.MapUrlFile, this.instance);
2021-07-02 19:03:34 +02:00
if (!roomId) throw new Error('Could not find the room from its exit key: ' + exitKey);
2021-06-23 12:42:24 +02:00
if (hash) {
urlManager.pushStartLayerNameToUrl(hash);
}
const menuScene: MenuScene = this.scene.get(MenuSceneName) as MenuScene;
menuScene.reset();
if (roomId !== this.scene.key) {
if (this.scene.get(roomId) === null) {
2021-07-02 19:03:34 +02:00
console.error('next room not loaded', exitKey);
return;
}
this.cleanupClosingScene();
this.scene.stop();
this.scene.remove(this.scene.key);
this.scene.start(roomId);
} else {
//if the exit points to the current map, we simply teleport the user back to the startLayer
2021-06-25 17:57:09 +02:00
this.startPositionCalculator.initPositionFromLayerName(hash, hash);
2021-06-25 18:03:43 +02:00
this.CurrentPlayer.x = this.startPositionCalculator.startPosition.x;
this.CurrentPlayer.y = this.startPositionCalculator.startPosition.y;
setTimeout(() => (this.mapTransitioning = false), 500);
}
}
public cleanupClosingScene(): void {
// stop playing audio, close any open website, stop any open Jitsi
coWebsiteManager.closeCoWebsite();
// Stop the script, if any
const scripts = this.getScriptUrls(this.mapFile);
for (const script of scripts) {
iframeListener.unregisterScript(script);
}
this.stopJitsi();
audioManager.unloadAudio();
// We are completely destroying the current scene to avoid using a half-backed instance when coming back to the same map.
this.connection?.closeConnection();
this.simplePeer?.closeAllConnections();
this.simplePeer?.unregister();
this.messageSubscription?.unsubscribe();
this.userInputManager.destroy();
this.pinchManager?.destroy();
this.emoteManager.destroy();
this.peerStoreUnsubscribe();
this.biggestAvailableAreaStoreUnsubscribe();
2021-07-02 19:03:34 +02:00
iframeListener.unregisterAnswerer('getState');
2021-05-31 12:06:11 +02:00
mediaManager.hideGameOverlay();
for (const iframeEvents of this.iframeSubscriptionList) {
iframeEvents.unsubscribe();
}
}
private removeAllRemotePlayers(): void {
this.MapPlayersByKey.forEach((player: RemotePlayer) => {
player.destroy();
if (player.companion) {
player.companion.destroy();
}
this.MapPlayers.remove(player);
});
this.MapPlayersByKey = new Map<number, RemotePlayer>();
}
private getExitUrl(layer: ITiledMapLayer): string | undefined {
2021-07-02 19:03:34 +02:00
return this.getProperty(layer, 'exitUrl') as string | undefined;
}
/**
* @deprecated the map property exitSceneUrl is deprecated
*/
private getExitSceneUrl(layer: ITiledMapLayer): string | undefined {
2021-07-02 19:03:34 +02:00
return this.getProperty(layer, 'exitSceneUrl') as string | undefined;
}
private getScriptUrls(map: ITiledMap): string[] {
2021-07-02 19:03:34 +02:00
return (this.getProperties(map, 'script') as string[]).map((script) =>
new URL(script, this.MapUrlFile).toString()
);
}
private getProperty(layer: ITiledMapLayer | ITiledMap, name: string): string | boolean | number | undefined {
const properties: ITiledMapLayerProperty[] | undefined = layer.properties;
if (!properties) {
return undefined;
}
const obj = properties.find(
(property: ITiledMapLayerProperty) => property.name.toLowerCase() === name.toLowerCase()
);
if (obj === undefined) {
return undefined;
}
return obj.value;
}
private getProperties(layer: ITiledMapLayer | ITiledMap, name: string): (string | number | boolean | undefined)[] {
const properties: ITiledMapLayerProperty[] | undefined = layer.properties;
if (!properties) {
return [];
}
return properties
.filter((property: ITiledMapLayerProperty) => property.name.toLowerCase() === name.toLowerCase())
.map((property) => property.value);
}
//todo: push that into the gameManager
2021-06-23 17:32:32 +02:00
private loadNextGame(exitSceneIdentifier: string): Promise<void> {
const { roomId, hash } = Room.getIdFromIdentifier(exitSceneIdentifier, this.MapUrlFile, this.instance);
2020-10-13 18:44:50 +02:00
const room = new Room(roomId);
return gameManager.loadMap(room, this.scene).catch(() => {});
}
//todo: in a dedicated class/function?
initCamera() {
this.cameras.main.setBounds(0, 0, this.Map.widthInPixels, this.Map.heightInPixels);
2021-04-21 18:01:26 +02:00
this.cameras.main.startFollow(this.CurrentPlayer, true);
biggestAvailableAreaStore.recompute();
}
2020-04-13 15:34:09 +02:00
createCollisionWithPlayer() {
//add collision layer
for (const phaserLayer of this.gameMap.phaserLayers) {
2021-06-24 12:10:44 +02:00
this.physics.add.collider(this.CurrentPlayer, phaserLayer, (object1: GameObject, object2: GameObject) => {
//this.CurrentPlayer.say("Collision with layer : "+ (object2 as Tile).layer.name)
2020-04-13 15:34:09 +02:00
});
phaserLayer.setCollisionByProperty({ collides: true });
2020-04-13 19:40:10 +02:00
if (DEBUG_MODE) {
//debug code to see the collision hitbox of the object in the top layer
2021-06-24 12:10:44 +02:00
phaserLayer.renderDebug(this.add.graphics(), {
2020-04-13 19:40:10 +02:00
tileColor: null, //non-colliding tiles
collidingTileColor: new Phaser.Display.Color(243, 134, 48, 200), // Colliding tiles,
faceColor: new Phaser.Display.Color(40, 39, 37, 255), // Colliding face edges
2020-04-13 19:40:10 +02:00
});
}
2021-06-24 12:10:44 +02:00
//});
}
2020-04-13 15:34:09 +02:00
}
createCurrentPlayer() {
//TODO create animation moving between exit and start
2021-01-26 15:21:23 +01:00
const texturesPromise = lazyLoadPlayerCharacterTextures(this.load, this.characterLayers);
2020-12-18 14:30:46 +01:00
try {
this.CurrentPlayer = new Player(
this,
2021-06-25 18:03:43 +02:00
this.startPositionCalculator.startPosition.x,
this.startPositionCalculator.startPosition.y,
2020-12-18 14:30:46 +01:00
this.playerName,
texturesPromise,
PlayerAnimationDirections.Down,
2020-12-18 14:30:46 +01:00
false,
2021-04-02 21:21:11 +02:00
this.userInputManager,
this.companion,
this.companion !== null ? lazyLoadCompanionResource(this.load, this.companion) : undefined
2020-12-18 14:30:46 +01:00
);
2021-07-02 19:03:34 +02:00
this.CurrentPlayer.on('pointerdown', (pointer: Phaser.Input.Pointer) => {
if (pointer.wasTouch && (pointer.event as TouchEvent).touches.length > 1) {
return; //we don't want the menu to open when pinching on a touch screen.
}
this.emoteManager
.getMenuImages()
.then((emoteMenuElements) => this.CurrentPlayer.openOrCloseEmoteMenu(emoteMenuElements));
});
2021-05-10 17:10:41 +02:00
this.CurrentPlayer.on(requestEmoteEventName, (emoteKey: string) => {
this.connection?.emitEmoteEvent(emoteKey);
});
} catch (err) {
if (err instanceof TextureError) {
2020-12-18 14:30:46 +01:00
gameManager.leaveGame(this, SelectCharacterSceneName, new SelectCharacterScene());
}
2020-12-18 16:30:22 +01:00
throw err;
2020-12-18 14:30:46 +01:00
}
2020-04-13 15:34:09 +02:00
//create collision
this.createCollisionWithPlayer();
}
2020-05-04 18:38:04 +02:00
pushPlayerPosition(event: HasPlayerMovedEvent) {
if (this.lastMoveEventSent === event) {
return;
}
// If the player is not moving, let's send the info right now.
if (event.moving === false) {
this.doPushPlayerPosition(event);
return;
}
// If the player is moving, and if it changed direction, let's send an event
if (event.direction !== this.lastMoveEventSent.direction) {
this.doPushPlayerPosition(event);
return;
}
// If more than 200ms happened since last event sent
if (this.currentTick - this.lastSentTick >= POSITION_DELAY) {
this.doPushPlayerPosition(event);
return;
}
// Otherwise, do nothing.
}
/**
* Finds the correct item to outline and outline it (if there is an item to be outlined)
* @param event
*/
private outlineItem(event: HasPlayerMovedEvent): void {
let x = event.x;
let y = event.y;
switch (event.direction) {
case PlayerAnimationDirections.Up:
y -= 32;
break;
case PlayerAnimationDirections.Down:
y += 32;
break;
case PlayerAnimationDirections.Left:
x -= 32;
break;
case PlayerAnimationDirections.Right:
x += 32;
break;
default:
throw new Error('Unexpected direction "' + event.direction + '"');
}
let shortestDistance: number = Infinity;
let selectedItem: ActionableItem | null = null;
2020-07-27 22:36:07 +02:00
for (const item of this.actionableItems.values()) {
2020-07-23 18:47:28 +02:00
const distance = item.actionableDistance(x, y);
if (distance !== null && distance < shortestDistance) {
shortestDistance = distance;
selectedItem = item;
}
}
if (this.outlinedItem === selectedItem) {
return;
}
this.outlinedItem?.notSelectable();
this.outlinedItem = selectedItem;
this.outlinedItem?.selectable();
}
private doPushPlayerPosition(event: HasPlayerMovedEvent): void {
this.lastMoveEventSent = event;
this.lastSentTick = this.currentTick;
const camera = this.cameras.main;
this.connection?.sharePosition(event.x, event.y, event.direction, event.moving, {
left: camera.scrollX,
top: camera.scrollY,
right: camera.scrollX + camera.width,
bottom: camera.scrollY + camera.height,
});
iframeListener.hasPlayerMoved(event);
2020-04-13 15:34:09 +02:00
}
/**
* @param time
* @param delta The delta time in ms since the last frame. This is a smoothed and capped value based on the FPS rate.
*/
update(time: number, delta: number): void {
this.dirty = false;
this.currentTick = time;
this.CurrentPlayer.moveUser(delta);
// Let's handle all events
2020-06-19 18:24:32 +02:00
while (this.pendingEvents.length !== 0) {
this.dirty = true;
2020-06-19 18:24:32 +02:00
const event = this.pendingEvents.dequeue();
switch (event.type) {
2021-07-02 19:03:34 +02:00
case 'InitUserPositionEvent':
this.doInitUsersPosition(event.event);
break;
2021-07-02 19:03:34 +02:00
case 'AddPlayerEvent':
this.doAddPlayer(event.event);
break;
2021-07-02 19:03:34 +02:00
case 'RemovePlayerEvent':
this.doRemovePlayer(event.userId);
break;
2021-07-02 19:03:34 +02:00
case 'UserMovedEvent':
this.doUpdatePlayerPosition(event.event);
break;
2021-07-02 19:03:34 +02:00
case 'GroupCreatedUpdatedEvent':
this.doShareGroupPosition(event.event);
break;
2021-07-02 19:03:34 +02:00
case 'DeleteGroupEvent':
this.doDeleteGroup(event.groupId);
break;
}
}
// Let's move all users
2020-06-09 23:13:26 +02:00
const updatedPlayersPositions = this.playersPositionInterpolator.getUpdatedPositions(time);
updatedPlayersPositions.forEach((moveEvent: HasPlayerMovedEvent, userId: number) => {
this.dirty = true;
const player: RemotePlayer | undefined = this.MapPlayersByKey.get(userId);
if (player === undefined) {
throw new Error('Cannot find player with ID "' + userId + '"');
}
player.updatePosition(moveEvent);
});
}
/**
* Called by the connexion when the full list of user position is received.
*/
private initUsersPosition(usersPosition: MessageUserPositionInterface[]): void {
this.pendingEvents.enqueue({
2021-07-02 19:03:34 +02:00
type: 'InitUserPositionEvent',
event: usersPosition,
});
}
/**
* Put all the players on the map on map load.
*/
private doInitUsersPosition(usersPosition: MessageUserPositionInterface[]): void {
const currentPlayerId = this.connection?.getUserId();
this.removeAllRemotePlayers();
// load map
usersPosition.forEach((userPosition: MessageUserPositionInterface) => {
if (userPosition.userId === currentPlayerId) {
return;
}
this.addPlayer(userPosition);
});
}
/**
* Called by the connexion when a new player arrives on a map
*/
public addPlayer(addPlayerData: AddPlayerInterface): void {
this.pendingEvents.enqueue({
2021-07-02 19:03:34 +02:00
type: 'AddPlayerEvent',
event: addPlayerData,
});
}
private doAddPlayer(addPlayerData: AddPlayerInterface): void {
//check if exist player, if exist, move position
if (this.MapPlayersByKey.has(addPlayerData.userId)) {
this.updatePlayerPosition({
userId: addPlayerData.userId,
position: addPlayerData.position,
});
return;
}
2020-10-20 16:39:23 +02:00
2021-01-26 15:21:23 +01:00
const texturesPromise = lazyLoadPlayerCharacterTextures(this.load, addPlayerData.characterLayers);
2020-06-09 23:13:26 +02:00
const player = new RemotePlayer(
addPlayerData.userId,
2020-04-13 15:34:09 +02:00
this,
addPlayerData.position.x,
addPlayerData.position.y,
addPlayerData.name,
texturesPromise,
addPlayerData.position.direction as PlayerAnimationDirections,
2021-04-02 21:21:11 +02:00
addPlayerData.position.moving,
addPlayerData.visitCardUrl,
addPlayerData.companion,
addPlayerData.companion !== null ? lazyLoadCompanionResource(this.load, addPlayerData.companion) : undefined
2020-04-13 15:34:09 +02:00
);
this.MapPlayers.add(player);
this.MapPlayersByKey.set(player.userId, player);
player.updatePosition(addPlayerData.position);
2020-04-07 20:41:35 +02:00
}
/**
* Called by the connexion when a player is removed from the map
*/
public removePlayer(userId: number) {
this.pendingEvents.enqueue({
2021-07-02 19:03:34 +02:00
type: 'RemovePlayerEvent',
userId,
});
}
private doRemovePlayer(userId: number) {
2020-06-09 23:13:26 +02:00
const player = this.MapPlayersByKey.get(userId);
if (player === undefined) {
2021-07-02 19:03:34 +02:00
console.error('Cannot find user with id ', userId);
2020-06-04 18:11:07 +02:00
} else {
player.destroy();
if (player.companion) {
player.companion.destroy();
}
2020-06-04 18:11:07 +02:00
this.MapPlayers.remove(player);
}
this.MapPlayersByKey.delete(userId);
this.playersPositionInterpolator.removePlayer(userId);
}
public updatePlayerPosition(message: MessageUserMovedInterface): void {
this.pendingEvents.enqueue({
2021-07-02 19:03:34 +02:00
type: 'UserMovedEvent',
event: message,
});
}
private doUpdatePlayerPosition(message: MessageUserMovedInterface): void {
const player: RemotePlayer | undefined = this.MapPlayersByKey.get(message.userId);
if (player === undefined) {
//throw new Error('Cannot find player with ID "' + message.userId +'"');
console.error('Cannot update position of player with ID "' + message.userId + '": player not found');
return;
}
// We do not update the player position directly (because it is sent only every 200ms).
// Instead we use the PlayersPositionInterpolator that will do a smooth animation over the next 200ms.
const playerMovement = new PlayerMovement(
{ x: player.x, y: player.y },
this.currentTick,
message.position,
this.currentTick + POSITION_DELAY
);
this.playersPositionInterpolator.updatePlayerPosition(player.userId, playerMovement);
}
public shareGroupPosition(groupPositionMessage: GroupCreatedUpdatedMessageInterface) {
this.pendingEvents.enqueue({
2021-07-02 19:03:34 +02:00
type: 'GroupCreatedUpdatedEvent',
event: groupPositionMessage,
});
}
private doShareGroupPosition(groupPositionMessage: GroupCreatedUpdatedMessageInterface) {
2020-10-21 23:08:05 +02:00
//delete previous group
this.doDeleteGroup(groupPositionMessage.groupId);
2020-10-21 23:08:05 +02:00
// TODO: circle radius should not be hard stored
//create new group
const sprite = new Sprite(
this,
Math.round(groupPositionMessage.position.x),
Math.round(groupPositionMessage.position.y),
2021-07-02 19:03:34 +02:00
groupPositionMessage.groupSize === MAX_PER_GROUP ? 'circleSprite-red' : 'circleSprite-white'
2020-10-21 23:08:05 +02:00
);
sprite.setDisplayOrigin(48, 48);
this.add.existing(sprite);
this.groups.set(groupPositionMessage.groupId, sprite);
return sprite;
}
2020-09-21 11:24:03 +02:00
deleteGroup(groupId: number): void {
this.pendingEvents.enqueue({
2021-07-02 19:03:34 +02:00
type: 'DeleteGroupEvent',
groupId,
});
}
2020-09-21 11:24:03 +02:00
doDeleteGroup(groupId: number): void {
2020-06-09 23:13:26 +02:00
const group = this.groups.get(groupId);
if (!group) {
return;
}
2020-06-04 18:11:07 +02:00
group.destroy();
this.groups.delete(groupId);
}
2020-07-27 22:36:07 +02:00
/**
* Sends to the server an event emitted by one of the ActionableItems.
*/
emitActionableEvent(itemId: number, eventName: string, state: unknown, parameters: unknown) {
this.connection?.emitActionableEvent(itemId, eventName, state, parameters);
2020-07-27 22:36:07 +02:00
}
public onResize(): void {
super.onResize();
this.reposition();
// Send new viewport to server
const camera = this.cameras.main;
this.connection?.setViewport({
left: camera.scrollX,
top: camera.scrollY,
right: camera.scrollX + camera.width,
bottom: camera.scrollY + camera.height,
});
}
private getObjectLayerData(objectName: string): ITiledMapObject | undefined {
for (const layer of this.mapFile.layers) {
2021-07-02 19:03:34 +02:00
if (layer.type === 'objectgroup' && layer.name === 'floorLayer') {
for (const object of layer.objects) {
if (object.name === objectName) {
return object;
}
}
}
}
return undefined;
}
2020-08-17 15:20:03 +02:00
private reposition(): void {
this.openChatIcon.setY(this.game.renderer.height - 2);
// Recompute camera offset if needed
biggestAvailableAreaStore.recompute();
}
/**
* Updates the offset of the character compared to the center of the screen according to the layout manager
* (tries to put the character in the center of the remaining space if there is a discussion going on.
*/
2021-06-17 10:35:08 +02:00
private updateCameraOffset(array: Box): void {
2021-05-05 13:14:00 +02:00
const xCenter = (array.xEnd - array.xStart) / 2 + array.xStart;
const yCenter = (array.yEnd - array.yStart) / 2 + array.yStart;
2021-07-02 19:03:34 +02:00
const game = HtmlUtils.querySelectorOrFail<HTMLCanvasElement>('#game canvas');
// Let's put this in Game coordinates by applying the zoom level:
this.cameras.main.setFollowOffset(
((xCenter - game.offsetWidth / 2) * window.devicePixelRatio) / this.scale.zoom,
((yCenter - game.offsetHeight / 2) * window.devicePixelRatio) / this.scale.zoom
);
2020-08-17 15:20:03 +02:00
}
2020-10-16 19:13:26 +02:00
public startJitsi(roomName: string, jwt?: string): void {
const allProps = this.gameMap.getCurrentProperties();
2021-07-02 19:03:34 +02:00
const jitsiConfig = this.safeParseJSONstring(allProps.get('jitsiConfig') as string | undefined, 'jitsiConfig');
const jitsiInterfaceConfig = this.safeParseJSONstring(
2021-07-02 19:03:34 +02:00
allProps.get('jitsiInterfaceConfig') as string | undefined,
'jitsiInterfaceConfig'
);
2021-07-02 19:03:34 +02:00
const jitsiUrl = allProps.get('jitsiUrl') as string | undefined;
jitsiFactory.start(roomName, this.playerName, jwt, jitsiConfig, jitsiInterfaceConfig, jitsiUrl);
this.connection?.setSilent(true);
2020-10-16 19:13:26 +02:00
mediaManager.hideGameOverlay();
2020-11-17 18:03:44 +01:00
//permit to stop jitsi when user close iframe
2021-07-02 19:03:34 +02:00
mediaManager.addTriggerCloseJitsiFrameButton('close-jisi', () => {
2020-11-17 18:03:44 +01:00
this.stopJitsi();
});
2020-10-16 19:13:26 +02:00
}
public stopJitsi(): void {
this.connection?.setSilent(false);
jitsiFactory.stop();
2020-10-16 19:13:26 +02:00
mediaManager.showGameOverlay();
2020-11-17 18:03:44 +01:00
2021-07-02 19:03:34 +02:00
mediaManager.removeTriggerCloseJitsiFrameButton('close-jisi');
2020-10-16 19:13:26 +02:00
}
2020-10-20 16:39:23 +02:00
//todo: put this into an 'orchestrator' scene (EntryScene?)
private bannedUser() {
2021-01-26 14:04:42 +01:00
this.cleanupClosingScene();
2021-03-22 16:10:21 +01:00
this.userInputManager.disableControls();
2021-01-26 08:57:10 +01:00
this.scene.start(ErrorSceneName, {
2021-07-02 19:03:34 +02:00
title: 'Banned',
subTitle: 'You were banned from WorkAdventure',
message: 'If you want more information, you may contact us at: workadventure@thecodingmachine.com',
2021-01-26 08:57:10 +01:00
});
2021-01-25 14:10:16 +01:00
}
//todo: put this into an 'orchestrator' scene (EntryScene?)
private showWorldFullError(message: string | null): void {
this.cleanupClosingScene();
this.scene.stop(ReconnectingSceneName);
this.scene.remove(ReconnectingSceneName);
this.userInputManager.disableControls();
//FIX ME to use status code
if (message == undefined) {
this.scene.start(ErrorSceneName, {
2021-07-02 19:03:34 +02:00
title: 'Connection rejected',
subTitle: 'The world you are trying to join is full. Try again later.',
message: 'If you want more information, you may contact us at: workadventure@thecodingmachine.com',
});
} else {
this.scene.start(ErrorSceneName, {
2021-07-02 19:03:34 +02:00
title: 'Connection rejected',
subTitle: 'You cannot join the World. Try again later. \n\r \n\r Error: ' + message + '.',
message:
2021-07-02 19:03:34 +02:00
'If you want more information, you may contact administrator or contact us at: workadventure@thecodingmachine.com',
});
}
}
zoomByFactor(zoomFactor: number) {
waScaleManager.zoomModifier *= zoomFactor;
biggestAvailableAreaStore.recompute();
}
}