pokerogue/src/battle-scene.ts

1242 lines
39 KiB
TypeScript
Raw Normal View History

2023-03-28 19:54:52 +01:00
import Phaser from 'phaser';
2023-04-20 20:46:05 +01:00
import { Biome } from './data/biome';
2023-04-25 03:32:12 +01:00
import UI from './ui/ui';
2023-10-20 19:19:22 +01:00
import { EncounterPhase, SummonPhase, NextEncounterPhase, NewBiomeEncounterPhase, SelectBiomePhase, MessagePhase, CheckLoadPhase, TurnInitPhase, ReturnPhase, ToggleDoublePositionPhase, CheckSwitchPhase, LevelCapPhase, TestMessagePhase, ShowTrainerPhase } from './battle-phases';
2023-05-30 14:46:42 +01:00
import Pokemon, { PlayerPokemon, EnemyPokemon } from './pokemon';
import PokemonSpecies, { PokemonSpeciesFilter, allSpecies, getPokemonSpecies, initSpecies } from './data/pokemon-species';
2023-03-28 19:54:52 +01:00
import * as Utils from './utils';
2023-06-01 16:22:34 +01:00
import { Modifier, ModifierBar, ConsumablePokemonModifier, ConsumableModifier, PokemonHpRestoreModifier, HealingBoosterModifier, PersistentModifier, PokemonHeldItemModifier, ModifierPredicate, DoubleBattleChanceBoosterModifier } from './modifier/modifier';
2023-04-20 20:46:05 +01:00
import { PokeballType } from './data/pokeball';
import { initAutoPlay } from './system/auto-play';
import { initCommonAnims, initMoveAnim, loadCommonAnimAssets, loadMoveAnimAssets, populateAnims } from './data/battle-anims';
2023-04-10 19:12:01 +01:00
import { BattlePhase } from './battle-phase';
2023-04-20 20:46:05 +01:00
import { initGameSpeed } from './system/game-speed';
import { Arena, ArenaBase, getBiomeHasProps, getBiomeKey } from './arena';
2023-04-20 20:46:05 +01:00
import { GameData } from './system/game-data';
import StarterSelectUiHandler from './ui/starter-select-ui-handler';
2023-04-20 20:46:05 +01:00
import { TextStyle, addTextObject } from './ui/text';
import { Moves, initMoves } from './data/move';
2023-04-21 02:32:48 +01:00
import { getDefaultModifierTypeForTier, getEnemyModifierTypesForWave } from './modifier/modifier-type';
2023-04-27 06:14:15 +01:00
import AbilityBar from './ui/ability-bar';
import { BlockItemTheftAbAttr, DoubleBattleChanceAbAttr, applyAbAttrs, initAbilities } from './data/ability';
import Battle, { BattleType, FixedBattleConfig, fixedBattles } from './battle';
import { GameMode } from './game-mode';
2023-06-02 23:33:51 +01:00
import SpritePipeline from './pipelines/sprite';
import PartyExpBar from './ui/party-exp-bar';
2023-10-07 21:08:33 +01:00
import { TrainerType, trainerConfigs } from './data/trainer-type';
import Trainer from './trainer';
import TrainerData from './system/trainer-data';
import SoundFade from 'phaser3-rex-plugins/plugins/soundfade';
import { pokemonPrevolutions } from './data/pokemon-evolutions';
import PokeballTray from './ui/pokeball-tray';
2023-03-28 19:54:52 +01:00
2023-04-09 05:22:14 +01:00
const enableAuto = true;
2023-04-21 19:05:16 +01:00
const quickStart = false;
export const startingLevel = 5;
export const startingWave = 1;
export const startingBiome = Biome.TOWN;
export const startingMoney = 1000;
2023-04-09 05:22:14 +01:00
export enum Button {
UP,
DOWN,
LEFT,
RIGHT,
ACTION,
CANCEL,
CYCLE_SHINY,
CYCLE_FORM,
CYCLE_GENDER,
2023-04-26 17:50:21 +01:00
CYCLE_ABILITY,
2023-04-13 00:09:15 +01:00
QUICK_START,
AUTO,
SPEED_UP,
SLOW_DOWN
}
2023-04-28 20:03:42 +01:00
export interface PokeballCounts {
[pb: string]: integer;
}
2023-03-28 19:54:52 +01:00
export default class BattleScene extends Phaser.Scene {
2023-04-09 05:22:14 +01:00
public auto: boolean;
2023-10-21 13:58:39 +01:00
public gameVolume: number = 0.5;
2023-04-12 16:30:47 +01:00
public gameSpeed: integer = 1;
2023-04-21 19:05:16 +01:00
public quickStart: boolean = quickStart;
public finalWave: integer = 200;
2023-10-21 13:58:39 +01:00
2023-04-18 06:32:26 +01:00
public gameData: GameData;
2023-04-10 19:12:01 +01:00
private phaseQueue: BattlePhase[];
private phaseQueuePrepend: BattlePhase[];
private phaseQueuePrependSpliceIndex: integer;
2023-03-28 19:54:52 +01:00
private currentPhase: BattlePhase;
public field: Phaser.GameObjects.Container;
public fieldUI: Phaser.GameObjects.Container;
public pbTray: PokeballTray;
public pbTrayEnemy: PokeballTray;
2023-04-27 06:14:15 +01:00
public abilityBar: AbilityBar;
public partyExpBar: PartyExpBar;
public arenaBg: Phaser.GameObjects.Sprite;
public arenaBgTransition: Phaser.GameObjects.Sprite;
public arenaPlayer: ArenaBase;
public arenaPlayerTransition: ArenaBase;
public arenaEnemy: ArenaBase;
public arenaNextEnemy: ArenaBase;
public arena: Arena;
public gameMode: GameMode;
2023-03-28 19:54:52 +01:00
public trainer: Phaser.GameObjects.Sprite;
public lastEnemyTrainer: Trainer;
2023-03-31 04:02:35 +01:00
public currentBattle: Battle;
public pokeballCounts: PokeballCounts;
public money: integer;
2023-03-28 19:54:52 +01:00
private party: PlayerPokemon[];
2023-04-19 04:54:07 +01:00
private waveCountText: Phaser.GameObjects.Text;
private moneyText: Phaser.GameObjects.Text;
2023-03-28 19:54:52 +01:00
private modifierBar: ModifierBar;
2023-04-21 00:44:56 +01:00
private enemyModifierBar: ModifierBar;
2023-04-10 00:15:21 +01:00
private modifiers: PersistentModifier[];
2023-04-21 00:44:56 +01:00
private enemyModifiers: PokemonHeldItemModifier[];
2023-03-28 19:54:52 +01:00
public uiContainer: Phaser.GameObjects.Container;
public ui: UI;
public seed: string;
public waveSeed: string;
2023-06-02 23:33:51 +01:00
public spritePipeline: SpritePipeline;
2023-04-04 01:47:41 +01:00
2023-03-28 19:54:52 +01:00
private bgm: Phaser.Sound.BaseSound;
2023-04-18 06:32:26 +01:00
private bgmResumeTimer: Phaser.Time.TimerEvent;
2023-03-28 19:54:52 +01:00
private buttonKeys: Phaser.Input.Keyboard.Key[][];
2023-03-28 19:54:52 +01:00
private blockInput: boolean;
constructor() {
super('battle');
initSpecies();
initMoves();
initAbilities();
2023-04-18 06:32:26 +01:00
this.gameData = new GameData(this);
2023-03-28 19:54:52 +01:00
this.phaseQueue = [];
this.phaseQueuePrepend = [];
this.phaseQueuePrependSpliceIndex = -1;
2023-03-28 19:54:52 +01:00
}
loadImage(key: string, folder: string, filename?: string) {
if (!filename)
filename = `${key}.png`;
this.load.image(key, `images/${folder}/${filename}`);
}
loadAtlas(key: string, folder: string, filenameRoot?: string) {
if (!filenameRoot)
filenameRoot = key;
if (folder)
folder += '/';
2023-04-10 21:17:25 +01:00
this.load.atlas(key, `images/${folder}${filenameRoot}.png`, `images/${folder}/${filenameRoot}.json`);
2023-03-28 19:54:52 +01:00
}
2023-04-04 01:47:41 +01:00
loadSpritesheet(key: string, folder: string, size: integer, filename?: string) {
if (!filename)
filename = `${key}.png`;
this.load.spritesheet(key, `images/${folder}/${filename}`, { frameWidth: size, frameHeight: size });
}
2023-03-28 19:54:52 +01:00
loadSe(key: string, folder?: string, filenames?: string | string[]) {
if (!filenames)
filenames = `${key}.wav`;
if (!folder)
folder = '';
else
folder += '/';
if (!Array.isArray(filenames))
filenames = [ filenames ];
for (let f of filenames as string[]) {
this.load.audio(key, `audio/se/${folder}${f}`);
}
}
loadBgm(key: string, filename?: string) {
if (!filename)
filename = `${key}.mp3`;
this.load.audio(key, `audio/bgm/${filename}`);
}
preload() {
// Load menu images
this.loadImage('bg', 'ui');
this.loadImage('bg_command', 'ui');
this.loadImage('bg_fight', 'ui');
this.loadAtlas('prompt', 'ui');
this.loadImage('cursor', 'ui');
this.loadImage('window', 'ui');
this.loadImage('namebox', 'ui');
2023-03-28 19:54:52 +01:00
this.loadImage('pbinfo_player', 'ui');
this.loadImage('pbinfo_player_mini', 'ui');
this.loadImage('pbinfo_enemy_mini', 'ui');
2023-03-28 19:54:52 +01:00
this.loadImage('overlay_lv', 'ui');
this.loadAtlas('numbers', 'ui');
this.loadAtlas('numbers_red', 'ui');
2023-03-28 19:54:52 +01:00
this.loadAtlas('overlay_hp', 'ui');
this.loadImage('overlay_exp', 'ui');
2023-04-18 06:32:26 +01:00
this.loadImage('icon_owned', 'ui');
2023-04-27 06:14:15 +01:00
this.loadImage('ability_bar', 'ui');
this.loadImage('party_exp_bar', 'ui');
2023-07-05 16:54:36 +01:00
this.loadImage('shiny_star', 'ui', 'shiny.png');
2023-03-28 19:54:52 +01:00
this.loadImage('pb_tray_overlay_player', 'ui');
this.loadImage('pb_tray_overlay_enemy', 'ui');
this.loadAtlas('pb_tray_ball', 'ui');
2023-03-28 19:54:52 +01:00
this.loadImage('party_bg', 'ui');
this.loadImage('party_bg_double', 'ui');
2023-03-28 19:54:52 +01:00
this.loadAtlas('party_slot_main', 'ui');
this.loadAtlas('party_slot', 'ui');
this.loadImage('party_slot_overlay_lv', 'ui');
this.loadImage('party_slot_hp_bar', 'ui');
this.loadAtlas('party_slot_hp_overlay', 'ui');
this.loadAtlas('party_pb', 'ui');
this.loadImage('party_message', 'ui');
this.loadImage('party_message_large', 'ui');
this.loadImage('party_message_options', 'ui');
2023-04-21 19:05:16 +01:00
this.loadImage('party_message_options_wide', 'ui');
this.loadImage('party_options_top', 'ui');
this.loadImage('party_options_center', 'ui');
this.loadImage('party_options_bottom', 'ui');
2023-04-21 19:05:16 +01:00
this.loadImage('party_options_wide_top', 'ui');
this.loadImage('party_options_wide_center', 'ui');
this.loadImage('party_options_wide_bottom', 'ui');
2023-03-28 19:54:52 +01:00
this.loadAtlas('party_cancel', 'ui');
this.loadImage('summary_bg', 'ui');
2023-04-06 15:05:12 +01:00
this.loadImage('summary_overlay_shiny', 'ui');
this.loadImage('summary_profile', 'ui');
2023-04-23 21:36:03 +01:00
this.loadImage('summary_stats', 'ui');
this.loadImage('summary_stats_overlay_exp', 'ui');
2023-04-06 15:05:12 +01:00
this.loadImage('summary_moves', 'ui');
this.loadImage('summary_moves_effect', 'ui');
this.loadImage('summary_moves_overlay_row', 'ui');
2023-04-09 01:35:45 +01:00
this.loadImage('summary_moves_overlay_pp', 'ui');
this.loadAtlas('summary_moves_cursor', 'ui');
2023-04-23 23:40:21 +01:00
for (let t = 1; t <= 3; t++)
this.loadImage(`summary_tabs_${t}`, 'ui');
2023-04-21 19:05:16 +01:00
for (let o = 1; o <= 3; o++)
this.loadImage(`option_select_window_${o}`, 'ui');
2023-04-12 05:37:56 +01:00
2023-04-13 00:09:15 +01:00
this.loadImage('starter_select_bg', 'ui');
this.loadImage('starter_select_message', 'ui');
this.loadImage('starter_select_cursor', 'ui');
this.loadImage('starter_select_cursor_highlight', 'ui');
this.loadImage('starter_select_gen_cursor', 'ui');
this.loadImage('starter_select_gen_cursor_highlight', 'ui');
this.loadImage('default_bg', 'arenas');
2023-03-28 19:54:52 +01:00
// Load arena images
Utils.getEnumValues(Biome).map(bt => {
const btKey = Biome[bt].toLowerCase();
this.loadImage(`${btKey}_bg`, 'arenas');
this.loadImage(`${btKey}_a`, 'arenas');
this.loadImage(`${btKey}_b`, 'arenas');
if (getBiomeHasProps(bt)) {
for (let p = 1; p <= 3; p++)
this.loadImage(`${btKey}_b_${p}`, 'arenas')
}
2023-03-29 23:55:41 +01:00
});
2023-03-28 19:54:52 +01:00
// Load trainer images
this.loadImage('trainer_m', 'trainer');
this.loadAtlas('trainer_m_pb', 'trainer');
2023-10-07 21:08:33 +01:00
Utils.getEnumValues(TrainerType).map(tt => {
const config = trainerConfigs[tt];
this.loadAtlas(config.getKey(), 'trainer');
if (config.isDouble)
this.loadAtlas(config.getKey(true), 'trainer');
});
2023-03-28 19:54:52 +01:00
// Load pokemon-related images
this.loadImage(`pkmn__back__sub`, 'pokemon/back', 'sub.png');
this.loadImage(`pkmn__sub`, 'pokemon', 'sub.png');
2023-04-11 04:15:06 +01:00
this.loadAtlas('battle_stats', 'effects');
2023-04-10 21:52:27 +01:00
this.loadAtlas('shiny', 'effects');
2023-04-10 12:59:00 +01:00
this.loadImage('evo_sparkle', 'effects');
2023-06-05 02:47:43 +01:00
this.load.video('evo_bg', 'images/effects/evo_bg.mp4', true);
2023-03-28 19:54:52 +01:00
this.loadAtlas('pb', '');
this.loadAtlas('items', '');
2023-04-01 01:19:57 +01:00
this.loadAtlas('types', '');
this.loadAtlas('statuses', '');
2023-04-09 01:35:45 +01:00
this.loadAtlas('categories', '');
2023-03-28 19:54:52 +01:00
2023-04-26 21:07:29 +01:00
for (let i = 0; i < 7; i++)
2023-03-28 19:54:52 +01:00
this.loadAtlas(`pokemon_icons_${i}`, 'ui');
this.loadSe('select');
this.loadSe('menu_open');
this.loadSe('hit');
this.loadSe('hit_strong');
this.loadSe('hit_weak');
2023-04-11 04:15:06 +01:00
this.loadSe('stat_up');
this.loadSe('stat_down');
2023-03-28 19:54:52 +01:00
this.loadSe('faint');
this.loadSe('flee');
2023-04-12 17:48:02 +01:00
this.loadSe('low_hp');
2023-03-28 19:54:52 +01:00
this.loadSe('exp');
this.loadSe('level_up');
2023-04-10 18:54:06 +01:00
this.loadSe('sparkle');
2023-03-28 19:54:52 +01:00
this.loadSe('restore');
2023-04-10 18:54:06 +01:00
this.loadSe('shine');
this.loadSe('charge');
this.loadSe('beam');
2023-04-12 17:48:02 +01:00
this.loadSe('upgrade');
2023-03-28 19:54:52 +01:00
this.loadSe('error');
this.loadSe('pb_rel');
this.loadSe('pb_throw');
this.loadSe('pb_bounce_1');
this.loadSe('pb_bounce_2');
this.loadSe('pb_move');
this.loadSe('pb_catch');
this.loadSe('pb_lock');
this.loadSe('pb_tray_enter');
this.loadSe('pb_tray_ball');
this.loadSe('pb_tray_empty');
2023-04-13 00:09:15 +01:00
this.loadBgm('menu');
this.loadBgm('level_up_fanfare', 'bw/level_up_fanfare.mp3');
this.loadBgm('heal', 'bw/heal.mp3');
this.loadBgm('victory_trainer', 'bw/victory_trainer.mp3');
this.loadBgm('victory_gym', 'bw/victory_gym.mp3');
this.loadBgm('victory_champion', 'bw/victory_champion.mp3');
this.loadBgm('evolution', 'bw/evolution.mp3');
this.loadBgm('evolution_fanfare', 'bw/evolution_fanfare.mp3');
2023-04-04 01:47:41 +01:00
populateAnims();
2023-03-28 19:54:52 +01:00
}
create() {
2023-04-12 16:30:47 +01:00
initGameSpeed.apply(this);
this.setupControls();
2023-03-28 19:54:52 +01:00
this.load.setBaseURL();
2023-06-02 23:33:51 +01:00
this.spritePipeline = new SpritePipeline(this.game);
(this.renderer as Phaser.Renderer.WebGL.WebGLRenderer).pipelines.add('Sprite', this.spritePipeline);
2023-04-04 01:47:41 +01:00
this.time.delayedCall(20, () => this.launchBattle());
}
update() {
this.checkInput();
}
launchBattle() {
2023-03-28 19:54:52 +01:00
const field = this.add.container(0, 0);
field.setScale(6);
this.field = field;
const fieldUI = this.add.container(0, this.game.canvas.height);
2023-04-11 04:15:06 +01:00
fieldUI.setDepth(1);
2023-03-28 19:54:52 +01:00
fieldUI.setScale(6);
this.fieldUI = fieldUI;
const uiContainer = this.add.container(0, 0);
2023-04-11 04:15:06 +01:00
uiContainer.setDepth(2);
2023-03-28 19:54:52 +01:00
uiContainer.setScale(6);
this.uiContainer = uiContainer;
this.modifiers = [];
2023-04-21 00:44:56 +01:00
this.enemyModifiers = [];
2023-03-28 19:54:52 +01:00
this.modifierBar = new ModifierBar(this);
2023-03-29 05:31:25 +01:00
this.add.existing(this.modifierBar);
uiContainer.add(this.modifierBar);
2023-03-28 19:54:52 +01:00
2023-04-21 00:44:56 +01:00
this.enemyModifierBar = new ModifierBar(this, true);
this.add.existing(this.enemyModifierBar);
uiContainer.add(this.enemyModifierBar);
this.pbTray = new PokeballTray(this, true);
this.pbTray.setup();
this.pbTrayEnemy = new PokeballTray(this, false);
this.pbTrayEnemy.setup();
this.fieldUI.add(this.pbTray);
this.fieldUI.add(this.pbTrayEnemy);
2023-04-27 06:14:15 +01:00
this.abilityBar = new AbilityBar(this);
this.abilityBar.setup();
this.fieldUI.add(this.abilityBar);
this.partyExpBar = new PartyExpBar(this);
this.partyExpBar.setup();
this.fieldUI.add(this.partyExpBar);
this.waveCountText = addTextObject(this, (this.game.canvas.width / 6) - 2, 0, startingWave.toString(), TextStyle.BATTLE_INFO);
2023-04-19 04:54:07 +01:00
this.waveCountText.setOrigin(1, 0);
this.fieldUI.add(this.waveCountText);
this.moneyText = addTextObject(this, (this.game.canvas.width / 6) - 2, 0, startingWave.toString(), TextStyle.MONEY);
this.moneyText.setOrigin(1, 0);
this.fieldUI.add(this.moneyText);
this.updateUIPositions();
2023-03-28 19:54:52 +01:00
this.party = [];
2023-03-29 05:31:25 +01:00
let loadPokemonAssets = [];
this.quickStart = this.quickStart || this.isButtonPressed(Button.QUICK_START);
this.arenaBg = this.add.sprite(0, 0, 'plains_bg');
this.arenaBgTransition = this.add.sprite(0, 0, `plains_bg`);
this.arenaPlayer = new ArenaBase(this, true);
this.arenaPlayerTransition = new ArenaBase(this, true);
this.arenaEnemy = new ArenaBase(this, false);
this.arenaNextEnemy = new ArenaBase(this, false);
2023-04-12 05:37:56 +01:00
this.arenaBgTransition.setVisible(false);
this.arenaPlayerTransition.setVisible(false);
[ this.arenaBg, this.arenaBgTransition, this.arenaPlayer, this.arenaPlayerTransition, this.arenaEnemy, this.arenaNextEnemy ].forEach(a => {
if (a instanceof Phaser.GameObjects.Sprite)
a.setOrigin(0, 0);
2023-04-12 05:37:56 +01:00
field.add(a);
});
2023-03-28 19:54:52 +01:00
const trainerPbFrameNames = this.anims.generateFrameNames('trainer_m_pb', { zeroPad: 2, start: 1, end: 12 });
this.anims.create({
key: 'trainer_m_pb',
frames: trainerPbFrameNames,
frameRate: 16
});
const trainer = this.add.sprite(0, 0, 'trainer_m');
2023-03-28 19:54:52 +01:00
trainer.setOrigin(0.5, 1);
field.add(trainer);
this.trainer = trainer;
this.anims.create({
key: 'prompt',
frames: this.anims.generateFrameNumbers('prompt', { start: 1, end: 4 }),
frameRate: 6,
repeat: -1,
showOnStart: true
});
this.reset();
if (this.quickStart) {
for (let s = 0; s < 3; s++) {
const playerSpecies = this.randomSpecies(startingWave, startingLevel, null, false);
const playerPokemon = new PlayerPokemon(this, playerSpecies, startingLevel, 0, 0);
playerPokemon.setVisible(false);
this.party.push(playerPokemon);
loadPokemonAssets.push(playerPokemon.loadAssets());
}
}
2023-03-28 19:54:52 +01:00
const ui = new UI(this);
this.uiContainer.add(ui);
this.ui = ui;
ui.setup();
2023-04-19 23:19:55 +01:00
Promise.all([
Promise.all(loadPokemonAssets),
initCommonAnims().then(() => loadCommonAnimAssets(this, true)),
initMoveAnim(Moves.STRUGGLE).then(() => loadMoveAnimAssets(this, [ Moves.STRUGGLE ], true))
]).then(() => {
2023-04-09 05:22:14 +01:00
if (enableAuto)
initAutoPlay.apply(this);
2023-03-29 05:31:25 +01:00
2023-03-31 21:04:39 +01:00
this.newBattle();
2023-03-29 05:31:25 +01:00
this.shiftPhase();
});
2023-03-28 19:54:52 +01:00
}
setupControls() {
const keyCodes = Phaser.Input.Keyboard.KeyCodes;
const keyConfig = {
[Button.UP]: [keyCodes.UP, keyCodes.W],
[Button.DOWN]: [keyCodes.DOWN, keyCodes.S],
[Button.LEFT]: [keyCodes.LEFT, keyCodes.A],
[Button.RIGHT]: [keyCodes.RIGHT, keyCodes.D],
[Button.ACTION]: [keyCodes.ENTER, keyCodes.SPACE, keyCodes.Z],
[Button.CANCEL]: [keyCodes.BACKSPACE, keyCodes.ESC, keyCodes.X],
[Button.CYCLE_SHINY]: [keyCodes.R],
[Button.CYCLE_FORM]: [keyCodes.F],
[Button.CYCLE_GENDER]: [keyCodes.G],
2023-04-26 17:50:21 +01:00
[Button.CYCLE_ABILITY]: [keyCodes.E],
2023-04-13 00:09:15 +01:00
[Button.QUICK_START]: [keyCodes.Q],
[Button.AUTO]: [keyCodes.F2],
[Button.SPEED_UP]: [keyCodes.PLUS],
[Button.SLOW_DOWN]: [keyCodes.MINUS]
};
this.buttonKeys = [];
for (let b of Utils.getEnumValues(Button)) {
const keys: Phaser.Input.Keyboard.Key[] = [];
if (keyConfig.hasOwnProperty(b)) {
for (let k of keyConfig[b])
keys.push(this.input.keyboard.addKey(k));
}
this.buttonKeys[b] = keys;
}
2023-03-28 19:54:52 +01:00
}
getParty(): PlayerPokemon[] {
return this.party;
}
getPlayerPokemon(): PlayerPokemon {
return this.getPlayerField().find(p => p.isActive());
2023-04-21 00:44:56 +01:00
}
getPlayerField(): PlayerPokemon[] {
const party = this.getParty();
return party.slice(0, Math.min(party.length, this.currentBattle?.double ? 2 : 1));
2023-03-28 19:54:52 +01:00
}
2023-10-07 21:08:33 +01:00
getEnemyParty(): EnemyPokemon[] {
return this.currentBattle?.enemyParty || [];
}
2023-03-28 19:54:52 +01:00
getEnemyPokemon(): EnemyPokemon {
return this.getEnemyField().find(p => p.isActive());
}
getEnemyField(): EnemyPokemon[] {
2023-10-07 21:08:33 +01:00
const party = this.getEnemyParty();
return party.slice(0, Math.min(party.length, this.currentBattle?.double ? 2 : 1));
}
getField(): Pokemon[] {
const ret = new Array(4).fill(null);
const playerField = this.getPlayerField();
const enemyField = this.getEnemyField();
ret.splice(0, playerField.length, ...playerField);
ret.splice(2, enemyField.length, ...enemyField);
return ret;
}
2023-04-23 03:14:53 +01:00
getPokemonById(pokemonId: integer): Pokemon {
const findInParty = (party: Pokemon[]) => party.find(p => p.id === pokemonId);
return findInParty(this.getParty()) || findInParty(this.getEnemyField());
2023-04-23 03:14:53 +01:00
}
reset(): void {
2023-10-20 03:19:14 +01:00
this.seed = Utils.randomString(16);
console.log('Seed:', this.seed);
this.money = startingMoney;
this.pokeballCounts = Object.fromEntries(Utils.getEnumValues(PokeballType).filter(p => p <= PokeballType.MASTER_BALL).map(t => [ t, 0 ]));
2023-04-25 03:32:12 +01:00
this.pokeballCounts[PokeballType.POKEBALL] += 5;
this.modifiers = [];
this.enemyModifiers = [];
this.modifierBar.removeAll(true);
this.enemyModifierBar.removeAll(true);
for (let p of this.getParty())
p.destroy();
this.party = [];
for (let p of this.getEnemyField())
p.destroy();
this.currentBattle = null;
this.waveCountText.setText(startingWave.toString());
2023-04-28 20:03:42 +01:00
this.waveCountText.setVisible(false);
this.updateMoneyText();
this.moneyText.setVisible(false);
2023-04-28 20:03:42 +01:00
this.newArena(startingBiome, true);
this.arenaBgTransition.setPosition(0, 0);
this.arenaPlayer.setPosition(300, 0);
2023-04-30 16:38:46 +01:00
this.arenaPlayerTransition.setPosition(0, 0);
[ this.arenaEnemy, this.arenaNextEnemy ].forEach(a => a.setPosition(-280, 0));
this.trainer.setTexture('trainer_m');
this.trainer.setPosition(406, 132);
2023-03-28 19:54:52 +01:00
}
2023-10-07 21:08:33 +01:00
newBattle(waveIndex?: integer, battleType?: BattleType, trainerData?: TrainerData, double?: boolean): Battle {
let newWaveIndex = waveIndex || ((this.currentBattle?.waveIndex || (startingWave - 1)) + 1);
let newDouble: boolean;
2023-10-07 21:08:33 +01:00
let newBattleType: BattleType;
let newTrainer: Trainer;
let battleConfig: FixedBattleConfig = null;
2023-10-07 21:08:33 +01:00
this.resetSeed(newWaveIndex);
if (fixedBattles.hasOwnProperty(newWaveIndex)) {
battleConfig = fixedBattles[newWaveIndex];
newDouble = battleConfig.double;
newBattleType = battleConfig.battleType;
this.executeWithSeedOffset(() => newTrainer = battleConfig.getTrainer(this), newWaveIndex);
if (newTrainer)
this.field.add(newTrainer);
} else {
if (battleType === undefined) {
if (newWaveIndex > 20 && !(newWaveIndex % 30))
newBattleType = BattleType.TRAINER;
else if (newWaveIndex % 10 !== 1 && newWaveIndex % 10) {
const trainerChance = this.arena.getTrainerChance();
newBattleType = trainerChance && !Utils.randSeedInt(trainerChance) ? BattleType.TRAINER : BattleType.WILD;
} else
newBattleType = BattleType.WILD;
} else
newBattleType = battleType;
if (newBattleType === BattleType.TRAINER) {
newTrainer = trainerData !== undefined ? trainerData.toTrainer(this) : new Trainer(this, this.arena.randomTrainerType(newWaveIndex), !!Utils.randSeedInt(2));
this.field.add(newTrainer);
}
2023-10-07 21:08:33 +01:00
}
const playerField = this.getPlayerField();
if (double === undefined && newWaveIndex > 1) {
2023-10-07 21:08:33 +01:00
if (newBattleType === BattleType.WILD) {
const doubleChance = new Utils.IntegerHolder(newWaveIndex % 10 === 0 ? 32 : 8);
this.applyModifiers(DoubleBattleChanceBoosterModifier, true, doubleChance);
playerField.forEach(p => applyAbAttrs(DoubleBattleChanceAbAttr, p, null, doubleChance));
newDouble = !Utils.randSeedInt(doubleChance.value);
} else if (newBattleType === BattleType.TRAINER)
2023-10-07 21:08:33 +01:00
newDouble = newTrainer.config.isDouble;
} else if (!battleConfig)
newDouble = !!double;
const lastBattle = this.currentBattle;
const maxExpLevel = this.getMaxExpLevel();
this.lastEnemyTrainer = lastBattle?.trainer ?? null;
2023-10-07 21:08:33 +01:00
this.currentBattle = new Battle(newWaveIndex, newBattleType, newTrainer, newDouble);
this.currentBattle.incrementTurn(this);
//this.pushPhase(new TestMessagePhase(this, trainerConfigs[TrainerType.RIVAL].encounterMessages[0]))
2023-04-28 20:03:42 +01:00
if (!waveIndex) {
const isNewBiome = !lastBattle || !(lastBattle.waveIndex % 10);
const showTrainer = isNewBiome || this.currentBattle.battleType === BattleType.TRAINER;
const availablePartyMemberCount = this.getParty().filter(p => !p.isFainted()).length;
if (lastBattle) {
this.getEnemyField().forEach(enemyPokemon => enemyPokemon.destroy());
if (showTrainer) {
playerField.forEach((_, p) => this.unshiftPhase(new ReturnPhase(this, p)));
this.unshiftPhase(new ShowTrainerPhase(this));
}
if (this.gameMode === GameMode.CLASSIC && !isNewBiome)
2023-04-28 20:03:42 +01:00
this.pushPhase(new NextEncounterPhase(this));
else {
this.pushPhase(new SelectBiomePhase(this));
this.pushPhase(new NewBiomeEncounterPhase(this));
const newMaxExpLevel = this.getMaxExpLevel();
if (newMaxExpLevel > maxExpLevel)
this.pushPhase(new LevelCapPhase(this));
2023-04-28 20:03:42 +01:00
}
if (showTrainer) {
this.pushPhase(new SummonPhase(this, 0));
if (this.currentBattle.double && availablePartyMemberCount > 1)
this.pushPhase(new SummonPhase(this, 1));
}
2023-04-28 20:03:42 +01:00
} else {
if (!this.quickStart)
this.pushPhase(new CheckLoadPhase(this));
else {
this.pushPhase(new EncounterPhase(this));
this.pushPhase(new SummonPhase(this, 0));
2023-04-28 20:03:42 +01:00
}
2023-03-31 21:04:39 +01:00
}
if (!showTrainer && (lastBattle?.double || false) !== newDouble) {
if (newDouble) {
if (availablePartyMemberCount > 1) {
this.pushPhase(new ToggleDoublePositionPhase(this, true));
this.pushPhase(new SummonPhase(this, 1));
}
} else {
if (availablePartyMemberCount > 1)
this.pushPhase(new ReturnPhase(this, 1));
this.pushPhase(new ToggleDoublePositionPhase(this, false));
}
}
if (lastBattle && this.currentBattle.battleType !== BattleType.TRAINER) {
this.pushPhase(new CheckSwitchPhase(this, 0, newDouble));
if (newDouble)
this.pushPhase(new CheckSwitchPhase(this, 1, newDouble));
}
}
2023-04-19 04:54:07 +01:00
2023-03-31 04:02:35 +01:00
return this.currentBattle;
2023-03-28 19:54:52 +01:00
}
2023-04-28 20:03:42 +01:00
newArena(biome: Biome, init?: boolean): Arena {
this.arena = new Arena(this, biome, Biome[biome].toLowerCase());
2023-04-28 20:03:42 +01:00
if (init) {
const biomeKey = getBiomeKey(biome);
2023-04-28 20:03:42 +01:00
this.arenaBg.setTexture(`${biomeKey}_bg`);
this.arenaBgTransition.setTexture(`${biomeKey}_bg`);
this.arenaPlayer.setBiome(biome);
this.arenaPlayerTransition.setBiome(biome);
this.arenaEnemy.setBiome(biome);
this.arenaNextEnemy.setBiome(biome);
2023-04-28 20:03:42 +01:00
}
2023-03-31 21:04:39 +01:00
return this.arena;
2023-03-28 19:54:52 +01:00
}
resetSeed(waveIndex?: integer): void {
this.waveSeed = Utils.shiftCharCodes(this.seed, waveIndex || this.currentBattle.waveIndex);
Phaser.Math.RND.sow([ this.waveSeed ]);
}
executeWithSeedOffset(func: Function, offset: integer): void {
if (!func)
return;
const state = Phaser.Math.RND.state();
Phaser.Math.RND.sow([ Utils.shiftCharCodes(this.seed, offset) ]);
func();
Phaser.Math.RND.state(state);
}
2023-04-21 00:44:56 +01:00
updateWaveCountText(): void {
2023-04-19 04:54:07 +01:00
const isBoss = !(this.currentBattle.waveIndex % 10);
this.waveCountText.setText(this.currentBattle.waveIndex.toString());
this.waveCountText.setColor(!isBoss ? '#404040' : '#f89890');
this.waveCountText.setShadowColor(!isBoss ? '#ded6b5' : '#984038');
2023-04-28 20:03:42 +01:00
this.waveCountText.setVisible(true);
2023-04-19 04:54:07 +01:00
}
updateMoneyText(): void {
this.moneyText.setText(`${this.money.toLocaleString('en-US')}`);
this.moneyText.setVisible(true);
}
updateUIPositions(): void {
2023-04-21 00:44:56 +01:00
this.waveCountText.setY(-(this.game.canvas.height / 6) + (this.enemyModifiers.length ? 15 : 0));
this.moneyText.setY(this.waveCountText.y + 10);
this.partyExpBar.setY(this.moneyText.y + 15);
2023-04-21 00:44:56 +01:00
}
getMaxExpLevel(): integer {
const lastWaveIndex = Math.ceil((this.currentBattle?.waveIndex || 1) / 10) * 10;
const baseLevel = (1 + lastWaveIndex / 2 + Math.pow(lastWaveIndex / 25, 2)) * 1.2;
return Math.min(Math.ceil(baseLevel / 2) * 2 + 2, 10000);
}
randomSpecies(waveIndex: integer, level: integer, speciesFilter?: PokemonSpeciesFilter, fromArenaPool?: boolean): PokemonSpecies {
if (fromArenaPool)
return this.arena.randomSpecies(waveIndex, level);
const filteredSpecies = speciesFilter ? [...new Set(allSpecies.slice(0, -1).filter(speciesFilter).map(s => {
while (pokemonPrevolutions.hasOwnProperty(s.speciesId))
s = getPokemonSpecies(pokemonPrevolutions[s.speciesId]);
return s;
}))] : allSpecies.slice(0, -1);
return getPokemonSpecies(filteredSpecies[Utils.randSeedInt(filteredSpecies.length)].getSpeciesForLevel(level, true));
2023-03-28 19:54:52 +01:00
}
checkInput(): boolean {
if (this.blockInput)
return;
if (this.isButtonPressed(Button.UP))
this.ui.processInput(Button.UP);
else if (this.isButtonPressed(Button.DOWN))
this.ui.processInput(Button.DOWN);
else if (this.isButtonPressed(Button.LEFT))
this.ui.processInput(Button.LEFT);
else if (this.isButtonPressed(Button.RIGHT))
this.ui.processInput(Button.RIGHT);
else if (this.isButtonPressed(Button.ACTION))
this.ui.processInput(Button.ACTION);
else if (this.isButtonPressed(Button.CANCEL))
this.ui.processInput(Button.CANCEL);
else if (this.ui?.getHandler() instanceof StarterSelectUiHandler) {
if (this.isButtonPressed(Button.CYCLE_SHINY))
this.ui.processInput(Button.CYCLE_SHINY);
else if (this.isButtonPressed(Button.CYCLE_FORM))
this.ui.processInput(Button.CYCLE_FORM);
else if (this.isButtonPressed(Button.CYCLE_GENDER))
this.ui.processInput(Button.CYCLE_GENDER);
2023-04-26 17:50:21 +01:00
else if (this.isButtonPressed(Button.CYCLE_ABILITY))
this.ui.processInput(Button.CYCLE_ABILITY);
else
return;
}
2023-04-12 16:30:47 +01:00
else if (this.isButtonPressed(Button.SPEED_UP)) {
if (!this.auto) {
if (this.gameSpeed < 2.5)
2023-04-12 16:30:47 +01:00
this.gameSpeed += 0.25;
} else if (this.gameSpeed < 20)
this.gameSpeed++;
} else if (this.isButtonPressed(Button.SLOW_DOWN)) {
if (this.gameSpeed > 1) {
if (!this.auto)
this.gameSpeed -= 0.25;
else
this.gameSpeed--;
2023-04-09 05:22:14 +01:00
}
2023-04-12 16:30:47 +01:00
} else if (enableAuto) {
if (this.isButtonPressed(Button.AUTO)) {
this.auto = !this.auto;
if (this.auto)
this.gameSpeed = Math.floor(this.gameSpeed);
else if (this.gameSpeed > 2.5)
this.gameSpeed = 2.5;
2023-04-12 16:30:47 +01:00
} else
return;
2023-04-09 05:22:14 +01:00
} else
2023-03-28 19:54:52 +01:00
return;
this.blockInput = true;
2023-04-12 16:30:47 +01:00
this.time.delayedCall(new Utils.FixedInt(250) as unknown as integer, () => this.blockInput = false);
2023-03-28 19:54:52 +01:00
}
isButtonPressed(button: Button): boolean {
return this.buttonKeys[button].filter(k => k.isDown).length >= 1;
}
isBgmPlaying(): boolean {
return this.bgm && this.bgm.isPlaying;
}
playBgm(bgmName?: string, fadeOut?: boolean): void {
if (bgmName === undefined)
bgmName = this.currentBattle.getBgmOverride() || this.arena.bgm;
if (this.bgm && bgmName === this.bgm.key) {
if (!this.bgm.isPlaying) {
this.bgm.play({
2023-10-21 13:58:39 +01:00
volume: this.gameVolume
});
}
2023-04-10 18:54:06 +01:00
return;
}
if (fadeOut && !this.bgm)
fadeOut = false;
this.loadBgm(bgmName);
let loopPoint = 0;
loopPoint = bgmName === this.arena.bgm
? this.arena.getBgmLoopPoint()
: this.getBgmLoopPoint(bgmName);
let loaded = false;
const playNewBgm = () => {
if (bgmName === null && this.bgm && !this.bgm.pendingRemove) {
this.bgm.play({
2023-10-21 13:58:39 +01:00
volume: this.gameVolume
});
return;
}
if (this.bgm && !this.bgm.pendingRemove && this.bgm.isPlaying)
this.bgm.stop();
this.bgm = this.sound.add(bgmName, { loop: true });
2023-10-21 13:58:39 +01:00
this.bgm.play({
volume: this.gameVolume
});
if (loopPoint)
this.bgm.on('looped', () => this.bgm.play({ seek: loopPoint }));
};
this.load.once(Phaser.Loader.Events.COMPLETE, () => {
loaded = true;
if (!fadeOut || !this.bgm.isPlaying)
playNewBgm();
});
if (fadeOut) {
const onBgmFaded = () => {
if (loaded && (!this.bgm.isPlaying || this.bgm.pendingRemove))
playNewBgm();
};
this.time.delayedCall(this.fadeOutBgm(500, true) ? 750 : 250, onBgmFaded);
}
if (!this.load.isLoading())
this.load.start();
2023-03-28 19:54:52 +01:00
}
pauseBgm(): void {
if (this.bgm && this.bgm.isPlaying)
2023-03-28 19:54:52 +01:00
this.bgm.pause();
}
resumeBgm(): void {
if (this.bgm && this.bgm.isPaused)
this.bgm.resume();
}
fadeOutBgm(duration?: integer, destroy?: boolean): boolean {
if (!this.bgm)
return;
if (!duration)
duration = 500;
if (destroy === undefined)
destroy = true;
const bgm = this.sound.get(this.bgm.key);
if (bgm) {
SoundFade.fadeOut(this, bgm, duration, destroy);
return true;
}
return false;
2023-04-10 18:54:06 +01:00
}
2023-10-21 13:58:39 +01:00
playSound(soundName: string, config?: object) {
if (config) {
if (config.hasOwnProperty('volume'))
config['volume'] *= this.gameVolume;
else
config['volume'] = this.gameVolume;
} else
config = { volume: this.gameVolume };
this.sound.play(soundName, config);
}
2023-04-18 06:32:26 +01:00
playSoundWithoutBgm(soundName: string, pauseDuration?: integer): void {
this.pauseBgm();
2023-10-21 13:58:39 +01:00
this.playSound(soundName);
2023-04-18 06:32:26 +01:00
const sound = this.sound.get(soundName);
if (this.bgmResumeTimer)
this.bgmResumeTimer.destroy();
this.bgmResumeTimer = this.time.delayedCall((pauseDuration || (sound.totalDuration * 1000)), () => {
this.resumeBgm();
this.bgmResumeTimer = null;
});
}
getBgmLoopPoint(bgmName: string): number {
switch (bgmName) {
case 'battle_champion':
return 27.653;
case 'battle_cynthia':
return 12.235;
case 'battle_elite':
return 17.730;
case 'battle_final':
return 16.453;
case 'battle_gym':
return 19.145;
case 'battle_legendary':
return 13.855;
case 'battle_legendary_k':
return 18.314;
case 'battle_legendary_rz':
return 18.329;
case 'battle_rival':
return 13.689;
case 'battle_rival_2':
return 17.714;
case 'battle_rival_3':
return 17.586;
case 'battle_trainer':
return 13.686;
case 'battle_wild':
return 12.703;
case 'battle_wild_strong':
return 13.940;
}
return 0;
}
2023-03-28 19:54:52 +01:00
getCurrentPhase(): BattlePhase {
return this.currentPhase;
}
pushPhase(phase: BattlePhase): void {
this.phaseQueue.push(phase);
}
unshiftPhase(phase: BattlePhase): void {
if (this.phaseQueuePrependSpliceIndex === -1)
this.phaseQueuePrepend.push(phase);
else
this.phaseQueuePrepend.splice(this.phaseQueuePrependSpliceIndex, 0, phase);
2023-03-28 19:54:52 +01:00
}
clearPhaseQueue(): void {
this.phaseQueue.splice(0, this.phaseQueue.length);
}
setPhaseQueueSplice(): void {
this.phaseQueuePrependSpliceIndex = this.phaseQueuePrepend.length;
}
clearPhaseQueueSplice(): void {
this.phaseQueuePrependSpliceIndex = -1;
}
2023-03-28 19:54:52 +01:00
shiftPhase(): void {
if (this.phaseQueuePrependSpliceIndex > -1)
this.clearPhaseQueueSplice();
2023-03-28 19:54:52 +01:00
if (this.phaseQueuePrepend.length) {
while (this.phaseQueuePrepend.length)
this.phaseQueue.unshift(this.phaseQueuePrepend.pop());
}
if (!this.phaseQueue.length)
this.populatePhaseQueue();
this.currentPhase = this.phaseQueue.shift();
this.currentPhase.start();
}
2023-05-07 22:05:19 +01:00
queueMessage(message: string, callbackDelay?: integer, prompt?: boolean, promptDelay?: integer) {
this.unshiftPhase(new MessagePhase(this, message, callbackDelay, prompt, promptDelay));
2023-04-22 00:30:04 +01:00
}
2023-03-28 19:54:52 +01:00
populatePhaseQueue(): void {
this.phaseQueue.push(new TurnInitPhase(this));
2023-03-28 19:54:52 +01:00
}
2023-04-21 00:44:56 +01:00
addModifier(modifier: Modifier, playSound?: boolean, virtual?: boolean): Promise<void> {
2023-04-04 23:28:21 +01:00
return new Promise(resolve => {
const soundName = modifier.type.soundName;
2023-04-10 00:15:21 +01:00
if (modifier instanceof PersistentModifier) {
if ((modifier as PersistentModifier).add(this.modifiers, !!virtual)) {
2023-04-21 00:44:56 +01:00
if (playSound && !this.sound.get(soundName))
2023-10-21 13:58:39 +01:00
this.playSound(soundName);
2023-04-20 20:46:05 +01:00
} else if (!virtual) {
const defaultModifierType = getDefaultModifierTypeForTier(modifier.type.tier);
2023-04-21 00:44:56 +01:00
this.addModifier(defaultModifierType.newModifier(), playSound).then(() => resolve());
2023-04-22 00:30:04 +01:00
this.queueMessage(`The stack for this item is full.\n You will receive ${defaultModifierType.name} instead.`, null, true);
return;
}
2023-03-28 19:54:52 +01:00
2023-04-10 00:15:21 +01:00
if (!virtual)
this.updateModifiers().then(() => resolve());
} else if (modifier instanceof ConsumableModifier) {
2023-04-21 00:44:56 +01:00
if (playSound && !this.sound.get(soundName))
2023-10-21 13:58:39 +01:00
this.playSound(soundName);
2023-03-28 19:54:52 +01:00
2023-04-10 00:15:21 +01:00
if (modifier instanceof ConsumablePokemonModifier) {
for (let p in this.party) {
const pokemon = this.party[p];
2023-04-04 23:28:21 +01:00
2023-04-10 00:15:21 +01:00
const args: any[] = [ pokemon ];
if (modifier instanceof PokemonHpRestoreModifier) {
2023-04-20 20:46:05 +01:00
if (!(modifier as PokemonHpRestoreModifier).fainted) {
const hpRestoreMultiplier = new Utils.IntegerHolder(1);
2023-04-21 00:44:56 +01:00
this.applyModifiers(HealingBoosterModifier, true, hpRestoreMultiplier);
2023-04-20 20:46:05 +01:00
args.push(hpRestoreMultiplier.value);
} else
args.push(1);
2023-04-10 00:15:21 +01:00
}
2023-04-11 16:04:39 +01:00
2023-04-04 23:28:21 +01:00
if (modifier.shouldApply(args))
modifier.apply(args);
}
2023-04-10 00:15:21 +01:00
Promise.allSettled(this.party.map(p => p.updateInfo())).then(() => resolve());
} else {
const args = [ this ];
if (modifier.shouldApply(args))
modifier.apply(args);
resolve();
}
}
});
}
2023-03-28 19:54:52 +01:00
2023-04-23 15:24:22 +01:00
addEnemyModifier(itemModifier: PokemonHeldItemModifier): Promise<void> {
return new Promise(resolve => {
itemModifier.add(this.enemyModifiers, false);
this.updateModifiers(false).then(() => resolve());
});
}
tryTransferHeldItemModifier(itemModifier: PokemonHeldItemModifier, target: Pokemon, transferStack: boolean, playSound: boolean): Promise<boolean> {
2023-04-21 20:45:48 +01:00
return new Promise(resolve => {
2023-04-23 15:24:22 +01:00
const source = itemModifier.getPokemon(target.scene);
2023-05-04 19:06:31 +01:00
const cancelled = new Utils.BooleanHolder(false);
applyAbAttrs(BlockItemTheftAbAttr, target, cancelled);
if (cancelled.value) {
resolve(false);
return;
}
const newItemModifier = itemModifier.clone() as PokemonHeldItemModifier;
2023-04-21 20:45:48 +01:00
newItemModifier.pokemonId = target.id;
const matchingModifier = target.scene.findModifier(m => m instanceof PokemonHeldItemModifier
2023-04-23 15:24:22 +01:00
&& (m as PokemonHeldItemModifier).matchType(itemModifier), target.isPlayer()) as PokemonHeldItemModifier;
2023-04-21 20:45:48 +01:00
let removeOld = true;
if (matchingModifier) {
const maxStackCount = matchingModifier.getMaxStackCount();
if (matchingModifier.stackCount >= maxStackCount) {
resolve(false);
return;
}
2023-04-23 15:24:22 +01:00
const newStackCount = matchingModifier.stackCount + (transferStack ? itemModifier.stackCount : 1);
2023-04-21 20:45:48 +01:00
if (newStackCount > maxStackCount) {
itemModifier.stackCount = newStackCount - maxStackCount;
newItemModifier.stackCount = maxStackCount;
removeOld = !itemModifier.stackCount;
}
2023-04-23 15:24:22 +01:00
} else if (!transferStack)
removeOld = !(--itemModifier.stackCount);
if (!removeOld || this.removeModifier(itemModifier, !source.isPlayer())) {
const addModifier = () => {
if (target.isPlayer())
this.addModifier(newItemModifier, playSound).then(() => resolve(true));
else
this.addEnemyModifier(newItemModifier).then(() => resolve(true));
};
if (source.isPlayer() !== target.isPlayer())
this.updateModifiers(source.isPlayer()).then(() => addModifier());
else
addModifier();
2023-04-21 20:45:48 +01:00
return;
}
resolve(false);
});
}
2023-04-11 14:41:11 +01:00
removePartyMemberModifiers(partyMemberIndex: integer): Promise<void> {
2023-04-10 00:15:21 +01:00
return new Promise(resolve => {
2023-04-11 14:41:11 +01:00
const pokemonId = this.getParty()[partyMemberIndex].id;
const modifiersToRemove = this.modifiers.filter(m => (m instanceof PokemonHeldItemModifier) && (m as PokemonHeldItemModifier).pokemonId === pokemonId);
for (let m of modifiersToRemove)
this.modifiers.splice(this.modifiers.indexOf(m), 1);
this.updateModifiers().then(() => resolve());
2023-04-10 00:15:21 +01:00
});
}
2023-04-04 23:28:21 +01:00
2023-04-21 00:44:56 +01:00
generateEnemyModifiers(): Promise<void> {
2023-04-10 00:15:21 +01:00
return new Promise(resolve => {
2023-04-21 00:44:56 +01:00
const waveIndex = this.currentBattle.waveIndex;
2023-04-21 03:26:38 +01:00
const chances = Math.ceil(waveIndex / 10);
2023-04-23 15:42:00 +01:00
const isBoss = !(waveIndex % 10);
2023-04-21 00:44:56 +01:00
let count = 0;
for (let c = 0; c < chances; c++) {
2023-04-21 03:26:38 +01:00
if (!Utils.randInt(!isBoss ? 12 : 4))
2023-04-21 00:44:56 +01:00
count++;
if (count === 12)
break;
}
if (isBoss)
2023-04-21 03:26:38 +01:00
count = Math.max(count, Math.floor(chances / 2));
const enemyField = this.getEnemyField();
getEnemyModifierTypesForWave(waveIndex, count, this.getEnemyField())
.map(mt => mt.newModifier(enemyField[Utils.randInt(enemyField.length)]).add(this.enemyModifiers, false));
2023-04-21 00:44:56 +01:00
this.updateModifiers(false).then(() => resolve());
});
}
clearEnemyModifiers(): void {
this.enemyModifiers.splice(0, this.enemyModifiers.length);
this.updateModifiers(false).then(() => this.updateUIPositions());
2023-04-21 00:44:56 +01:00
}
updateModifiers(player?: boolean): Promise<void> {
if (player === undefined)
player = true;
return new Promise(resolve => {
2023-04-24 05:38:28 +01:00
const modifiers = player ? this.modifiers : this.enemyModifiers as PersistentModifier[];
for (let m = 0; m < modifiers.length; m++) {
const modifier = modifiers[m];
if (modifier instanceof PokemonHeldItemModifier && !this.getPokemonById((modifier as PokemonHeldItemModifier).pokemonId))
modifiers.splice(m--, 1);
}
2023-04-21 00:44:56 +01:00
for (let modifier of modifiers) {
2023-04-10 00:15:21 +01:00
if (modifier instanceof PersistentModifier)
(modifier as PersistentModifier).virtualStackCount = 0;
}
2023-04-21 00:44:56 +01:00
const modifiersClone = modifiers.slice(0);
for (let modifier of modifiersClone) {
2023-04-10 00:15:21 +01:00
if (!modifier.getStackCount())
2023-04-21 00:44:56 +01:00
modifiers.splice(modifiers.indexOf(modifier), 1);
2023-03-28 19:54:52 +01:00
}
2023-04-04 23:28:21 +01:00
this.updatePartyForModifiers(player ? this.getParty() : this.getEnemyField().filter(p => p.isActive())).then(() => {
2023-04-21 00:44:56 +01:00
(player ? this.modifierBar : this.enemyModifierBar).updateModifiers(modifiers);
if (!player)
this.updateUIPositions();
2023-04-04 23:28:21 +01:00
resolve();
2023-04-10 00:15:21 +01:00
});
2023-04-04 23:28:21 +01:00
});
2023-03-28 19:54:52 +01:00
}
2023-04-21 00:44:56 +01:00
updatePartyForModifiers(party: Pokemon[]): Promise<void> {
2023-04-11 14:41:11 +01:00
return new Promise(resolve => {
2023-04-21 00:44:56 +01:00
Promise.allSettled(party.map(p => {
2023-04-11 14:41:11 +01:00
p.calculateStats();
return p.updateInfo();
})).then(() => resolve());
});
}
2023-04-21 00:44:56 +01:00
removeModifier(modifier: PersistentModifier, enemy?: boolean): boolean {
const modifiers = !enemy ? this.modifiers : this.enemyModifiers;
const modifierIndex = modifiers.indexOf(modifier);
if (modifierIndex > -1) {
2023-04-21 00:44:56 +01:00
modifiers.splice(modifierIndex, 1);
return true;
}
return false;
}
2023-04-28 20:03:42 +01:00
getModifiers(modifierType: { new(...args: any[]): Modifier }, player?: boolean): PersistentModifier[] {
2023-04-21 00:44:56 +01:00
if (player === undefined)
player = true;
return (player ? this.modifiers : this.enemyModifiers).filter(m => m instanceof modifierType);
}
2023-04-28 20:03:42 +01:00
findModifiers(modifierFilter: ModifierPredicate, player?: boolean): PersistentModifier[] {
2023-04-21 00:44:56 +01:00
if (player === undefined)
player = true;
return (player ? this.modifiers : this.enemyModifiers).filter(m => (modifierFilter as ModifierPredicate)(m));
2023-03-31 04:02:35 +01:00
}
2023-04-28 20:03:42 +01:00
findModifier(modifierFilter: ModifierPredicate, player?: boolean): PersistentModifier {
2023-04-21 00:44:56 +01:00
if (player === undefined)
player = true;
return (player ? this.modifiers : this.enemyModifiers).find(m => (modifierFilter as ModifierPredicate)(m));
2023-04-14 23:21:33 +01:00
}
2023-04-21 00:44:56 +01:00
applyModifiers(modifierType: { new(...args: any[]): Modifier }, player?: boolean, ...args: any[]): void {
if (player === undefined)
player = true;
const modifiers = (player ? this.modifiers : this.enemyModifiers).filter(m => m instanceof modifierType && m.shouldApply(args));
2023-03-28 19:54:52 +01:00
for (let modifier of modifiers) {
if (modifier.apply(args))
2023-04-21 00:44:56 +01:00
console.log('Applied', modifier.type.name, !player ? '(enemy)' : '');
2023-03-28 19:54:52 +01:00
}
}
2023-04-20 20:46:05 +01:00
2023-04-21 00:44:56 +01:00
applyModifier(modifierType: { new(...args: any[]): Modifier }, player?: boolean, ...args: any[]): PersistentModifier {
if (player === undefined)
player = true;
const modifiers = (player ? this.modifiers : this.enemyModifiers).filter(m => m instanceof modifierType && m.shouldApply(args));
2023-04-20 20:46:05 +01:00
for (let modifier of modifiers) {
if (modifier.apply(args)) {
2023-04-21 00:44:56 +01:00
console.log('Applied', modifier.type.name, !player ? '(enemy)' : '');
2023-04-20 20:46:05 +01:00
return modifier;
}
}
return null;
}
2023-03-28 19:54:52 +01:00
}