[feature]Implemented needed parts for discard function from issue #4780:

-TryDiscardFunction in battlescene;
-Created a party discard mode button;
-Updated Transfer button in modifier-select-ui-handler to Manage items;
-Created tests for the discard function in test/ui;
-Added images for the new discard and transfer buttons to loading-scene;
-Created placeholder messages for discard feature in party-ui-handler;

Co-authored-by: Tiago Rodrigues <tiago.n.rodrigues@tecnico.ulisboa.pt>
This commit is contained in:
Mikhail Shueb 2025-06-11 15:46:46 +01:00
parent 39b8dc9a85
commit 9b7da35fdc
13 changed files with 537 additions and 12 deletions

View File

@ -0,0 +1,62 @@
{
"textures": [
{
"image": "party_discard.png",
"format": "RGBA8888",
"size": {
"w": 75,
"h": 46
},
"scale": 1,
"frames": [
{
"filename": "normal",
"rotated": false,
"trimmed": false,
"sourceSize": {
"w": 75,
"h": 23
},
"spriteSourceSize": {
"x": 0,
"y": 0,
"w": 75,
"h": 23
},
"frame": {
"x": 0,
"y": 0,
"w": 75,
"h": 23
}
},
{
"filename": "selected",
"rotated": false,
"trimmed": false,
"sourceSize": {
"w": 75,
"h": 23
},
"spriteSourceSize": {
"x": 0,
"y": 0,
"w": 75,
"h": 23
},
"frame": {
"x": 0,
"y": 23,
"w": 75,
"h": 23
}
}
]
}
],
"meta": {
"app": "https://www.codeandweb.com/texturepacker",
"version": "3.0",
"smartupdate": "$TexturePacker:SmartUpdate:17219773dfffd6b1204d988fea3f9462:1127ad21d64bc7ebb9df4fc28f3d2d39:7ad46e8fb4648c3d3d84a746ecb371ea$"
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.8 KiB

View File

@ -0,0 +1,62 @@
{
"textures": [
{
"image": "party_discard.png",
"format": "RGBA8888",
"size": {
"w": 75,
"h": 46
},
"scale": 1,
"frames": [
{
"filename": "normal",
"rotated": false,
"trimmed": false,
"sourceSize": {
"w": 75,
"h": 23
},
"spriteSourceSize": {
"x": 0,
"y": 0,
"w": 75,
"h": 23
},
"frame": {
"x": 0,
"y": 0,
"w": 75,
"h": 23
}
},
{
"filename": "selected",
"rotated": false,
"trimmed": false,
"sourceSize": {
"w": 75,
"h": 23
},
"spriteSourceSize": {
"x": 0,
"y": 0,
"w": 75,
"h": 23
},
"frame": {
"x": 0,
"y": 23,
"w": 75,
"h": 23
}
}
]
}
],
"meta": {
"app": "https://www.codeandweb.com/texturepacker",
"version": "3.0",
"smartupdate": "$TexturePacker:SmartUpdate:17219773dfffd6b1204d988fea3f9462:1127ad21d64bc7ebb9df4fc28f3d2d39:7ad46e8fb4648c3d3d84a746ecb371ea$"
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.8 KiB

View File

@ -0,0 +1,62 @@
{
"textures": [
{
"image": "party_discard.png",
"format": "RGBA8888",
"size": {
"w": 75,
"h": 46
},
"scale": 1,
"frames": [
{
"filename": "normal",
"rotated": false,
"trimmed": false,
"sourceSize": {
"w": 75,
"h": 23
},
"spriteSourceSize": {
"x": 0,
"y": 0,
"w": 75,
"h": 23
},
"frame": {
"x": 0,
"y": 0,
"w": 75,
"h": 23
}
},
{
"filename": "selected",
"rotated": false,
"trimmed": false,
"sourceSize": {
"w": 75,
"h": 23
},
"spriteSourceSize": {
"x": 0,
"y": 0,
"w": 75,
"h": 23
},
"frame": {
"x": 0,
"y": 23,
"w": 75,
"h": 23
}
}
]
}
],
"meta": {
"app": "https://www.codeandweb.com/texturepacker",
"version": "3.0",
"smartupdate": "$TexturePacker:SmartUpdate:17219773dfffd6b1204d988fea3f9462:1127ad21d64bc7ebb9df4fc28f3d2d39:7ad46e8fb4648c3d3d84a746ecb371ea$"
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.8 KiB

View File

@ -0,0 +1,62 @@
{
"textures": [
{
"image": "party_discard.png",
"format": "RGBA8888",
"size": {
"w": 75,
"h": 46
},
"scale": 1,
"frames": [
{
"filename": "normal",
"rotated": false,
"trimmed": false,
"sourceSize": {
"w": 75,
"h": 23
},
"spriteSourceSize": {
"x": 0,
"y": 0,
"w": 75,
"h": 23
},
"frame": {
"x": 0,
"y": 0,
"w": 75,
"h": 23
}
},
{
"filename": "selected",
"rotated": false,
"trimmed": false,
"sourceSize": {
"w": 75,
"h": 23
},
"spriteSourceSize": {
"x": 0,
"y": 0,
"w": 75,
"h": 23
},
"frame": {
"x": 0,
"y": 23,
"w": 75,
"h": 23
}
}
]
}
],
"meta": {
"app": "https://www.codeandweb.com/texturepacker",
"version": "3.0",
"smartupdate": "$TexturePacker:SmartUpdate:17219773dfffd6b1204d988fea3f9462:1127ad21d64bc7ebb9df4fc28f3d2d39:7ad46e8fb4648c3d3d84a746ecb371ea$"
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.8 KiB

View File

@ -3118,6 +3118,19 @@ export default class BattleScene extends SceneBase {
return false;
}
tryDiscardHeldItemModifier(itemModifier: PokemonHeldItemModifier, discardQuantity = 1): boolean {
const countTaken = Math.min(discardQuantity, itemModifier.stackCount);
itemModifier.stackCount -= countTaken;
const removeOld = itemModifier.stackCount === 0;
if (removeOld) {
return this.removeModifier(itemModifier);
}
return true;
}
canTransferHeldItemModifier(itemModifier: PokemonHeldItemModifier, target: Pokemon, transferQuantity = 1): boolean {
const mod = itemModifier.clone() as PokemonHeldItemModifier;
const source = mod.pokemonId ? mod.getPokemon() : null;

View File

@ -136,6 +136,8 @@ export class LoadingScene extends SceneBase {
this.loadAtlas("party_slot_hp_overlay", "ui");
this.loadAtlas("party_pb", "ui");
this.loadAtlas("party_cancel", "ui");
this.loadAtlas("party_discard", "ui");
this.loadAtlas("party_transfer", "ui");
this.loadImage("summary_bg", "ui");
this.loadImage("summary_overlay_shiny", "ui");

View File

@ -68,7 +68,7 @@ export default class ModifierSelectUiHandler extends AwaitableUiHandler {
if (context) {
context.font = styleOptions.fontSize + "px " + styleOptions.fontFamily;
this.transferButtonWidth = context.measureText(i18next.t("modifierSelectUiHandler:transfer")).width;
this.transferButtonWidth = context.measureText("Manage Items").width; // Missing "Manage Items" call from locales.
this.checkButtonWidth = context.measureText(i18next.t("modifierSelectUiHandler:checkTeam")).width;
}
@ -80,7 +80,7 @@ export default class ModifierSelectUiHandler extends AwaitableUiHandler {
this.transferButtonContainer.setVisible(false);
ui.add(this.transferButtonContainer);
const transferButtonText = addTextObject(-4, -2, i18next.t("modifierSelectUiHandler:transfer"), TextStyle.PARTY);
const transferButtonText = addTextObject(-4, -2, "Manage Items", TextStyle.PARTY); // Missing "Manage Items" call from locales.
transferButtonText.setName("text-transfer-btn");
transferButtonText.setOrigin(1, 0);
this.transferButtonContainer.add(transferButtonText);

View File

@ -104,6 +104,11 @@ export enum PartyUiMode {
* This is generally used in for Mystery Encounter or special effects that require the player to select a Pokemon
*/
SELECT,
/**
* Indicates that the party UI is open to select a party member from which to discard an item from
* This type of selection can be cancelled.
*/
DISCARD,
}
export enum PartyOption {
@ -122,6 +127,7 @@ export enum PartyOption {
RELEASE,
RENAME,
SELECT,
DISCARD,
SCROLL_UP = 1000,
SCROLL_DOWN = 1001,
FORM_CHANGE_ITEM = 2000,
@ -156,6 +162,7 @@ export default class PartyUiHandler extends MessageUiHandler {
private partySlotsContainer: Phaser.GameObjects.Container;
private partySlots: PartySlot[];
private partyCancelButton: PartyCancelButton;
private PartyDiscardModeButton: PartyDiscardModeButton;
private partyMessageBox: Phaser.GameObjects.NineSlice;
private moveInfoOverlay: MoveInfoOverlay;
@ -309,6 +316,11 @@ export default class PartyUiHandler extends MessageUiHandler {
this.iconAnimHandler = new PokemonIconAnimHandler();
this.iconAnimHandler.setup();
const partyDiscardModeButton = new PartyDiscardModeButton(60, -globalScene.game.canvas.height / 15 - 1);
partyContainer.add(partyDiscardModeButton);
this.PartyDiscardModeButton = partyDiscardModeButton;
// prepare move overlay. in case it appears to be too big, set the overlayScale to .5
const overlayScale = 1;
this.moveInfoOverlay = new MoveInfoOverlay({
@ -352,6 +364,7 @@ export default class PartyUiHandler extends MessageUiHandler {
this.partyContainer.setVisible(true);
this.partyBg.setTexture(`party_bg${globalScene.currentBattle.double ? "_double" : ""}`);
this.populatePartySlots();
this.PartyDiscardModeButton.toggleIcon(this.partyUiMode);
this.setCursor(0);
return true;
@ -596,7 +609,7 @@ export default class PartyUiHandler extends MessageUiHandler {
const option = this.options[this.optionsCursor];
if (button === Button.LEFT) {
/** Decrease quantity for the current item and update UI */
if (this.partyUiMode === PartyUiMode.MODIFIER_TRANSFER) {
if (this.isItemManageMode()) {
this.transferQuantities[option] =
this.transferQuantities[option] === 1
? this.transferQuantitiesMax[option]
@ -610,7 +623,7 @@ export default class PartyUiHandler extends MessageUiHandler {
if (button === Button.RIGHT) {
/** Increase quantity for the current item and update UI */
if (this.partyUiMode === PartyUiMode.MODIFIER_TRANSFER) {
if (this.isItemManageMode()) {
this.transferQuantities[option] =
this.transferQuantities[option] === this.transferQuantitiesMax[option]
? 1
@ -640,6 +653,34 @@ export default class PartyUiHandler extends MessageUiHandler {
return success;
}
private processDiscardMenuInput(pokemon: PlayerPokemon) {
const ui = this.getUi();
this.clearOptions();
const itemModifiers = this.getTransferrableItemsFromPokemon(pokemon);
let success = true;
if (this.optionsCursor === itemModifiers.length) {
for (let i = 0; i < itemModifiers.length; i++) {
success = globalScene.tryDiscardHeldItemModifier(itemModifiers[i], this.transferQuantities[i]);
}
} else {
success = globalScene.tryDiscardHeldItemModifier(
itemModifiers[this.optionsCursor],
this.transferQuantities[this.optionsCursor],
);
}
if (success) {
this.showText("Item has been discarded.", undefined, () => this.showText("", 0), null, true); // missing confirm message call from i18next.t.
ui.playSelect();
} else {
ui.playError();
}
return success;
}
private moveOptionCursor(button: Button.UP | Button.DOWN): boolean {
if (button === Button.UP) {
return this.setCursor(this.optionsCursor ? this.optionsCursor - 1 : this.options.length - 1);
@ -726,6 +767,10 @@ export default class PartyUiHandler extends MessageUiHandler {
return this.processModifierTransferModeInput(pokemon);
}
if (this.partyUiMode === PartyUiMode.DISCARD) {
return this.processDiscardMenuInput(pokemon);
}
// options specific to the mode (moves)
if (this.partyUiMode === PartyUiMode.REMEMBER_MOVE_MODIFIER) {
return this.processRememberMoveModeInput(pokemon);
@ -865,7 +910,7 @@ export default class PartyUiHandler extends MessageUiHandler {
}
if (button === Button.LEFT || button === Button.RIGHT) {
if (this.partyUiMode === PartyUiMode.MODIFIER_TRANSFER) {
if (this.isItemManageMode()) {
return this.processModifierTransferModeLeftRightInput(button);
}
}
@ -920,10 +965,17 @@ export default class PartyUiHandler extends MessageUiHandler {
return !(this.partyUiMode === PartyUiMode.FAINT_SWITCH || this.partyUiMode === PartyUiMode.REVIVAL_BLESSING);
}
private isItemManageMode(): boolean {
return this.partyUiMode === PartyUiMode.MODIFIER_TRANSFER || this.partyUiMode === PartyUiMode.DISCARD;
}
private processPartyActionInput(): boolean {
const ui = this.getUi();
if (this.cursor < 6) {
if (this.partyUiMode === PartyUiMode.MODIFIER_TRANSFER && !this.transferMode) {
if (
(this.partyUiMode === PartyUiMode.MODIFIER_TRANSFER && !this.transferMode) ||
this.partyUiMode === PartyUiMode.DISCARD
) {
/** Initialize item quantities for the selected Pokemon */
const itemModifiers = globalScene.findModifiers(
m =>
@ -937,6 +989,24 @@ export default class PartyUiHandler extends MessageUiHandler {
this.showOptions();
ui.playSelect();
}
if (this.cursor === 7) {
switch (this.partyUiMode) {
case PartyUiMode.DISCARD:
this.partyUiMode = PartyUiMode.MODIFIER_TRANSFER;
ui.playSelect();
break;
case PartyUiMode.MODIFIER_TRANSFER:
this.partyUiMode = PartyUiMode.DISCARD;
ui.playSelect();
break;
default:
ui.playError();
break;
}
this.PartyDiscardModeButton.toggleIcon(this.partyUiMode);
ui.playSelect();
}
// Pressing return button
if (this.cursor === 6) {
if (!this.allowCancel()) {
@ -957,6 +1027,7 @@ export default class PartyUiHandler extends MessageUiHandler {
this.clearTransfer();
ui.playSelect();
} else if (this.allowCancel()) {
this.PartyDiscardModeButton.clear();
if (this.selectCallback) {
const selectCallback = this.selectCallback;
this.selectCallback = null;
@ -978,9 +1049,19 @@ export default class PartyUiHandler extends MessageUiHandler {
let success = false;
switch (button) {
case Button.UP:
if (this.cursor === 7 && this.isItemManageMode()) {
success = this.setCursor(0);
break;
}
success = this.setCursor(this.cursor ? (this.cursor < 6 ? this.cursor - 1 : slotCount - 1) : 6);
break;
case Button.DOWN:
if (this.cursor === 0) {
if (this.isItemManageMode()) {
success = this.setCursor(7);
break;
}
}
success = this.setCursor(this.cursor < 6 ? (this.cursor < slotCount - 1 ? this.cursor + 1 : 6) : 0);
break;
case Button.LEFT:
@ -989,7 +1070,7 @@ export default class PartyUiHandler extends MessageUiHandler {
}
break;
case Button.RIGHT:
if (slotCount === battlerCount) {
if ((this.cursor === 7 && this.isItemManageMode()) || slotCount === battlerCount) {
success = this.setCursor(6);
break;
}
@ -1045,11 +1126,15 @@ export default class PartyUiHandler extends MessageUiHandler {
this.partySlots[this.lastCursor].deselect();
} else if (this.lastCursor === 6) {
this.partyCancelButton.deselect();
} else if (this.lastCursor === 7) {
this.PartyDiscardModeButton.deselect();
}
if (cursor < 6) {
this.partySlots[cursor].select();
} else if (cursor === 6) {
this.partyCancelButton.select();
} else if (cursor === 7) {
this.PartyDiscardModeButton.select();
}
}
return changed;
@ -1144,14 +1229,16 @@ export default class PartyUiHandler extends MessageUiHandler {
optionsMessage = i18next.t("partyUiHandler:selectAnotherPokemonToSplice");
}
break;
case PartyUiMode.DISCARD:
optionsMessage = "Select a held item to discard.\nUse < and > to change quantity."; //Missing discard i18next.t call message
}
this.showText(optionsMessage, 0);
this.updateOptions();
/** When an item is being selected for transfer, the message box is taller as the message occupies two lines */
if (this.partyUiMode === PartyUiMode.MODIFIER_TRANSFER) {
/** When an item is being selected for transfer or discard, the message box is taller as the message occupies two lines */
if (this.isItemManageMode()) {
this.partyMessageBox.setSize(262 - Math.max(this.optionsBg.displayWidth - 56, 0), 42);
} else {
this.partyMessageBox.setSize(262 - Math.max(this.optionsBg.displayWidth - 56, 0), 30);
@ -1278,6 +1365,10 @@ export default class PartyUiHandler extends MessageUiHandler {
this.addCommonOptions(pokemon);
}
break;
case PartyUiMode.DISCARD:
this.updateOptionsWithModifierTransferMode(pokemon);
//maybe change the name, this is the item selection menu
break;
// TODO: This still needs to be broken up.
// It could use a rework differentiating different kind of switches
// to treat baton passing separately from switching on faint.
@ -1382,7 +1473,8 @@ export default class PartyUiHandler extends MessageUiHandler {
optionName = "↓";
} else if (
(this.partyUiMode !== PartyUiMode.REMEMBER_MOVE_MODIFIER &&
(this.partyUiMode !== PartyUiMode.MODIFIER_TRANSFER || this.transferMode)) ||
(this.partyUiMode !== PartyUiMode.MODIFIER_TRANSFER || this.transferMode) &&
this.partyUiMode !== PartyUiMode.DISCARD) ||
option === PartyOption.CANCEL
) {
switch (option) {
@ -1443,7 +1535,7 @@ export default class PartyUiHandler extends MessageUiHandler {
const itemModifiers = this.getItemModifiers(pokemon);
const itemModifier = itemModifiers[option];
if (
this.partyUiMode === PartyUiMode.MODIFIER_TRANSFER &&
this.isItemManageMode() &&
this.transferQuantitiesMax[option] > 1 &&
!this.transferMode &&
itemModifier !== undefined &&
@ -1473,7 +1565,6 @@ export default class PartyUiHandler extends MessageUiHandler {
optionText.x = 15 - this.optionsBg.width;
}
}
startTransfer(): void {
this.transferMode = true;
this.transferCursor = this.cursor;
@ -1965,3 +2056,76 @@ class PartyCancelButton extends Phaser.GameObjects.Container {
this.partyCancelPb.setFrame("party_pb");
}
}
class PartyDiscardModeButton extends Phaser.GameObjects.Container {
private selected: boolean;
private transferIcon: Phaser.GameObjects.Sprite;
private discardIcon: Phaser.GameObjects.Sprite;
private textBox: Phaser.GameObjects.Text;
constructor(x: number, y: number) {
super(globalScene, x, y);
this.setup();
}
setup() {
this.transferIcon = globalScene.add.sprite(0, 0, "party_transfer", "normal");
this.discardIcon = globalScene.add.sprite(0, 0, "party_discard", "normal");
this.textBox = addTextObject(-8, -7, "Transfer", TextStyle.PARTY);
this.add(this.transferIcon);
this.add(this.discardIcon);
this.add(this.textBox);
this.clear();
}
select() {
if (this.selected) {
return;
}
this.selected = true;
this.transferIcon.setFrame("selected");
this.discardIcon.setFrame("selected");
}
deselect() {
if (!this.selected) {
return;
}
this.selected = false;
this.transferIcon.setFrame("normal");
this.discardIcon.setFrame("normal");
}
toggleIcon(partyMode: number) {
switch (partyMode) {
case PartyUiMode.DISCARD:
this.transferIcon.setVisible(true);
this.discardIcon.setVisible(false);
this.textBox.setVisible(true);
this.textBox.setText("Transfer");
break;
case PartyUiMode.MODIFIER_TRANSFER:
this.transferIcon.setVisible(false);
this.discardIcon.setVisible(true);
this.textBox.setVisible(true);
this.textBox.setText("Discard");
break;
default:
return;
}
}
clear() {
this.transferIcon.setVisible(false);
this.discardIcon.setVisible(false);
this.textBox.setVisible(false);
}
}

View File

@ -0,0 +1,98 @@
import { BerryType } from "#app/enums/berry-type";
import { Button } from "#app/enums/buttons";
import { MoveId } from "#enums/move-id";
import { SpeciesId } from "#enums/species-id";
import ModifierSelectUiHandler from "#app/ui/modifier-select-ui-handler";
import PartyUiHandler, { PartyUiMode } from "#app/ui/party-ui-handler";
import { UiMode } from "#enums/ui-mode";
import GameManager from "#test/testUtils/gameManager";
import Phaser from "phaser";
import { afterEach, beforeAll, beforeEach, describe, expect, it } from "vitest";
describe("UI - Transfer Items", () => {
let phaserGame: Phaser.Game;
let game: GameManager;
beforeAll(() => {
phaserGame = new Phaser.Game({
type: Phaser.HEADLESS,
});
});
afterEach(() => {
game.phaseInterceptor.restoreOg();
});
beforeEach(async () => {
game = new GameManager(phaserGame);
game.override.battleStyle("single");
game.override.startingLevel(100);
game.override.startingWave(1);
game.override.startingHeldItems([
{ name: "BERRY", count: 1, type: BerryType.SITRUS },
{ name: "BERRY", count: 2, type: BerryType.APICOT },
{ name: "BERRY", count: 2, type: BerryType.LUM },
]);
game.override.moveset([MoveId.DRAGON_CLAW]);
game.override.enemySpecies(SpeciesId.MAGIKARP);
game.override.enemyMoveset([MoveId.SPLASH]);
await game.classicMode.startBattle([SpeciesId.RAYQUAZA, SpeciesId.RAYQUAZA, SpeciesId.RAYQUAZA]);
game.move.select(MoveId.DRAGON_CLAW);
});
it("manage button exists in the proper screen", async () => {
game.onNextPrompt("SelectModifierPhase", UiMode.MODIFIER_SELECT, () => {
expect(game.scene.ui.getHandler()).toBeInstanceOf(ModifierSelectUiHandler);
const handler = game.scene.ui.getHandler() as ModifierSelectUiHandler;
handler.setCursor(1); //manage items menu, manage button should exist
handler.processInput(Button.ACTION);
void game.scene.ui.setModeWithoutClear(UiMode.PARTY, PartyUiMode.MODIFIER_TRANSFER);
});
await game.phaseInterceptor.to("BattleEndPhase");
game.phaseInterceptor.addToNextPrompt("SelectModifierPhase", UiMode.PARTY, () => {
expect(game.scene.ui.getHandler()).toBeInstanceOf(PartyUiHandler);
const handler = game.scene.ui.getHandler() as PartyUiHandler;
handler.processInput(Button.DOWN);
handler.processInput(Button.ACTION);
expect(handler.optionsContainer.list.length).toBe(0); //should select manage button,which has no menu
game.phaseInterceptor.unlock();
});
await game.phaseInterceptor.to("SelectModifierPhase");
}, 20000);
it("manage button doesn't exist in the other screens", async () => {
game.onNextPrompt("SelectModifierPhase", UiMode.MODIFIER_SELECT, () => {
expect(game.scene.ui.getHandler()).toBeInstanceOf(ModifierSelectUiHandler);
const handler = game.scene.ui.getHandler() as ModifierSelectUiHandler;
handler.setCursor(2); //check team menu,manage button shouldn't exist
handler.processInput(Button.ACTION);
void game.scene.ui.setModeWithoutClear(UiMode.PARTY, PartyUiMode.MODIFIER_TRANSFER);
});
await game.phaseInterceptor.to("BattleEndPhase");
game.phaseInterceptor.addToNextPrompt("SelectModifierPhase", UiMode.PARTY, () => {
expect(game.scene.ui.getHandler()).toBeInstanceOf(PartyUiHandler);
const handler = game.scene.ui.getHandler() as PartyUiHandler;
handler.processInput(Button.DOWN);
handler.processInput(Button.ACTION);
expect(handler.optionsContainer.list.length).toBeGreaterThan(0); //should select a pokemon, which has at least the cancel option
game.phaseInterceptor.unlock();
});
await game.phaseInterceptor.to("SelectModifierPhase");
}, 20000);
});