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-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-19 19:11:12 +02:00
|
|
|
import {GroupCreatedUpdatedMessageInterface, MessageUserJoined, MessageUserPositionInterface} from "../../Connexion";
|
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-19 19:11:12 +02:00
|
|
|
export class LogincScene extends Phaser.Scene {
|
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
|
2020-05-23 17:27:49 +02:00
|
|
|
let instanceAndMapUrl = this.findMapUrl();
|
|
|
|
if (instanceAndMapUrl !== null) {
|
|
|
|
let [mapUrl, instance] = instanceAndMapUrl;
|
|
|
|
let key = gameManager.loadMap(mapUrl, this.scene, instance);
|
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.
|
2020-05-24 22:53:10 +02:00
|
|
|
return gameManager.loadStartMap().then((scene : any) => {
|
2020-05-13 00:02:39 +02:00
|
|
|
if (!scene) {
|
|
|
|
return;
|
|
|
|
}
|
2020-05-23 17:27:49 +02:00
|
|
|
let key = gameManager.loadMap(window.location.protocol + "//" + scene.mapUrlStart, this.scene, scene.startInstance);
|
2020-05-13 00:02:39 +02:00
|
|
|
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-23 17:27:49 +02:00
|
|
|
/**
|
|
|
|
* Returns the map URL and the instance from the current URL
|
|
|
|
*/
|
|
|
|
private findMapUrl(): [string, string]|null {
|
2020-05-13 00:02:39 +02:00
|
|
|
let path = window.location.pathname;
|
|
|
|
if (!path.startsWith('/_/')) {
|
|
|
|
return null;
|
|
|
|
}
|
2020-05-23 17:27:49 +02:00
|
|
|
let instanceAndMap = path.substr(3);
|
|
|
|
let firstSlash = instanceAndMap.indexOf('/');
|
|
|
|
if (firstSlash === -1) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
let instance = instanceAndMap.substr(0, firstSlash);
|
|
|
|
return [window.location.protocol+'//'+instanceAndMap.substr(firstSlash+1), instance];
|
2020-05-13 00:02:39 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2020-05-01 22:23:41 +02:00
|
|
|
}
|