Skip to content

Commit

Permalink
[Ability][Checkup][Bug] Defeatist checkup + getHpRatio() precision im…
Browse files Browse the repository at this point in the history
…proved (#450)

* Wrote documentation for revised functions with new parameters / uses.

* Fixed decimal value parameters

* Wrote Defeatist test.

* Beta Merge Part 2

* Readded fix to Potion generation.

* Forgot about hyper potions

* getHpRatio is now unrounded

* Forgot to save a file before committing.

* Apply suggestions from code review

Co-authored-by: PigeonBar <56974298+PigeonBar@users.noreply.github.com>

---------

Co-authored-by: frutescens <info@laptop>
Co-authored-by: PigeonBar <56974298+PigeonBar@users.noreply.github.com>
  • Loading branch information
3 people authored Feb 12, 2025
1 parent e94599e commit 4a73632
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 4 deletions.
8 changes: 6 additions & 2 deletions src/field/pokemon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1312,8 +1312,12 @@ export abstract class Pokemon extends Phaser.GameObjects.Container {
return this.getMaxHp() - this.hp;
}

getHpRatio(precise: boolean = false): number {
return precise ? this.hp / this.getMaxHp() : Math.round((this.hp / this.getMaxHp()) * 100) / 100;
/**
* Helper function that returns a Pokemon's unrounded HP ratio
* @returns the Pokemon's current HP divided by its max HP
*/
getHpRatio(): number {
return this.hp / this.getMaxHp();
}

generateGender(): void {
Expand Down
4 changes: 2 additions & 2 deletions src/ui/battle-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ export default class BattleInfo extends Phaser.GameObjects.Container {
}
}

this.hpBar.setScale(pokemon.getHpRatio(true), 1);
this.hpBar.setScale(pokemon.getHpRatio(), 1);
this.lastHpFrame = this.hpBar.scaleX > 0.5 ? "high" : this.hpBar.scaleX > 0.25 ? "medium" : "low";
this.hpBar.setFrame(this.lastHpFrame);
if (this.player) {
Expand Down Expand Up @@ -714,7 +714,7 @@ export default class BattleInfo extends Phaser.GameObjects.Container {
globalScene.tweens.add({
targets: this.hpBar,
ease: "Sine.easeOut",
scaleX: pokemon.getHpRatio(true),
scaleX: pokemon.getHpRatio(),
duration: duration,
onUpdate: () => {
if (this.player && this.lastHp !== pokemon.hp) {
Expand Down
61 changes: 61 additions & 0 deletions test/abilities/defeatist.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { Abilities } from "#enums/abilities";
import { MoveId } from "#enums/move-id";
import { Species } from "#enums/species";
import { GameManager } from "#test/testUtils/gameManager";
import Phaser from "phaser";
import { afterEach, beforeAll, beforeEach, describe, expect, it } from "vitest";
import { Stat } from "#enums/stat";

describe("Abilities - Defeatist", () => {
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
.moveset([MoveId.SPLASH])
.ability(Abilities.DEFEATIST)
.battleType("single")
.disableCrits()
.enemySpecies(Species.MAGIKARP)
.enemyAbility(Abilities.BALL_FETCH)
.enemyMoveset(MoveId.SPLASH);
});

it.each([
{ statName: "attack", stat: Stat.ATK },
{ statName: "special attack", stat: Stat.SPATK },
])("should halve the user's $statName if the user's HP is at or below 50%", async ({ stat }) => {
await game.classicMode.startBattle([Species.FEEBAS]);
const playerPokemon = game.field.getPlayerPokemon();
const expectedStat = Math.floor(playerPokemon.getStat(stat as number) / 2);
playerPokemon.hp = 1;
const defeatistStat = playerPokemon.getEffectiveStat(stat as number);

expect(playerPokemon.getHpRatio()).toBeLessThanOrEqual(0.5);
expect(defeatistStat).toBe(expectedStat);
});

it.each([
{ statName: "attack", stat: Stat.ATK },
{ statName: "special attack", stat: Stat.SPATK },
])("should have no effect on $statName if the user's HP is above 50%", async ({ stat }) => {
await game.classicMode.startBattle([Species.FEEBAS]);
const playerPokemon = game.field.getPlayerPokemon();
const expectedStat = playerPokemon.getStat(stat as number);
const defeatistStat = playerPokemon.getEffectiveStat(stat as number);

expect(playerPokemon.getHpRatio()).toBeGreaterThan(0.5);
expect(defeatistStat).toBe(expectedStat);
});
});

0 comments on commit 4a73632

Please sign in to comment.