2021-11-24 15:21:07 +01:00
|
|
|
import { Loader } from "../Components/Loader";
|
2021-08-16 11:43:29 +02:00
|
|
|
import { gameManager } from "../Game/GameManager";
|
2021-04-02 23:00:51 +02:00
|
|
|
import { ResizableScene } from "./ResizableScene";
|
2021-04-02 23:13:03 +02:00
|
|
|
import { EnableCameraSceneName } from "./EnableCameraScene";
|
|
|
|
import { localUserStore } from "../../Connexion/LocalUserStore";
|
2021-05-12 09:13:25 +02:00
|
|
|
import type { CompanionResourceDescriptionInterface } from "../Companion/CompanionTextures";
|
2021-04-06 18:54:45 +02:00
|
|
|
import { getAllCompanionResources } from "../Companion/CompanionTexturesLoadingManager";
|
2021-08-16 11:43:29 +02:00
|
|
|
import { touchScreenManager } from "../../Touch/TouchScreenManager";
|
|
|
|
import { PinchManager } from "../UserInput/PinchManager";
|
|
|
|
import { selectCompanionSceneVisibleStore } from "../../Stores/SelectCompanionStore";
|
|
|
|
import { waScaleManager } from "../Services/WaScaleManager";
|
|
|
|
import { isMobile } from "../../Enum/EnvironmentVariable";
|
2021-04-02 23:00:51 +02:00
|
|
|
|
|
|
|
export const SelectCompanionSceneName = "SelectCompanionScene";
|
|
|
|
|
2021-04-22 13:10:23 +02:00
|
|
|
export class SelectCompanionScene extends ResizableScene {
|
2021-04-02 23:00:51 +02:00
|
|
|
private selectedCompanion!: Phaser.Physics.Arcade.Sprite;
|
|
|
|
private companions: Array<Phaser.Physics.Arcade.Sprite> = new Array<Phaser.Physics.Arcade.Sprite>();
|
2021-04-22 13:10:23 +02:00
|
|
|
private companionModels: Array<CompanionResourceDescriptionInterface> = [];
|
2021-06-02 16:08:31 +02:00
|
|
|
private saveZoom: number = 0;
|
2021-04-02 23:00:51 +02:00
|
|
|
|
2021-04-22 13:10:23 +02:00
|
|
|
private currentCompanion = 0;
|
2021-06-03 17:08:56 +02:00
|
|
|
private pointerClicked: boolean = false;
|
2021-06-04 09:39:33 +02:00
|
|
|
private pointerTimer: number = 0;
|
2021-11-24 15:21:07 +01:00
|
|
|
private loader: Loader;
|
2021-04-13 00:44:51 +02:00
|
|
|
|
2021-04-02 23:00:51 +02:00
|
|
|
constructor() {
|
|
|
|
super({
|
2021-08-16 11:43:29 +02:00
|
|
|
key: SelectCompanionSceneName,
|
2021-04-02 23:00:51 +02:00
|
|
|
});
|
2021-11-24 15:21:07 +01:00
|
|
|
this.loader = new Loader(this);
|
2021-04-02 23:00:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
preload() {
|
2021-08-16 11:43:29 +02:00
|
|
|
getAllCompanionResources(this.load).forEach((model) => {
|
2021-04-02 23:13:03 +02:00
|
|
|
this.companionModels.push(model);
|
2021-04-02 23:00:51 +02:00
|
|
|
});
|
2021-05-04 15:15:23 +02:00
|
|
|
|
2021-05-04 15:47:45 +02:00
|
|
|
//this function must stay at the end of preload function
|
2021-11-24 15:21:07 +01:00
|
|
|
this.loader.addLoader();
|
2021-04-02 23:00:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
create() {
|
2021-05-31 12:06:11 +02:00
|
|
|
selectCompanionSceneVisibleStore.set(true);
|
2021-04-22 13:10:23 +02:00
|
|
|
|
2021-06-02 16:08:31 +02:00
|
|
|
waScaleManager.saveZoom();
|
|
|
|
waScaleManager.zoomModifier = isMobile() ? 2 : 1;
|
|
|
|
|
2021-04-14 17:47:26 +02:00
|
|
|
if (touchScreenManager.supportTouchScreen) {
|
|
|
|
new PinchManager(this);
|
|
|
|
}
|
2021-04-02 23:00:51 +02:00
|
|
|
|
|
|
|
// input events
|
2021-08-16 11:43:29 +02:00
|
|
|
this.input.keyboard.on("keyup-ENTER", this.selectCompanion.bind(this));
|
2021-04-02 23:00:51 +02:00
|
|
|
|
2021-08-16 11:43:29 +02:00
|
|
|
this.input.keyboard.on("keydown-RIGHT", this.moveToRight.bind(this));
|
|
|
|
this.input.keyboard.on("keydown-LEFT", this.moveToLeft.bind(this));
|
2021-04-02 23:00:51 +02:00
|
|
|
|
2021-08-16 11:43:29 +02:00
|
|
|
if (localUserStore.getCompanion()) {
|
|
|
|
const companionIndex = this.companionModels.findIndex(
|
|
|
|
(companion) => companion.name === localUserStore.getCompanion()
|
|
|
|
);
|
|
|
|
if (companionIndex > -1 || companionIndex < this.companions.length) {
|
2021-04-22 13:10:23 +02:00
|
|
|
this.currentCompanion = companionIndex;
|
|
|
|
this.selectedCompanion = this.companions[companionIndex];
|
|
|
|
}
|
2021-04-06 20:12:10 +02:00
|
|
|
}
|
|
|
|
|
2021-04-22 13:10:23 +02:00
|
|
|
localUserStore.setCompanion(null);
|
|
|
|
gameManager.setCompanion(null);
|
2021-04-06 20:12:10 +02:00
|
|
|
|
2021-04-22 13:10:23 +02:00
|
|
|
this.createCurrentCompanion();
|
|
|
|
this.updateSelectedCompanion();
|
2021-04-02 23:00:51 +02:00
|
|
|
}
|
|
|
|
|
2021-04-22 13:10:23 +02:00
|
|
|
update(time: number, delta: number): void {
|
2021-06-07 09:15:34 +02:00
|
|
|
// pointerTimer is set to 250 when pointerdown events is trigger
|
|
|
|
// After 250ms, pointerClicked is set to false and the pointerdown events can be trigger again
|
2021-06-04 09:39:33 +02:00
|
|
|
this.pointerTimer -= delta;
|
|
|
|
if (this.pointerTimer <= 0) {
|
|
|
|
this.pointerClicked = false;
|
|
|
|
}
|
2021-04-06 20:31:08 +02:00
|
|
|
}
|
|
|
|
|
2021-05-31 12:06:11 +02:00
|
|
|
public selectCompanion(): void {
|
2021-04-22 13:10:23 +02:00
|
|
|
localUserStore.setCompanion(this.companionModels[this.currentCompanion].name);
|
|
|
|
gameManager.setCompanion(this.companionModels[this.currentCompanion].name);
|
2021-04-02 23:00:51 +02:00
|
|
|
|
2021-05-31 12:06:11 +02:00
|
|
|
this.closeScene();
|
2021-04-06 20:12:10 +02:00
|
|
|
}
|
|
|
|
|
2021-08-16 11:43:29 +02:00
|
|
|
public closeScene() {
|
2021-04-02 23:00:51 +02:00
|
|
|
// next scene
|
|
|
|
this.scene.stop(SelectCompanionSceneName);
|
2021-06-02 16:08:31 +02:00
|
|
|
waScaleManager.restoreZoom();
|
2021-08-16 11:43:29 +02:00
|
|
|
gameManager.tryResumingGame(EnableCameraSceneName);
|
2021-04-02 23:00:51 +02:00
|
|
|
this.scene.remove(SelectCompanionSceneName);
|
2021-05-31 12:06:11 +02:00
|
|
|
selectCompanionSceneVisibleStore.set(false);
|
2021-04-02 23:00:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private createCurrentCompanion(): void {
|
|
|
|
for (let i = 0; i < this.companionModels.length; i++) {
|
2021-08-16 11:43:29 +02:00
|
|
|
const companionResource = this.companionModels[i];
|
2021-04-22 13:10:23 +02:00
|
|
|
const [middleX, middleY] = this.getCompanionPosition();
|
|
|
|
const companion = this.physics.add.sprite(middleX, middleY, companionResource.name, 0);
|
|
|
|
this.setUpCompanion(companion, i);
|
|
|
|
this.anims.create({
|
|
|
|
key: companionResource.name,
|
2021-08-16 11:43:29 +02:00
|
|
|
frames: this.anims.generateFrameNumbers(companionResource.name, { start: 0, end: 2 }),
|
2021-04-22 13:10:23 +02:00
|
|
|
frameRate: 10,
|
2021-08-16 11:43:29 +02:00
|
|
|
repeat: -1,
|
2021-04-22 13:10:23 +02:00
|
|
|
});
|
2021-04-02 23:00:51 +02:00
|
|
|
|
|
|
|
companion.setInteractive().on("pointerdown", () => {
|
2021-06-03 17:08:56 +02:00
|
|
|
if (this.pointerClicked) {
|
|
|
|
return;
|
|
|
|
}
|
2021-06-07 09:15:34 +02:00
|
|
|
//To not trigger two time the pointerdown events :
|
|
|
|
// We set a boolean to true so that pointerdown events does nothing when the boolean is true
|
|
|
|
// We set a timer that we decrease in update function to not trigger the pointerdown events twice
|
2021-06-03 17:08:56 +02:00
|
|
|
this.pointerClicked = true;
|
2021-06-04 09:39:33 +02:00
|
|
|
this.pointerTimer = 250;
|
2021-04-22 13:10:23 +02:00
|
|
|
this.currentCompanion = i;
|
2021-04-27 23:58:33 +02:00
|
|
|
this.moveCompanion();
|
2021-04-02 23:00:51 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
this.companions.push(companion);
|
|
|
|
}
|
2021-04-22 13:10:23 +02:00
|
|
|
this.selectedCompanion = this.companions[this.currentCompanion];
|
2021-04-02 23:00:51 +02:00
|
|
|
}
|
|
|
|
|
2021-05-31 10:20:30 +02:00
|
|
|
public onResize(): void {
|
2021-04-22 13:10:23 +02:00
|
|
|
this.moveCompanion();
|
2021-04-02 23:00:51 +02:00
|
|
|
}
|
|
|
|
|
2021-04-22 13:10:23 +02:00
|
|
|
private updateSelectedCompanion(): void {
|
|
|
|
this.selectedCompanion?.anims.pause();
|
|
|
|
const companion = this.companions[this.currentCompanion];
|
|
|
|
companion.play(this.companionModels[this.currentCompanion].name);
|
|
|
|
this.selectedCompanion = companion;
|
|
|
|
}
|
2021-04-02 23:00:51 +02:00
|
|
|
|
2021-08-16 11:43:29 +02:00
|
|
|
private moveCompanion() {
|
|
|
|
for (let i = 0; i < this.companions.length; i++) {
|
2021-04-02 23:00:51 +02:00
|
|
|
const companion = this.companions[i];
|
2021-04-22 13:10:23 +02:00
|
|
|
this.setUpCompanion(companion, i);
|
2021-04-02 23:00:51 +02:00
|
|
|
}
|
2021-04-22 13:10:23 +02:00
|
|
|
this.updateSelectedCompanion();
|
|
|
|
}
|
2021-04-02 23:00:51 +02:00
|
|
|
|
2021-08-16 11:43:29 +02:00
|
|
|
public moveToRight() {
|
|
|
|
if (this.currentCompanion === this.companions.length - 1) {
|
2021-04-22 13:10:23 +02:00
|
|
|
return;
|
|
|
|
}
|
2021-05-31 12:06:11 +02:00
|
|
|
this.currentCompanion += 1;
|
2021-04-22 13:10:23 +02:00
|
|
|
this.moveCompanion();
|
2021-04-02 23:00:51 +02:00
|
|
|
}
|
|
|
|
|
2021-08-16 11:43:29 +02:00
|
|
|
public moveToLeft() {
|
|
|
|
if (this.currentCompanion === 0) {
|
2021-04-22 13:10:23 +02:00
|
|
|
return;
|
|
|
|
}
|
2021-05-31 12:06:11 +02:00
|
|
|
this.currentCompanion -= 1;
|
2021-04-22 13:10:23 +02:00
|
|
|
this.moveCompanion();
|
|
|
|
}
|
2021-04-02 23:00:51 +02:00
|
|
|
|
2021-08-16 11:43:29 +02:00
|
|
|
private defineSetupCompanion(num: number) {
|
2021-04-22 13:10:23 +02:00
|
|
|
const deltaX = 30;
|
|
|
|
const deltaY = 2;
|
|
|
|
let [companionX, companionY] = this.getCompanionPosition();
|
|
|
|
let companionVisible = true;
|
|
|
|
let companionScale = 1.5;
|
|
|
|
let companionOpactity = 1;
|
2021-08-16 11:43:29 +02:00
|
|
|
if (this.currentCompanion !== num) {
|
2021-04-22 13:10:23 +02:00
|
|
|
companionVisible = false;
|
|
|
|
}
|
2021-08-16 11:43:29 +02:00
|
|
|
if (num === this.currentCompanion + 1) {
|
2021-04-22 13:10:23 +02:00
|
|
|
companionY -= deltaY;
|
|
|
|
companionX += deltaX;
|
|
|
|
companionScale = 0.8;
|
|
|
|
companionOpactity = 0.6;
|
|
|
|
companionVisible = true;
|
|
|
|
}
|
2021-08-16 11:43:29 +02:00
|
|
|
if (num === this.currentCompanion + 2) {
|
2021-04-22 13:10:23 +02:00
|
|
|
companionY -= deltaY;
|
2021-08-16 11:43:29 +02:00
|
|
|
companionX += deltaX * 2;
|
2021-04-22 13:10:23 +02:00
|
|
|
companionScale = 0.8;
|
|
|
|
companionOpactity = 0.6;
|
|
|
|
companionVisible = true;
|
|
|
|
}
|
2021-08-16 11:43:29 +02:00
|
|
|
if (num === this.currentCompanion - 1) {
|
2021-04-22 13:10:23 +02:00
|
|
|
companionY -= deltaY;
|
|
|
|
companionX -= deltaX;
|
|
|
|
companionScale = 0.8;
|
|
|
|
companionOpactity = 0.6;
|
|
|
|
companionVisible = true;
|
2021-04-02 23:00:51 +02:00
|
|
|
}
|
2021-08-16 11:43:29 +02:00
|
|
|
if (num === this.currentCompanion - 2) {
|
2021-04-22 13:10:23 +02:00
|
|
|
companionY -= deltaY;
|
2021-08-16 11:43:29 +02:00
|
|
|
companionX -= deltaX * 2;
|
2021-04-22 13:10:23 +02:00
|
|
|
companionScale = 0.8;
|
|
|
|
companionOpactity = 0.6;
|
|
|
|
companionVisible = true;
|
|
|
|
}
|
2021-08-16 11:43:29 +02:00
|
|
|
return { companionX, companionY, companionScale, companionOpactity, companionVisible };
|
2021-04-22 13:10:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns pixel position by on column and row number
|
|
|
|
*/
|
|
|
|
private getCompanionPosition(): [number, number] {
|
2021-08-16 11:43:29 +02:00
|
|
|
return [this.game.renderer.width / 2, this.game.renderer.height / 3];
|
2021-04-22 13:10:23 +02:00
|
|
|
}
|
2021-04-02 23:00:51 +02:00
|
|
|
|
2021-08-16 11:43:29 +02:00
|
|
|
private setUpCompanion(companion: Phaser.Physics.Arcade.Sprite, numero: number) {
|
|
|
|
const { companionX, companionY, companionScale, companionOpactity, companionVisible } =
|
|
|
|
this.defineSetupCompanion(numero);
|
2021-04-22 13:10:23 +02:00
|
|
|
companion.setBounce(0.2);
|
|
|
|
companion.setCollideWorldBounds(true);
|
2021-08-16 11:43:29 +02:00
|
|
|
companion.setVisible(companionVisible);
|
2021-04-22 13:10:23 +02:00
|
|
|
companion.setScale(companionScale, companionScale);
|
|
|
|
companion.setAlpha(companionOpactity);
|
|
|
|
companion.setX(companionX);
|
|
|
|
companion.setY(companionY);
|
|
|
|
}
|
2021-04-02 23:00:51 +02:00
|
|
|
}
|