Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Refactor] Code readability update #3085

Merged
merged 6 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
DayKev marked this conversation as resolved.
Show resolved Hide resolved
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 {
DayKev marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -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;
}
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);
});
});
Loading