pokerogue/src/main.ts
NightKev b76ecf55fd
[Dev] Add isApp & build:app for AdmiralBilly's offline app (#6632)
- Consolidate `isBeta`/etc into `src/constants/app-constants.ts`

- Rename `isLocal` to `isDev`

- Replace `import.meta.env.DEV` with `isBeta || isDev`
2025-10-06 18:51:33 -07:00

143 lines
4.7 KiB
TypeScript

import "#app/polyfills";
// All polyfills MUST be loaded first for side effects
import { InvertPostFX } from "#app/pipelines/invert";
import { initI18n } from "#app/plugins/i18n";
import { isBeta, isDev } from "#constants/app-constants";
import { version } from "#package.json";
import Phaser from "phaser";
import BBCodeTextPlugin from "phaser3-rex-plugins/plugins/bbcodetext-plugin";
import InputTextPlugin from "phaser3-rex-plugins/plugins/inputtext-plugin";
import TransitionImagePackPlugin from "phaser3-rex-plugins/templates/transitionimagepack/transitionimagepack-plugin";
import UIPlugin from "phaser3-rex-plugins/templates/ui/ui-plugin";
if (isBeta || isDev) {
document.title += " (Beta)";
}
// Catch global errors and display them in an alert so users can report the issue.
window.onerror = (_message, _source, _lineno, _colno, error) => {
console.error(error);
// const errorString = `Received unhandled error. Open browser console and click OK to see details.\nError: ${message}\nSource: ${source}\nLine: ${lineno}\nColumn: ${colno}\nStack: ${error.stack}`;
//alert(errorString);
// Avoids logging the error a second time.
return true;
};
// Catch global promise rejections and display them in an alert so users can report the issue.
window.addEventListener("unhandledrejection", event => {
// const errorString = `Received unhandled promise rejection. Open browser console and click OK to see details.\nReason: ${event.reason}`;
console.error(event.reason);
//alert(errorString);
});
/**
* Set this object's position relative to another object with a given offset.
* @param guideObject - The object to base this object's position off of; must have defined
* x/y co-ordinates, an origin and width/height
* @param x - The X-position to set, relative to `guideObject`'s `x` value
* @param y - The Y-position to set, relative to `guideObject`'s `y` value
* @returns `this`
*/
function setPositionRelative<T extends Phaser.GameObjects.Components.Transform>(
this: T,
guideObject: Pick<Phaser.GameObjects.Components.ComputedSize, "width" | "height"> &
Pick<Phaser.GameObjects.Components.Transform, "x" | "y"> &
Pick<Phaser.GameObjects.Components.Origin, "originX" | "originY">,
x: number,
y: number,
): T {
const offsetX = guideObject.width * (-0.5 + (0.5 - guideObject.originX));
const offsetY = guideObject.height * (-0.5 + (0.5 - guideObject.originY));
return this.setPosition(guideObject.x + offsetX + x, guideObject.y + offsetY + y);
}
Phaser.GameObjects.Container.prototype.setPositionRelative = setPositionRelative;
Phaser.GameObjects.Sprite.prototype.setPositionRelative = setPositionRelative;
Phaser.GameObjects.Image.prototype.setPositionRelative = setPositionRelative;
Phaser.GameObjects.NineSlice.prototype.setPositionRelative = setPositionRelative;
Phaser.GameObjects.Text.prototype.setPositionRelative = setPositionRelative;
Phaser.GameObjects.Rectangle.prototype.setPositionRelative = setPositionRelative;
document.fonts.load("16px emerald").then(() => document.fonts.load("10px pkmnems"));
// biome-ignore lint: TODO
let game;
// biome-ignore lint: TODO
let manifest;
const startGame = async () => {
await initI18n();
const LoadingScene = (await import("./loading-scene")).LoadingScene;
const BattleScene = (await import("./battle-scene")).BattleScene;
game = new Phaser.Game({
type: Phaser.WEBGL,
parent: "app",
scale: {
width: 1920,
height: 1080,
mode: Phaser.Scale.FIT,
},
plugins: {
global: [
{
key: "rexInputTextPlugin",
plugin: InputTextPlugin,
start: true,
},
{
key: "rexBBCodeTextPlugin",
plugin: BBCodeTextPlugin,
start: true,
},
{
key: "rexTransitionImagePackPlugin",
plugin: TransitionImagePackPlugin,
start: true,
},
],
scene: [
{
key: "rexUI",
plugin: UIPlugin,
mapping: "rexUI",
},
],
},
input: {
mouse: {
target: "app",
},
touch: {
target: "app",
},
gamepad: true,
},
dom: {
createContainer: true,
},
antialias: false,
pipeline: [InvertPostFX] as unknown as Phaser.Types.Core.PipelineConfig,
scene: [LoadingScene, BattleScene],
version,
});
game.sound.pauseOnBlur = false;
if (manifest) {
game["manifest"] = manifest;
}
};
fetch("/manifest.json")
.then(res => res.json())
.then(jsonResponse => {
manifest = jsonResponse.manifest;
})
.catch(err => {
// Manifest not found (likely local build or path error on live)
console.log(`Manifest not found. ${err}`);
})
.finally(() => {
startGame();
});
export default game;