Skip to content

Commit

Permalink
refactor: improve typing (#2501)
Browse files Browse the repository at this point in the history
  • Loading branch information
2wheeh authored Aug 13, 2024
1 parent 0d3fcd8 commit 452fbbb
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 14 deletions.
7 changes: 7 additions & 0 deletions src/data/pokemon-evolutions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
14 changes: 7 additions & 7 deletions src/data/pokemon-species.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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])

Expand All @@ -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) {
Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/field/pokemon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/phases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -832,7 +832,7 @@ export class EncounterPhase extends BattlePhase {
this.scene.unshiftPhase(new GameOverPhase(this.scene));
}

const loadEnemyAssets: Promise<any>[] = [];
const loadEnemyAssets: Promise<void>[] = [];

const battle = this.scene.currentBattle;

Expand Down
8 changes: 4 additions & 4 deletions src/ui/starter-select-ui-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand Down

0 comments on commit 452fbbb

Please sign in to comment.