mirror of
https://github.com/pagefaultgames/pokerogue.git
synced 2025-08-14 03:19:28 +02:00
Updated modifier-bar.ts
This commit is contained in:
parent
64b1cf1669
commit
9a79882e05
@ -1,35 +1,39 @@
|
|||||||
|
import type Pokemon from "#app/field/pokemon";
|
||||||
import { globalScene } from "#app/global-scene";
|
import { globalScene } from "#app/global-scene";
|
||||||
import { type Modifier, type PersistentModifier, PokemonHeldItemModifier } from "./modifier";
|
import { allHeldItems } from "#app/items/all-held-items";
|
||||||
|
import type { HeldItemId } from "#enums/held-item-id";
|
||||||
|
import type { Modifier, PersistentModifier } from "./modifier";
|
||||||
|
|
||||||
const iconOverflowIndex = 24;
|
const iconOverflowIndex = 24;
|
||||||
|
|
||||||
export const modifierSortFunc = (a: Modifier, b: Modifier): number => {
|
export const modifierSortFunc = (a: Modifier, b: Modifier): number => {
|
||||||
const itemNameMatch = a.type.name.localeCompare(b.type.name);
|
const itemNameMatch = a.type.name.localeCompare(b.type.name);
|
||||||
const typeNameMatch = a.constructor.name.localeCompare(b.constructor.name);
|
const typeNameMatch = a.constructor.name.localeCompare(b.constructor.name);
|
||||||
const aId = a instanceof PokemonHeldItemModifier && a.pokemonId ? a.pokemonId : 4294967295;
|
|
||||||
const bId = b instanceof PokemonHeldItemModifier && b.pokemonId ? b.pokemonId : 4294967295;
|
|
||||||
|
|
||||||
//First sort by pokemonID
|
//Then sort by item type
|
||||||
if (aId < bId) {
|
if (typeNameMatch === 0) {
|
||||||
return 1;
|
return itemNameMatch;
|
||||||
|
//Finally sort by item name
|
||||||
}
|
}
|
||||||
if (aId > bId) {
|
return typeNameMatch;
|
||||||
return -1;
|
};
|
||||||
|
|
||||||
|
//TODO: revisit this function
|
||||||
|
export const heldItemSortFunc = (a: HeldItemId, b: HeldItemId): number => {
|
||||||
|
const itemNameMatch = allHeldItems[a].name.localeCompare(allHeldItems[b].name);
|
||||||
|
const itemIdMatch = a - b;
|
||||||
|
|
||||||
|
if (itemIdMatch === 0) {
|
||||||
|
return itemNameMatch;
|
||||||
|
//Finally sort by item name
|
||||||
}
|
}
|
||||||
if (aId === bId) {
|
return itemIdMatch;
|
||||||
//Then sort by item type
|
|
||||||
if (typeNameMatch === 0) {
|
|
||||||
return itemNameMatch;
|
|
||||||
//Finally sort by item name
|
|
||||||
}
|
|
||||||
return typeNameMatch;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export class ModifierBar extends Phaser.GameObjects.Container {
|
export class ModifierBar extends Phaser.GameObjects.Container {
|
||||||
private player: boolean;
|
private player: boolean;
|
||||||
private modifierCache: PersistentModifier[];
|
private modifierCache: (PersistentModifier | HeldItemId)[];
|
||||||
|
private totalVisibleLength = 0;
|
||||||
|
|
||||||
constructor(enemy?: boolean) {
|
constructor(enemy?: boolean) {
|
||||||
super(globalScene, 1 + (enemy ? 302 : 0), 2);
|
super(globalScene, 1 + (enemy ? 302 : 0), 2);
|
||||||
@ -43,48 +47,60 @@ export class ModifierBar extends Phaser.GameObjects.Container {
|
|||||||
* @param {PersistentModifier[]} modifiers - The list of modifiers to be displayed in the {@linkcode ModifierBar}
|
* @param {PersistentModifier[]} modifiers - The list of modifiers to be displayed in the {@linkcode ModifierBar}
|
||||||
* @param {boolean} hideHeldItems - If set to "true", only modifiers not assigned to a Pokémon are displayed
|
* @param {boolean} hideHeldItems - If set to "true", only modifiers not assigned to a Pokémon are displayed
|
||||||
*/
|
*/
|
||||||
updateModifiers(modifiers: PersistentModifier[], hideHeldItems = false) {
|
updateModifiers(modifiers: PersistentModifier[], pokemonA: Pokemon, pokemonB?: Pokemon, hideHeldItems = false) {
|
||||||
this.removeAll(true);
|
this.removeAll(true);
|
||||||
|
|
||||||
const visibleIconModifiers = modifiers.filter(m => m.isIconVisible());
|
const sortedVisibleModifiers = modifiers.filter(m => m.isIconVisible()).sort(modifierSortFunc);
|
||||||
const nonPokemonSpecificModifiers = visibleIconModifiers
|
|
||||||
.filter(m => !(m as PokemonHeldItemModifier).pokemonId)
|
|
||||||
.sort(modifierSortFunc);
|
|
||||||
const pokemonSpecificModifiers = visibleIconModifiers
|
|
||||||
.filter(m => (m as PokemonHeldItemModifier).pokemonId)
|
|
||||||
.sort(modifierSortFunc);
|
|
||||||
|
|
||||||
const sortedVisibleIconModifiers = hideHeldItems
|
const heldItemsA = pokemonA.getHeldItems().sort(heldItemSortFunc);
|
||||||
? nonPokemonSpecificModifiers
|
const heldItemsB = pokemonB ? pokemonB.getHeldItems().sort(heldItemSortFunc) : [];
|
||||||
: nonPokemonSpecificModifiers.concat(pokemonSpecificModifiers);
|
|
||||||
|
|
||||||
sortedVisibleIconModifiers.forEach((modifier: PersistentModifier, i: number) => {
|
this.totalVisibleLength = sortedVisibleModifiers.length;
|
||||||
|
if (!hideHeldItems) {
|
||||||
|
this.totalVisibleLength += heldItemsA.length + heldItemsB.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
sortedVisibleModifiers.forEach((modifier: PersistentModifier, i: number) => {
|
||||||
const icon = modifier.getIcon();
|
const icon = modifier.getIcon();
|
||||||
if (i >= iconOverflowIndex) {
|
this.addIcon(icon, i, modifier.type.name, modifier.type.getDescription());
|
||||||
icon.setVisible(false);
|
});
|
||||||
}
|
|
||||||
this.add(icon);
|
heldItemsA.forEach((item: HeldItemId, i: number) => {
|
||||||
this.setModifierIconPosition(icon, sortedVisibleIconModifiers.length);
|
const icon = allHeldItems[item].createPokemonIcon(pokemonA);
|
||||||
icon.setInteractive(new Phaser.Geom.Rectangle(0, 0, 32, 24), Phaser.Geom.Rectangle.Contains);
|
this.addIcon(icon, i, allHeldItems[item].name, allHeldItems[item].description);
|
||||||
icon.on("pointerover", () => {
|
});
|
||||||
globalScene.ui.showTooltip(modifier.type.name, modifier.type.getDescription());
|
|
||||||
if (this.modifierCache && this.modifierCache.length > iconOverflowIndex) {
|
heldItemsB.forEach((item: HeldItemId, i: number) => {
|
||||||
this.updateModifierOverflowVisibility(true);
|
const icon = allHeldItems[item].createPokemonIcon(pokemonB);
|
||||||
}
|
this.addIcon(icon, i, allHeldItems[item].name, allHeldItems[item].description);
|
||||||
});
|
|
||||||
icon.on("pointerout", () => {
|
|
||||||
globalScene.ui.hideTooltip();
|
|
||||||
if (this.modifierCache && this.modifierCache.length > iconOverflowIndex) {
|
|
||||||
this.updateModifierOverflowVisibility(false);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
for (const icon of this.getAll()) {
|
for (const icon of this.getAll()) {
|
||||||
this.sendToBack(icon);
|
this.sendToBack(icon);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.modifierCache = modifiers;
|
this.modifierCache = (modifiers as (PersistentModifier | HeldItemId)[]).concat(heldItemsA).concat(heldItemsB);
|
||||||
|
}
|
||||||
|
|
||||||
|
addIcon(icon: Phaser.GameObjects.Container, i: number, name: string, description: string) {
|
||||||
|
if (i >= iconOverflowIndex) {
|
||||||
|
icon.setVisible(false);
|
||||||
|
}
|
||||||
|
this.add(icon);
|
||||||
|
this.setModifierIconPosition(icon, this.totalVisibleLength);
|
||||||
|
icon.setInteractive(new Phaser.Geom.Rectangle(0, 0, 32, 24), Phaser.Geom.Rectangle.Contains);
|
||||||
|
icon.on("pointerover", () => {
|
||||||
|
globalScene.ui.showTooltip(name, description);
|
||||||
|
if (this.modifierCache && this.modifierCache.length > iconOverflowIndex) {
|
||||||
|
this.updateModifierOverflowVisibility(true);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
icon.on("pointerout", () => {
|
||||||
|
globalScene.ui.hideTooltip();
|
||||||
|
if (this.modifierCache && this.modifierCache.length > iconOverflowIndex) {
|
||||||
|
this.updateModifierOverflowVisibility(false);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
updateModifierOverflowVisibility(ignoreLimit: boolean) {
|
updateModifierOverflowVisibility(ignoreLimit: boolean) {
|
||||||
|
Loading…
Reference in New Issue
Block a user