Add game speed functionality

This commit is contained in:
Flashfyre 2023-04-12 11:30:47 -04:00
parent 4c892c2c40
commit 05736530c6
4 changed files with 68 additions and 51 deletions

View File

@ -16,43 +16,6 @@ import { Mode } from "./ui/ui";
export function initAutoPlay() { export function initAutoPlay() {
const thisArg = this as BattleScene; 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; PlayerPokemon.prototype.getNextMove = EnemyPokemon.prototype.getNextMove;
const playerPokemon = this.getParty()[0] as PlayerPokemon; const playerPokemon = this.getParty()[0] as PlayerPokemon;

View File

@ -12,6 +12,7 @@ import { initAutoPlay } from './auto-play';
import { Battle } from './battle'; import { Battle } from './battle';
import { initCommonAnims, loadCommonAnimAssets, populateAnims } from './battle-anims'; import { initCommonAnims, loadCommonAnimAssets, populateAnims } from './battle-anims';
import { BattlePhase } from './battle-phase'; import { BattlePhase } from './battle-phase';
import { initGameSpeed } from './game-speed';
const enableAuto = true; const enableAuto = true;
@ -30,7 +31,7 @@ export enum Button {
export default class BattleScene extends Phaser.Scene { export default class BattleScene extends Phaser.Scene {
public auto: boolean; public auto: boolean;
public autoSpeed: integer = 1; public gameSpeed: integer = 1;
private phaseQueue: BattlePhase[]; private phaseQueue: BattlePhase[];
private phaseQueuePrepend: BattlePhase[]; private phaseQueuePrepend: BattlePhase[];
@ -222,6 +223,8 @@ export default class BattleScene extends Phaser.Scene {
} }
create() { create() {
initGameSpeed.apply(this);
this.setupControls(); this.setupControls();
this.load.setBaseURL(); this.load.setBaseURL();
@ -424,23 +427,32 @@ export default class BattleScene extends Phaser.Scene {
this.ui.processInput(Button.ACTION); this.ui.processInput(Button.ACTION);
else if (this.isButtonPressed(Button.CANCEL)) else if (this.isButtonPressed(Button.CANCEL))
this.ui.processInput(Button.CANCEL); this.ui.processInput(Button.CANCEL);
else if (enableAuto) { else if (this.isButtonPressed(Button.SPEED_UP)) {
if (this.isButtonPressed(Button.AUTO)) if (!this.auto) {
this.auto = !this.auto; if (this.gameSpeed < 2)
else if (this.isButtonPressed(Button.SPEED_UP)) { this.gameSpeed += 0.25;
if (this.autoSpeed < 20) } else if (this.gameSpeed < 20)
this.autoSpeed++; this.gameSpeed++;
} else if (this.isButtonPressed(Button.SLOW_DOWN)) { } else if (this.isButtonPressed(Button.SLOW_DOWN)) {
if (this.autoSpeed > 1) if (this.gameSpeed > 1) {
this.autoSpeed--; 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 } else
return; return;
this.blockInput = true; this.blockInput = true;
this.time.delayedCall(250, () => { this.time.delayedCall(new Utils.FixedInt(250) as unknown as integer, () => this.blockInput = false);
this.blockInput = false;
});
} }
isButtonPressed(button: Button): boolean { isButtonPressed(button: Button): boolean {

35
src/game-speed.ts Normal file
View File

@ -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 ]);
};
}

View File

@ -69,10 +69,17 @@ export class NumberHolder {
this.value = value; this.value = value;
} }
} }
export class IntegerHolder { export class IntegerHolder {
public value: integer; public value: integer;
constructor(value: integer) { constructor(value: integer) {
this.value = value; this.value = value;
} }
}
export class FixedInt extends IntegerHolder {
constructor(value: integer) {
super(value);
}
} }