diff --git a/src/constants.ts b/src/constants.ts new file mode 100644 index 00000000000..a2f7e47b996 --- /dev/null +++ b/src/constants.ts @@ -0,0 +1 @@ +export const PLAYER_PARTY_MAX_SIZE = 6; diff --git a/src/field/pokemon.ts b/src/field/pokemon.ts index 6d29e30254a..e2f238e29af 100644 --- a/src/field/pokemon.ts +++ b/src/field/pokemon.ts @@ -58,6 +58,7 @@ import { StatStageChangePhase } from "#app/phases/stat-stage-change-phase"; import { SwitchSummonPhase } from "#app/phases/switch-summon-phase"; import { ToggleDoublePositionPhase } from "#app/phases/toggle-double-position-phase"; import { Challenges } from "#enums/challenges"; +import { PLAYER_PARTY_MAX_SIZE } from "#app/constants"; export enum FieldPosition { CENTER, @@ -4465,17 +4466,29 @@ export class EnemyPokemon extends Pokemon { return BattlerIndex.ENEMY + this.getFieldIndex(); } - addToParty(pokeballType: PokeballType) { + /** + * Add a new pokemon to the player's party (at `slotIndex` if set). + * @param pokeballType the type of pokeball the pokemon was caught with + * @param slotIndex an optional index to place the pokemon in the party + * @returns the pokemon that was added or null if the pokemon could not be added + */ + addToParty(pokeballType: PokeballType, slotIndex: number = -1) { const party = this.scene.getParty(); let ret: PlayerPokemon | null = null; - if (party.length < 6) { + if (party.length < PLAYER_PARTY_MAX_SIZE) { this.pokeball = pokeballType; this.metLevel = this.level; this.metBiome = this.scene.arena.biomeType; this.metSpecies = this.species.speciesId; const newPokemon = this.scene.addPlayerPokemon(this.species, this.level, this.abilityIndex, this.formIndex, this.gender, this.shiny, this.variant, this.ivs, this.nature, this); - party.push(newPokemon); + + if (Utils.isBetween(slotIndex, 0, PLAYER_PARTY_MAX_SIZE - 1)) { + party.splice(slotIndex, 0, newPokemon); + } else { + party.push(newPokemon); + } + ret = newPokemon; this.scene.triggerPokemonFormChange(newPokemon, SpeciesFormChangeActiveTrigger, true); } diff --git a/src/phases/attempt-capture-phase.ts b/src/phases/attempt-capture-phase.ts index 55a82affaf6..cf9ce997bfd 100644 --- a/src/phases/attempt-capture-phase.ts +++ b/src/phases/attempt-capture-phase.ts @@ -221,8 +221,8 @@ export class AttemptCapturePhase extends PokemonPhase { this.scene.clearEnemyHeldItemModifiers(); this.scene.field.remove(pokemon, true); }; - const addToParty = () => { - const newPokemon = pokemon.addToParty(this.pokeballType); + const addToParty = (slotIndex?: number) => { + const newPokemon = pokemon.addToParty(this.pokeballType, slotIndex); const modifiers = this.scene.findModifiers(m => m instanceof PokemonHeldItemModifier, false); if (this.scene.getParty().filter(p => p.isShiny()).length === 6) { this.scene.validateAchv(achvs.SHINY_PARTY); @@ -253,7 +253,7 @@ export class AttemptCapturePhase extends PokemonPhase { this.scene.ui.setMode(Mode.PARTY, PartyUiMode.RELEASE, this.fieldIndex, (slotIndex: integer, _option: PartyOption) => { this.scene.ui.setMode(Mode.MESSAGE).then(() => { if (slotIndex < 6) { - addToParty(); + addToParty(slotIndex); } else { promptRelease(); } diff --git a/src/test/field/pokemon.test.ts b/src/test/field/pokemon.test.ts index d597cd5219c..f7c1cf8bc3d 100644 --- a/src/test/field/pokemon.test.ts +++ b/src/test/field/pokemon.test.ts @@ -1,6 +1,8 @@ import { Species } from "#app/enums/species"; import { afterEach, beforeAll, beforeEach, describe, expect, it } from "vitest"; import GameManager from "../utils/gameManager"; +import { PokeballType } from "#app/enums/pokeball"; +import BattleScene from "#app/battle-scene"; describe("Spec - Pokemon", () => { let phaserGame: Phaser.Game; @@ -28,4 +30,37 @@ describe("Spec - Pokemon", () => { expect(pkm.trySetStatus(undefined)).toBe(true); }); + + describe("Add To Party", () => { + let scene: BattleScene; + + beforeEach(async () => { + game.override.enemySpecies(Species.ZUBAT); + await game.classicMode.runToSummon([Species.ABRA, Species.ABRA, Species.ABRA, Species.ABRA, Species.ABRA]); // 5 Abra, only 1 slot left + scene = game.scene; + }); + + it("should append a new pokemon by default", async () => { + const zubat = scene.getEnemyPokemon()!; + zubat.addToParty(PokeballType.LUXURY_BALL); + + const party = scene.getParty(); + expect(party).toHaveLength(6); + party.forEach((pkm, index) =>{ + expect(pkm.species.speciesId).toBe(index === 5 ? Species.ZUBAT : Species.ABRA); + }); + }); + + it("should put a new pokemon into the passed slotIndex", async () => { + const slotIndex = 1; + const zubat = scene.getEnemyPokemon()!; + zubat.addToParty(PokeballType.LUXURY_BALL, slotIndex); + + const party = scene.getParty(); + expect(party).toHaveLength(6); + party.forEach((pkm, index) =>{ + expect(pkm.species.speciesId).toBe(index === slotIndex ? Species.ZUBAT : Species.ABRA); + }); + }); + }); }); diff --git a/src/utils.ts b/src/utils.ts index 592981c7643..7decf9bb4c0 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -609,3 +609,14 @@ export function toDmgValue(value: number, minValue: number = 1) { export function getLocalizedSpriteKey(baseKey: string) { return `${baseKey}${verifyLang(i18next.resolvedLanguage) ? `_${i18next.resolvedLanguage}` : ""}`; } + +/** + * Check if a number is **inclusive** between two numbers + * @param num the number to check + * @param min the minimum value (included) + * @param max the maximum value (included) + * @returns true if number is **inclusive** between min and max + */ +export function isBetween(num: number, min: number, max: number): boolean { + return num >= min && num <= max; +}