From bc138698cc8280277f6127cfad15d749563efa2f Mon Sep 17 00:00:00 2001 From: NightKev <34855794+DayKev@users.noreply.github.com> Date: Sun, 21 Jul 2024 01:55:41 -0700 Subject: [PATCH 1/6] Clean up/clarify `src/field/pokemon.ts` a bit Code provided by DerTapp on Discord --- src/field/pokemon.ts | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/field/pokemon.ts b/src/field/pokemon.ts index ecda76025693..15ba47540b3a 100644 --- a/src/field/pokemon.ts +++ b/src/field/pokemon.ts @@ -126,9 +126,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; } From bafd9c43aff2c0e0ae4e15c6c99bea718a70ccc6 Mon Sep 17 00:00:00 2001 From: NightKev <34855794+DayKev@users.noreply.github.com> Date: Sun, 21 Jul 2024 02:37:07 -0700 Subject: [PATCH 2/6] Update `PokemonSpeciesForm.getAbilityCount()` --- src/data/pokemon-species.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/data/pokemon-species.ts b/src/data/pokemon-species.ts index 2b488f330c45..bb30efb3a576 100644 --- a/src/data/pokemon-species.ts +++ b/src/data/pokemon-species.ts @@ -178,7 +178,14 @@ export abstract class PokemonSpeciesForm { } getAbilityCount(): integer { - return this.ability2 ? this.abilityHidden ? 3 : 2 : this.abilityHidden ? 2 : 1; + let count = 1; + if (this.ability2) { + count += 1; + } + if (this.abilityHidden) { + count += 1; + } + return count; } getAbility(abilityIndex: integer): Abilities { From 71f4205fc7f9b896021f435e55c240fa8648045b Mon Sep 17 00:00:00 2001 From: NightKev <34855794+DayKev@users.noreply.github.com> Date: Sun, 21 Jul 2024 03:38:15 -0700 Subject: [PATCH 3/6] Update `PokemonSpeciesForm.getAbility()` --- src/data/pokemon-species.ts | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/data/pokemon-species.ts b/src/data/pokemon-species.ts index bb30efb3a576..5e68f285cf7b 100644 --- a/src/data/pokemon-species.ts +++ b/src/data/pokemon-species.ts @@ -189,7 +189,19 @@ export abstract class PokemonSpeciesForm { } 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 { From 351252bdb1a391e62f7a154d7914ac367d32cb09 Mon Sep 17 00:00:00 2001 From: NightKev <34855794+DayKev@users.noreply.github.com> Date: Sun, 21 Jul 2024 14:43:25 -0700 Subject: [PATCH 4/6] Add explicit `Abilities.NONE` checks --- src/data/pokemon-species.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/data/pokemon-species.ts b/src/data/pokemon-species.ts index 5e68f285cf7b..be7b748061e3 100644 --- a/src/data/pokemon-species.ts +++ b/src/data/pokemon-species.ts @@ -179,10 +179,10 @@ export abstract class PokemonSpeciesForm { getAbilityCount(): integer { let count = 1; - if (this.ability2) { + if (this.ability2 !== Abilities.NONE) { count += 1; } - if (this.abilityHidden) { + if (this.abilityHidden !== Abilities.NONE) { count += 1; } return count; From 0f4a1319e6c00034ddb4f60d165bfd9feb5e779f Mon Sep 17 00:00:00 2001 From: NightKev <34855794+DayKev@users.noreply.github.com> Date: Sun, 21 Jul 2024 15:12:55 -0700 Subject: [PATCH 5/6] Add tests --- src/test/internals.test.ts | 43 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 src/test/internals.test.ts diff --git a/src/test/internals.test.ts b/src/test/internals.test.ts new file mode 100644 index 000000000000..07ae43613876 --- /dev/null +++ b/src/test/internals.test.ts @@ -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("PokemonSpeciesForm - .getAbilityCount() and .getAbility()", 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("Pokemon.abilityIndex definition", async () => { + await game.runToSummon([Species.EEVEE]); + const eevee = game.scene.getPlayerPokemon(); + + const abilityIndexInRange = ([0, 1, 2].includes(eevee.abilityIndex)); + expect(abilityIndexInRange).toBe(true); + }); +}); From bec4659e387e85ba132648d68afb361a2eb1e563 Mon Sep 17 00:00:00 2001 From: NightKev <34855794+DayKev@users.noreply.github.com> Date: Mon, 22 Jul 2024 16:44:59 -0700 Subject: [PATCH 6/6] Add jsdoc and implement test suggestions --- src/data/pokemon-species.ts | 9 +++++++++ src/test/internals.test.ts | 8 ++++---- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/data/pokemon-species.ts b/src/data/pokemon-species.ts index be7b748061e3..675372fa0af8 100644 --- a/src/data/pokemon-species.ts +++ b/src/data/pokemon-species.ts @@ -177,6 +177,10 @@ 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 { let count = 1; if (this.ability2 !== Abilities.NONE) { @@ -188,6 +192,11 @@ export abstract class PokemonSpeciesForm { 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 { let ret: Abilities; if (abilityIndex === 0) { diff --git a/src/test/internals.test.ts b/src/test/internals.test.ts index 07ae43613876..a54b8b015445 100644 --- a/src/test/internals.test.ts +++ b/src/test/internals.test.ts @@ -22,7 +22,7 @@ describe("Internals", () => { game = new GameManager(phaserGame); }); - it("PokemonSpeciesForm - .getAbilityCount() and .getAbility()", async () => { + it("should provide Eevee with 3 defined abilities", async () => { await game.runToSummon([Species.EEVEE]); const eevee = game.scene.getPlayerPokemon(); @@ -33,11 +33,11 @@ describe("Internals", () => { expect(eevee.getSpeciesForm().getAbility(2)).toBe(Abilities.ANTICIPATION); }); - it("Pokemon.abilityIndex definition", async () => { + it("should set Eeeve abilityIndex between 0-2", async () => { await game.runToSummon([Species.EEVEE]); const eevee = game.scene.getPlayerPokemon(); - const abilityIndexInRange = ([0, 1, 2].includes(eevee.abilityIndex)); - expect(abilityIndexInRange).toBe(true); + expect(eevee.abilityIndex).toBeGreaterThanOrEqual(0); + expect(eevee.abilityIndex).toBeLessThanOrEqual(2); }); });