2024-02-22 23:03:36 +00:00
|
|
|
import BattleScene from "../battle-scene";
|
2024-02-25 02:16:19 +00:00
|
|
|
import * as Utils from "../utils";
|
2024-02-22 23:03:36 +00:00
|
|
|
|
|
|
|
export default class CharSprite extends Phaser.GameObjects.Container {
|
|
|
|
private sprite: Phaser.GameObjects.Sprite;
|
|
|
|
private transitionSprite: Phaser.GameObjects.Sprite;
|
|
|
|
|
|
|
|
public key: string;
|
|
|
|
public variant: string;
|
|
|
|
public shown: boolean;
|
|
|
|
|
|
|
|
constructor(scene: BattleScene) {
|
|
|
|
super(scene, (scene.game.canvas.width / 6) + 32, -42);
|
|
|
|
}
|
|
|
|
|
|
|
|
setup(): void {
|
|
|
|
[ this.sprite, this.transitionSprite ] = new Array(2).fill(null).map(() => {
|
2024-05-23 16:03:10 +01:00
|
|
|
const ret = this.scene.add.sprite(0, 0, "", "");
|
2024-02-22 23:03:36 +00:00
|
|
|
ret.setOrigin(0.5, 1);
|
|
|
|
this.add(ret);
|
|
|
|
return ret;
|
|
|
|
});
|
|
|
|
|
|
|
|
this.transitionSprite.setVisible(false);
|
|
|
|
|
|
|
|
this.setVisible(false);
|
|
|
|
this.shown = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
showCharacter(key: string, variant: string): Promise<void> {
|
|
|
|
return new Promise(resolve => {
|
2024-05-23 16:03:10 +01:00
|
|
|
if (!key.startsWith("c_")) {
|
2024-02-22 23:03:36 +00:00
|
|
|
key = `c_${key}`;
|
2024-05-23 16:03:10 +01:00
|
|
|
}
|
2024-02-22 23:03:36 +00:00
|
|
|
if (this.shown) {
|
2024-05-23 16:03:10 +01:00
|
|
|
if (key === this.key && variant === this.variant) {
|
2024-02-22 23:03:36 +00:00
|
|
|
return resolve();
|
2024-05-23 16:03:10 +01:00
|
|
|
}
|
|
|
|
if (key !== this.key) {
|
2024-02-22 23:03:36 +00:00
|
|
|
return this.hide().then(() => this.showCharacter(key, variant));
|
2024-05-23 16:03:10 +01:00
|
|
|
}
|
2024-02-22 23:03:36 +00:00
|
|
|
this.setVariant(variant).then(() => resolve());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.sprite.setTexture(key, variant);
|
|
|
|
|
|
|
|
(this.scene as BattleScene).fieldUI.bringToTop(this);
|
|
|
|
|
|
|
|
this.scene.tweens.add({
|
|
|
|
targets: this,
|
|
|
|
x: (this.scene.game.canvas.width / 6) - 102,
|
|
|
|
duration: 750,
|
2024-05-23 16:03:10 +01:00
|
|
|
ease: "Cubic.easeOut",
|
2024-02-22 23:03:36 +00:00
|
|
|
onComplete: () => {
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-02-25 02:16:19 +00:00
|
|
|
this.setVisible(this.scene.textures.get(key).key !== Utils.MissingTextureKey);
|
2024-02-22 23:03:36 +00:00
|
|
|
this.shown = true;
|
|
|
|
|
|
|
|
this.key = key;
|
|
|
|
this.variant = variant;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
setVariant(variant: string): Promise<void> {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
(this.scene as BattleScene).fieldUI.bringToTop(this);
|
|
|
|
|
|
|
|
this.transitionSprite.setTexture(this.key, variant);
|
|
|
|
this.transitionSprite.setAlpha(0);
|
|
|
|
this.transitionSprite.setVisible(true);
|
|
|
|
this.scene.tweens.add({
|
|
|
|
targets: this.transitionSprite,
|
|
|
|
alpha: 1,
|
|
|
|
duration: 250,
|
2024-05-23 16:03:10 +01:00
|
|
|
ease: "Sine.easeIn",
|
2024-02-22 23:03:36 +00:00
|
|
|
onComplete: () => {
|
|
|
|
this.sprite.setTexture(this.key, variant);
|
|
|
|
this.transitionSprite.setVisible(false);
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
this.variant = variant;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
hide(): Promise<void> {
|
|
|
|
return new Promise(resolve => {
|
2024-05-23 16:03:10 +01:00
|
|
|
if (!this.shown) {
|
2024-02-22 23:03:36 +00:00
|
|
|
return resolve();
|
2024-05-23 16:03:10 +01:00
|
|
|
}
|
2024-02-22 23:03:36 +00:00
|
|
|
|
|
|
|
this.scene.tweens.add({
|
|
|
|
targets: this,
|
|
|
|
x: (this.scene.game.canvas.width / 6) + 32,
|
|
|
|
duration: 750,
|
2024-05-23 16:03:10 +01:00
|
|
|
ease: "Cubic.easeIn",
|
2024-02-22 23:03:36 +00:00
|
|
|
onComplete: () => {
|
2024-05-23 16:03:10 +01:00
|
|
|
if (!this.shown) {
|
2024-02-22 23:03:36 +00:00
|
|
|
this.setVisible(false);
|
2024-05-23 16:03:10 +01:00
|
|
|
}
|
2024-02-22 23:03:36 +00:00
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
this.shown = false;
|
|
|
|
});
|
2024-05-23 16:03:10 +01:00
|
|
|
}
|
|
|
|
}
|