Break up initInfo into different methods

This commit is contained in:
Sirz Benjie 2025-04-23 11:14:45 -05:00
parent 2628b324f0
commit 13cead1d31
No known key found for this signature in database
GPG Key ID: 4A524B4D196C759E
2 changed files with 110 additions and 79 deletions

View File

@ -1,6 +1,6 @@
import type { default as Pokemon } from "../../field/pokemon"; import type { default as Pokemon } from "../../field/pokemon";
import { getLevelTotalExp, getLevelRelExp } from "../../data/exp"; import { getLevelRelExp } from "../../data/exp";
import { getLocalizedSpriteKey, fixedInt } from "#app/utils/common"; import { getLocalizedSpriteKey, fixedInt, getShinyDescriptor } from "#app/utils/common";
import { addTextObject, TextStyle } from "../text"; import { addTextObject, TextStyle } from "../text";
import { getGenderSymbol, getGenderColor, Gender } from "../../data/gender"; import { getGenderSymbol, getGenderColor, Gender } from "../../data/gender";
import { StatusEffect } from "#enums/status-effect"; import { StatusEffect } from "#enums/status-effect";
@ -265,103 +265,73 @@ export default abstract class BattleInfo extends Phaser.GameObjects.Container {
return this.statValuesContainer; return this.statValuesContainer;
} }
initInfo(pokemon: Pokemon) { //#region Initialization methods
this.updateNameText(pokemon);
const nameTextWidth = this.nameText.displayWidth;
this.name = pokemon.getNameToRender();
this.box.name = pokemon.getNameToRender();
this.genderText.setText(getGenderSymbol(pokemon.gender));
this.genderText.setColor(getGenderColor(pokemon.gender));
this.genderText.setPositionRelative(this.nameText, nameTextWidth, 0);
this.lastTeraType = pokemon.getTeraType();
this.teraIcon.setPositionRelative(this.nameText, nameTextWidth + this.genderText.displayWidth + 1, 2);
this.teraIcon.setVisible(pokemon.isTerastallized);
this.teraIcon.on("pointerover", () => {
if (pokemon.isTerastallized) {
globalScene.ui.showTooltip(
"",
i18next.t("fightUiHandler:teraHover", {
type: i18next.t(`pokemonInfo:Type.${PokemonType[this.lastTeraType]}`),
}),
);
}
});
this.teraIcon.on("pointerout", () => globalScene.ui.hideTooltip());
const isFusion = pokemon.isFusion(true);
initSplicedIcon(pokemon: Pokemon, baseWidth: number) {
this.splicedIcon.setPositionRelative( this.splicedIcon.setPositionRelative(
this.nameText, this.nameText,
nameTextWidth + this.genderText.displayWidth + 1 + (this.teraIcon.visible ? this.teraIcon.displayWidth + 1 : 0), baseWidth + this.genderText.displayWidth + 1 + (this.teraIcon.visible ? this.teraIcon.displayWidth + 1 : 0),
2.5, 2.5,
); );
this.splicedIcon.setVisible(isFusion); this.splicedIcon.setVisible(pokemon.isFusion(true));
if (this.splicedIcon.visible) { if (!this.splicedIcon.visible) {
this.splicedIcon.on("pointerover", () => return;
}
this.splicedIcon
.on("pointerover", () =>
globalScene.ui.showTooltip( globalScene.ui.showTooltip(
"", "",
`${pokemon.species.getName(pokemon.formIndex)}/${pokemon.fusionSpecies?.getName(pokemon.fusionFormIndex)}`, `${pokemon.species.getName(pokemon.formIndex)}/${pokemon.fusionSpecies?.getName(pokemon.fusionFormIndex)}`,
), ),
); )
this.splicedIcon.on("pointerout", () => globalScene.ui.hideTooltip()); .on("pointerout", () => globalScene.ui.hideTooltip());
} }
const doubleShiny = isFusion && pokemon.shiny && pokemon.fusionShiny; /** Called by {@linkcode initInfo} to initialize the shiny icon
* @param pokemon - The pokemon object attached to this battle info
* @param baseXOffset - The x offset to use for the shiny icon
* @param doubleShiny - Whether the pokemon is shiny and its fusion species is also shiny
*/
protected initShinyIcon(pokemon: Pokemon, xOffset: number, doubleShiny: boolean) {
const baseVariant = !doubleShiny ? pokemon.getVariant(true) : pokemon.variant; const baseVariant = !doubleShiny ? pokemon.getVariant(true) : pokemon.variant;
this.shinyIcon.setPositionRelative( this.shinyIcon.setPositionRelative(
this.nameText, this.nameText,
nameTextWidth + xOffset +
this.genderText.displayWidth + this.genderText.displayWidth +
1 + 1 +
(this.teraIcon.visible ? this.teraIcon.displayWidth + 1 : 0) + (this.teraIcon.visible ? this.teraIcon.displayWidth + 1 : 0) +
(this.splicedIcon.visible ? this.splicedIcon.displayWidth + 1 : 0), (this.splicedIcon.visible ? this.splicedIcon.displayWidth + 1 : 0),
2.5, 2.5,
); );
this.shinyIcon.setTexture(`shiny_star${doubleShiny ? "_1" : ""}`); this.shinyIcon
this.shinyIcon.setVisible(pokemon.isShiny()); .setTexture(`shiny_star${doubleShiny ? "_1" : ""}`)
this.shinyIcon.setTint(getVariantTint(baseVariant)); .setVisible(pokemon.isShiny())
if (this.shinyIcon.visible) { .setTint(getVariantTint(baseVariant));
const shinyDescriptor =
doubleShiny || baseVariant if (!this.shinyIcon.visible) {
? `${baseVariant === 2 ? i18next.t("common:epicShiny") : baseVariant === 1 ? i18next.t("common:rareShiny") : i18next.t("common:commonShiny")}${doubleShiny ? `/${pokemon.fusionVariant === 2 ? i18next.t("common:epicShiny") : pokemon.fusionVariant === 1 ? i18next.t("common:rareShiny") : i18next.t("common:commonShiny")}` : ""}` return;
: "";
this.shinyIcon.on("pointerover", () =>
globalScene.ui.showTooltip(
"",
`${i18next.t("common:shinyOnHover")}${shinyDescriptor ? ` (${shinyDescriptor})` : ""}`,
),
);
this.shinyIcon.on("pointerout", () => globalScene.ui.hideTooltip());
} }
this.fusionShinyIcon.setPosition(this.shinyIcon.x, this.shinyIcon.y); let shinyDescriptor = "";
this.fusionShinyIcon.setVisible(doubleShiny); if (doubleShiny || baseVariant) {
if (isFusion) { shinyDescriptor = " (" + getShinyDescriptor(baseVariant);
this.fusionShinyIcon.setTint(getVariantTint(pokemon.fusionVariant)); if (doubleShiny) {
shinyDescriptor += "/" + getShinyDescriptor(pokemon.fusionVariant);
}
shinyDescriptor += ")";
} }
this.hpBar.setScale(pokemon.getHpRatio(true), 1); this.shinyIcon
this.lastHpFrame = this.hpBar.scaleX > 0.5 ? "high" : this.hpBar.scaleX > 0.25 ? "medium" : "low"; .on("pointerover", () => globalScene.ui.showTooltip("", i18next.t("common:shinyOnHover") + shinyDescriptor))
this.hpBar.setFrame(this.lastHpFrame); .on("pointerout", () => globalScene.ui.hideTooltip());
if (this.player) {
this.setHpNumbers(pokemon.hp, pokemon.getMaxHp());
} }
this.lastHp = pokemon.hp;
this.lastMaxHp = pokemon.getMaxHp();
this.setLevel(pokemon.level); /** Called by {@linkcode initInfo} to initialize the type icons beside the battle info */
this.lastLevel = pokemon.level; initTypes(types: PokemonType[]) {
this.type1Icon
this.shinyIcon.setVisible(pokemon.isShiny()); .setTexture(`pbinfo_${this.player ? "player" : "enemy"}_type${types.length > 1 ? "1" : ""}`)
.setFrame(PokemonType[types[0]].toLowerCase());
const types = pokemon.getTypes(true, false, undefined, true);
this.type1Icon.setTexture(`pbinfo_${this.player ? "player" : "enemy"}_type${types.length > 1 ? "1" : ""}`);
this.type1Icon.setFrame(PokemonType[types[0]].toLowerCase());
this.type2Icon.setVisible(types.length > 1); this.type2Icon.setVisible(types.length > 1);
this.type3Icon.setVisible(types.length > 2); this.type3Icon.setVisible(types.length > 2);
if (types.length > 1) { if (types.length > 1) {
@ -370,20 +340,67 @@ export default abstract class BattleInfo extends Phaser.GameObjects.Container {
if (types.length > 2) { if (types.length > 2) {
this.type3Icon.setFrame(PokemonType[types[2]].toLowerCase()); this.type3Icon.setFrame(PokemonType[types[2]].toLowerCase());
} }
if (this.player) {
this.expMaskRect.x = (pokemon.levelExp / getLevelTotalExp(pokemon.level, pokemon.species.growthRate)) * 510;
this.lastExp = pokemon.exp;
this.lastLevelExp = pokemon.levelExp;
this.statValuesContainer.setPosition(8, 7);
} }
initInfo(pokemon: Pokemon) {
this.updateNameText(pokemon);
const nameTextWidth = this.nameText.displayWidth;
this.name = pokemon.getNameToRender();
this.box.name = pokemon.getNameToRender();
this.genderText
.setText(getGenderSymbol(pokemon.gender))
.setColor(getGenderColor(pokemon.gender))
.setPositionRelative(this.nameText, nameTextWidth, 0);
this.lastTeraType = pokemon.getTeraType();
this.teraIcon
.setVisible(pokemon.isTerastallized)
.on("pointerover", () => {
if (pokemon.isTerastallized) {
globalScene.ui.showTooltip(
"",
i18next.t("fightUiHandler:teraHover", {
type: i18next.t(`pokemonInfo:Type.${PokemonType[this.lastTeraType]}`),
}),
);
}
})
.on("pointerout", () => globalScene.ui.hideTooltip())
.setPositionRelative(this.nameText, nameTextWidth + this.genderText.displayWidth + 1, 2);
const isFusion = pokemon.isFusion(true);
this.initSplicedIcon(pokemon, nameTextWidth);
const doubleShiny = isFusion && pokemon.shiny && pokemon.fusionShiny;
this.initShinyIcon(pokemon, nameTextWidth, doubleShiny);
this.fusionShinyIcon.setVisible(doubleShiny).copyPosition(this.shinyIcon);
if (isFusion) {
this.fusionShinyIcon.setTint(getVariantTint(pokemon.fusionVariant));
}
this.hpBar.setScale(pokemon.getHpRatio(true), 1);
this.lastHpFrame = this.hpBar.scaleX > 0.5 ? "high" : this.hpBar.scaleX > 0.25 ? "medium" : "low";
this.hpBar.setFrame(this.lastHpFrame);
this.lastHp = pokemon.hp;
this.lastMaxHp = pokemon.getMaxHp();
this.setLevel(pokemon.level);
this.lastLevel = pokemon.level;
this.shinyIcon.setVisible(pokemon.isShiny());
this.initTypes(pokemon.getTypes(true));
const stats = this.statOrder.map(() => 0); const stats = this.statOrder.map(() => 0);
this.lastStats = stats.join(""); this.lastStats = stats.join("");
this.updateStats(stats); this.updateStats(stats);
} }
//#endregion
getTextureName(): string { getTextureName(): string {
return `pbinfo_${this.player ? "player" : "enemy"}${!this.player && this.boss ? "_boss" : this.mini ? "_mini" : ""}`; return `pbinfo_${this.player ? "player" : "enemy"}${!this.player && this.boss ? "_boss" : this.mini ? "_mini" : ""}`;

View File

@ -1,8 +1,12 @@
import { getLevelTotalExp } from "#app/data/exp";
import type { PlayerPokemon } from "#app/field/pokemon";
import { globalScene } from "#app/global-scene"; import { globalScene } from "#app/global-scene";
import { Stat } from "#enums/stat"; import { Stat } from "#enums/stat";
import BattleInfo from "./battle-info"; import BattleInfo from "./battle-info";
export class PlayerBattleInfo extends BattleInfo { export class PlayerBattleInfo extends BattleInfo {
protected player: true = true;
override get statOrder(): Stat[] { override get statOrder(): Stat[] {
return [Stat.ATK, Stat.DEF, Stat.SPATK, Stat.SPDEF, Stat.ACC, Stat.EVA, Stat.SPD]; return [Stat.ATK, Stat.DEF, Stat.SPATK, Stat.SPDEF, Stat.ACC, Stat.EVA, Stat.SPD];
} }
@ -29,4 +33,14 @@ export class PlayerBattleInfo extends BattleInfo {
this.expBar = expBar; this.expBar = expBar;
this.expMaskRect = expMaskRect; this.expMaskRect = expMaskRect;
} }
override initInfo(pokemon: PlayerPokemon): void {
super.initInfo(pokemon);
this.setHpNumbers(pokemon.hp, pokemon.getMaxHp());
this.expMaskRect.x = (pokemon.levelExp / getLevelTotalExp(pokemon.level, pokemon.species.growthRate)) * 510;
this.lastExp = pokemon.exp;
this.lastLevelExp = pokemon.levelExp;
this.statValuesContainer.setPosition(8, 7);
}
} }