2020-05-25 23:26:27 +02:00
|
|
|
import {gameManager} from "../Game/GameManager";
|
|
|
|
import {TextField} from "../Components/TextField";
|
|
|
|
import Image = Phaser.GameObjects.Image;
|
|
|
|
import Rectangle = Phaser.GameObjects.Rectangle;
|
2020-06-23 12:24:36 +02:00
|
|
|
import {EnableCameraSceneName} from "./EnableCameraScene";
|
2020-07-28 15:53:44 +02:00
|
|
|
import {CustomizeSceneName} from "./CustomizeScene";
|
2020-10-20 13:44:57 +02:00
|
|
|
import {localUserStore} from "../../Connexion/LocalUserStore";
|
2021-04-14 17:47:26 +02:00
|
|
|
import {loadAllDefaultModels} from "../Entity/PlayerTexturesLoadingManager";
|
2021-01-07 17:11:22 +01:00
|
|
|
import {addLoader} from "../Components/Loader";
|
|
|
|
import {BodyResourceDescriptionInterface} from "../Entity/PlayerTextures";
|
2021-01-24 18:00:30 +01:00
|
|
|
import {AbstractCharacterScene} from "./AbstractCharacterScene";
|
2021-04-07 14:09:45 +02:00
|
|
|
import {areCharacterLayersValid} from "../../Connexion/LocalUser";
|
2021-04-14 17:47:26 +02:00
|
|
|
import {touchScreenManager} from "../../Touch/TouchScreenManager";
|
|
|
|
import {PinchManager} from "../UserInput/PinchManager";
|
2021-04-22 02:29:13 +02:00
|
|
|
import {MenuScene} from "../Menu/MenuScene";
|
2020-07-28 15:53:44 +02:00
|
|
|
|
2020-05-25 23:26:27 +02:00
|
|
|
|
|
|
|
//todo: put this constants in a dedicated file
|
|
|
|
export const SelectCharacterSceneName = "SelectCharacterScene";
|
|
|
|
enum LoginTextures {
|
|
|
|
playButton = "play_button",
|
|
|
|
icon = "icon",
|
2020-07-28 15:53:44 +02:00
|
|
|
mainFont = "main_font",
|
|
|
|
customizeButton = "customize_button",
|
|
|
|
customizeButtonSelected = "customize_button_selected"
|
2020-05-25 23:26:27 +02:00
|
|
|
}
|
|
|
|
|
2021-04-22 02:29:13 +02:00
|
|
|
const selectCharacterKey = 'selectCharacterScene';
|
|
|
|
|
2021-01-24 18:00:30 +01:00
|
|
|
export class SelectCharacterScene extends AbstractCharacterScene {
|
2021-01-12 13:19:51 +01:00
|
|
|
private readonly nbCharactersPerRow = 6;
|
2020-08-07 23:39:06 +02:00
|
|
|
private selectedPlayer!: Phaser.Physics.Arcade.Sprite|null; // null if we are selecting the "customize" option
|
2020-05-25 23:26:27 +02:00
|
|
|
private players: Array<Phaser.Physics.Arcade.Sprite> = new Array<Phaser.Physics.Arcade.Sprite>();
|
2021-01-07 17:11:22 +01:00
|
|
|
private playerModels!: BodyResourceDescriptionInterface[];
|
2020-05-25 23:26:27 +02:00
|
|
|
|
2021-04-22 02:29:13 +02:00
|
|
|
private selectCharacterSceneElement!: Phaser.GameObjects.DOMElement;
|
|
|
|
private currentSelectUser = 0;
|
|
|
|
|
2020-05-25 23:26:27 +02:00
|
|
|
constructor() {
|
|
|
|
super({
|
2021-04-22 02:29:13 +02:00
|
|
|
key: SelectCharacterSceneName,
|
2020-05-25 23:26:27 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
preload() {
|
2021-01-07 17:11:22 +01:00
|
|
|
addLoader(this);
|
2021-01-24 18:00:30 +01:00
|
|
|
|
2021-04-22 02:29:13 +02:00
|
|
|
this.load.html(selectCharacterKey, 'resources/html/selectCharacterScene.html');
|
|
|
|
|
2021-01-24 18:00:30 +01:00
|
|
|
this.loadSelectSceneCharacters().then((bodyResourceDescriptions) => {
|
|
|
|
bodyResourceDescriptions.forEach((bodyResourceDescription) => {
|
|
|
|
this.playerModels.push(bodyResourceDescription);
|
|
|
|
});
|
|
|
|
})
|
2021-01-07 17:11:22 +01:00
|
|
|
this.playerModels = loadAllDefaultModels(this.load);
|
2021-02-09 12:41:35 +01:00
|
|
|
addLoader(this);
|
2020-05-25 23:26:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
create() {
|
2021-04-22 02:29:13 +02:00
|
|
|
const middleX = this.getMiddleX();
|
|
|
|
this.selectCharacterSceneElement = this.add.dom(middleX, 0).createFromCache(selectCharacterKey);
|
|
|
|
MenuScene.revealMenusAfterInit(this.selectCharacterSceneElement, selectCharacterKey);
|
|
|
|
|
|
|
|
this.selectCharacterSceneElement.addListener('click');
|
|
|
|
this.selectCharacterSceneElement.on('click', (event:MouseEvent) => {
|
|
|
|
event.preventDefault();
|
|
|
|
if((event?.target as HTMLInputElement).id === 'selectCharacterButtonLeft') {
|
|
|
|
this.moveToLeft();
|
|
|
|
}else if((event?.target as HTMLInputElement).id === 'selectCharacterButtonRight') {
|
|
|
|
this.moveToRight();
|
|
|
|
}else if((event?.target as HTMLInputElement).id === 'selectCharacterSceneFormSubmit') {
|
|
|
|
this.nextSceneToCameraScene();
|
|
|
|
}else if((event?.target as HTMLInputElement).id === 'selectCharacterSceneFormCustomYourOwnSubmit') {
|
|
|
|
this.nextSceneToCustomizeScene();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-04-14 17:47:26 +02:00
|
|
|
if (touchScreenManager.supportTouchScreen) {
|
|
|
|
new PinchManager(this);
|
|
|
|
}
|
2020-05-25 23:26:27 +02:00
|
|
|
|
2021-04-22 02:29:13 +02:00
|
|
|
/*create user*/
|
|
|
|
this.createCurrentPlayer();
|
|
|
|
const playerNumber = localUserStore.getPlayerCharacterIndex();
|
2020-05-25 23:26:27 +02:00
|
|
|
|
|
|
|
this.input.keyboard.on('keyup-ENTER', () => {
|
2021-04-22 02:29:13 +02:00
|
|
|
return this.nextSceneToCameraScene();
|
2020-05-25 23:26:27 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
this.input.keyboard.on('keydown-RIGHT', () => {
|
2021-04-22 02:29:13 +02:00
|
|
|
this.moveToRight();
|
2020-05-25 23:26:27 +02:00
|
|
|
});
|
|
|
|
this.input.keyboard.on('keydown-LEFT', () => {
|
2021-04-22 02:29:13 +02:00
|
|
|
this.moveToLeft();
|
2020-05-25 23:26:27 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-04-22 02:29:13 +02:00
|
|
|
private nextSceneToCameraScene(): void {
|
2021-04-07 14:09:45 +02:00
|
|
|
if (this.selectedPlayer !== null && !areCharacterLayersValid([this.selectedPlayer.texture.key])) {
|
|
|
|
return;
|
|
|
|
}
|
2021-04-22 02:29:13 +02:00
|
|
|
if(!this.selectedPlayer){
|
|
|
|
return;
|
2020-07-28 15:53:44 +02:00
|
|
|
}
|
2021-04-22 02:29:13 +02:00
|
|
|
this.scene.stop(SelectCharacterSceneName);
|
|
|
|
gameManager.setCharacterLayers([this.selectedPlayer.texture.key]);
|
|
|
|
gameManager.tryResumingGame(this, EnableCameraSceneName);
|
2020-12-16 15:09:58 +01:00
|
|
|
this.scene.remove(SelectCharacterSceneName);
|
2020-05-25 23:26:27 +02:00
|
|
|
}
|
|
|
|
|
2021-04-22 02:29:13 +02:00
|
|
|
private nextSceneToCustomizeScene(): void {
|
|
|
|
if (this.selectedPlayer !== null && !areCharacterLayersValid([this.selectedPlayer.texture.key])) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.scene.sleep(SelectCharacterSceneName);
|
|
|
|
this.scene.run(CustomizeSceneName);
|
|
|
|
}
|
|
|
|
|
2020-05-25 23:26:27 +02:00
|
|
|
createCurrentPlayer(): void {
|
2021-01-07 17:11:22 +01:00
|
|
|
for (let i = 0; i <this.playerModels.length; i++) {
|
|
|
|
const playerResource = this.playerModels[i];
|
2020-05-25 23:26:27 +02:00
|
|
|
|
2021-04-22 02:29:13 +02:00
|
|
|
const [middleX, middleY] = this.getCharacterPosition();
|
|
|
|
const player = this.physics.add.sprite(middleX, middleY, playerResource.name, 0);
|
|
|
|
this.setUpPlayer(player, i);
|
2020-05-25 23:26:27 +02:00
|
|
|
this.anims.create({
|
|
|
|
key: playerResource.name,
|
|
|
|
frames: this.anims.generateFrameNumbers(playerResource.name, {start: 0, end: 2,}),
|
|
|
|
frameRate: 10,
|
|
|
|
repeat: -1
|
|
|
|
});
|
|
|
|
player.setInteractive().on("pointerdown", () => {
|
2021-04-22 02:29:13 +02:00
|
|
|
if(this.currentSelectUser === i){
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.currentSelectUser = i;
|
|
|
|
this.moveUser();
|
2020-05-25 23:26:27 +02:00
|
|
|
});
|
|
|
|
this.players.push(player);
|
|
|
|
}
|
2020-07-28 15:53:44 +02:00
|
|
|
|
2021-04-22 02:29:13 +02:00
|
|
|
this.selectedPlayer = this.players[this.currentSelectUser];
|
|
|
|
this.selectedPlayer.play(this.playerModels[this.currentSelectUser].name);
|
|
|
|
}
|
2020-07-28 15:53:44 +02:00
|
|
|
|
2021-04-22 02:29:13 +02:00
|
|
|
private moveUser(){
|
|
|
|
for(let i = 0; i < this.players.length; i++){
|
|
|
|
const player = this.players[i];
|
|
|
|
this.setUpPlayer(player, i);
|
|
|
|
}
|
|
|
|
this.updateSelectedPlayer();
|
|
|
|
}
|
|
|
|
|
|
|
|
private moveToLeft(){
|
|
|
|
if(this.currentSelectUser === 0){
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.currentSelectUser -= 1;
|
|
|
|
this.moveUser();
|
|
|
|
}
|
2020-07-28 15:53:44 +02:00
|
|
|
|
2021-04-22 02:29:13 +02:00
|
|
|
private moveToRight(){
|
|
|
|
if(this.currentSelectUser === (this.players.length - 1)){
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.currentSelectUser += 1;
|
|
|
|
this.moveUser();
|
|
|
|
}
|
|
|
|
|
|
|
|
private defineSetupPlayer(numero: number){
|
|
|
|
const deltaX = 30;
|
|
|
|
let [playerX, playerY] = this.getCharacterPosition();
|
|
|
|
let playerVisible = true;
|
|
|
|
let playerScale = 1.5;
|
|
|
|
let playserOpactity = 1;
|
|
|
|
if( this.currentSelectUser !== numero ){
|
|
|
|
playerVisible = false;
|
|
|
|
}
|
|
|
|
if( numero === (this.currentSelectUser + 1) ){
|
|
|
|
playerX += deltaX;
|
|
|
|
playerScale = 0.8;
|
|
|
|
playserOpactity = 0.6;
|
|
|
|
playerVisible = true;
|
|
|
|
}
|
|
|
|
if( numero === (this.currentSelectUser + 2) ){
|
|
|
|
playerX += (deltaX * 2);
|
|
|
|
playerScale = 0.8;
|
|
|
|
playserOpactity = 0.6;
|
|
|
|
playerVisible = true;
|
|
|
|
}
|
|
|
|
if( numero === (this.currentSelectUser - 1) ){
|
|
|
|
playerX -= deltaX;
|
|
|
|
playerScale = 0.8;
|
|
|
|
playserOpactity = 0.6;
|
|
|
|
playerVisible = true;
|
|
|
|
}
|
|
|
|
if( numero === (this.currentSelectUser - 2) ){
|
|
|
|
playerX -= (deltaX * 2);
|
|
|
|
playerScale = 0.8;
|
|
|
|
playserOpactity = 0.6;
|
|
|
|
playerVisible = true;
|
|
|
|
}
|
|
|
|
return {playerX, playerY, playerScale, playserOpactity, playerVisible}
|
|
|
|
}
|
|
|
|
|
|
|
|
private setUpPlayer(player: Phaser.Physics.Arcade.Sprite, numero: number){
|
|
|
|
|
|
|
|
const {playerX, playerY, playerScale, playserOpactity, playerVisible} = this.defineSetupPlayer(numero);
|
|
|
|
player.setBounce(0.2);
|
|
|
|
player.setCollideWorldBounds(true);
|
|
|
|
player.setVisible( playerVisible );
|
|
|
|
player.setScale(playerScale, playerScale);
|
|
|
|
player.setAlpha(playserOpactity);
|
|
|
|
player.setX(playerX);
|
|
|
|
player.setY(playerY);
|
2020-05-25 23:26:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns pixel position by on column and row number
|
|
|
|
*/
|
2021-04-22 02:29:13 +02:00
|
|
|
private getCharacterPosition(): [number, number] {
|
2020-05-25 23:26:27 +02:00
|
|
|
return [
|
2021-04-22 02:29:13 +02:00
|
|
|
this.game.renderer.width / 2,
|
|
|
|
this.game.renderer.height / 3
|
2020-05-25 23:26:27 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
private updateSelectedPlayer(): void {
|
2020-07-28 15:53:44 +02:00
|
|
|
this.selectedPlayer?.anims.pause();
|
2021-04-22 02:29:13 +02:00
|
|
|
const player = this.players[this.currentSelectUser];
|
|
|
|
player.play(this.playerModels[this.currentSelectUser].name);
|
2020-05-25 23:26:27 +02:00
|
|
|
this.selectedPlayer = player;
|
2021-04-22 02:29:13 +02:00
|
|
|
localUserStore.setPlayerCharacterIndex(this.currentSelectUser);
|
2020-05-25 23:26:27 +02:00
|
|
|
}
|
2020-10-08 15:10:46 +02:00
|
|
|
|
2021-04-22 02:29:13 +02:00
|
|
|
update(time: number, delta: number): void {
|
|
|
|
const middleX = this.getMiddleX();
|
|
|
|
this.tweens.add({
|
|
|
|
targets: this.selectCharacterSceneElement,
|
|
|
|
x: middleX,
|
|
|
|
duration: 1000,
|
|
|
|
ease: 'Power3'
|
|
|
|
});
|
|
|
|
}
|
2020-10-08 15:10:46 +02:00
|
|
|
|
2021-04-22 02:29:13 +02:00
|
|
|
public onResize(ev: UIEvent): void {
|
|
|
|
//move position of user
|
|
|
|
this.moveUser();
|
|
|
|
|
|
|
|
const middleX = this.getMiddleX();
|
|
|
|
this.tweens.add({
|
|
|
|
targets: this.selectCharacterSceneElement,
|
|
|
|
x: middleX,
|
|
|
|
duration: 1000,
|
|
|
|
ease: 'Power3'
|
|
|
|
});
|
2020-10-08 15:10:46 +02:00
|
|
|
}
|
|
|
|
|
2021-04-22 02:29:13 +02:00
|
|
|
private getMiddleX() : number{
|
|
|
|
return (this.game.renderer.width / 2) -
|
|
|
|
(
|
|
|
|
this.selectCharacterSceneElement
|
|
|
|
&& this.selectCharacterSceneElement.node
|
|
|
|
&& this.selectCharacterSceneElement.node.getBoundingClientRect().width > 0
|
|
|
|
? (this.selectCharacterSceneElement.node.getBoundingClientRect().width / 4)
|
|
|
|
: 150
|
|
|
|
);
|
|
|
|
}
|
2020-05-25 23:26:27 +02:00
|
|
|
}
|