From 452fbbb345d3f5f962a045ce2b96892bc5106c01 Mon Sep 17 00:00:00 2001 From: wnhlee <40269597+2wheeh@users.noreply.github.com> Date: Wed, 14 Aug 2024 05:49:06 +0900 Subject: [PATCH] refactor: improve typing (#2501) --- src/data/pokemon-evolutions.ts | 7 +++++++ src/data/pokemon-species.ts | 14 +++++++------- src/field/pokemon.ts | 4 ++-- src/phases.ts | 2 +- src/ui/starter-select-ui-handler.ts | 8 ++++---- 5 files changed, 21 insertions(+), 14 deletions(-) diff --git a/src/data/pokemon-evolutions.ts b/src/data/pokemon-evolutions.ts index 745d64c0d3a..0c91263f1f3 100644 --- a/src/data/pokemon-evolutions.ts +++ b/src/data/pokemon-evolutions.ts @@ -54,6 +54,13 @@ export enum EvolutionItem { SYRUPY_APPLE } +/** + * Pokemon Evolution tuple type consisting of: + * @property 0 {@linkcode Species} The species of the Pokemon. + * @property 1 {@linkcode integer} The level at which the Pokemon evolves. + */ +export type EvolutionLevel = [species: Species, level: integer]; + export type EvolutionConditionPredicate = (p: Pokemon) => boolean; export type EvolutionConditionEnforceFunc = (p: Pokemon) => void; diff --git a/src/data/pokemon-species.ts b/src/data/pokemon-species.ts index 837cad4a0df..31de12a837b 100644 --- a/src/data/pokemon-species.ts +++ b/src/data/pokemon-species.ts @@ -3,7 +3,7 @@ import BattleScene, { AnySound } from "../battle-scene"; import { Variant, variantColorCache } from "./variant"; import { variantData } from "./variant"; import { GrowthRate } from "./exp"; -import { SpeciesWildEvolutionDelay, pokemonEvolutions, pokemonPrevolutions } from "./pokemon-evolutions"; +import { EvolutionLevel,SpeciesWildEvolutionDelay, pokemonEvolutions, pokemonPrevolutions } from "./pokemon-evolutions"; import { Type } from "./type"; import { LevelMoves, pokemonFormLevelMoves, pokemonFormLevelMoves as pokemonSpeciesFormLevelMoves, pokemonSpeciesLevelMoves } from "./pokemon-level-moves"; import { uncatchableSpecies } from "./biomes"; @@ -761,8 +761,8 @@ export default class PokemonSpecies extends PokemonSpeciesForm implements Locali return this.speciesId; } - getEvolutionLevels(): [Species, integer][] { - const evolutionLevels: [Species, integer][] = []; + getEvolutionLevels(): EvolutionLevel[] { + const evolutionLevels: EvolutionLevel[] = []; //console.log(Species[this.speciesId], pokemonEvolutions[this.speciesId]) @@ -782,8 +782,8 @@ export default class PokemonSpecies extends PokemonSpeciesForm implements Locali return evolutionLevels; } - getPrevolutionLevels(): [Species, integer][] { - const prevolutionLevels: [Species, integer][] = []; + getPrevolutionLevels(): EvolutionLevel[] { + const prevolutionLevels: EvolutionLevel[] = []; const allEvolvingPokemon = Object.keys(pokemonEvolutions); for (const p of allEvolvingPokemon) { @@ -804,8 +804,8 @@ export default class PokemonSpecies extends PokemonSpeciesForm implements Locali } // This could definitely be written better and more accurate to the getSpeciesForLevel logic, but it is only for generating movesets for evolved Pokemon - getSimulatedEvolutionChain(currentLevel: integer, forTrainer: boolean = false, isBoss: boolean = false, player: boolean = false): [Species, integer][] { - const ret: [Species, integer][] = []; + getSimulatedEvolutionChain(currentLevel: integer, forTrainer: boolean = false, isBoss: boolean = false, player: boolean = false): EvolutionLevel[] { + const ret: EvolutionLevel[] = []; if (pokemonPrevolutions.hasOwnProperty(this.speciesId)) { const prevolutionLevels = this.getPrevolutionLevels().reverse(); const levelDiff = player ? 0 : forTrainer || isBoss ? forTrainer && isBoss ? 2.5 : 5 : 10; diff --git a/src/field/pokemon.ts b/src/field/pokemon.ts index c7d653c54fd..4e6b365d093 100644 --- a/src/field/pokemon.ts +++ b/src/field/pokemon.ts @@ -1386,7 +1386,7 @@ export default abstract class Pokemon extends Phaser.GameObjects.Container { const evolutionChain = this.species.getSimulatedEvolutionChain(this.level, this.hasTrainer(), this.isBoss(), this.isPlayer()); for (let e = 0; e < evolutionChain.length; e++) { // TODO: Might need to pass specific form index in simulated evolution chain - const speciesLevelMoves = getPokemonSpeciesForm(evolutionChain[e][0] as Species, this.formIndex).getLevelMoves(); + const speciesLevelMoves = getPokemonSpeciesForm(evolutionChain[e][0], this.formIndex).getLevelMoves(); if (includeRelearnerMoves) { levelMoves.push(...speciesLevelMoves); } else { @@ -1401,7 +1401,7 @@ export default abstract class Pokemon extends Phaser.GameObjects.Container { const fusionEvolutionChain = this.fusionSpecies.getSimulatedEvolutionChain(this.level, this.hasTrainer(), this.isBoss(), this.isPlayer()); for (let e = 0; e < fusionEvolutionChain.length; e++) { // TODO: Might need to pass specific form index in simulated evolution chain - const speciesLevelMoves = getPokemonSpeciesForm(fusionEvolutionChain[e][0] as Species, this.fusionFormIndex).getLevelMoves(); + const speciesLevelMoves = getPokemonSpeciesForm(fusionEvolutionChain[e][0], this.fusionFormIndex).getLevelMoves(); if (includeRelearnerMoves) { levelMoves.push(...speciesLevelMoves.filter(lm => (includeEvolutionMoves && lm[0] === 0) || lm[0] !== 0)); } else { diff --git a/src/phases.ts b/src/phases.ts index 9edd851970e..adfcdeca64d 100644 --- a/src/phases.ts +++ b/src/phases.ts @@ -832,7 +832,7 @@ export class EncounterPhase extends BattlePhase { this.scene.unshiftPhase(new GameOverPhase(this.scene)); } - const loadEnemyAssets: Promise[] = []; + const loadEnemyAssets: Promise[] = []; const battle = this.scene.currentBattle; diff --git a/src/ui/starter-select-ui-handler.ts b/src/ui/starter-select-ui-handler.ts index e80a82be5ff..10a547f37d7 100644 --- a/src/ui/starter-select-ui-handler.ts +++ b/src/ui/starter-select-ui-handler.ts @@ -17,7 +17,7 @@ import PokemonSpecies, { allSpecies, getPokemonSpecies, getPokemonSpeciesForm, g import { Type } from "../data/type"; import { GameModes } from "../game-mode"; import { SelectChallengePhase, TitlePhase } from "../phases"; -import { AbilityAttr, DexAttr, DexAttrProps, DexEntry, StarterFormMoveData, StarterMoveset, StarterAttributes, StarterPreferences, StarterPrefs } from "../system/game-data"; +import { AbilityAttr, DexAttr, DexAttrProps, DexEntry, StarterMoveset, StarterAttributes, StarterPreferences, StarterPrefs } from "../system/game-data"; import { Tutorial, handleTutorial } from "../tutorial"; import * as Utils from "../utils"; import { OptionSelectItem } from "./abstact-option-select-ui-handler"; @@ -3026,8 +3026,8 @@ export default class StarterSelectUiHandler extends MessageUiHandler { const speciesMoveData = this.scene.gameData.starterData[species.speciesId].moveset; const moveData: StarterMoveset | null = speciesMoveData ? Array.isArray(speciesMoveData) - ? speciesMoveData as StarterMoveset - : (speciesMoveData as StarterFormMoveData)[formIndex!] // TODO: is this bang correct? + ? speciesMoveData + : speciesMoveData[formIndex!] // TODO: is this bang correct? : null; const availableStarterMoves = this.speciesStarterMoves.concat(speciesEggMoves.hasOwnProperty(species.speciesId) ? speciesEggMoves[species.speciesId].filter((_, em: integer) => this.scene.gameData.starterData[species.speciesId].eggMoves & (1 << em)) : []); this.starterMoveset = (moveData || (this.speciesStarterMoves.slice(0, 4) as StarterMoveset)).filter(m => availableStarterMoves.find(sm => sm === m)) as StarterMoveset; @@ -3464,7 +3464,7 @@ export default class StarterSelectUiHandler extends MessageUiHandler { } } - checkIconId(icon: Phaser.GameObjects.Sprite, species: PokemonSpecies, female, formIndex, shiny, variant) { + checkIconId(icon: Phaser.GameObjects.Sprite, species: PokemonSpecies, female: boolean, formIndex: number, shiny: boolean, variant: number) { if (icon.frame.name !== species.getIconId(female, formIndex, shiny, variant)) { console.log(`${species.name}'s variant icon does not exist. Replacing with default.`); icon.setTexture(species.getIconAtlasKey(formIndex, false, variant));