2020-12-04 11:30:35 +01:00
|
|
|
import {gameManager, HasMovedEvent} from "./GameManager";
|
2020-05-19 19:11:12 +02:00
|
|
|
import {
|
2020-08-16 23:19:04 +02:00
|
|
|
GroupCreatedUpdatedMessageInterface,
|
|
|
|
MessageUserJoined,
|
2020-05-19 19:11:12 +02:00
|
|
|
MessageUserMovedInterface,
|
2020-12-03 16:39:44 +01:00
|
|
|
MessageUserPositionInterface, OnConnectInterface,
|
2020-08-16 23:19:04 +02:00
|
|
|
PointInterface,
|
2020-08-17 22:51:37 +02:00
|
|
|
PositionInterface,
|
|
|
|
RoomJoinedMessageInterface
|
2020-09-25 18:29:22 +02:00
|
|
|
} from "../../Connexion/ConnexionModels";
|
2020-06-04 18:54:34 +02:00
|
|
|
import {CurrentGamerInterface, hasMovedEventName, Player} from "../Player/Player";
|
2020-10-19 11:07:49 +02:00
|
|
|
import {
|
|
|
|
DEBUG_MODE,
|
|
|
|
JITSI_PRIVATE_MODE,
|
|
|
|
POSITION_DELAY,
|
|
|
|
RESOLUTION,
|
|
|
|
ZOOM_LEVEL
|
|
|
|
} from "../../Enum/EnvironmentVariable";
|
2020-06-19 16:36:40 +02:00
|
|
|
import {
|
|
|
|
ITiledMap,
|
|
|
|
ITiledMapLayer,
|
2020-07-23 18:09:24 +02:00
|
|
|
ITiledMapLayerProperty, ITiledMapObject,
|
2020-06-19 16:36:40 +02:00
|
|
|
ITiledTileSet
|
|
|
|
} from "../Map/ITiledMap";
|
2020-05-19 19:11:12 +02:00
|
|
|
import {AddPlayerInterface} from "./AddPlayerInterface";
|
2020-05-22 22:59:43 +02:00
|
|
|
import {PlayerAnimationNames} from "../Player/Animation";
|
2020-06-02 13:44:42 +02:00
|
|
|
import {PlayerMovement} from "./PlayerMovement";
|
|
|
|
import {PlayersPositionInterpolator} from "./PlayersPositionInterpolator";
|
2020-06-04 18:54:34 +02:00
|
|
|
import {RemotePlayer} from "../Entity/RemotePlayer";
|
2020-08-16 23:19:04 +02:00
|
|
|
import {Queue} from 'queue-typescript';
|
2020-08-18 00:12:38 +02:00
|
|
|
import {SimplePeer, UserSimplePeerInterface} from "../../WebRtc/SimplePeer";
|
2020-06-22 15:00:23 +02:00
|
|
|
import {ReconnectingSceneName} from "../Reconnecting/ReconnectingScene";
|
2020-10-23 16:16:30 +02:00
|
|
|
import {loadAllLayers, loadObject, loadPlayerCharacters} from "../Entity/body_character";
|
2020-11-23 20:34:05 +01:00
|
|
|
import {
|
|
|
|
CenterListener,
|
|
|
|
layoutManager,
|
|
|
|
LayoutMode,
|
|
|
|
ON_ACTION_TRIGGER_BUTTON, TRIGGER_JITSI_PROPERTIES, TRIGGER_WEBSITE_PROPERTIES
|
|
|
|
} from "../../WebRtc/LayoutManager";
|
2020-08-16 23:19:04 +02:00
|
|
|
import Texture = Phaser.Textures.Texture;
|
|
|
|
import Sprite = Phaser.GameObjects.Sprite;
|
|
|
|
import CanvasTexture = Phaser.Textures.CanvasTexture;
|
|
|
|
import GameObject = Phaser.GameObjects.GameObject;
|
2020-07-07 22:52:22 +02:00
|
|
|
import FILE_LOAD_ERROR = Phaser.Loader.Events.FILE_LOAD_ERROR;
|
2020-08-30 15:44:22 +02:00
|
|
|
import {GameMap} from "./GameMap";
|
2020-10-27 16:59:12 +01:00
|
|
|
import {coWebsiteManager} from "../../WebRtc/CoWebsiteManager";
|
2020-09-01 14:43:21 +02:00
|
|
|
import {mediaManager} from "../../WebRtc/MediaManager";
|
2020-07-07 22:52:22 +02:00
|
|
|
import {FourOFourSceneName} from "../Reconnecting/FourOFourScene";
|
2020-07-23 18:09:24 +02:00
|
|
|
import {ItemFactoryInterface} from "../Items/ItemFactoryInterface";
|
|
|
|
import {ActionableItem} from "../Items/ActionableItem";
|
2020-07-27 22:36:07 +02:00
|
|
|
import {UserInputManager} from "../UserInput/UserInputManager";
|
2020-09-24 11:16:08 +02:00
|
|
|
import {UserMovedMessage} from "../../Messages/generated/messages_pb";
|
2020-09-18 15:51:15 +02:00
|
|
|
import {ProtobufClientUtils} from "../../Network/ProtobufClientUtils";
|
2020-09-25 18:29:22 +02:00
|
|
|
import {connectionManager} from "../../Connexion/ConnectionManager";
|
|
|
|
import {RoomConnection} from "../../Connexion/RoomConnection";
|
2020-09-20 17:12:27 +02:00
|
|
|
import {GlobalMessageManager} from "../../Administration/GlobalMessageManager";
|
2020-10-19 19:32:47 +02:00
|
|
|
import {UserMessageManager} from "../../Administration/UserMessageManager";
|
2020-09-20 17:12:27 +02:00
|
|
|
import {ConsoleGlobalMessageManager} from "../../Administration/ConsoleGlobalMessageManager";
|
2020-10-08 16:00:29 +02:00
|
|
|
import {ResizableScene} from "../Login/ResizableScene";
|
2020-10-13 17:08:24 +02:00
|
|
|
import {Room} from "../../Connexion/Room";
|
2020-10-23 16:16:30 +02:00
|
|
|
import {jitsiFactory} from "../../WebRtc/JitsiFactory";
|
2020-11-18 18:15:57 +01:00
|
|
|
import {urlManager} from "../../Url/UrlManager";
|
2020-12-04 11:30:35 +01:00
|
|
|
import {PresentationModeIcon} from "../Components/PresentationModeIcon";
|
|
|
|
import {ChatModeIcon} from "../Components/ChatModeIcon";
|
|
|
|
import {OpenChatIcon, openChatIconName} from "../Components/OpenChatIcon";
|
2020-12-18 13:56:25 +01:00
|
|
|
import {SelectCharacterScene, SelectCharacterSceneName} from "../Login/SelectCharacterScene";
|
2020-12-18 14:30:46 +01:00
|
|
|
import {TextureError} from "../../Exception/TextureError";
|
2020-04-11 18:17:36 +02:00
|
|
|
|
2020-06-07 13:23:32 +02:00
|
|
|
export interface GameSceneInitInterface {
|
2020-11-13 18:00:22 +01:00
|
|
|
initPosition: PointInterface|null,
|
|
|
|
reconnecting: boolean
|
2020-05-23 15:43:26 +02:00
|
|
|
}
|
|
|
|
|
2020-06-19 18:18:43 +02:00
|
|
|
interface InitUserPositionEventInterface {
|
|
|
|
type: 'InitUserPositionEvent'
|
|
|
|
event: MessageUserPositionInterface[]
|
|
|
|
}
|
|
|
|
|
|
|
|
interface AddPlayerEventInterface {
|
|
|
|
type: 'AddPlayerEvent'
|
|
|
|
event: AddPlayerInterface
|
|
|
|
}
|
|
|
|
|
|
|
|
interface RemovePlayerEventInterface {
|
|
|
|
type: 'RemovePlayerEvent'
|
2020-09-18 13:57:38 +02:00
|
|
|
userId: number
|
2020-06-19 18:18:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
interface UserMovedEventInterface {
|
|
|
|
type: 'UserMovedEvent'
|
|
|
|
event: MessageUserMovedInterface
|
|
|
|
}
|
|
|
|
|
|
|
|
interface GroupCreatedUpdatedEventInterface {
|
|
|
|
type: 'GroupCreatedUpdatedEvent'
|
|
|
|
event: GroupCreatedUpdatedMessageInterface
|
|
|
|
}
|
|
|
|
|
|
|
|
interface DeleteGroupEventInterface {
|
|
|
|
type: 'DeleteGroupEvent'
|
2020-09-21 11:24:03 +02:00
|
|
|
groupId: number
|
2020-06-19 18:18:43 +02:00
|
|
|
}
|
|
|
|
|
2020-11-19 14:32:18 +01:00
|
|
|
const defaultStartLayerName = 'start';
|
|
|
|
|
2020-10-08 16:00:29 +02:00
|
|
|
export class GameScene extends ResizableScene implements CenterListener {
|
2020-04-15 19:39:26 +02:00
|
|
|
Terrains : Array<Phaser.Tilemaps.Tileset>;
|
2020-08-07 23:39:06 +02:00
|
|
|
CurrentPlayer!: CurrentGamerInterface;
|
|
|
|
MapPlayers!: Phaser.Physics.Arcade.Group;
|
2020-09-18 13:57:38 +02:00
|
|
|
MapPlayersByKey : Map<number, RemotePlayer> = new Map<number, RemotePlayer>();
|
2020-08-07 23:39:06 +02:00
|
|
|
Map!: Phaser.Tilemaps.Tilemap;
|
|
|
|
Layers!: Array<Phaser.Tilemaps.StaticTilemapLayer>;
|
|
|
|
Objects!: Array<Phaser.Physics.Arcade.Sprite>;
|
|
|
|
mapFile!: ITiledMap;
|
2020-09-21 11:24:03 +02:00
|
|
|
groups: Map<number, Sprite>;
|
2020-08-07 23:39:06 +02:00
|
|
|
startX!: number;
|
|
|
|
startY!: number;
|
|
|
|
circleTexture!: CanvasTexture;
|
2020-10-21 16:07:42 +02:00
|
|
|
circleRedTexture!: CanvasTexture;
|
2020-06-19 18:18:43 +02:00
|
|
|
pendingEvents: Queue<InitUserPositionEventInterface|AddPlayerEventInterface|RemovePlayerEventInterface|UserMovedEventInterface|GroupCreatedUpdatedEventInterface|DeleteGroupEventInterface> = new Queue<InitUserPositionEventInterface|AddPlayerEventInterface|RemovePlayerEventInterface|UserMovedEventInterface|GroupCreatedUpdatedEventInterface|DeleteGroupEventInterface>();
|
2020-06-04 18:11:07 +02:00
|
|
|
private initPosition: PositionInterface|null = null;
|
2020-06-02 13:44:42 +02:00
|
|
|
private playersPositionInterpolator = new PlayersPositionInterpolator();
|
2020-12-04 11:30:35 +01:00
|
|
|
public connection!: RoomConnection;
|
2020-08-07 23:39:06 +02:00
|
|
|
private simplePeer!: SimplePeer;
|
2020-09-16 18:38:50 +02:00
|
|
|
private GlobalMessageManager!: GlobalMessageManager;
|
2020-10-19 19:32:47 +02:00
|
|
|
private UserMessageManager!: UserMessageManager;
|
2020-12-04 11:30:35 +01:00
|
|
|
public ConsoleGlobalMessageManager!: ConsoleGlobalMessageManager;
|
2020-07-27 22:36:07 +02:00
|
|
|
private connectionAnswerPromise: Promise<RoomJoinedMessageInterface>;
|
2020-08-17 22:51:37 +02:00
|
|
|
private connectionAnswerPromiseResolve!: (value?: RoomJoinedMessageInterface | PromiseLike<RoomJoinedMessageInterface>) => void;
|
2020-07-23 18:09:24 +02:00
|
|
|
// A promise that will resolve when the "create" method is called (signaling loading is ended)
|
|
|
|
private createPromise: Promise<void>;
|
2020-08-17 22:51:37 +02:00
|
|
|
private createPromiseResolve!: (value?: void | PromiseLike<void>) => void;
|
2020-04-15 19:39:26 +02:00
|
|
|
|
2020-05-09 19:41:21 +02:00
|
|
|
MapUrlFile: string;
|
2020-05-23 17:27:49 +02:00
|
|
|
RoomId: string;
|
|
|
|
instance: string;
|
2020-05-09 19:41:21 +02:00
|
|
|
|
2020-08-07 23:39:06 +02:00
|
|
|
currentTick!: number;
|
|
|
|
lastSentTick!: number; // The last tick at which a position was sent.
|
2020-06-01 22:42:18 +02:00
|
|
|
lastMoveEventSent: HasMovedEvent = {
|
|
|
|
direction: '',
|
|
|
|
moving: false,
|
|
|
|
x: -1000,
|
|
|
|
y: -1000
|
|
|
|
}
|
|
|
|
|
2020-08-17 15:20:03 +02:00
|
|
|
private presentationModeSprite!: Sprite;
|
|
|
|
private chatModeSprite!: Sprite;
|
2020-08-30 15:44:22 +02:00
|
|
|
private gameMap!: GameMap;
|
2020-07-27 22:36:07 +02:00
|
|
|
private actionableItems: Map<number, ActionableItem> = new Map<number, ActionableItem>();
|
2020-07-23 18:09:24 +02:00
|
|
|
// The item that can be selected by pressing the space key.
|
|
|
|
private outlinedItem: ActionableItem|null = null;
|
2020-08-17 22:51:37 +02:00
|
|
|
private userInputManager!: UserInputManager;
|
2020-11-13 18:00:22 +01:00
|
|
|
private isReconnecting: boolean = false;
|
2020-11-19 14:32:18 +01:00
|
|
|
private startLayerName!: string | null;
|
2020-12-04 11:30:35 +01:00
|
|
|
private openChatIcon!: OpenChatIcon;
|
|
|
|
private playerName!: string;
|
|
|
|
private characterLayers!: string[];
|
2020-05-09 21:28:50 +02:00
|
|
|
|
2020-12-18 15:58:49 +01:00
|
|
|
constructor(private room: Room, MapUrlFile: string, customKey?: string|undefined) {
|
2020-04-07 19:23:21 +02:00
|
|
|
super({
|
2020-12-18 15:58:49 +01:00
|
|
|
key: customKey ?? room.id
|
2020-04-07 19:23:21 +02:00
|
|
|
});
|
2020-04-15 19:39:26 +02:00
|
|
|
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
|
|
|
|
2020-05-09 19:41:21 +02:00
|
|
|
|
|
|
|
this.MapUrlFile = MapUrlFile;
|
2020-10-13 18:44:50 +02:00
|
|
|
this.RoomId = room.id;
|
2020-07-23 18:09:24 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
})
|
2020-04-07 19:23:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//hook preload scene
|
|
|
|
preload(): void {
|
2020-12-04 11:30:35 +01:00
|
|
|
this.load.image(openChatIconName, 'resources/objects/talk.png');
|
2020-07-07 22:52:22 +02:00
|
|
|
this.load.on(FILE_LOAD_ERROR, (file: {src: string}) => {
|
|
|
|
this.scene.start(FourOFourSceneName, {
|
|
|
|
file: file.src
|
|
|
|
});
|
|
|
|
});
|
2020-11-18 18:15:57 +01:00
|
|
|
this.load.on('filecomplete-tilemapJSON-'+this.MapUrlFile, (key: string, type: string, data: unknown) => {
|
2020-06-03 10:45:25 +02:00
|
|
|
this.onMapLoad(data);
|
2020-04-15 19:23:06 +02:00
|
|
|
});
|
2020-05-10 17:55:30 +02:00
|
|
|
//TODO strategy to add access token
|
2020-11-18 18:15:57 +01:00
|
|
|
this.load.tilemapTiledJSON(this.MapUrlFile, this.MapUrlFile);
|
2020-06-03 10:45:25 +02:00
|
|
|
// 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.
|
2020-11-18 18:15:57 +01:00
|
|
|
if (this.cache.tilemap.exists(this.MapUrlFile)) {
|
|
|
|
const data = this.cache.tilemap.get(this.MapUrlFile);
|
2020-06-03 10:45:25 +02:00
|
|
|
this.onMapLoad(data);
|
|
|
|
}
|
2020-05-06 01:50:01 +02:00
|
|
|
|
|
|
|
//add player png
|
2020-10-06 23:56:27 +02:00
|
|
|
loadPlayerCharacters(this.load);
|
2020-07-29 11:40:05 +02:00
|
|
|
loadAllLayers(this.load);
|
2020-10-06 23:56:27 +02:00
|
|
|
loadObject(this.load);
|
2020-07-29 11:40:05 +02:00
|
|
|
|
2020-05-01 23:38:09 +02:00
|
|
|
this.load.bitmapFont('main_font', 'resources/fonts/arcade.png', 'resources/fonts/arcade.xml');
|
2020-04-07 19:23:21 +02:00
|
|
|
}
|
|
|
|
|
2020-06-19 16:36:40 +02:00
|
|
|
// 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
|
2020-07-23 18:09:24 +02:00
|
|
|
private async onMapLoad(data: any): Promise<void> {
|
2020-06-03 10:45:25 +02:00
|
|
|
// Triggered when the map is loaded
|
|
|
|
// Load tiles attached to the map recursively
|
2020-06-03 23:17:52 +02:00
|
|
|
this.mapFile = data.data;
|
2020-06-09 23:13:26 +02:00
|
|
|
const url = this.MapUrlFile.substr(0, this.MapUrlFile.lastIndexOf('/'));
|
2020-06-03 23:17:52 +02:00
|
|
|
this.mapFile.tilesets.forEach((tileset) => {
|
2020-06-03 10:45:25 +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
|
2020-07-07 18:22:10 +02:00
|
|
|
this.load.image(`${url}/${tileset.image}`, `${url}/${tileset.image}`);
|
2020-06-03 10:45:25 +02:00
|
|
|
})
|
2020-07-23 18:09:24 +02:00
|
|
|
|
|
|
|
// 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:09:24 +02:00
|
|
|
|
2020-07-23 18:47:28 +02:00
|
|
|
for (const layer of this.mapFile.layers) {
|
2020-07-23 18:09:24 +02:00
|
|
|
if (layer.type === 'objectgroup') {
|
2020-07-23 18:47:28 +02:00
|
|
|
for (const object of layer.objects) {
|
2020-07-23 18:09:24 +02:00
|
|
|
let objectsOfType: ITiledMapObject[]|undefined;
|
|
|
|
if (!objects.has(object.type)) {
|
|
|
|
objectsOfType = new Array<ITiledMapObject>();
|
|
|
|
} else {
|
|
|
|
objectsOfType = objects.get(object.type);
|
|
|
|
if (objectsOfType === undefined) {
|
|
|
|
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) {
|
2020-07-23 18:09:24 +02:00
|
|
|
// 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) {
|
2020-07-23 18:47:28 +02:00
|
|
|
case 'computer': {
|
|
|
|
const module = await import('../Items/Computer/computer');
|
|
|
|
itemFactory = module.default;
|
2020-07-23 18:09:24 +02:00
|
|
|
break;
|
2020-07-23 18:47:28 +02:00
|
|
|
}
|
2020-07-23 18:09:24 +02:00
|
|
|
default:
|
|
|
|
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.
|
|
|
|
|
|
|
|
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 () => {
|
2020-07-23 18:09:24 +02:00
|
|
|
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) {
|
2020-07-23 18:09:24 +02:00
|
|
|
// 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);
|
2020-07-23 18:09:24 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// import(/* webpackIgnore: true */ scriptUrl).then(result => {
|
|
|
|
//
|
|
|
|
// result.default.preload(this.load);
|
|
|
|
//
|
|
|
|
// this.load.start(); // Let's manually start the loader because the import might be over AFTER the loading ends.
|
|
|
|
// 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
|
|
|
|
// this.createPromise.then(() => {
|
|
|
|
// result.default.create(this);
|
|
|
|
//
|
|
|
|
// for (let object of objectsOfType) {
|
|
|
|
// // TODO: we should pass here a factory to create sprites (maybe?)
|
|
|
|
// let objectSprite = result.default.factory(this, object);
|
|
|
|
// }
|
|
|
|
// });
|
|
|
|
// });
|
|
|
|
// });
|
|
|
|
}
|
2020-06-03 10:45:25 +02:00
|
|
|
}
|
|
|
|
|
2020-04-07 19:23:21 +02:00
|
|
|
//hook initialisation
|
2020-05-23 15:43:26 +02:00
|
|
|
init(initData : GameSceneInitInterface) {
|
2020-06-04 18:11:07 +02:00
|
|
|
if (initData.initPosition !== undefined) {
|
2020-11-18 18:15:57 +01:00
|
|
|
this.initPosition = initData.initPosition; //todo: still used?
|
2020-06-04 18:11:07 +02:00
|
|
|
}
|
2020-11-13 18:00:22 +01:00
|
|
|
if (initData.initPosition !== undefined) {
|
|
|
|
this.isReconnecting = initData.reconnecting;
|
|
|
|
}
|
2020-05-23 15:43:26 +02:00
|
|
|
}
|
2020-04-07 19:23:21 +02:00
|
|
|
|
|
|
|
//hook create scene
|
|
|
|
create(): void {
|
2020-12-16 15:09:58 +01:00
|
|
|
gameManager.gameSceneIsCreated(this);
|
2020-11-25 17:17:48 +01:00
|
|
|
urlManager.pushRoomIdToUrl(this.room);
|
2020-11-19 14:32:18 +01:00
|
|
|
this.startLayerName = urlManager.getStartLayerNameFromUrl();
|
2020-12-11 13:00:11 +01:00
|
|
|
|
2020-12-04 11:30:35 +01:00
|
|
|
const playerName = gameManager.getPlayerName();
|
|
|
|
if (!playerName) {
|
|
|
|
throw 'playerName is not set';
|
|
|
|
}
|
|
|
|
this.playerName = playerName;
|
|
|
|
const characterLayers = gameManager.getCharacterLayers();
|
|
|
|
if (!characterLayers) {
|
|
|
|
throw 'characterLayers are not set';
|
|
|
|
}
|
|
|
|
this.characterLayers = characterLayers;
|
|
|
|
|
|
|
|
|
2020-04-13 15:34:09 +02:00
|
|
|
//initalise map
|
2020-11-18 18:15:57 +01:00
|
|
|
this.Map = this.add.tilemap(this.MapUrlFile);
|
2020-08-30 15:44:22 +02:00
|
|
|
this.gameMap = new GameMap(this.mapFile);
|
2020-07-07 18:22:10 +02:00
|
|
|
const mapDirUrl = this.MapUrlFile.substr(0, this.MapUrlFile.lastIndexOf('/'));
|
2020-06-03 23:17:52 +02:00
|
|
|
this.mapFile.tilesets.forEach((tileset: ITiledTileSet) => {
|
2020-07-07 18:22:10 +02:00
|
|
|
this.Terrains.push(this.Map.addTilesetImage(tileset.name, `${mapDirUrl}/${tileset.image}`, tileset.tilewidth, tileset.tileheight, tileset.margin, tileset.spacing/*, tileset.firstgid*/));
|
2020-04-15 19:39:26 +02:00
|
|
|
});
|
2020-04-13 15:34:09 +02:00
|
|
|
|
|
|
|
//permit to set bound collision
|
2020-10-13 20:39:29 +02:00
|
|
|
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.Layers = new Array<Phaser.Tilemaps.StaticTilemapLayer>();
|
2020-04-15 23:10:12 +02:00
|
|
|
let depth = -2;
|
2020-06-09 23:13:26 +02:00
|
|
|
for (const layer of this.mapFile.layers) {
|
2020-05-08 17:35:40 +02:00
|
|
|
if (layer.type === 'tilelayer') {
|
|
|
|
this.addLayer(this.Map.createStaticLayer(layer.name, this.Terrains, 0, 0).setDepth(depth));
|
2020-12-11 13:00:11 +01:00
|
|
|
|
2020-11-18 18:15:57 +01:00
|
|
|
const exitSceneUrl = this.getExitSceneUrl(layer);
|
|
|
|
if (exitSceneUrl !== undefined) {
|
|
|
|
this.loadNextGame(exitSceneUrl);
|
|
|
|
}
|
|
|
|
const exitUrl = this.getExitUrl(layer);
|
|
|
|
if (exitUrl !== undefined) {
|
|
|
|
this.loadNextGame(exitUrl);
|
|
|
|
}
|
2020-05-09 21:28:50 +02:00
|
|
|
}
|
2020-05-08 17:35:40 +02:00
|
|
|
if (layer.type === 'objectgroup' && layer.name === 'floorLayer') {
|
|
|
|
depth = 10000;
|
2020-04-15 23:10:12 +02:00
|
|
|
}
|
2020-06-07 23:00:05 +02:00
|
|
|
}
|
2020-04-15 23:10:12 +02:00
|
|
|
if (depth === -2) {
|
|
|
|
throw new Error('Your map MUST contain a layer of type "objectgroup" whose name is "floorLayer" that represents the layer characters are drawn at.');
|
|
|
|
}
|
2020-04-13 15:34:09 +02:00
|
|
|
|
2020-11-17 15:02:21 +01:00
|
|
|
this.initStartXAndStartY();
|
2020-06-07 13:23:32 +02:00
|
|
|
|
2020-04-13 15:34:09 +02:00
|
|
|
//add entities
|
|
|
|
this.Objects = new Array<Phaser.Physics.Arcade.Sprite>();
|
|
|
|
|
|
|
|
//initialise list of other player
|
2020-10-13 20:39:29 +02:00
|
|
|
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);
|
2020-07-27 22:36:07 +02:00
|
|
|
|
2020-04-10 12:54:05 +02:00
|
|
|
//notify game manager can to create currentUser in map
|
2020-05-24 23:33:56 +02:00
|
|
|
this.createCurrentPlayer();
|
2020-04-13 19:56:41 +02:00
|
|
|
|
|
|
|
//initialise camera
|
|
|
|
this.initCamera();
|
2020-05-08 00:35:36 +02:00
|
|
|
|
2020-05-08 16:09:50 +02:00
|
|
|
// Let's generate the circle for the group delimiter
|
2020-10-21 17:22:17 +02:00
|
|
|
let circleElement = Object.values(this.textures.list).find((object: Texture) => object.key === 'circleSprite-white');
|
2020-10-13 20:39:29 +02:00
|
|
|
if (circleElement) {
|
2020-10-21 17:22:17 +02:00
|
|
|
this.textures.remove('circleSprite-white');
|
|
|
|
}
|
|
|
|
|
|
|
|
circleElement = Object.values(this.textures.list).find((object: Texture) => object.key === 'circleSprite-red');
|
|
|
|
if (circleElement) {
|
|
|
|
this.textures.remove('circleSprite-red');
|
2020-05-09 19:41:21 +02:00
|
|
|
}
|
2020-10-21 16:07:42 +02:00
|
|
|
|
|
|
|
//create white circle canvas use to create sprite
|
|
|
|
this.circleTexture = this.textures.createCanvas('circleSprite-white', 96, 96);
|
2020-06-09 23:13:26 +02:00
|
|
|
const context = this.circleTexture.context;
|
2020-05-08 16:09:50 +02:00
|
|
|
context.beginPath();
|
|
|
|
context.arc(48, 48, 48, 0, 2 * Math.PI, false);
|
|
|
|
// context.lineWidth = 5;
|
|
|
|
context.strokeStyle = '#ffffff';
|
|
|
|
context.stroke();
|
|
|
|
this.circleTexture.refresh();
|
2020-05-12 00:07:50 +02:00
|
|
|
|
2020-10-21 16:07:42 +02:00
|
|
|
//create red circle canvas use to create sprite
|
|
|
|
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;
|
|
|
|
contextRed.strokeStyle = '#ff0000';
|
|
|
|
contextRed.stroke();
|
|
|
|
this.circleRedTexture.refresh();
|
|
|
|
|
2020-06-22 11:58:07 +02:00
|
|
|
// Let's pause the scene if the connection is not established yet
|
2020-11-13 18:00:22 +01:00
|
|
|
if (this.isReconnecting) {
|
|
|
|
setTimeout(() => {
|
|
|
|
this.scene.sleep();
|
|
|
|
this.scene.launch(ReconnectingSceneName);
|
|
|
|
}, 0);
|
|
|
|
} else if (this.connection === undefined) {
|
2020-06-22 15:00:23 +02:00
|
|
|
// Let's wait 0.5 seconds before printing the "connecting" screen to avoid blinking
|
|
|
|
setTimeout(() => {
|
|
|
|
if (this.connection === undefined) {
|
|
|
|
this.scene.sleep();
|
|
|
|
this.scene.launch(ReconnectingSceneName);
|
|
|
|
}
|
|
|
|
}, 500);
|
2020-06-22 11:58:07 +02:00
|
|
|
}
|
2020-07-23 18:09:24 +02:00
|
|
|
|
|
|
|
this.createPromiseResolve();
|
2020-07-27 22:36:07 +02:00
|
|
|
|
2020-10-13 20:39:29 +02:00
|
|
|
this.userInputManager.spaceEvent(() => {
|
2020-07-27 22:36:07 +02:00
|
|
|
this.outlinedItem?.activate();
|
|
|
|
});
|
2020-08-17 22:51:37 +02:00
|
|
|
|
2020-12-04 11:30:35 +01:00
|
|
|
this.presentationModeSprite = new PresentationModeIcon(this, 36, this.game.renderer.height - 2);
|
2020-08-17 15:20:03 +02:00
|
|
|
this.presentationModeSprite.on('pointerup', this.switchLayoutMode.bind(this));
|
2020-12-04 11:30:35 +01:00
|
|
|
this.chatModeSprite = new ChatModeIcon(this, 70, this.game.renderer.height - 2);
|
2020-08-17 15:20:03 +02:00
|
|
|
this.chatModeSprite.on('pointerup', this.switchLayoutMode.bind(this));
|
2020-12-15 18:00:04 +01:00
|
|
|
this.openChatIcon = new OpenChatIcon(this, 2, this.game.renderer.height - 2)
|
2020-08-17 16:12:53 +02:00
|
|
|
|
|
|
|
// FIXME: change this to use the UserInputManager class for input
|
2020-12-04 11:30:35 +01:00
|
|
|
this.input.keyboard.on('keyup-M', () => {
|
2020-08-17 15:20:03 +02:00
|
|
|
this.switchLayoutMode();
|
2020-08-16 23:19:04 +02:00
|
|
|
});
|
2020-08-17 15:20:03 +02:00
|
|
|
|
|
|
|
this.reposition();
|
2020-08-24 14:19:36 +02:00
|
|
|
|
|
|
|
// From now, this game scene will be notified of reposition events
|
|
|
|
layoutManager.setListener(this);
|
2020-11-10 17:00:23 +01:00
|
|
|
this.triggerOnMapLayerPropertyChange();
|
2020-10-12 11:22:41 +02:00
|
|
|
|
2020-10-06 18:09:23 +02:00
|
|
|
const camera = this.cameras.main;
|
|
|
|
|
|
|
|
connectionManager.connectToRoomSocket(
|
|
|
|
this.RoomId,
|
2020-12-04 11:30:35 +01:00
|
|
|
this.playerName,
|
|
|
|
this.characterLayers,
|
2020-10-06 18:09:23 +02:00
|
|
|
{
|
|
|
|
x: this.startX,
|
|
|
|
y: this.startY
|
|
|
|
},
|
|
|
|
{
|
|
|
|
left: camera.scrollX,
|
|
|
|
top: camera.scrollY,
|
|
|
|
right: camera.scrollX + camera.width,
|
|
|
|
bottom: camera.scrollY + camera.height,
|
2020-12-03 16:39:44 +01:00
|
|
|
}).then((onConnect: OnConnectInterface) => {
|
|
|
|
this.connection = onConnect.connection;
|
2020-10-06 18:09:23 +02:00
|
|
|
|
2020-12-03 16:39:44 +01:00
|
|
|
this.connection.onUserJoins((message: MessageUserJoined) => {
|
2020-10-06 18:09:23 +02:00
|
|
|
const userMessage: AddPlayerInterface = {
|
|
|
|
userId: message.userId,
|
|
|
|
characterLayers: message.characterLayers,
|
|
|
|
name: message.name,
|
|
|
|
position: message.position
|
|
|
|
}
|
|
|
|
this.addPlayer(userMessage);
|
|
|
|
});
|
|
|
|
|
2020-12-03 16:39:44 +01:00
|
|
|
this.connection.onUserMoved((message: UserMovedMessage) => {
|
2020-10-06 18:09:23 +02:00
|
|
|
const position = message.getPosition();
|
|
|
|
if (position === undefined) {
|
|
|
|
throw new Error('Position missing from UserMovedMessage');
|
|
|
|
}
|
|
|
|
|
|
|
|
const messageUserMoved: MessageUserMovedInterface = {
|
|
|
|
userId: message.getUserid(),
|
|
|
|
position: ProtobufClientUtils.toPointInterface(position)
|
|
|
|
}
|
|
|
|
|
|
|
|
this.updatePlayerPosition(messageUserMoved);
|
|
|
|
});
|
|
|
|
|
2020-12-03 16:39:44 +01:00
|
|
|
this.connection.onUserLeft((userId: number) => {
|
2020-10-06 18:09:23 +02:00
|
|
|
this.removePlayer(userId);
|
|
|
|
});
|
|
|
|
|
2020-12-03 16:39:44 +01:00
|
|
|
this.connection.onGroupUpdatedOrCreated((groupPositionMessage: GroupCreatedUpdatedMessageInterface) => {
|
2020-10-06 18:09:23 +02:00
|
|
|
this.shareGroupPosition(groupPositionMessage);
|
2020-12-04 11:30:35 +01:00
|
|
|
this.openChatIcon.setVisible(true);
|
2020-10-06 18:09:23 +02:00
|
|
|
})
|
|
|
|
|
2020-12-03 16:39:44 +01:00
|
|
|
this.connection.onGroupDeleted((groupId: number) => {
|
2020-10-06 18:09:23 +02:00
|
|
|
try {
|
|
|
|
this.deleteGroup(groupId);
|
2020-12-04 11:30:35 +01:00
|
|
|
this.openChatIcon.setVisible(false);
|
2020-10-06 18:09:23 +02:00
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-12-03 16:39:44 +01:00
|
|
|
this.connection.onServerDisconnected(() => {
|
2020-10-06 18:09:23 +02:00
|
|
|
console.log('Player disconnected from server. Reloading scene.');
|
|
|
|
|
|
|
|
this.simplePeer.closeAllConnections();
|
|
|
|
this.simplePeer.unregister();
|
|
|
|
|
2020-10-13 20:39:29 +02:00
|
|
|
const gameSceneKey = 'somekey' + Math.round(Math.random() * 10000);
|
2020-12-18 15:58:49 +01:00
|
|
|
const game: Phaser.Scene = new GameScene(this.room, this.MapUrlFile, gameSceneKey);
|
2020-10-08 18:51:24 +02:00
|
|
|
this.scene.add(gameSceneKey, game, true,
|
2020-10-06 18:09:23 +02:00
|
|
|
{
|
|
|
|
initPosition: {
|
|
|
|
x: this.CurrentPlayer.x,
|
|
|
|
y: this.CurrentPlayer.y
|
2020-11-13 18:00:22 +01:00
|
|
|
},
|
|
|
|
reconnecting: true
|
2020-10-06 18:09:23 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
this.scene.stop(this.scene.key);
|
|
|
|
this.scene.remove(this.scene.key);
|
|
|
|
})
|
|
|
|
|
2020-12-03 16:39:44 +01:00
|
|
|
this.connection.onActionableEvent((message => {
|
2020-10-06 18:09:23 +02:00
|
|
|
const item = this.actionableItems.get(message.itemId);
|
|
|
|
if (item === undefined) {
|
2020-10-13 20:39:29 +02:00
|
|
|
console.warn('Received an event about object "' + message.itemId + '" but cannot find this item on the map.');
|
2020-10-06 18:09:23 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
item.fire(message.event, message.state, message.parameters);
|
|
|
|
}));
|
|
|
|
|
2020-10-19 11:07:49 +02:00
|
|
|
/**
|
|
|
|
* Triggered when we receive the JWT token to connect to Jitsi
|
|
|
|
*/
|
2020-12-03 16:39:44 +01:00
|
|
|
this.connection.onStartJitsiRoom((jwt, room) => {
|
2020-10-16 19:13:26 +02:00
|
|
|
this.startJitsi(room, jwt);
|
|
|
|
});
|
|
|
|
|
2020-10-06 18:09:23 +02:00
|
|
|
// When connection is performed, let's connect SimplePeer
|
2020-12-04 11:30:35 +01:00
|
|
|
this.simplePeer = new SimplePeer(this.connection, !this.room.isPublic, this.playerName);
|
2020-10-06 18:09:23 +02:00
|
|
|
this.GlobalMessageManager = new GlobalMessageManager(this.connection);
|
2020-10-19 19:32:47 +02:00
|
|
|
this.UserMessageManager = new UserMessageManager(this.connection);
|
2020-10-06 18:09:23 +02:00
|
|
|
|
|
|
|
const self = this;
|
|
|
|
this.simplePeer.registerPeerConnectionListener({
|
|
|
|
onConnect(user: UserSimplePeerInterface) {
|
|
|
|
self.presentationModeSprite.setVisible(true);
|
|
|
|
self.chatModeSprite.setVisible(true);
|
|
|
|
},
|
|
|
|
onDisconnect(userId: number) {
|
|
|
|
if (self.simplePeer.getNbConnections() === 0) {
|
|
|
|
self.presentationModeSprite.setVisible(false);
|
|
|
|
self.chatModeSprite.setVisible(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
//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: HasMovedEvent) => {
|
|
|
|
this.gameMap.setPosition(event.x, event.y);
|
|
|
|
})
|
|
|
|
|
2020-12-03 16:39:44 +01:00
|
|
|
//this.initUsersPosition(roomJoinedMessage.users);
|
|
|
|
this.connectionAnswerPromiseResolve(onConnect.room);
|
|
|
|
// Analyze tags to find if we are admin. If yes, show console.
|
2020-12-04 11:30:35 +01:00
|
|
|
this.ConsoleGlobalMessageManager = new ConsoleGlobalMessageManager(this.connection, this.userInputManager, this.connection.isAdmin());
|
2020-12-11 13:06:10 +01:00
|
|
|
|
2020-12-11 13:00:11 +01:00
|
|
|
|
2020-12-18 16:08:04 +01:00
|
|
|
this.scene.wake();
|
|
|
|
this.scene.stop(ReconnectingSceneName);
|
2020-12-03 16:39:44 +01:00
|
|
|
|
2020-11-10 17:00:23 +01:00
|
|
|
//init user position and play trigger to check layers properties
|
|
|
|
this.gameMap.setPosition(this.CurrentPlayer.x, this.CurrentPlayer.y);
|
|
|
|
|
2020-12-03 16:39:44 +01:00
|
|
|
return this.connection;
|
2020-10-06 18:09:23 +02:00
|
|
|
});
|
2020-08-17 15:20:03 +02:00
|
|
|
}
|
|
|
|
|
2020-11-10 17:00:23 +01:00
|
|
|
private triggerOnMapLayerPropertyChange(){
|
2020-11-18 18:15:57 +01:00
|
|
|
this.gameMap.onPropertyChange('exitSceneUrl', (newValue, oldValue) => {
|
|
|
|
if (newValue) this.onMapExit(newValue as string);
|
|
|
|
});
|
|
|
|
this.gameMap.onPropertyChange('exitUrl', (newValue, oldValue) => {
|
|
|
|
if (newValue) this.onMapExit(newValue as string);
|
|
|
|
});
|
2020-11-21 15:40:24 +01:00
|
|
|
this.gameMap.onPropertyChange('openWebsite', (newValue, oldValue, allProps) => {
|
2020-11-10 17:00:23 +01:00
|
|
|
if (newValue === undefined) {
|
2020-11-21 15:40:24 +01:00
|
|
|
layoutManager.removeActionButton('openWebsite', this.userInputManager);
|
2020-11-10 17:00:23 +01:00
|
|
|
coWebsiteManager.closeCoWebsite();
|
2020-11-21 15:40:24 +01:00
|
|
|
}else{
|
|
|
|
const openWebsiteFunction = () => {
|
|
|
|
coWebsiteManager.loadCoWebsite(newValue as string);
|
|
|
|
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) {
|
|
|
|
layoutManager.addActionButton('openWebsite', 'Click on SPACE to open the web site', () => {
|
2020-11-21 15:40:24 +01:00
|
|
|
openWebsiteFunction();
|
|
|
|
}, this.userInputManager);
|
|
|
|
}else{
|
|
|
|
openWebsiteFunction();
|
|
|
|
}
|
2020-11-10 17:00:23 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
this.gameMap.onPropertyChange('jitsiRoom', (newValue, oldValue, allProps) => {
|
|
|
|
if (newValue === undefined) {
|
2020-11-21 15:40:24 +01:00
|
|
|
layoutManager.removeActionButton('jitsiRoom', this.userInputManager);
|
2020-11-10 17:00:23 +01:00
|
|
|
this.stopJitsi();
|
2020-11-21 15:40:24 +01:00
|
|
|
}else{
|
|
|
|
const openJitsiRoomFunction = () => {
|
|
|
|
if (JITSI_PRIVATE_MODE) {
|
|
|
|
const adminTag = allProps.get("jitsiRoomAdminTag") as string|undefined;
|
|
|
|
|
|
|
|
this.connection.emitQueryJitsiJwtMessage(this.instance.replace('/', '-') + "-" + newValue, adminTag);
|
|
|
|
} else {
|
|
|
|
this.startJitsi(newValue as string);
|
|
|
|
}
|
|
|
|
layoutManager.removeActionButton('jitsiRoom', this.userInputManager);
|
|
|
|
}
|
2020-11-10 17:00:23 +01:00
|
|
|
|
2020-11-23 20:34:05 +01:00
|
|
|
const jitsiTriggerValue = allProps.get(TRIGGER_JITSI_PROPERTIES);
|
|
|
|
if(jitsiTriggerValue && jitsiTriggerValue === ON_ACTION_TRIGGER_BUTTON) {
|
2020-11-21 15:40:24 +01:00
|
|
|
layoutManager.addActionButton('jitsiRoom', 'Click on SPACE to enter in jitsi meet room', () => {
|
|
|
|
openJitsiRoomFunction();
|
|
|
|
}, this.userInputManager);
|
|
|
|
}else{
|
|
|
|
openJitsiRoomFunction();
|
2020-11-10 17:00:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
this.gameMap.onPropertyChange('silent', (newValue, oldValue) => {
|
|
|
|
if (newValue === undefined || newValue === false || newValue === '') {
|
|
|
|
this.connection.setSilent(false);
|
|
|
|
} else {
|
|
|
|
this.connection.setSilent(true);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2020-12-11 13:00:11 +01:00
|
|
|
|
2020-11-18 18:15:57 +01:00
|
|
|
private onMapExit(exitKey: string) {
|
|
|
|
const {roomId, hash} = Room.getIdFromIdentifier(exitKey, this.MapUrlFile, this.instance);
|
|
|
|
if (!roomId) throw new Error('Could not find the room from its exit key: '+exitKey);
|
2020-11-19 14:32:18 +01:00
|
|
|
urlManager.pushStartLayerNameToUrl(hash);
|
2020-11-18 18:15:57 +01:00
|
|
|
if (roomId !== this.scene.key) {
|
2020-12-04 11:30:35 +01:00
|
|
|
this.cleanupClosingScene();
|
2020-11-18 18:15:57 +01:00
|
|
|
this.scene.stop();
|
|
|
|
this.scene.remove(this.scene.key);
|
2020-11-19 14:32:18 +01:00
|
|
|
this.scene.start(roomId);
|
2020-11-18 18:15:57 +01:00
|
|
|
} else {
|
|
|
|
//if the exit points to the current map, we simply teleport the user back to the startLayer
|
2020-11-19 14:32:18 +01:00
|
|
|
this.initPositionFromLayerName(hash || defaultStartLayerName);
|
2020-11-18 18:15:57 +01:00
|
|
|
this.CurrentPlayer.x = this.startX;
|
|
|
|
this.CurrentPlayer.y = this.startY;
|
|
|
|
}
|
|
|
|
}
|
2020-11-10 17:00:23 +01:00
|
|
|
|
2020-12-04 11:30:35 +01:00
|
|
|
public cleanupClosingScene(): void {
|
|
|
|
// We are completely destroying the current scene to avoid using a half-backed instance when coming back to the same map.
|
2020-12-18 13:56:25 +01:00
|
|
|
if(this.connection) {
|
|
|
|
this.connection.closeConnection();
|
|
|
|
}
|
|
|
|
if(this.simplePeer) {
|
|
|
|
this.simplePeer.unregister();
|
|
|
|
}
|
2020-12-04 11:30:35 +01:00
|
|
|
}
|
|
|
|
|
2020-08-17 15:20:03 +02:00
|
|
|
private switchLayoutMode(): void {
|
2020-10-27 20:46:53 +01:00
|
|
|
//if discussion is activated, this layout cannot be activated
|
|
|
|
if(mediaManager.activatedDiscussion){
|
|
|
|
return;
|
|
|
|
}
|
2020-08-17 15:20:03 +02:00
|
|
|
const mode = layoutManager.getLayoutMode();
|
|
|
|
if (mode === LayoutMode.Presentation) {
|
|
|
|
layoutManager.switchLayoutMode(LayoutMode.VideoChat);
|
|
|
|
this.presentationModeSprite.setFrame(1);
|
|
|
|
this.chatModeSprite.setFrame(2);
|
|
|
|
} else {
|
|
|
|
layoutManager.switchLayoutMode(LayoutMode.Presentation);
|
|
|
|
this.presentationModeSprite.setFrame(0);
|
|
|
|
this.chatModeSprite.setFrame(3);
|
|
|
|
}
|
2020-04-13 19:56:41 +02:00
|
|
|
}
|
|
|
|
|
2020-11-17 15:02:21 +01:00
|
|
|
private initStartXAndStartY() {
|
|
|
|
// If there is an init position passed
|
|
|
|
if (this.initPosition !== null) {
|
|
|
|
this.startX = this.initPosition.x;
|
|
|
|
this.startY = this.initPosition.y;
|
|
|
|
} else {
|
|
|
|
// Now, let's find the start layer
|
2020-11-19 14:32:18 +01:00
|
|
|
if (this.startLayerName) {
|
|
|
|
this.initPositionFromLayerName(this.startLayerName);
|
2020-11-17 15:02:21 +01:00
|
|
|
}
|
|
|
|
if (this.startX === undefined) {
|
|
|
|
// If we have no start layer specified or if the hash passed does not exist, let's go with the default start position.
|
2020-11-19 14:32:18 +01:00
|
|
|
this.initPositionFromLayerName(defaultStartLayerName);
|
2020-11-17 15:02:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Still no start position? Something is wrong with the map, we need a "start" layer.
|
|
|
|
if (this.startX === undefined) {
|
|
|
|
console.warn('This map is missing a layer named "start" that contains the available default start positions.');
|
|
|
|
// Let's start in the middle of the map
|
|
|
|
this.startX = this.mapFile.width * 16;
|
|
|
|
this.startY = this.mapFile.height * 16;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private initPositionFromLayerName(layerName: string) {
|
|
|
|
for (const layer of this.mapFile.layers) {
|
2020-11-19 14:32:18 +01:00
|
|
|
if (layerName === layer.name && layer.type === 'tilelayer' && (layerName === defaultStartLayerName || this.isStartLayer(layer))) {
|
2020-11-17 15:02:21 +01:00
|
|
|
const startPosition = this.startUser(layer);
|
2020-11-25 15:58:10 +01:00
|
|
|
this.startX = startPosition.x + this.mapFile.tilewidth/2;
|
|
|
|
this.startY = startPosition.y + this.mapFile.tileheight/2;
|
2020-11-17 15:02:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-04-13 19:56:41 +02:00
|
|
|
|
2020-10-15 15:50:51 +02:00
|
|
|
private getExitUrl(layer: ITiledMapLayer): string|undefined {
|
|
|
|
return this.getProperty(layer, "exitUrl") as string|undefined;
|
|
|
|
}
|
|
|
|
|
2020-11-18 18:15:57 +01:00
|
|
|
/**
|
|
|
|
* @deprecated the map property exitSceneUrl is deprecated
|
|
|
|
*/
|
2020-05-11 18:49:20 +02:00
|
|
|
private getExitSceneUrl(layer: ITiledMapLayer): string|undefined {
|
2020-06-07 13:23:32 +02:00
|
|
|
return this.getProperty(layer, "exitSceneUrl") as string|undefined;
|
2020-05-11 18:49:20 +02:00
|
|
|
}
|
|
|
|
|
2020-06-07 13:23:32 +02:00
|
|
|
private isStartLayer(layer: ITiledMapLayer): boolean {
|
|
|
|
return this.getProperty(layer, "startLayer") == true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private getProperty(layer: ITiledMapLayer, name: string): string|boolean|number|undefined {
|
2020-06-19 16:36:40 +02:00
|
|
|
const properties = layer.properties;
|
2020-05-23 17:27:49 +02:00
|
|
|
if (!properties) {
|
|
|
|
return undefined;
|
|
|
|
}
|
2020-11-04 13:33:58 +01:00
|
|
|
const obj = properties.find((property: ITiledMapLayerProperty) => property.name.toLowerCase() === name.toLowerCase());
|
2020-05-23 17:27:49 +02:00
|
|
|
if (obj === undefined) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
return obj.value;
|
|
|
|
}
|
|
|
|
|
2020-10-15 15:50:51 +02:00
|
|
|
//todo: push that into the gameManager
|
2020-11-18 18:15:57 +01:00
|
|
|
private async loadNextGame(exitSceneIdentifier: string){
|
|
|
|
const {roomId, hash} = Room.getIdFromIdentifier(exitSceneIdentifier, this.MapUrlFile, this.instance);
|
2020-10-13 18:44:50 +02:00
|
|
|
const room = new Room(roomId);
|
2020-11-18 18:15:57 +01:00
|
|
|
await gameManager.loadMap(room, this.scene);
|
2020-05-09 21:28:50 +02:00
|
|
|
}
|
|
|
|
|
2020-06-03 11:14:04 +02:00
|
|
|
private startUser(layer: ITiledMapLayer): PositionInterface {
|
2020-06-19 16:36:40 +02:00
|
|
|
const tiles = layer.data;
|
|
|
|
if (typeof(tiles) === 'string') {
|
|
|
|
throw new Error('The content of a JSON map must be filled as a JSON array, not as a string');
|
|
|
|
}
|
|
|
|
const possibleStartPositions : PositionInterface[] = [];
|
2020-05-09 21:28:50 +02:00
|
|
|
tiles.forEach((objectKey : number, key: number) => {
|
|
|
|
if(objectKey === 0){
|
|
|
|
return;
|
|
|
|
}
|
2020-06-09 23:13:26 +02:00
|
|
|
const y = Math.floor(key / layer.width);
|
|
|
|
const x = key % layer.width;
|
2020-05-09 21:28:50 +02:00
|
|
|
|
2020-06-03 11:14:04 +02:00
|
|
|
possibleStartPositions.push({x: x*32, y: y*32});
|
2020-05-09 21:28:50 +02:00
|
|
|
});
|
2020-06-03 11:14:04 +02:00
|
|
|
// Get a value at random amongst allowed values
|
|
|
|
if (possibleStartPositions.length === 0) {
|
|
|
|
console.warn('The start layer "'+layer.name+'" for this map is empty.');
|
|
|
|
return {
|
|
|
|
x: 0,
|
|
|
|
y: 0
|
|
|
|
};
|
|
|
|
}
|
|
|
|
// Choose one of the available start positions at random amongst the list of available start positions.
|
|
|
|
return possibleStartPositions[Math.floor(Math.random() * possibleStartPositions.length)];
|
2020-05-09 21:28:50 +02:00
|
|
|
}
|
|
|
|
|
2020-04-13 19:56:41 +02:00
|
|
|
//todo: in a dedicated class/function?
|
|
|
|
initCamera() {
|
|
|
|
this.cameras.main.setBounds(0,0, this.Map.widthInPixels, this.Map.heightInPixels);
|
2020-08-24 14:19:36 +02:00
|
|
|
this.updateCameraOffset();
|
2020-04-13 19:56:41 +02:00
|
|
|
this.cameras.main.setZoom(ZOOM_LEVEL);
|
2020-04-10 12:54:05 +02:00
|
|
|
}
|
|
|
|
|
2020-04-13 15:34:09 +02:00
|
|
|
addLayer(Layer : Phaser.Tilemaps.StaticTilemapLayer){
|
|
|
|
this.Layers.push(Layer);
|
|
|
|
}
|
|
|
|
|
|
|
|
createCollisionWithPlayer() {
|
|
|
|
//add collision layer
|
|
|
|
this.Layers.forEach((Layer: Phaser.Tilemaps.StaticTilemapLayer) => {
|
2020-06-09 23:12:54 +02:00
|
|
|
this.physics.add.collider(this.CurrentPlayer, Layer, (object1: GameObject, object2: GameObject) => {
|
2020-04-13 16:56:06 +02:00
|
|
|
//this.CurrentPlayer.say("Collision with layer : "+ (object2 as Tile).layer.name)
|
2020-04-13 15:34:09 +02:00
|
|
|
});
|
|
|
|
Layer.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
|
|
|
|
Layer.renderDebug(this.add.graphics(), {
|
|
|
|
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 15:34:09 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-05-14 23:19:48 +02:00
|
|
|
createCurrentPlayer(){
|
2020-04-13 15:34:09 +02:00
|
|
|
//initialise player
|
2020-05-23 17:27:49 +02:00
|
|
|
//TODO create animation moving between exit and start
|
2020-12-18 14:30:46 +01:00
|
|
|
try {
|
|
|
|
this.CurrentPlayer = new Player(
|
|
|
|
this,
|
|
|
|
this.startX,
|
|
|
|
this.startY,
|
|
|
|
this.playerName,
|
|
|
|
this.characterLayers,
|
|
|
|
PlayerAnimationNames.WalkDown,
|
|
|
|
false,
|
|
|
|
this.userInputManager
|
|
|
|
);
|
|
|
|
}catch (err){
|
|
|
|
if(err instanceof TextureError) {
|
|
|
|
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-02 16:54:52 +02:00
|
|
|
}
|
2020-05-04 18:38:04 +02:00
|
|
|
|
2020-05-02 16:54:52 +02:00
|
|
|
pushPlayerPosition(event: HasMovedEvent) {
|
2020-06-01 22:42:18 +02:00
|
|
|
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.
|
|
|
|
}
|
|
|
|
|
2020-07-23 18:09:24 +02:00
|
|
|
/**
|
|
|
|
* Finds the correct item to outline and outline it (if there is an item to be outlined)
|
|
|
|
* @param event
|
|
|
|
*/
|
|
|
|
private outlineItem(event: HasMovedEvent): void {
|
|
|
|
let x = event.x;
|
|
|
|
let y = event.y;
|
|
|
|
switch (event.direction) {
|
|
|
|
case PlayerAnimationNames.WalkUp:
|
|
|
|
y -= 32;
|
|
|
|
break;
|
|
|
|
case PlayerAnimationNames.WalkDown:
|
|
|
|
y += 32;
|
|
|
|
break;
|
|
|
|
case PlayerAnimationNames.WalkLeft:
|
|
|
|
x -= 32;
|
|
|
|
break;
|
|
|
|
case PlayerAnimationNames.WalkRight:
|
|
|
|
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);
|
2020-07-23 18:09:24 +02:00
|
|
|
if (distance !== null && distance < shortestDistance) {
|
|
|
|
shortestDistance = distance;
|
|
|
|
selectedItem = item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.outlinedItem === selectedItem) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.outlinedItem?.notSelectable();
|
|
|
|
this.outlinedItem = selectedItem;
|
|
|
|
this.outlinedItem?.selectable();
|
|
|
|
}
|
|
|
|
|
2020-06-01 22:42:18 +02:00
|
|
|
private doPushPlayerPosition(event: HasMovedEvent): void {
|
|
|
|
this.lastMoveEventSent = event;
|
|
|
|
this.lastSentTick = this.currentTick;
|
2020-09-15 10:06:11 +02:00
|
|
|
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,
|
|
|
|
});
|
2020-04-13 15:34:09 +02:00
|
|
|
}
|
|
|
|
|
2020-04-18 17:16:39 +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 {
|
2020-11-21 18:47:38 +01:00
|
|
|
mediaManager.setLastUpdateScene();
|
2020-06-01 22:42:18 +02:00
|
|
|
this.currentTick = time;
|
2020-04-18 17:16:39 +02:00
|
|
|
this.CurrentPlayer.moveUser(delta);
|
2020-06-02 13:44:42 +02:00
|
|
|
|
2020-06-19 18:18:43 +02:00
|
|
|
// Let's handle all events
|
2020-06-19 18:24:32 +02:00
|
|
|
while (this.pendingEvents.length !== 0) {
|
|
|
|
const event = this.pendingEvents.dequeue();
|
2020-06-19 18:18:43 +02:00
|
|
|
switch (event.type) {
|
|
|
|
case "InitUserPositionEvent":
|
|
|
|
this.doInitUsersPosition(event.event);
|
|
|
|
break;
|
|
|
|
case "AddPlayerEvent":
|
|
|
|
this.doAddPlayer(event.event);
|
|
|
|
break;
|
|
|
|
case "RemovePlayerEvent":
|
|
|
|
this.doRemovePlayer(event.userId);
|
|
|
|
break;
|
|
|
|
case "UserMovedEvent":
|
|
|
|
this.doUpdatePlayerPosition(event.event);
|
|
|
|
break;
|
|
|
|
case "GroupCreatedUpdatedEvent":
|
|
|
|
this.doShareGroupPosition(event.event);
|
|
|
|
break;
|
|
|
|
case "DeleteGroupEvent":
|
|
|
|
this.doDeleteGroup(event.groupId);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-02 13:44:42 +02:00
|
|
|
// Let's move all users
|
2020-06-09 23:13:26 +02:00
|
|
|
const updatedPlayersPositions = this.playersPositionInterpolator.getUpdatedPositions(time);
|
2020-09-18 13:57:38 +02:00
|
|
|
updatedPlayersPositions.forEach((moveEvent: HasMovedEvent, userId: number) => {
|
2020-06-09 23:13:26 +02:00
|
|
|
const player : RemotePlayer | undefined = this.MapPlayersByKey.get(userId);
|
2020-06-02 13:44:42 +02:00
|
|
|
if (player === undefined) {
|
|
|
|
throw new Error('Cannot find player with ID "' + userId +'"');
|
|
|
|
}
|
|
|
|
player.updatePosition(moveEvent);
|
|
|
|
});
|
2020-05-09 21:28:50 +02:00
|
|
|
}
|
|
|
|
|
2020-06-19 18:18:43 +02:00
|
|
|
/**
|
|
|
|
* Called by the connexion when the full list of user position is received.
|
|
|
|
*/
|
2020-06-22 11:58:07 +02:00
|
|
|
private initUsersPosition(usersPosition: MessageUserPositionInterface[]): void {
|
2020-06-19 18:18:43 +02:00
|
|
|
this.pendingEvents.enqueue({
|
|
|
|
type: "InitUserPositionEvent",
|
|
|
|
event: usersPosition
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Put all the players on the map on map load.
|
|
|
|
*/
|
|
|
|
private doInitUsersPosition(usersPosition: MessageUserPositionInterface[]): void {
|
2020-06-22 18:42:54 +02:00
|
|
|
const currentPlayerId = this.connection.getUserId();
|
2020-05-19 19:11:12 +02:00
|
|
|
|
|
|
|
// clean map
|
2020-06-04 18:54:34 +02:00
|
|
|
this.MapPlayersByKey.forEach((player: RemotePlayer) => {
|
2020-05-19 19:11:12 +02:00
|
|
|
player.destroy();
|
|
|
|
this.MapPlayers.remove(player);
|
|
|
|
});
|
2020-09-18 13:57:38 +02:00
|
|
|
this.MapPlayersByKey = new Map<number, RemotePlayer>();
|
2020-05-19 19:11:12 +02:00
|
|
|
|
|
|
|
// load map
|
|
|
|
usersPosition.forEach((userPosition : MessageUserPositionInterface) => {
|
|
|
|
if(userPosition.userId === currentPlayerId){
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.addPlayer(userPosition);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-06-19 18:18:43 +02:00
|
|
|
/**
|
|
|
|
* Called by the connexion when a new player arrives on a map
|
|
|
|
*/
|
|
|
|
public addPlayer(addPlayerData : AddPlayerInterface) : void {
|
|
|
|
this.pendingEvents.enqueue({
|
|
|
|
type: "AddPlayerEvent",
|
|
|
|
event: addPlayerData
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-04-10 12:54:05 +02:00
|
|
|
/**
|
2020-04-13 15:34:09 +02:00
|
|
|
* Create new player
|
2020-04-10 12:54:05 +02:00
|
|
|
*/
|
2020-10-20 16:39:23 +02:00
|
|
|
private async doAddPlayer(addPlayerData : AddPlayerInterface) : Promise<void> {
|
2020-05-23 14:00:36 +02:00
|
|
|
//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
|
|
|
// Load textures (in case it is a custom texture)
|
|
|
|
const characterLayerList: string[] = [];
|
|
|
|
const loadPromises: Promise<void>[] = [];
|
|
|
|
for (const characterLayer of addPlayerData.characterLayers) {
|
|
|
|
characterLayerList.push(characterLayer.name);
|
|
|
|
if (characterLayer.img) {
|
|
|
|
console.log('LOADING ', characterLayer.name, characterLayer.img)
|
|
|
|
loadPromises.push(this.loadSpritesheet(characterLayer.name, characterLayer.img));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (loadPromises.length > 0) {
|
|
|
|
this.load.start();
|
|
|
|
}
|
|
|
|
|
2020-04-13 15:34:09 +02:00
|
|
|
//initialise player
|
2020-06-09 23:13:26 +02:00
|
|
|
const player = new RemotePlayer(
|
2020-05-19 19:11:12 +02:00
|
|
|
addPlayerData.userId,
|
2020-04-13 15:34:09 +02:00
|
|
|
this,
|
2020-05-19 19:11:12 +02:00
|
|
|
addPlayerData.position.x,
|
|
|
|
addPlayerData.position.y,
|
|
|
|
addPlayerData.name,
|
2020-10-20 16:39:23 +02:00
|
|
|
[], // Let's go with no textures and let's load textures when promises have returned.
|
2020-05-22 22:59:43 +02:00
|
|
|
addPlayerData.position.direction,
|
|
|
|
addPlayerData.position.moving
|
2020-04-13 15:34:09 +02:00
|
|
|
);
|
|
|
|
this.MapPlayers.add(player);
|
2020-05-19 19:11:12 +02:00
|
|
|
this.MapPlayersByKey.set(player.userId, player);
|
|
|
|
player.updatePosition(addPlayerData.position);
|
2020-04-13 15:34:09 +02:00
|
|
|
|
2020-10-20 16:39:23 +02:00
|
|
|
|
|
|
|
await Promise.all(loadPromises);
|
|
|
|
|
|
|
|
player.addTextures(characterLayerList, 1);
|
2020-04-07 20:41:35 +02:00
|
|
|
}
|
2020-05-08 00:35:36 +02:00
|
|
|
|
2020-06-19 18:18:43 +02:00
|
|
|
/**
|
|
|
|
* Called by the connexion when a player is removed from the map
|
|
|
|
*/
|
2020-09-18 13:57:38 +02:00
|
|
|
public removePlayer(userId: number) {
|
2020-06-19 18:18:43 +02:00
|
|
|
this.pendingEvents.enqueue({
|
|
|
|
type: "RemovePlayerEvent",
|
|
|
|
userId
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-09-18 13:57:38 +02:00
|
|
|
private doRemovePlayer(userId: number) {
|
2020-06-09 23:13:26 +02:00
|
|
|
const player = this.MapPlayersByKey.get(userId);
|
2020-05-19 19:11:12 +02:00
|
|
|
if (player === undefined) {
|
|
|
|
console.error('Cannot find user with id ', userId);
|
2020-06-04 18:11:07 +02:00
|
|
|
} else {
|
|
|
|
player.destroy();
|
|
|
|
this.MapPlayers.remove(player);
|
2020-05-19 19:11:12 +02:00
|
|
|
}
|
|
|
|
this.MapPlayersByKey.delete(userId);
|
2020-06-02 13:44:42 +02:00
|
|
|
this.playersPositionInterpolator.removePlayer(userId);
|
2020-05-19 19:11:12 +02:00
|
|
|
}
|
|
|
|
|
2020-06-19 18:18:43 +02:00
|
|
|
public updatePlayerPosition(message: MessageUserMovedInterface): void {
|
|
|
|
this.pendingEvents.enqueue({
|
|
|
|
type: "UserMovedEvent",
|
|
|
|
event: message
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private doUpdatePlayerPosition(message: MessageUserMovedInterface): void {
|
2020-06-09 23:13:26 +02:00
|
|
|
const player : RemotePlayer | undefined = this.MapPlayersByKey.get(message.userId);
|
2020-05-19 19:11:12 +02:00
|
|
|
if (player === undefined) {
|
2020-06-22 18:42:54 +02:00
|
|
|
//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;
|
2020-05-19 19:11:12 +02:00
|
|
|
}
|
2020-06-02 13:44:42 +02:00
|
|
|
|
|
|
|
// 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.
|
2020-06-09 23:13:26 +02:00
|
|
|
const playerMovement = new PlayerMovement({ x: player.x, y: player.y }, this.currentTick, message.position, this.currentTick + POSITION_DELAY);
|
2020-06-02 13:44:42 +02:00
|
|
|
this.playersPositionInterpolator.updatePlayerPosition(player.userId, playerMovement);
|
2020-05-19 19:11:12 +02:00
|
|
|
}
|
|
|
|
|
2020-06-19 18:18:43 +02:00
|
|
|
public shareGroupPosition(groupPositionMessage: GroupCreatedUpdatedMessageInterface) {
|
|
|
|
this.pendingEvents.enqueue({
|
|
|
|
type: "GroupCreatedUpdatedEvent",
|
|
|
|
event: groupPositionMessage
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private doShareGroupPosition(groupPositionMessage: GroupCreatedUpdatedMessageInterface) {
|
2020-10-21 23:08:05 +02:00
|
|
|
//delete previous group
|
|
|
|
this.doDeleteGroup(groupPositionMessage.groupId);
|
2020-05-08 00:35:36 +02:00
|
|
|
|
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),
|
|
|
|
groupPositionMessage.groupSize === 4 ? 'circleSprite-red' : 'circleSprite-white'
|
|
|
|
);
|
|
|
|
sprite.setDisplayOrigin(48, 48);
|
|
|
|
this.add.existing(sprite);
|
|
|
|
this.groups.set(groupPositionMessage.groupId, sprite);
|
|
|
|
return sprite;
|
2020-05-08 00:35:36 +02:00
|
|
|
}
|
|
|
|
|
2020-09-21 11:24:03 +02:00
|
|
|
deleteGroup(groupId: number): void {
|
2020-06-19 18:18:43 +02:00
|
|
|
this.pendingEvents.enqueue({
|
|
|
|
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);
|
2020-06-04 18:11:07 +02:00
|
|
|
if(!group){
|
2020-05-11 13:17:02 +02:00
|
|
|
return;
|
|
|
|
}
|
2020-06-04 18:11:07 +02:00
|
|
|
group.destroy();
|
2020-05-08 00:35:36 +02:00
|
|
|
this.groups.delete(groupId);
|
|
|
|
}
|
2020-05-23 17:27:49 +02:00
|
|
|
|
2020-10-08 18:51:24 +02:00
|
|
|
|
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-08-17 22:51:37 +02:00
|
|
|
|
2020-10-08 16:00:29 +02:00
|
|
|
public onResize(): void {
|
2020-09-15 16:21:41 +02:00
|
|
|
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,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-08-17 15:20:03 +02:00
|
|
|
private reposition(): void {
|
|
|
|
this.presentationModeSprite.setY(this.game.renderer.height - 2);
|
|
|
|
this.chatModeSprite.setY(this.game.renderer.height - 2);
|
2020-08-24 14:19:36 +02:00
|
|
|
|
|
|
|
// Recompute camera offset if needed
|
|
|
|
this.updateCameraOffset();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates the offset of the character compared to the center of the screen according to the layout mananger
|
|
|
|
* (tries to put the character in the center of the reamining space if there is a discussion going on.
|
|
|
|
*/
|
|
|
|
private updateCameraOffset(): void {
|
|
|
|
const array = layoutManager.findBiggestAvailableArray();
|
|
|
|
let xCenter = (array.xEnd - array.xStart) / 2 + array.xStart;
|
|
|
|
let yCenter = (array.yEnd - array.yStart) / 2 + array.yStart;
|
|
|
|
|
|
|
|
// Let's put this in Game coordinates by applying the zoom level:
|
|
|
|
xCenter /= ZOOM_LEVEL * RESOLUTION;
|
|
|
|
yCenter /= ZOOM_LEVEL * RESOLUTION;
|
|
|
|
|
|
|
|
//console.log("updateCameraOffset", array, xCenter, yCenter, this.game.renderer.width, this.game.renderer.height);
|
|
|
|
|
|
|
|
this.cameras.main.startFollow(this.CurrentPlayer, true, 1, 1, xCenter - this.game.renderer.width / 2, yCenter - this.game.renderer.height / 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
public onCenterChange(): void {
|
|
|
|
this.updateCameraOffset();
|
2020-08-17 15:20:03 +02:00
|
|
|
}
|
2020-10-16 19:13:26 +02:00
|
|
|
|
2020-10-19 11:07:49 +02:00
|
|
|
public startJitsi(roomName: string, jwt?: string): void {
|
2020-12-04 11:30:35 +01:00
|
|
|
jitsiFactory.start(roomName, this.playerName, jwt);
|
2020-10-16 19:13:26 +02:00
|
|
|
this.connection.setSilent(true);
|
|
|
|
mediaManager.hideGameOverlay();
|
2020-11-17 18:03:44 +01:00
|
|
|
|
|
|
|
//permit to stop jitsi when user close iframe
|
|
|
|
mediaManager.addTriggerCloseJitsiFrameButton('close-jisi',() => {
|
|
|
|
this.stopJitsi();
|
|
|
|
});
|
2020-10-16 19:13:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public stopJitsi(): void {
|
|
|
|
this.connection.setSilent(false);
|
2020-10-23 16:16:30 +02:00
|
|
|
jitsiFactory.stop();
|
2020-10-16 19:13:26 +02:00
|
|
|
mediaManager.showGameOverlay();
|
2020-11-17 18:03:44 +01:00
|
|
|
|
|
|
|
mediaManager.removeTriggerCloseJitsiFrameButton('close-jisi');
|
2020-10-16 19:13:26 +02:00
|
|
|
}
|
2020-10-20 16:39:23 +02:00
|
|
|
|
|
|
|
private loadSpritesheet(name: string, url: string): Promise<void> {
|
|
|
|
return new Promise<void>(((resolve, reject) => {
|
2020-10-21 10:34:55 +02:00
|
|
|
if (this.textures.exists(name)) {
|
|
|
|
resolve();
|
|
|
|
return;
|
|
|
|
}
|
2020-10-20 16:39:23 +02:00
|
|
|
this.load.spritesheet(
|
|
|
|
name,
|
|
|
|
url,
|
|
|
|
{frameWidth: 32, frameHeight: 32}
|
|
|
|
);
|
|
|
|
this.load.on('filecomplete-spritesheet-'+name, () => {
|
|
|
|
console.log('RESOURCE LOADED!');
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
}))
|
|
|
|
}
|
2020-12-18 15:58:49 +01:00
|
|
|
|
2020-12-04 11:30:35 +01:00
|
|
|
|
2020-04-07 19:23:21 +02:00
|
|
|
}
|