pokerogue/src/system/game-speed.ts

70 lines
2.7 KiB
TypeScript
Raw Normal View History

import SoundFade from "phaser3-rex-plugins/plugins/soundfade";
import FadeIn from 'phaser3-rex-plugins/plugins/audio/fade/FadeIn';
import FadeOut from 'phaser3-rex-plugins/plugins/audio/fade/FadeOut';
2023-04-20 15:46:05 -04:00
import BattleScene from "../battle-scene";
import * as Utils from "../utils";
2023-04-12 11:30:47 -04:00
type FadeIn = typeof FadeIn;
type FadeOut = typeof FadeOut;
2023-04-12 11:30:47 -04:00
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 | Phaser.Types.Tweens.TweenChainBuilderConfig | Phaser.Tweens.Tween | Phaser.Tweens.TweenChain) {
2023-04-12 11:30:47 -04:00
if (config.duration)
config.duration = transformValue(config.duration);
if (config.delay)
config.delay = transformValue(config.delay);
if (config.repeatDelay)
config.repeatDelay = transformValue(config.repeatDelay);
if (config.loopDelay)
config.loopDelay = transformValue(config.loopDelay);
if (config.hold)
config.hold = transformValue(config.hold);
2023-04-12 11:30:47 -04:00
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);
if (config.repeatDelay)
config.repeatDelay = transformValue(config.repeatDelay);
if (config.loopDelay)
config.loopDelay = transformValue(config.loopDelay);
if (config.hold)
config.hold = transformValue(config.hold);
2023-04-12 11:30:47 -04:00
return originalAddCounter.apply(this, [ config ]);
};
const originalFadeOut = SoundFade.fadeOut;
SoundFade.fadeOut = ((
scene: Phaser.Scene,
sound: Phaser.Sound.BaseSound,
duration: number,
destroy?: boolean
2023-11-04 00:32:12 -04:00
) => originalFadeOut(scene, sound, transformValue(duration), destroy)) as FadeOut;
const originalFadeIn = SoundFade.fadeIn;
SoundFade.fadeIn = ((
scene: Phaser.Scene,
sound: string | Phaser.Sound.BaseSound,
duration: number,
endVolume?: number,
startVolume?: number
2023-11-04 00:32:12 -04:00
) => originalFadeIn(scene, sound, transformValue(duration), endVolume, startVolume)) as FadeIn;
2023-04-12 11:30:47 -04:00
}