Implement Double Battles (#1)

* Add WiP logic for double battles

* Minor changes for double battles

* More fixes for double battles

* Show battle info for both in double battles

* Improvements to double battles

* Add double battle version of party UI

* Fix some issues with double battles

* Updates to double battles

* More work on double battles for stability

* Fix issues with ability bar and evolution screen

* Add chance for double battles
This commit is contained in:
Samuel H authored and GitHub committed 2023-05-18 11:11:06 -04:00
1 parent e2d6890072
commit b9f7ba173d
31 files changed
+1583 -702

No files matched your search

+5 -1
View File
@@ -4,6 +4,7 @@ import { TextStyle, addTextObject } from "./text";
const hiddenX = -91;
const shownX = 10;
const baseY = -116;
export default class AbilityBar extends Phaser.GameObjects.Container {
private bg: Phaser.GameObjects.Image;
@@ -15,7 +16,7 @@ export default class AbilityBar extends Phaser.GameObjects.Container {
public shown: boolean;
constructor(scene: BattleScene) {
super(scene, hiddenX, (-scene.game.canvas.height / 6) + 64);
super(scene, hiddenX, baseY);
}
setup(): void {
@@ -43,9 +44,12 @@ export default class AbilityBar extends Phaser.GameObjects.Container {
if (this.shown)
return;
(this.scene as BattleScene).fieldUI.bringToTop(this);
if (this.tween)
this.tween.stop();
this.y = baseY + ((this.scene as BattleScene).currentBattle.double ? 14 : 0);
this.tween = this.scene.tweens.add({
targets: this,
x: shownX,
+3 -3
View File
@@ -60,12 +60,12 @@ export default class BallUiHandler extends UiHandler {
let success = false;
const pokeballTypeCount = Object.keys(this.scene.pokeballCounts).length;
const pokeballTypeCount = Object.keys(this.scene.currentBattle.turnPokeballCounts).length;
if (button === Button.ACTION || button === Button.CANCEL) {
success = true;
if (button === Button.ACTION && this.cursor < pokeballTypeCount) {
if (this.scene.pokeballCounts[this.cursor]) {
if (this.scene.currentBattle.turnPokeballCounts[this.cursor]) {
if ((this.scene.getCurrentPhase() as CommandPhase).handleCommand(Command.BALL, this.cursor)) {
this.scene.ui.setMode(Mode.COMMAND);
this.scene.ui.setMode(Mode.MESSAGE);
@@ -93,7 +93,7 @@ export default class BallUiHandler extends UiHandler {
}
updateCounts() {
this.countsText.setText(Object.values(this.scene.pokeballCounts).map(c => `x${c}`).join('\n'));
this.countsText.setText(Object.values(this.scene.currentBattle.turnPokeballCounts).map(c => `x${c}`).join('\n'));
}
setCursor(cursor: integer): boolean {
+48 -7
View File
@@ -4,10 +4,12 @@ import * as Utils from '../utils';
import { addTextObject, TextStyle } from './text';
import { getGenderSymbol, getGenderColor } from '../data/gender';
import { StatusEffect } from '../data/status-effect';
import BattleScene, { maxExpLevel } from '../battle-scene';
import { maxExpLevel } from '../battle-scene';
export default class BattleInfo extends Phaser.GameObjects.Container {
private player: boolean;
private mini: boolean;
private offset: boolean;
private lastName: string;
private lastStatus: StatusEffect;
private lastHp: integer;
@@ -17,9 +19,10 @@ export default class BattleInfo extends Phaser.GameObjects.Container {
private lastLevelExp: integer;
private lastLevel: integer;
private box: Phaser.GameObjects.Sprite;
private nameText: Phaser.GameObjects.Text;
private genderText: Phaser.GameObjects.Text;
private ownedIcon: Phaser.GameObjects.Image;
private ownedIcon: Phaser.GameObjects.Sprite;
private statusIndicator: Phaser.GameObjects.Sprite;
private levelContainer: Phaser.GameObjects.Container;
private hpBar: Phaser.GameObjects.Image;
@@ -30,6 +33,8 @@ export default class BattleInfo extends Phaser.GameObjects.Container {
constructor(scene: Phaser.Scene, x: number, y: number, player: boolean) {
super(scene, x, y);
this.player = player;
this.mini = !player;
this.offset = false;
this.lastName = null;
this.lastStatus = StatusEffect.NONE;
this.lastHp = -1;
@@ -42,9 +47,9 @@ export default class BattleInfo extends Phaser.GameObjects.Container {
// Initially invisible and shown via Pokemon.showInfo
this.setVisible(false);
const box = this.scene.add.image(0, 0, `pbinfo_${player ? 'player' : 'enemy'}`);
box.setOrigin(1, 0.5);
this.add(box);
this.box = this.scene.add.sprite(0, 0, this.getTextureName());
this.box.setOrigin(1, 0.5);
this.add(this.box);
this.nameText = addTextObject(this.scene, player ? -115 : -124, player ? -15.2 : -11.2, '', TextStyle.BATTLE_INFO);
this.nameText.setOrigin(0, 0);
@@ -56,7 +61,7 @@ export default class BattleInfo extends Phaser.GameObjects.Container {
this.add(this.genderText);
if (!this.player) {
this.ownedIcon = this.scene.add.image(0, 0, 'icon_owned');
this.ownedIcon = this.scene.add.sprite(0, 0, 'icon_owned');
this.ownedIcon.setVisible(false);
this.ownedIcon.setOrigin(0, 0);
this.ownedIcon.setPositionRelative(this.nameText, 0, 11.5);
@@ -115,10 +120,11 @@ export default class BattleInfo extends Phaser.GameObjects.Container {
}
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)
this.setHpNumbers(pokemon.hp, pokemon.getMaxHp());
this.lastHp = pokemon.hp;
this.lastHpFrame = this.hpBar.scaleX > 0.5 ? 'high' : this.hpBar.scaleX > 0.25 ? 'medium' : 'low';
this.lastMaxHp = pokemon.getMaxHp();
this.setLevel(pokemon.level);
@@ -131,6 +137,39 @@ export default class BattleInfo extends Phaser.GameObjects.Container {
}
}
getTextureName(): string {
return `pbinfo_${this.player ? 'player' : 'enemy'}${this.mini ? '_mini' : ''}`;
}
setMini(mini: boolean): void {
if (this.mini === mini)
return;
this.mini = mini;
this.box.setTexture(this.getTextureName());
if (this.player) {
this.y -= 12 * (mini ? 1 : -1);
}
const offsetElements = [ this.nameText, this.genderText, this.statusIndicator, this.levelContainer ];
offsetElements.forEach(el => el.y += 1.5 * (mini ? -1 : 1));
const toggledElements = [ this.hpNumbersContainer, this.expBar ];
toggledElements.forEach(el => el.setVisible(!mini));
}
setOffset(offset: boolean): void {
if (this.offset === offset)
return;
this.offset = offset;
this.x += 10 * (offset === this.player ? 1 : -1);
this.y += 27 * (offset ? 1 : -1);
}
updateInfo(pokemon: Pokemon, instant?: boolean): Promise<void> {
return new Promise(resolve => {
if (!this.scene) {
@@ -290,4 +329,6 @@ export class EnemyBattleInfo extends BattleInfo {
constructor(scene: Phaser.Scene) {
super(scene, 140, -141, false);
}
setMini(mini: boolean): void { } // Always mini
}
+2 -2
View File
@@ -43,7 +43,7 @@ export default class CommandUiHandler extends UiHandler {
const messageHandler = this.getUi().getMessageHandler();
messageHandler.bg.setTexture('bg_command');
messageHandler.message.setWordWrapWidth(1110);
messageHandler.showText(`What will\n${this.scene.getPlayerPokemon().name} do?`, 0);
messageHandler.showText(`What will\n${(this.scene.getCurrentPhase() as CommandPhase).getPokemon().name} do?`, 0);
this.setCursor(this.cursor);
}
@@ -65,7 +65,7 @@ export default class CommandUiHandler extends UiHandler {
success = true;
break;
case 2:
ui.setMode(Mode.PARTY, PartyUiMode.SWITCH);
ui.setMode(Mode.PARTY, PartyUiMode.SWITCH, (this.scene.getCurrentPhase() as CommandPhase).getPokemon().getFieldIndex());
success = true;
break;
case 3:
+6
View File
@@ -13,6 +13,12 @@ export default class EvolutionSceneHandler extends UiHandler {
this.evolutionContainer = this.scene.add.container(0, -this.scene.game.canvas.height / 6);
this.scene.fieldUI.add(this.evolutionContainer);
}
show(_args: any[]): void {
super.show(_args);
this.scene.fieldUI.bringToTop(this.evolutionContainer);
}
processInput(button: Button) {
this.scene.ui.getMessageHandler().processInput(button);
+2 -2
View File
@@ -91,7 +91,7 @@ export default class FightUiHandler extends UiHandler {
ui.add(this.cursorObj);
}
const moveset = this.scene.getPlayerPokemon().getMoveset();
const moveset = (this.scene.getCurrentPhase() as CommandPhase).getPokemon().getMoveset();
const hasMove = cursor < moveset.length;
@@ -114,7 +114,7 @@ export default class FightUiHandler extends UiHandler {
}
displayMoves() {
const moveset = this.scene.getPlayerPokemon().getMoveset();
const moveset = (this.scene.getCurrentPhase() as CommandPhase).getPokemon().getMoveset();
for (let m = 0; m < 4; m++) {
const moveText = addTextObject(this.scene, m % 2 === 0 ? 0 : 100, m < 2 ? 0 : 16, '-', TextStyle.WINDOW);
if (m < moveset.length)
+35 -23
View File
@@ -42,7 +42,9 @@ export type PokemonMoveSelectFilter = (pokemonMove: PokemonMove) => string;
export default class PartyUiHandler extends MessageUiHandler {
private partyUiMode: PartyUiMode;
private fieldIndex: integer;
private partyBg: Phaser.GameObjects.Image;
private partyContainer: Phaser.GameObjects.Container;
private partySlotsContainer: Phaser.GameObjects.Container;
private partySlots: PartySlot[];
@@ -67,7 +69,7 @@ export default class PartyUiHandler extends MessageUiHandler {
private static FilterAll = (_pokemon: PlayerPokemon) => null;
public static FilterNonFainted = (pokemon: PlayerPokemon) => {
if (!pokemon.hp)
if (pokemon.isFainted())
return `${pokemon.name} has no energy\nleft to battle!`;
return null;
};
@@ -96,10 +98,10 @@ export default class PartyUiHandler extends MessageUiHandler {
this.partyContainer = partyContainer;
const partyBg = this.scene.add.image(0, 0, 'party_bg');
partyContainer.add(partyBg);
this.partyBg = this.scene.add.image(0, 0, 'party_bg');
partyContainer.add(this.partyBg);
partyBg.setOrigin(0, 1);
this.partyBg.setOrigin(0, 1);
const partySlotsContainer = this.scene.add.container(0, 0);
partyContainer.add(partySlotsContainer);
@@ -143,17 +145,20 @@ export default class PartyUiHandler extends MessageUiHandler {
this.partyUiMode = args[0] as PartyUiMode;
this.fieldIndex = args.length > 1 ? args[1] as integer : -1;
this.partyContainer.setVisible(true);
this.partyBg.setTexture(`party_bg${this.scene.currentBattle.double ? '_double' : ''}`);
this.populatePartySlots();
this.setCursor(this.cursor < 6 ? this.cursor : 0);
if (args.length > 1 && args[1] instanceof Function)
this.selectCallback = args[1];
this.selectFilter = args.length > 2 && args[2] instanceof Function
? args[2] as PokemonSelectFilter
if (args.length > 2 && args[2] instanceof Function)
this.selectCallback = args[2];
this.selectFilter = args.length > 3 && args[3] instanceof Function
? args[3] as PokemonSelectFilter
: PartyUiHandler.FilterAll;
this.moveSelectFilter = args.length > 3 && args[3] instanceof Function
? args[3] as PokemonMoveSelectFilter
this.moveSelectFilter = args.length > 4 && args[4] instanceof Function
? args[4] as PokemonMoveSelectFilter
: PartyUiHandler.FilterAllMoves;
}
@@ -226,7 +231,7 @@ export default class PartyUiHandler extends MessageUiHandler {
} else if (option === PartyOption.RELEASE) {
this.clearOptions();
ui.playSelect();
if (this.cursor) {
if (this.cursor >= this.scene.currentBattle.getBattlerCount()) {
this.showText(`Do you really want to release ${pokemon.name}?`, null, () => {
ui.setModeWithoutClear(Mode.CONFIRM, () => {
ui.setMode(Mode.PARTY);
@@ -292,12 +297,13 @@ export default class PartyUiHandler extends MessageUiHandler {
success = this.setCursor(this.cursor < 6 ? this.cursor < slotCount - 1 ? this.cursor + 1 : 6 : 0);
break;
case Button.LEFT:
if (this.cursor && this.cursor < 6)
if (this.cursor >= this.scene.currentBattle.getBattlerCount() && this.cursor < 6)
success = this.setCursor(0);
break;
case Button.RIGHT:
if (!this.cursor)
success = this.setCursor(this.lastCursor < 6 ? this.lastCursor || 1 : 1);
const battlerCount = this.scene.currentBattle.getBattlerCount();
if (this.cursor < battlerCount)
success = this.setCursor(this.lastCursor < 6 ? this.lastCursor || battlerCount : battlerCount);
break;
}
}
@@ -411,10 +417,11 @@ export default class PartyUiHandler extends MessageUiHandler {
case PartyUiMode.SWITCH:
case PartyUiMode.FAINT_SWITCH:
case PartyUiMode.POST_BATTLE_SWITCH:
if (this.cursor) {
if (this.cursor >= this.scene.currentBattle.getBattlerCount()) {
this.options.push(PartyOption.SEND_OUT);
if (this.partyUiMode !== PartyUiMode.FAINT_SWITCH
&& this.scene.findModifier(m => m instanceof SwitchEffectTransferModifier && (m as SwitchEffectTransferModifier).pokemonId === this.scene.getPlayerPokemon().id))
&& this.scene.findModifier(m => m instanceof SwitchEffectTransferModifier
&& (m as SwitchEffectTransferModifier).pokemonId === this.scene.getPlayerField()[this.fieldIndex].id))
this.options.push(PartyOption.PASS_BATON);
}
break;
@@ -578,7 +585,9 @@ class PartySlot extends Phaser.GameObjects.Container {
private slotHpOverlay: Phaser.GameObjects.Sprite;
constructor(scene: BattleScene, slotIndex: integer, pokemon: PlayerPokemon) {
super(scene, slotIndex ? 230.5 : 64, slotIndex ? -184 + 28 * slotIndex : -124);
super(scene, slotIndex >= scene.currentBattle.getBattlerCount() ? 230.5 : 64,
slotIndex >= scene.currentBattle.getBattlerCount() ? -184 + (scene.currentBattle.double ? -38 : 0)
+ (28 + (scene.currentBattle.double ? 6 : 0)) * slotIndex : -124 + (scene.currentBattle.double ? -8 : 0) + slotIndex * 64);
this.slotIndex = slotIndex;
this.pokemon = pokemon;
@@ -587,14 +596,16 @@ class PartySlot extends Phaser.GameObjects.Container {
}
setup() {
const slotKey = `party_slot${this.slotIndex ? '' : '_main'}`;
const battlerCount = (this.scene as BattleScene).currentBattle.getBattlerCount();
const slotKey = `party_slot${this.slotIndex >= battlerCount ? '' : '_main'}`;
const slotBg = this.scene.add.sprite(0, 0, slotKey, `${slotKey}${this.pokemon.hp ? '' : '_fnt'}`);
this.slotBg = slotBg;
this.add(slotBg);
const slotPb = this.scene.add.sprite(this.slotIndex ? -85.5 : -51, this.slotIndex ? 0 : -20.5, 'party_pb');
const slotPb = this.scene.add.sprite(this.slotIndex >= battlerCount ? -85.5 : -51, this.slotIndex >= battlerCount ? 0 : -20.5, 'party_pb');
this.slotPb = slotPb;
this.add(slotPb);
@@ -609,7 +620,7 @@ class PartySlot extends Phaser.GameObjects.Container {
this.add(slotInfoContainer);
const slotName = addTextObject(this.scene, 0, 0, this.pokemon.name, TextStyle.PARTY);
slotName.setPositionRelative(slotBg, this.slotIndex ? 21 : 24, this.slotIndex ? 3 : 10);
slotName.setPositionRelative(slotBg, this.slotIndex >= battlerCount ? 21 : 24, this.slotIndex >= battlerCount ? 3 : 10);
slotName.setOrigin(0, 0);
const slotLevelLabel = this.scene.add.image(0, 0, 'party_slot_overlay_lv');
@@ -621,7 +632,7 @@ class PartySlot extends Phaser.GameObjects.Container {
slotLevelText.setOrigin(0, 0.25);
const slotHpBar = this.scene.add.image(0, 0, 'party_slot_hp_bar');
slotHpBar.setPositionRelative(slotBg, this.slotIndex ? 72 : 8, this.slotIndex ? 7 : 31);
slotHpBar.setPositionRelative(slotBg, this.slotIndex >= battlerCount ? 72 : 8, this.slotIndex >= battlerCount ? 7 : 31);
slotHpBar.setOrigin(0, 0);
const hpRatio = this.pokemon.getHpRatio();
@@ -669,8 +680,9 @@ class PartySlot extends Phaser.GameObjects.Container {
}
private updateSlotTexture(): void {
this.slotBg.setTexture(`party_slot${this.slotIndex ? '' : '_main'}`,
`party_slot${this.slotIndex ? '' : '_main'}${this.transfer ? '_swap' : this.pokemon.hp ? '' : '_fnt'}${this.selected ? '_sel' : ''}`);
const battlerCount = (this.scene as BattleScene).currentBattle.getBattlerCount();
this.slotBg.setTexture(`party_slot${this.slotIndex >= battlerCount ? '' : '_main'}`,
`party_slot${this.slotIndex >= battlerCount ? '' : '_main'}${this.transfer ? '_swap' : this.pokemon.hp ? '' : '_fnt'}${this.selected ? '_sel' : ''}`);
}
}
+121
View File
@@ -0,0 +1,121 @@
import { BattlerIndex } from "../battle";
import BattleScene, { Button } from "../battle-scene";
import { Moves, getMoveTargets } from "../data/move";
import { Mode } from "./ui";
import UiHandler from "./uiHandler";
import * as Utils from "../utils";
export type TargetSelectCallback = (cursor: integer) => void;
export default class TargetSelectUiHandler extends UiHandler {
private fieldIndex: integer;
private move: Moves;
private targetSelectCallback: TargetSelectCallback;
private targets: BattlerIndex[];
private targetFlashTween: Phaser.Tweens.Tween;
constructor(scene: BattleScene) {
super(scene, Mode.TARGET_SELECT);
this.cursor = -1;
}
setup(): void { }
show(args: any[]) {
if (args.length < 3)
return;
super.show(args);
this.fieldIndex = args[0] as integer;
this.move = args[1] as Moves;
this.targetSelectCallback = args[2] as TargetSelectCallback;
this.targets = getMoveTargets(this.scene.getPlayerField()[this.fieldIndex], this.move).targets;
if (!this.targets.length)
return;
this.setCursor(this.targets.indexOf(this.cursor) > -1 ? this.cursor : this.targets[0]);
}
processInput(button: Button) {
const ui = this.getUi();
let success = false;
if (button === Button.ACTION || button === Button.CANCEL) {
this.targetSelectCallback(button === Button.ACTION ? this.cursor : -1);
success = true;
} else {
switch (button) {
case Button.UP:
if (this.cursor < BattlerIndex.ENEMY && this.targets.find(t => t >= BattlerIndex.ENEMY))
success = this.setCursor(this.targets.find(t => t >= BattlerIndex.ENEMY));
break;
case Button.DOWN:
if (this.cursor >= BattlerIndex.ENEMY && this.targets.find(t => t < BattlerIndex.ENEMY))
success = this.setCursor(this.targets.find(t => t < BattlerIndex.ENEMY));
break;
case Button.LEFT:
if (this.cursor % 2 && this.targets.find(t => t === this.cursor - 1))
success = this.setCursor(this.cursor - 1);
break;
case Button.RIGHT:
if (!(this.cursor % 2) && this.targets.find(t => t === this.cursor + 1))
success = this.setCursor(this.cursor + 1);
break;
}
}
if (success)
ui.playSelect();
}
setCursor(cursor: integer): boolean {
const lastCursor = this.cursor;
const ret = super.setCursor(cursor);
if (this.targetFlashTween) {
this.targetFlashTween.stop();
const lastTarget = this.scene.getField()[lastCursor];
if (lastTarget)
lastTarget.setAlpha(1);
}
const target = this.scene.getField()[cursor];
this.targetFlashTween = this.scene.tweens.add({
targets: [ target ],
alpha: 0,
loop: -1,
duration: new Utils.FixedInt(250) as unknown as integer,
ease: 'Sine.easeIn',
yoyo: true,
onUpdate: t => {
if (target)
target.setAlpha(t.getValue());
}
});
return ret;
}
eraseCursor() {
const target = this.scene.getField()[this.cursor];
if (this.targetFlashTween) {
this.targetFlashTween.stop();
this.targetFlashTween = null;
}
if (target)
target.setAlpha(1);
}
clear() {
super.clear();
this.eraseCursor();
}
}
+3
View File
@@ -12,12 +12,14 @@ import SummaryUiHandler from './summary-ui-handler';
import StarterSelectUiHandler from './starter-select-ui-handler';
import EvolutionSceneHandler from './evolution-scene-handler';
import BiomeSelectUiHandler from './biome-select-ui-handler';
import TargetSelectUiHandler from './target-select-ui-handler';
export enum Mode {
MESSAGE,
COMMAND,
FIGHT,
BALL,
TARGET_SELECT,
MODIFIER_SELECT,
PARTY,
SUMMARY,
@@ -54,6 +56,7 @@ export default class UI extends Phaser.GameObjects.Container {
new CommandUiHandler(scene),
new FightUiHandler(scene),
new BallUiHandler(scene),
new TargetSelectUiHandler(scene),
new ModifierSelectUiHandler(scene),
new PartyUiHandler(scene),
new SummaryUiHandler(scene),