From 05736530c651f38c56545fdb2d7339508aed5370 Mon Sep 17 00:00:00 2001 From: Flashfyre Date: Wed, 12 Apr 2023 11:30:47 -0400 Subject: [PATCH] Add game speed functionality --- src/auto-play.ts | 37 ------------------------------------- src/battle-scene.ts | 40 ++++++++++++++++++++++++++-------------- src/game-speed.ts | 35 +++++++++++++++++++++++++++++++++++ src/utils.ts | 7 +++++++ 4 files changed, 68 insertions(+), 51 deletions(-) create mode 100644 src/game-speed.ts diff --git a/src/auto-play.ts b/src/auto-play.ts index 98dfc49a3..bfb075d05 100644 --- a/src/auto-play.ts +++ b/src/auto-play.ts @@ -16,43 +16,6 @@ import { Mode } from "./ui/ui"; export function initAutoPlay() { const thisArg = this as BattleScene; - const originalDelayedCall = this.time.delayedCall; - this.time.delayedCall = function (delay: number, callback: Function, args?: any[], callbackScope?: any) { - if (thisArg.auto) - delay /= thisArg.autoSpeed; - originalDelayedCall.apply(this, [ delay, callback, args, callbackScope ]); - }; - const originalAddEvent = this.time.addEvent; - this.time.addEvent = function (config: Phaser.Time.TimerEvent | Phaser.Types.Time.TimerEventConfig) { - if (thisArg.auto) { - if (config.delay) - config.delay = Math.ceil(config.delay / thisArg.autoSpeed); - if (config.startAt) - config.startAt = Math.ceil(config.startAt / thisArg.autoSpeed); - } - return originalAddEvent.apply(this, [ config ]); - }; - const originalTweensAdd = this.tweens.add; - this.tweens.add = function (config: Phaser.Types.Tweens.TweenBuilderConfig | object) { - if (thisArg.auto) { - if (config.duration) - config.duration = Math.ceil(config.duration / thisArg.autoSpeed); - if (config.delay) - config.delay = Math.ceil(config.delay / thisArg.autoSpeed); - } - return originalTweensAdd.apply(this, [ config ]); - }; - const originalAddCounter = this.tweens.addCounter; - this.tweens.addCounter = function (config: Phaser.Types.Tweens.NumberTweenBuilderConfig) { - if (thisArg.auto) { - if (config.duration) - config.duration = Math.ceil(config.duration / thisArg.autoSpeed); - if (config.delay) - config.delay = Math.ceil(config.delay / thisArg.autoSpeed); - } - return originalAddCounter.apply(this, [ config ]); - }; - PlayerPokemon.prototype.getNextMove = EnemyPokemon.prototype.getNextMove; const playerPokemon = this.getParty()[0] as PlayerPokemon; diff --git a/src/battle-scene.ts b/src/battle-scene.ts index b6dde2d79..d71a8fd74 100644 --- a/src/battle-scene.ts +++ b/src/battle-scene.ts @@ -12,6 +12,7 @@ import { initAutoPlay } from './auto-play'; import { Battle } from './battle'; import { initCommonAnims, loadCommonAnimAssets, populateAnims } from './battle-anims'; import { BattlePhase } from './battle-phase'; +import { initGameSpeed } from './game-speed'; const enableAuto = true; @@ -30,7 +31,7 @@ export enum Button { export default class BattleScene extends Phaser.Scene { public auto: boolean; - public autoSpeed: integer = 1; + public gameSpeed: integer = 1; private phaseQueue: BattlePhase[]; private phaseQueuePrepend: BattlePhase[]; @@ -222,6 +223,8 @@ export default class BattleScene extends Phaser.Scene { } create() { + initGameSpeed.apply(this); + this.setupControls(); this.load.setBaseURL(); @@ -424,23 +427,32 @@ export default class BattleScene extends Phaser.Scene { this.ui.processInput(Button.ACTION); else if (this.isButtonPressed(Button.CANCEL)) this.ui.processInput(Button.CANCEL); - else if (enableAuto) { - if (this.isButtonPressed(Button.AUTO)) - this.auto = !this.auto; - else if (this.isButtonPressed(Button.SPEED_UP)) { - if (this.autoSpeed < 20) - this.autoSpeed++; - } else if (this.isButtonPressed(Button.SLOW_DOWN)) { - if (this.autoSpeed > 1) - this.autoSpeed--; + else if (this.isButtonPressed(Button.SPEED_UP)) { + if (!this.auto) { + if (this.gameSpeed < 2) + 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--; } - return; + } 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) + this.gameSpeed = 2; + } else + return; } else return; this.blockInput = true; - this.time.delayedCall(250, () => { - this.blockInput = false; - }); + this.time.delayedCall(new Utils.FixedInt(250) as unknown as integer, () => this.blockInput = false); } isButtonPressed(button: Button): boolean { diff --git a/src/game-speed.ts b/src/game-speed.ts new file mode 100644 index 000000000..3ff8c159b --- /dev/null +++ b/src/game-speed.ts @@ -0,0 +1,35 @@ +import BattleScene from "./battle-scene"; +import * as Utils from "./utils"; + +export function initGameSpeed() { + const thisArg = this as BattleScene; + + const transformValue = (value: number | Utils.FixedInt): number => { + if (value instanceof Utils.FixedInt) + return (value as Utils.FixedInt).value; + return thisArg.gameSpeed === 1 ? value : Math.ceil(value /= thisArg.gameSpeed); + }; + + const originalAddEvent = this.time.addEvent; + this.time.addEvent = function (config: Phaser.Time.TimerEvent | Phaser.Types.Time.TimerEventConfig) { + if (config.delay) + config.delay = transformValue(config.delay); + return originalAddEvent.apply(this, [ config ]); + }; + const originalTweensAdd = this.tweens.add; + this.tweens.add = function (config: Phaser.Types.Tweens.TweenBuilderConfig | object) { + if (config.duration) + config.duration = transformValue(config.duration); + if (config.delay) + config.delay = transformValue(config.delay); + return originalTweensAdd.apply(this, [ config ]); + }; + const originalAddCounter = this.tweens.addCounter; + this.tweens.addCounter = function (config: Phaser.Types.Tweens.NumberTweenBuilderConfig) { + if (config.duration) + config.duration = transformValue(config.duration); + if (config.delay) + config.delay = transformValue(config.delay); + return originalAddCounter.apply(this, [ config ]); + }; +} \ No newline at end of file diff --git a/src/utils.ts b/src/utils.ts index dd3ec7c70..3dccaf031 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -69,10 +69,17 @@ export class NumberHolder { this.value = value; } } + export class IntegerHolder { public value: integer; constructor(value: integer) { this.value = value; } +} + +export class FixedInt extends IntegerHolder { + constructor(value: integer) { + super(value); + } } \ No newline at end of file