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-08 15:10:46 +02:00
|
|
|
import {ResizableScene} from "./ResizableScene";
|
2020-10-20 13:44:57 +02:00
|
|
|
import {localUserStore} from "../../Connexion/LocalUserStore";
|
2021-01-07 17:11:22 +01:00
|
|
|
import {loadAllDefaultModels} from "../Entity/PlayerTexturesLoadingManager";
|
|
|
|
import {addLoader} from "../Components/Loader";
|
|
|
|
import {BodyResourceDescriptionInterface} from "../Entity/PlayerTextures";
|
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
|
|
|
}
|
|
|
|
|
2020-10-08 15:10:46 +02:00
|
|
|
export class SelectCharacterScene extends ResizableScene {
|
2020-05-25 23:26:27 +02:00
|
|
|
private readonly nbCharactersPerRow = 4;
|
2020-08-07 23:39:06 +02:00
|
|
|
private textField!: TextField;
|
|
|
|
private pressReturnField!: TextField;
|
|
|
|
private logo!: Image;
|
|
|
|
private customizeButton!: Image;
|
|
|
|
private customizeButtonSelected!: Image;
|
2020-05-25 23:26:27 +02:00
|
|
|
|
2020-08-07 23:39:06 +02:00
|
|
|
private selectedRectangle!: Rectangle;
|
2020-05-25 23:26:27 +02:00
|
|
|
private selectedRectangleXPos = 0; // Number of the character selected in the rows
|
|
|
|
private selectedRectangleYPos = 0; // Number of the character selected in the columns
|
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
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super({
|
|
|
|
key: SelectCharacterSceneName
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
preload() {
|
2021-01-07 17:11:22 +01:00
|
|
|
addLoader(this);
|
2020-05-25 23:26:27 +02:00
|
|
|
this.load.image(LoginTextures.playButton, "resources/objects/play_button.png");
|
|
|
|
this.load.image(LoginTextures.icon, "resources/logos/tcm_full.png");
|
|
|
|
// 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');
|
2021-01-07 17:11:22 +01:00
|
|
|
this.playerModels = loadAllDefaultModels(this.load);
|
2020-07-28 15:53:44 +02:00
|
|
|
this.load.image(LoginTextures.customizeButton, 'resources/objects/customize.png');
|
|
|
|
this.load.image(LoginTextures.customizeButtonSelected, 'resources/objects/customize_selected.png');
|
2020-05-25 23:26:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
create() {
|
|
|
|
this.textField = new TextField(this, this.game.renderer.width / 2, 50, 'Select your character');
|
|
|
|
|
2020-07-28 15:53:44 +02:00
|
|
|
this.pressReturnField = new TextField(this, this.game.renderer.width / 2, 256, 'Press enter to start');
|
2020-05-25 23:26:27 +02:00
|
|
|
|
2020-06-09 23:13:26 +02:00
|
|
|
const rectangleXStart = this.game.renderer.width / 2 - (this.nbCharactersPerRow / 2) * 32 + 16;
|
2020-05-25 23:26:27 +02:00
|
|
|
|
|
|
|
this.selectedRectangle = this.add.rectangle(rectangleXStart, 90, 32, 32).setStrokeStyle(2, 0xFFFFFF);
|
|
|
|
|
|
|
|
this.logo = new Image(this, this.game.renderer.width - 30, this.game.renderer.height - 20, LoginTextures.icon);
|
|
|
|
this.add.existing(this.logo);
|
|
|
|
|
|
|
|
this.input.keyboard.on('keyup-ENTER', () => {
|
2020-07-28 15:53:44 +02:00
|
|
|
return this.nextScene();
|
2020-05-25 23:26:27 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
this.input.keyboard.on('keydown-RIGHT', () => {
|
|
|
|
if (this.selectedRectangleXPos < this.nbCharactersPerRow - 1) {
|
|
|
|
this.selectedRectangleXPos++;
|
|
|
|
}
|
|
|
|
this.updateSelectedPlayer();
|
|
|
|
});
|
|
|
|
this.input.keyboard.on('keydown-LEFT', () => {
|
|
|
|
if (this.selectedRectangleXPos > 0) {
|
|
|
|
this.selectedRectangleXPos--;
|
|
|
|
}
|
|
|
|
this.updateSelectedPlayer();
|
|
|
|
});
|
|
|
|
this.input.keyboard.on('keydown-DOWN', () => {
|
2021-01-07 17:11:22 +01:00
|
|
|
if (this.selectedRectangleYPos < Math.ceil(this.playerModels.length / this.nbCharactersPerRow)) {
|
2020-05-25 23:26:27 +02:00
|
|
|
this.selectedRectangleYPos++;
|
|
|
|
}
|
|
|
|
this.updateSelectedPlayer();
|
|
|
|
});
|
|
|
|
this.input.keyboard.on('keydown-UP', () => {
|
|
|
|
if (this.selectedRectangleYPos > 0) {
|
|
|
|
this.selectedRectangleYPos--;
|
|
|
|
}
|
|
|
|
this.updateSelectedPlayer();
|
|
|
|
});
|
|
|
|
|
|
|
|
/*create user*/
|
|
|
|
this.createCurrentPlayer();
|
2020-10-20 13:44:57 +02:00
|
|
|
|
|
|
|
const playerNumber = localUserStore.getPlayerCharacterIndex();
|
|
|
|
if (playerNumber && playerNumber !== -1) {
|
2020-05-26 22:26:50 +02:00
|
|
|
this.selectedRectangleXPos = playerNumber % this.nbCharactersPerRow;
|
|
|
|
this.selectedRectangleYPos = Math.floor(playerNumber / this.nbCharactersPerRow);
|
|
|
|
this.updateSelectedPlayer();
|
2020-10-20 13:44:57 +02:00
|
|
|
} else if (playerNumber === -1) {
|
2021-01-07 17:11:22 +01:00
|
|
|
this.selectedRectangleYPos = Math.ceil(this.playerModels.length / this.nbCharactersPerRow);
|
2020-10-20 13:44:57 +02:00
|
|
|
this.updateSelectedPlayer();
|
2020-05-26 22:26:50 +02:00
|
|
|
}
|
2020-05-25 23:26:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
update(time: number, delta: number): void {
|
|
|
|
this.pressReturnField.setVisible(!!(Math.floor(time / 500) % 2));
|
|
|
|
}
|
|
|
|
|
2020-07-28 15:53:44 +02:00
|
|
|
private nextScene(): void {
|
2020-12-16 15:09:58 +01:00
|
|
|
this.scene.stop(SelectCharacterSceneName);
|
2020-07-28 15:53:44 +02:00
|
|
|
if (this.selectedPlayer !== null) {
|
2020-10-20 13:44:57 +02:00
|
|
|
gameManager.setCharacterLayers([this.selectedPlayer.texture.key]);
|
2020-12-15 18:00:04 +01:00
|
|
|
gameManager.tryResumingGame(this, EnableCameraSceneName);
|
2020-07-28 15:53:44 +02:00
|
|
|
} else {
|
2020-12-04 11:30:35 +01:00
|
|
|
this.scene.run(CustomizeSceneName);
|
2020-07-28 15:53:44 +02:00
|
|
|
}
|
2020-12-16 15:09:58 +01:00
|
|
|
this.scene.remove(SelectCharacterSceneName);
|
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
|
|
|
|
2020-06-09 23:13:26 +02:00
|
|
|
const col = i % this.nbCharactersPerRow;
|
|
|
|
const row = Math.floor(i / this.nbCharactersPerRow);
|
2020-05-25 23:26:27 +02:00
|
|
|
|
2020-06-09 23:13:26 +02:00
|
|
|
const [x, y] = this.getCharacterPosition(col, row);
|
|
|
|
const player = this.physics.add.sprite(x, y, playerResource.name, 0);
|
2020-05-25 23:26:27 +02:00
|
|
|
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", () => {
|
2020-05-26 17:02:04 +02:00
|
|
|
this.selectedRectangleXPos = col;
|
|
|
|
this.selectedRectangleYPos = row;
|
2020-05-26 22:26:50 +02:00
|
|
|
this.updateSelectedPlayer();
|
2020-05-25 23:26:27 +02:00
|
|
|
});
|
|
|
|
this.players.push(player);
|
|
|
|
}
|
2020-07-28 15:53:44 +02:00
|
|
|
|
|
|
|
this.customizeButton = new Image(this, this.game.renderer.width / 2, 90 + 32 * 4 + 6, LoginTextures.customizeButton);
|
|
|
|
this.customizeButton.setOrigin(0.5, 0.5);
|
|
|
|
this.add.existing(this.customizeButton);
|
|
|
|
this.customizeButtonSelected = new Image(this, this.game.renderer.width / 2, 90 + 32 * 4 + 6, LoginTextures.customizeButtonSelected);
|
|
|
|
this.customizeButtonSelected.setOrigin(0.5, 0.5);
|
|
|
|
this.customizeButtonSelected.setVisible(false);
|
|
|
|
this.add.existing(this.customizeButtonSelected);
|
|
|
|
|
|
|
|
this.customizeButton.setInteractive().on("pointerdown", () => {
|
2021-01-07 17:11:22 +01:00
|
|
|
this.selectedRectangleYPos = Math.ceil(this.playerModels.length / this.nbCharactersPerRow);
|
2020-07-28 15:53:44 +02:00
|
|
|
this.updateSelectedPlayer();
|
|
|
|
});
|
|
|
|
|
2020-05-25 23:26:27 +02:00
|
|
|
this.selectedPlayer = this.players[0];
|
2021-01-07 17:11:22 +01:00
|
|
|
this.selectedPlayer.play(this.playerModels[0].name);
|
2020-05-25 23:26:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns pixel position by on column and row number
|
|
|
|
*/
|
|
|
|
private getCharacterPosition(x: number, y: number): [number, number] {
|
|
|
|
return [
|
|
|
|
this.game.renderer.width / 2 + 16 + (x - this.nbCharactersPerRow / 2) * 32,
|
|
|
|
y * 32 + 90
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
private updateSelectedPlayer(): void {
|
2020-07-28 15:53:44 +02:00
|
|
|
this.selectedPlayer?.anims.pause();
|
|
|
|
// If we selected the customize button
|
2021-01-07 17:11:22 +01:00
|
|
|
if (this.selectedRectangleYPos === Math.ceil(this.playerModels.length / this.nbCharactersPerRow)) {
|
2020-07-28 15:53:44 +02:00
|
|
|
this.selectedPlayer = null;
|
|
|
|
this.selectedRectangle.setVisible(false);
|
|
|
|
this.customizeButtonSelected.setVisible(true);
|
|
|
|
this.customizeButton.setVisible(false);
|
2020-10-20 13:44:57 +02:00
|
|
|
localUserStore.setPlayerCharacterIndex(-1);
|
2020-07-28 15:53:44 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.customizeButtonSelected.setVisible(false);
|
|
|
|
this.customizeButton.setVisible(true);
|
2020-06-09 23:13:26 +02:00
|
|
|
const [x, y] = this.getCharacterPosition(this.selectedRectangleXPos, this.selectedRectangleYPos);
|
2020-07-28 15:53:44 +02:00
|
|
|
this.selectedRectangle.setVisible(true);
|
2020-05-25 23:26:27 +02:00
|
|
|
this.selectedRectangle.setX(x);
|
|
|
|
this.selectedRectangle.setY(y);
|
2020-07-28 15:53:44 +02:00
|
|
|
this.selectedRectangle.setSize(32, 32);
|
2020-06-09 23:13:26 +02:00
|
|
|
const playerNumber = this.selectedRectangleXPos + this.selectedRectangleYPos * this.nbCharactersPerRow;
|
|
|
|
const player = this.players[playerNumber];
|
2021-01-07 17:11:22 +01:00
|
|
|
player.play(this.playerModels[playerNumber].name);
|
2020-05-25 23:26:27 +02:00
|
|
|
this.selectedPlayer = player;
|
2020-10-20 13:44:57 +02:00
|
|
|
localUserStore.setPlayerCharacterIndex(playerNumber);
|
2020-05-25 23:26:27 +02:00
|
|
|
}
|
2020-10-08 15:10:46 +02:00
|
|
|
|
|
|
|
public onResize(ev: UIEvent): void {
|
|
|
|
this.textField.x = this.game.renderer.width / 2;
|
|
|
|
this.pressReturnField.x = this.game.renderer.width / 2;
|
|
|
|
this.logo.x = this.game.renderer.width - 30;
|
|
|
|
this.logo.y = this.game.renderer.height - 20;
|
|
|
|
this.customizeButton.x = this.game.renderer.width / 2;
|
2020-10-08 16:00:29 +02:00
|
|
|
this.customizeButtonSelected.x = this.game.renderer.width / 2;
|
2020-10-08 15:10:46 +02:00
|
|
|
|
2021-01-07 17:11:22 +01:00
|
|
|
for (let i = 0; i <this.playerModels.length; i++) {
|
2020-10-08 15:10:46 +02:00
|
|
|
const player = this.players[i];
|
|
|
|
|
|
|
|
const col = i % this.nbCharactersPerRow;
|
|
|
|
const row = Math.floor(i / this.nbCharactersPerRow);
|
|
|
|
|
|
|
|
const [x, y] = this.getCharacterPosition(col, row);
|
|
|
|
player.x = x;
|
|
|
|
player.y = y;
|
|
|
|
}
|
|
|
|
this.updateSelectedPlayer();
|
|
|
|
}
|
|
|
|
|
2020-05-25 23:26:27 +02:00
|
|
|
}
|