Add game speed functionality

pull/1/head
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() {
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;

View File

@ -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++;
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.autoSpeed > 1)
this.autoSpeed--;
if (this.gameSpeed > 1) {
if (!this.auto)
this.gameSpeed -= 0.25;
else
this.gameSpeed--;
}
} 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 {

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,6 +69,7 @@ export class NumberHolder {
this.value = value;
}
}
export class IntegerHolder {
public value: integer;
@ -76,3 +77,9 @@ export class IntegerHolder {
this.value = value;
}
}
export class FixedInt extends IntegerHolder {
constructor(value: integer) {
super(value);
}
}