Skip to content

Commit

Permalink
[Refactor] Code readability update (#3085)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
DayKev authored Jul 23, 2024
1 parent 4a04ef5 commit ef5e0d4
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 5 deletions.
32 changes: 30 additions & 2 deletions src/data/pokemon-species.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,40 @@ export abstract class PokemonSpeciesForm {
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 {
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 {
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 {
Expand Down
16 changes: 13 additions & 3 deletions src/field/pokemon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,19 @@ export default abstract class Pokemon extends Phaser.GameObjects.Container {
this.species = species;
this.pokeball = dataSource?.pokeball || PokeballType.POKEBALL;
this.level = level;
this.abilityIndex = abilityIndex !== undefined
? abilityIndex
: (species.abilityHidden && hasHiddenAbility ? species.ability2 ? 2 : 1 : species.ability2 ? randAbilityIndex : 0);
// Determine the ability index
if (abilityIndex !== undefined) {
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) {
this.formIndex = formIndex;
}
Expand Down
43 changes: 43 additions & 0 deletions src/test/internals.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});

0 comments on commit ef5e0d4

Please sign in to comment.