Skip to content

Commit

Permalink
Implement Power & Guard Swap with Unit Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
xsn34kzx committed Aug 21, 2024
1 parent 27a971e commit e97e965
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 23 deletions.
64 changes: 42 additions & 22 deletions src/data/move.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2795,33 +2795,53 @@ export class ResetStatsAttr extends MoveEffectAttr {
}

/**
* Attribute used for moves which swap the user and the target's stat stages.
* Attribute used for status moves, specifically Heart, Guard, and Power Swap,
* that swaps the user's and target's corresponding stat stages.
* @extends MoveEffectAttr
* @see {@linkcode apply}
*/
export class SwapStatStagesAttr extends MoveEffectAttr {
/** The stat stages to be swapped between the user and the target */
private stats: readonly BattleStat[];

constructor(stats: readonly BattleStat[]) {
super();

this.stats = stats;
}

/**
* Swaps the user and the target's stat changes.
* @param user Pokemon that used the move
* @param target The target of the move
* @param move Move with this attribute
* For all {@linkcode stats}, swaps the user's and target's corresponding stat
* stage.
* @param user the {@linkcode Pokemon} that used the move
* @param target the {@linkcode Pokemon} that the move was used on
* @param move N/A
* @param args N/A
* @returns true if the function succeeds
* @returns true if attribute application succeeds
*/
apply(user: Pokemon, target: Pokemon, move: Move, args: any []): boolean {
if (!super.apply(user, target, move, args)) {
return false;
} //Exits if the move can't apply
if (super.apply(user, target, move, args)) {
for (const s of BATTLE_STATS) {
const temp = user.getStatStage(s);
user.setStatStage(s, target.getStatStage(s));
target.setStatStage(s, temp);
}

let temp: number;
for (const s of BATTLE_STATS) {
temp = user.getStatStage(s);
user.setStatStage(s, target.getStatStage(s));
target.setStatStage(s, temp);
target.updateInfo();
user.updateInfo();

if (this.stats.length === 7) {
target.scene.queueMessage(i18next.t("moveTriggers:switchedStatChanges", { pokemonName: getPokemonNameWithAffix(user) }));
} else if (this.stats.length === 2) {
target.scene.queueMessage(i18next.t("moveTriggers:switchedTwoStatChanges", {
pokemonName: getPokemonNameWithAffix(user),
firstStat: i18next.t(getStatKey(this.stats[0])),
secondStat: i18next.t(getStatKey(this.stats[1]))
}));
}
return true;
}

target.updateInfo();
user.updateInfo();
target.scene.queueMessage(i18next.t("moveTriggers:switchedStatChanges", {pokemonName: getPokemonNameWithAffix(user)}));
return true;
return false;
}
}

Expand Down Expand Up @@ -7345,9 +7365,9 @@ export function initMoves() {
.attr(CopyMoveAttr)
.ignoresVirtual(),
new StatusMove(Moves.POWER_SWAP, Type.PSYCHIC, -1, 10, 100, 0, 4)
.unimplemented(),
.attr(SwapStatStagesAttr, [ Stat.ATK, Stat.SPATK ]),
new StatusMove(Moves.GUARD_SWAP, Type.PSYCHIC, -1, 10, 100, 0, 4)
.unimplemented(),
.attr(SwapStatStagesAttr, [ Stat.DEF, Stat.SPDEF ]),
new AttackMove(Moves.PUNISHMENT, Type.DARK, MoveCategory.PHYSICAL, -1, 100, 5, -1, 0, 4)
.makesContact(true)
.attr(PunishmentPowerAttr),
Expand All @@ -7361,7 +7381,7 @@ export function initMoves() {
.attr(AddArenaTrapTagAttr, ArenaTagType.TOXIC_SPIKES)
.target(MoveTarget.ENEMY_SIDE),
new StatusMove(Moves.HEART_SWAP, Type.PSYCHIC, -1, 10, -1, 0, 4)
.attr(SwapStatStagesAttr),
.attr(SwapStatStagesAttr, BATTLE_STATS),
new SelfStatusMove(Moves.AQUA_RING, Type.WATER, -1, 20, -1, 0, 4)
.attr(AddBattlerTagAttr, BattlerTagType.AQUA_RING, true, true),
new SelfStatusMove(Moves.MAGNET_RISE, Type.ELECTRIC, -1, 10, -1, 0, 4)
Expand Down
1 change: 1 addition & 0 deletions src/locales/en/move-trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export const moveTriggers: SimpleTranslationEntries = {
"cutHpPowerUpMove": "{{pokemonName}} cut its own HP to power up its move!",
"absorbedElectricity": "{{pokemonName}} absorbed electricity!",
"switchedStatChanges": "{{pokemonName}} switched stat changes with the target!",
"switchedTwoStatChanges": "{{pokemonName}} switched all changes to its {{firstStat}}\nand {{secondStat}} with its target!",
"goingAllOutForAttack": "{{pokemonName}} is going all out for this attack!",
"regainedHealth": "{{pokemonName}} regained\nhealth!",
"keptGoingAndCrashed": "{{pokemonName}} kept going\nand crashed!",
Expand Down
63 changes: 63 additions & 0 deletions src/test/moves/guard_swap.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { afterEach, beforeAll, beforeEach, describe, expect, it } from "vitest";
import Phaser from "phaser";
import GameManager from "#app/test/utils/gameManager";
import { Species } from "#enums/species";
import { TurnEndPhase } from "#app/phases/turn-end-phase";
import { Moves } from "#enums/moves";
import { Stat } from "#enums/stat";
import { getMovePosition } from "#app/test/utils/gameManagerUtils";
import { Abilities } from "#enums/abilities";
import { MoveEndPhase } from "#app/phases/move-end-phase.js";

describe("Moves - Guard Swap", () => {
let phaserGame: Phaser.Game;
let game: GameManager;

beforeAll(() => {
phaserGame = new Phaser.Game({
type: Phaser.HEADLESS,
});
});

afterEach(() => {
game.phaseInterceptor.restoreOg();
});

beforeEach(() => {
game = new GameManager(phaserGame);
game.override.battleType("single");
game.override.enemyAbility(Abilities.BALL_FETCH);
game.override.enemyMoveset(new Array(4).fill(Moves.SHELL_SMASH));
game.override.enemySpecies(Species.MEW);
game.override.enemyLevel(200);
game.override.moveset([ Moves.GUARD_SWAP ]);
game.override.ability(Abilities.NONE);
});

it("should swap the user's DEF AND SPDEF stat stages with the target's", async () => {
await game.startBattle([
Species.INDEEDEE
]);

// Should start with no stat stages
const player = game.scene.getPlayerPokemon()!;
// After Shell Smash, should have +2 in ATK and SPATK, -1 in DEF and SPDEF
const enemy = game.scene.getEnemyPokemon()!;

game.doAttack(getMovePosition(game.scene, 0, Moves.GUARD_SWAP));

await game.phaseInterceptor.to(MoveEndPhase);

expect(player.getStatStage(Stat.DEF)).toBe(0);
expect(player.getStatStage(Stat.SPDEF)).toBe(0);
expect(enemy.getStatStage(Stat.DEF)).toBe(-1);
expect(enemy.getStatStage(Stat.SPDEF)).toBe(-1);

await game.phaseInterceptor.to(TurnEndPhase);

expect(player.getStatStage(Stat.DEF)).toBe(-1);
expect(player.getStatStage(Stat.SPDEF)).toBe(-1);
expect(enemy.getStatStage(Stat.DEF)).toBe(0);
expect(enemy.getStatStage(Stat.SPDEF)).toBe(0);
}, 20000);
});
62 changes: 62 additions & 0 deletions src/test/moves/power_swap.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { afterEach, beforeAll, beforeEach, describe, expect, it } from "vitest";
import Phaser from "phaser";
import GameManager from "#app/test/utils/gameManager";
import { Species } from "#enums/species";
import { TurnEndPhase } from "#app/phases/turn-end-phase";
import { Moves } from "#enums/moves";
import { Stat } from "#enums/stat";
import { getMovePosition } from "#app/test/utils/gameManagerUtils";
import { Abilities } from "#enums/abilities";
import { MoveEndPhase } from "#app/phases/move-end-phase.js";

describe("Moves - Power Swap", () => {
let phaserGame: Phaser.Game;
let game: GameManager;

beforeAll(() => {
phaserGame = new Phaser.Game({
type: Phaser.HEADLESS,
});
});

afterEach(() => {
game.phaseInterceptor.restoreOg();
});

beforeEach(() => {
game = new GameManager(phaserGame);
game.override.battleType("single");
game.override.enemyAbility(Abilities.BALL_FETCH);
game.override.enemyMoveset(new Array(4).fill(Moves.SHELL_SMASH));
game.override.enemySpecies(Species.MEW);
game.override.enemyLevel(200);
game.override.moveset([ Moves.POWER_SWAP ]);
game.override.ability(Abilities.NONE);
});

it("should swap the user's ATK AND SPATK stat stages with the target's", async () => {
await game.startBattle([
Species.INDEEDEE
]);

// Should start with no stat stages
const player = game.scene.getPlayerPokemon()!;
// After Shell Smash, should have +2 in ATK and SPATK, -1 in DEF and SPDEF
const enemy = game.scene.getEnemyPokemon()!;
game.doAttack(getMovePosition(game.scene, 0, Moves.POWER_SWAP));

await game.phaseInterceptor.to(MoveEndPhase);

expect(player.getStatStage(Stat.ATK)).toBe(0);
expect(player.getStatStage(Stat.SPATK)).toBe(0);
expect(enemy.getStatStage(Stat.ATK)).toBe(2);
expect(enemy.getStatStage(Stat.SPATK)).toBe(2);

await game.phaseInterceptor.to(TurnEndPhase);

expect(player.getStatStage(Stat.ATK)).toBe(2);
expect(player.getStatStage(Stat.SPATK)).toBe(2);
expect(enemy.getStatStage(Stat.ATK)).toBe(0);
expect(enemy.getStatStage(Stat.SPATK)).toBe(0);
}, 20000);
});
2 changes: 1 addition & 1 deletion src/test/moves/speed_swap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ describe("Moves - Speed Swap", () => {
game = new GameManager(phaserGame);
game.override.battleType("single");
game.override.enemyAbility(Abilities.NONE);
game.override.enemyMoveset(SPLASH_ONLY);
game.override.enemySpecies(Species.MEW);
game.override.enemyLevel(200);
game.override.moveset([ Moves.SPEED_SWAP ]);
game.override.ability(Abilities.NONE);
});

it("should swap the user's SPD and the target's SPD stats", async () => {
game.override.enemyMoveset(SPLASH_ONLY);
await game.startBattle([
Species.INDEEDEE
]);
Expand Down

0 comments on commit e97e965

Please sign in to comment.