2020-05-06 01:50:01 +02:00
|
|
|
import {gameManager} from "../Game/GameManager";
|
2020-04-26 17:54:56 +02:00
|
|
|
import {TextField} from "../Components/TextField";
|
|
|
|
import {TextInput} from "../Components/TextInput";
|
|
|
|
import {ClickButton} from "../Components/ClickButton";
|
2020-05-09 19:41:21 +02:00
|
|
|
import {GameScene, GameSceneInterface} from "../Game/GameScene";
|
2020-05-01 22:23:41 +02:00
|
|
|
import Image = Phaser.GameObjects.Image;
|
2020-05-04 01:48:14 +02:00
|
|
|
import Rectangle = Phaser.GameObjects.Rectangle;
|
2020-05-06 01:50:01 +02:00
|
|
|
import {PLAYER_RESOURCES} from "../Entity/PlayableCaracter";
|
2020-05-04 18:38:04 +02:00
|
|
|
import {cypressAsserter} from "../../Cypress/CypressAsserter";
|
2020-05-09 16:47:55 +02:00
|
|
|
import {GroupCreatedUpdatedMessageInterface, MessageUserPositionInterface} from "../../Connexion";
|
2020-04-26 17:54:56 +02:00
|
|
|
|
2020-05-10 18:34:55 +02:00
|
|
|
export function getMapKeyByUrl(mapUrlStart: string){
|
2020-05-13 00:02:39 +02:00
|
|
|
// FIXME: the key should be computed from the full URL of the map.
|
|
|
|
let startPos = mapUrlStart.indexOf('://')+3;
|
|
|
|
let endPos = mapUrlStart.indexOf(".json");
|
|
|
|
return mapUrlStart.substring(startPos, endPos);
|
|
|
|
|
|
|
|
|
2020-05-10 18:34:55 +02:00
|
|
|
let tab = mapUrlStart.split("/");
|
|
|
|
return tab[tab.length -1].substr(0, tab[tab.length -1].indexOf(".json"));
|
|
|
|
}
|
|
|
|
|
2020-04-26 17:54:56 +02:00
|
|
|
//todo: put this constants in a dedicated file
|
|
|
|
export const LoginSceneName = "LoginScene";
|
|
|
|
enum LoginTextures {
|
2020-05-01 23:19:51 +02:00
|
|
|
//playButton = "play_button",
|
|
|
|
icon = "icon",
|
|
|
|
mainFont = "main_font"
|
2020-04-26 17:54:56 +02:00
|
|
|
}
|
|
|
|
|
2020-05-04 01:48:14 +02:00
|
|
|
export class LogincScene extends Phaser.Scene implements GameSceneInterface {
|
2020-05-01 23:19:51 +02:00
|
|
|
private nameInput: TextInput;
|
2020-04-26 17:54:56 +02:00
|
|
|
private textField: TextField;
|
|
|
|
private playButton: ClickButton;
|
2020-04-26 18:48:41 +02:00
|
|
|
private infoTextField: TextField;
|
2020-05-01 23:19:51 +02:00
|
|
|
private pressReturnField: TextField;
|
2020-05-01 22:23:41 +02:00
|
|
|
private logo: Image;
|
2020-04-29 00:01:37 +02:00
|
|
|
|
2020-05-04 01:48:14 +02:00
|
|
|
private selectedRectangle: Rectangle;
|
|
|
|
private selectedPlayer: Phaser.Physics.Arcade.Sprite;
|
|
|
|
private players: Array<Phaser.Physics.Arcade.Sprite> = new Array<Phaser.Physics.Arcade.Sprite>();
|
|
|
|
|
2020-04-26 17:54:56 +02:00
|
|
|
constructor() {
|
|
|
|
super({
|
|
|
|
key: LoginSceneName
|
|
|
|
});
|
|
|
|
}
|
2020-04-29 00:01:37 +02:00
|
|
|
|
2020-04-26 17:54:56 +02:00
|
|
|
preload() {
|
2020-05-04 18:38:04 +02:00
|
|
|
cypressAsserter.preloadStarted();
|
2020-05-01 23:19:51 +02:00
|
|
|
//this.load.image(LoginTextures.playButton, "resources/objects/play_button.png");
|
2020-05-01 22:23:41 +02:00
|
|
|
this.load.image(LoginTextures.icon, "resources/logos/tcm_full.png");
|
2020-05-01 23:19:51 +02:00
|
|
|
// Note: arcade.png from the Phaser 3 examples at: https://github.com/photonstorm/phaser3-examples/tree/master/public/assets/fonts/bitmap
|
|
|
|
this.load.bitmapFont(LoginTextures.mainFont, 'resources/fonts/arcade.png', 'resources/fonts/arcade.xml');
|
2020-05-04 18:38:04 +02:00
|
|
|
cypressAsserter.preloadFinished();
|
2020-05-04 01:48:14 +02:00
|
|
|
//add player png
|
2020-05-06 01:50:01 +02:00
|
|
|
PLAYER_RESOURCES.forEach((playerResource: any) => {
|
2020-05-04 01:48:14 +02:00
|
|
|
this.load.spritesheet(
|
|
|
|
playerResource.name,
|
|
|
|
playerResource.img,
|
|
|
|
{frameWidth: 32, frameHeight: 32}
|
|
|
|
);
|
|
|
|
});
|
2020-04-26 17:54:56 +02:00
|
|
|
}
|
2020-04-29 00:01:37 +02:00
|
|
|
|
2020-04-26 17:54:56 +02:00
|
|
|
create() {
|
2020-05-04 18:38:04 +02:00
|
|
|
cypressAsserter.initStarted();
|
|
|
|
|
2020-05-01 23:19:51 +02:00
|
|
|
this.textField = new TextField(this, this.game.renderer.width / 2, 50, 'Enter your name:');
|
|
|
|
this.textField.setOrigin(0.5).setCenterAlign()
|
|
|
|
this.nameInput = new TextInput(this, this.game.renderer.width / 2 - 64, 70, 4);
|
2020-04-26 18:48:41 +02:00
|
|
|
|
2020-05-01 23:19:51 +02:00
|
|
|
this.pressReturnField = new TextField(this, this.game.renderer.width / 2, 130, 'Press enter to start');
|
|
|
|
this.pressReturnField.setOrigin(0.5).setCenterAlign()
|
|
|
|
|
2020-05-04 01:48:14 +02:00
|
|
|
this.selectedRectangle = this.add.rectangle(32, 32, 32, 32).setStrokeStyle(2, 0xFFFFFF);
|
2020-04-26 18:48:41 +02:00
|
|
|
|
2020-05-01 22:23:41 +02:00
|
|
|
this.logo = new Image(this, this.game.renderer.width - 30, this.game.renderer.height - 20, LoginTextures.icon);
|
|
|
|
this.add.existing(this.logo);
|
|
|
|
|
|
|
|
let infoText = "Commands: \n - Arrows or Z,Q,S,D to move\n - SHIFT to run";
|
2020-05-01 23:19:51 +02:00
|
|
|
this.infoTextField = new TextField(this, 10, this.game.renderer.height - 35, infoText);
|
|
|
|
|
|
|
|
this.input.keyboard.on('keyup-ENTER', () => {
|
|
|
|
let name = this.nameInput.getText();
|
|
|
|
if (name === '') {
|
|
|
|
return
|
2020-05-01 23:48:30 +02:00
|
|
|
}
|
2020-05-03 15:18:15 +02:00
|
|
|
return this.login(name);
|
2020-05-01 23:19:51 +02:00
|
|
|
});
|
|
|
|
|
2020-05-04 01:48:14 +02:00
|
|
|
/*create user*/
|
2020-05-14 23:19:48 +02:00
|
|
|
this.createCurrentPlayer();
|
2020-05-04 18:38:04 +02:00
|
|
|
cypressAsserter.initFinished();
|
2020-04-26 17:54:56 +02:00
|
|
|
}
|
2020-04-29 00:01:37 +02:00
|
|
|
|
2020-04-26 17:54:56 +02:00
|
|
|
update(time: number, delta: number): void {
|
2020-05-01 23:19:51 +02:00
|
|
|
if (this.nameInput.getText() == '') {
|
|
|
|
this.pressReturnField.setVisible(false);
|
|
|
|
} else {
|
|
|
|
this.pressReturnField.setVisible(!!(Math.floor(time / 500) % 2));
|
|
|
|
}
|
2020-04-26 17:54:56 +02:00
|
|
|
}
|
2020-04-29 00:01:37 +02:00
|
|
|
|
2020-05-03 15:18:15 +02:00
|
|
|
private async login(name: string) {
|
2020-05-10 17:31:27 +02:00
|
|
|
return gameManager.connect(name, this.selectedPlayer.texture.key).then(() => {
|
2020-05-13 00:02:39 +02:00
|
|
|
// Do we have a start URL in the address bar? If so, let's redirect to this address
|
|
|
|
let mapUrl = this.findMapUrl();
|
|
|
|
if (mapUrl !== null) {
|
|
|
|
let key = gameManager.loadMap(mapUrl, this.scene);
|
2020-05-10 18:34:55 +02:00
|
|
|
this.scene.start(key);
|
2020-05-13 00:02:39 +02:00
|
|
|
return mapUrl;
|
|
|
|
} else {
|
|
|
|
// If we do not have a map address in the URL, let's ask the server for a start map.
|
|
|
|
return gameManager.loadMaps().then((scene : any) => {
|
|
|
|
if (!scene) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let key = gameManager.loadMap(window.location.protocol+"//"+scene.mapUrlStart, this.scene);
|
|
|
|
this.scene.start(key);
|
|
|
|
return scene;
|
|
|
|
}).catch((err) => {
|
|
|
|
console.error(err);
|
|
|
|
throw err;
|
|
|
|
});
|
|
|
|
}
|
2020-05-09 19:41:21 +02:00
|
|
|
}).catch((err) => {
|
|
|
|
console.error(err);
|
|
|
|
throw err;
|
2020-04-29 00:01:37 +02:00
|
|
|
});
|
2020-05-03 15:18:15 +02:00
|
|
|
}
|
2020-05-04 01:48:14 +02:00
|
|
|
|
2020-05-13 00:02:39 +02:00
|
|
|
private findMapUrl(): string|null {
|
|
|
|
let path = window.location.pathname;
|
|
|
|
if (!path.startsWith('/_/')) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return window.location.protocol+'//'+path.substr(3);
|
|
|
|
}
|
|
|
|
|
2020-05-04 01:48:14 +02:00
|
|
|
Map: Phaser.Tilemaps.Tilemap;
|
|
|
|
|
|
|
|
initAnimation(): void {
|
2020-05-09 19:41:21 +02:00
|
|
|
throw new Error("Method not implemented.");
|
2020-05-04 01:48:14 +02:00
|
|
|
}
|
|
|
|
|
2020-05-14 23:19:48 +02:00
|
|
|
createCurrentPlayer(): void {
|
2020-05-06 01:50:01 +02:00
|
|
|
for (let i = 0; i <PLAYER_RESOURCES.length; i++) {
|
|
|
|
let playerResource = PLAYER_RESOURCES[i];
|
2020-05-04 01:48:14 +02:00
|
|
|
let player = this.physics.add.sprite(playerResource.x, playerResource.y, playerResource.name, playerResource.name);
|
|
|
|
player.setBounce(0.2);
|
|
|
|
player.setCollideWorldBounds(true);
|
|
|
|
this.anims.create({
|
|
|
|
key: playerResource.name,
|
|
|
|
frames: this.anims.generateFrameNumbers(playerResource.name, {start: 0, end: 2,}),
|
|
|
|
frameRate: 10,
|
|
|
|
repeat: -1
|
|
|
|
});
|
|
|
|
player.setInteractive().on("pointerdown", () => {
|
|
|
|
this.selectedPlayer.anims.pause();
|
|
|
|
this.selectedRectangle.setY(player.y);
|
|
|
|
this.selectedRectangle.setX(player.x);
|
|
|
|
player.play(playerResource.name);
|
|
|
|
this.selectedPlayer = player;
|
|
|
|
});
|
|
|
|
this.players.push(player);
|
|
|
|
}
|
|
|
|
this.selectedPlayer = this.players[0];
|
2020-05-06 01:50:01 +02:00
|
|
|
this.selectedPlayer.play(PLAYER_RESOURCES[0].name);
|
2020-05-04 01:48:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
shareUserPosition(UsersPosition: import("../../Connexion").MessageUserPositionInterface[]): void {
|
|
|
|
throw new Error("Method not implemented.");
|
|
|
|
}
|
2020-05-09 16:47:55 +02:00
|
|
|
|
|
|
|
deleteGroup(groupId: string): void {
|
|
|
|
throw new Error("Method not implemented.");
|
|
|
|
}
|
|
|
|
|
|
|
|
shareGroupPosition(groupPositionMessage: GroupCreatedUpdatedMessageInterface): void {
|
|
|
|
throw new Error("Method not implemented.");
|
|
|
|
}
|
|
|
|
|
|
|
|
updateOrCreateMapPlayer(UsersPosition: Array<MessageUserPositionInterface>): void {
|
|
|
|
throw new Error("Method not implemented.");
|
|
|
|
}
|
2020-05-01 22:23:41 +02:00
|
|
|
}
|