[Refactor] Code readability update (#3085)
* Clean up/clarify `src/field/pokemon.ts` a bit Code provided by DerTapp on Discord * Update `PokemonSpeciesForm.getAbilityCount()` * Update `PokemonSpeciesForm.getAbility()` * Add explicit `Abilities.NONE` checks * Add tests * Add jsdoc and implement test suggestions
This commit is contained in:
parent
4a04ef50c3
commit
ef5e0d4c24
|
@ -177,12 +177,40 @@ export abstract class PokemonSpeciesForm {
|
||||||
return this.type1 === type || (this.type2 !== null && this.type2 === type);
|
return this.type1 === type || (this.type2 !== null && this.type2 === type);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method to get the total number of abilities a Pokemon species has.
|
||||||
|
* @returns Number of abilities
|
||||||
|
*/
|
||||||
getAbilityCount(): integer {
|
getAbilityCount(): integer {
|
||||||
return this.ability2 ? this.abilityHidden ? 3 : 2 : this.abilityHidden ? 2 : 1;
|
let count = 1;
|
||||||
|
if (this.ability2 !== Abilities.NONE) {
|
||||||
|
count += 1;
|
||||||
|
}
|
||||||
|
if (this.abilityHidden !== Abilities.NONE) {
|
||||||
|
count += 1;
|
||||||
|
}
|
||||||
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method to get the ability of a Pokemon species.
|
||||||
|
* @param abilityIndex Which ability to get (should only be 0-2)
|
||||||
|
* @returns The id of the Ability
|
||||||
|
*/
|
||||||
getAbility(abilityIndex: integer): Abilities {
|
getAbility(abilityIndex: integer): Abilities {
|
||||||
return !abilityIndex ? this.ability1 : abilityIndex === 1 && this.ability2 ? this.ability2 : this.abilityHidden;
|
let ret: Abilities;
|
||||||
|
if (abilityIndex === 0) {
|
||||||
|
ret = this.ability1;
|
||||||
|
} else if (abilityIndex === 1) {
|
||||||
|
if (this.ability2 !== Abilities.NONE) {
|
||||||
|
ret = this.ability2;
|
||||||
|
} else {
|
||||||
|
ret = this.abilityHidden;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
ret = this.abilityHidden;
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
getLevelMoves(): LevelMoves {
|
getLevelMoves(): LevelMoves {
|
||||||
|
|
|
@ -128,9 +128,19 @@ export default abstract class Pokemon extends Phaser.GameObjects.Container {
|
||||||
this.species = species;
|
this.species = species;
|
||||||
this.pokeball = dataSource?.pokeball || PokeballType.POKEBALL;
|
this.pokeball = dataSource?.pokeball || PokeballType.POKEBALL;
|
||||||
this.level = level;
|
this.level = level;
|
||||||
this.abilityIndex = abilityIndex !== undefined
|
// Determine the ability index
|
||||||
? abilityIndex
|
if (abilityIndex !== undefined) {
|
||||||
: (species.abilityHidden && hasHiddenAbility ? species.ability2 ? 2 : 1 : species.ability2 ? randAbilityIndex : 0);
|
this.abilityIndex = abilityIndex; // Use the provided ability index if it is defined
|
||||||
|
} else {
|
||||||
|
// If abilityIndex is not provided, determine it based on species and hidden ability
|
||||||
|
if (species.abilityHidden && hasHiddenAbility) {
|
||||||
|
// If the species has a hidden ability and the hidden ability is present
|
||||||
|
this.abilityIndex = species.ability2 ? 2 : 1; // Use ability index 2 if species has a second ability, otherwise use 1
|
||||||
|
} else {
|
||||||
|
// If there is no hidden ability or species does not have a hidden ability
|
||||||
|
this.abilityIndex = species.ability2 ? randAbilityIndex : 0; // Use random ability index if species has a second ability, otherwise use 0
|
||||||
|
}
|
||||||
|
}
|
||||||
if (formIndex !== undefined) {
|
if (formIndex !== undefined) {
|
||||||
this.formIndex = formIndex;
|
this.formIndex = formIndex;
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,43 @@
|
||||||
|
import { afterEach, beforeAll, beforeEach, describe, expect, it } from "vitest";
|
||||||
|
import Phaser from "phaser";
|
||||||
|
import GameManager from "#app/test/utils/gameManager";
|
||||||
|
import { Species } from "#app/enums/species.js";
|
||||||
|
import { Abilities } from "#app/enums/abilities.js";
|
||||||
|
|
||||||
|
describe("Internals", () => {
|
||||||
|
let phaserGame: Phaser.Game;
|
||||||
|
let game: GameManager;
|
||||||
|
|
||||||
|
beforeAll(() => {
|
||||||
|
phaserGame = new Phaser.Game({
|
||||||
|
type: Phaser.HEADLESS,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
game.phaseInterceptor.restoreOg();
|
||||||
|
});
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
game = new GameManager(phaserGame);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should provide Eevee with 3 defined abilities", async () => {
|
||||||
|
await game.runToSummon([Species.EEVEE]);
|
||||||
|
const eevee = game.scene.getPlayerPokemon();
|
||||||
|
|
||||||
|
expect(eevee.getSpeciesForm().getAbilityCount()).toBe(3);
|
||||||
|
|
||||||
|
expect(eevee.getSpeciesForm().getAbility(0)).toBe(Abilities.RUN_AWAY);
|
||||||
|
expect(eevee.getSpeciesForm().getAbility(1)).toBe(Abilities.ADAPTABILITY);
|
||||||
|
expect(eevee.getSpeciesForm().getAbility(2)).toBe(Abilities.ANTICIPATION);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should set Eeeve abilityIndex between 0-2", async () => {
|
||||||
|
await game.runToSummon([Species.EEVEE]);
|
||||||
|
const eevee = game.scene.getPlayerPokemon();
|
||||||
|
|
||||||
|
expect(eevee.abilityIndex).toBeGreaterThanOrEqual(0);
|
||||||
|
expect(eevee.abilityIndex).toBeLessThanOrEqual(2);
|
||||||
|
});
|
||||||
|
});
|
Loading…
Reference in New Issue