Merge pull request #6819 from Bertie690/set-position-relative

[Misc] Dedupe + move phaser method stubs into separate file
This commit is contained in:
Bertie690 2025-12-04 04:20:21 -05:00 committed by GitHub
parent e13ea6e17d
commit 6af869cff1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 72 additions and 84 deletions

66
src/extensions.ts Normal file
View File

@ -0,0 +1,66 @@
import "phaser";
//#region Methods/Interfaces
/**
* Interface representing an object that can be passed to {@linkcode setPositionRelative}.
*/
interface GuideObject
extends Pick<Phaser.GameObjects.Components.ComputedSize, "width" | "height">,
Pick<Phaser.GameObjects.Components.Transform, "x" | "y">,
Pick<Phaser.GameObjects.Components.Origin, "originX" | "originY"> {}
/**
* 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: GuideObject,
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;
//#endregion
//#region Declaration Merging
interface hasSetPositionRelative {
/**
* 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`
*/
setPositionRelative: typeof setPositionRelative;
}
declare module "phaser" {
namespace GameObjects {
interface Container extends hasSetPositionRelative {}
interface Sprite extends hasSetPositionRelative {}
interface Image extends hasSetPositionRelative {}
interface NineSlice extends hasSetPositionRelative {}
interface Text extends hasSetPositionRelative {}
interface Rectangle extends hasSetPositionRelative {}
}
}
//#endregion

View File

@ -1,3 +1,5 @@
import "#app/extensions"; // Setup Phaser extension methods/etc
import { initAbilities } from "#abilities/ability"; import { initAbilities } from "#abilities/ability";
import { initBiomes } from "#balance/init-biomes"; import { initBiomes } from "#balance/init-biomes";
import { initPokemonPrevolutions, initPokemonStarters } from "#balance/pokemon-evolutions"; import { initPokemonPrevolutions, initPokemonStarters } from "#balance/pokemon-evolutions";

View File

@ -30,37 +30,6 @@ window.addEventListener("unhandledrejection", event => {
//alert(errorString); //alert(errorString);
}); });
interface GuideObject
extends Pick<Phaser.GameObjects.Components.ComputedSize, "width" | "height">,
Pick<Phaser.GameObjects.Components.Transform, "x" | "y">,
Pick<Phaser.GameObjects.Components.Origin, "originX" | "originY"> {}
/**
* 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: GuideObject,
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;
async function startGame(gameManifest?: Record<string, string>): Promise<void> { async function startGame(gameManifest?: Record<string, string>): Promise<void> {
await initI18n(); await initI18n();
const LoadingScene = (await import("./loading-scene")).LoadingScene; const LoadingScene = (await import("./loading-scene")).LoadingScene;

View File

@ -1,38 +1,6 @@
import "phaser"; import "phaser";
/**
* Interface representing an object that can be passed to {@linkcode setPositionRelative}.
* @interface
*/
type GuideObject = Pick<Phaser.GameObjects.Components.ComputedSize, "width" | "height"> &
Pick<Phaser.GameObjects.Components.Transform, "x" | "y"> &
Pick<Phaser.GameObjects.Components.Origin, "originX" | "originY">;
type setPositionRelative =
/**
* 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`
*/
<T extends Phaser.GameObjects.Components.Transform>(this: T, guideObject: GuideObject, x: number, y: number) => T;
interface hasSetPositionRelative {
setPositionRelative: setPositionRelative;
}
declare module "phaser" { declare module "phaser" {
namespace GameObjects {
interface Container extends hasSetPositionRelative {}
interface Sprite extends hasSetPositionRelative {}
interface Image extends hasSetPositionRelative {}
interface NineSlice extends hasSetPositionRelative {}
interface Text extends hasSetPositionRelative {}
interface Rectangle extends hasSetPositionRelative {}
}
namespace Math { namespace Math {
interface RandomDataGenerator { interface RandomDataGenerator {
pick<T>(array: ArrayLike<T>): T; pick<T>(array: ArrayLike<T>): T;

View File

@ -42,7 +42,7 @@ vi.mock(import("i18next"), async importOriginal => {
} }
return HttpResponse.json(json); return HttpResponse.json(json);
} catch (err) { } catch (err) {
console.log(`Failed to load locale ${filename}!`, err); console.error(`Failed to load locale ${filename}\n`, err);
return HttpResponse.json({}); return HttpResponse.json({});
} }
}), }),

View File

@ -42,7 +42,8 @@ function initTestFile(): void {
* @todo Investigate why this resets on new test suite start * @todo Investigate why this resets on new test suite start
*/ */
function setupStubs(): void { function setupStubs(): void {
Object.defineProperties(global, { // TODO: Make this type safe
Object.defineProperties(globalThis, {
localStorage: { localStorage: {
value: mockLocalStorage(), value: mockLocalStorage(),
}, },
@ -76,25 +77,6 @@ function setupStubs(): void {
}; };
navigator.getGamepads = () => []; navigator.getGamepads = () => [];
setCookie(SESSION_ID_COOKIE_NAME, "fake_token"); setCookie(SESSION_ID_COOKIE_NAME, "fake_token");
/**
* Sets this object's position relative to another object with a given offset
* @param guideObject - The {@linkcode Phaser.GameObjects.GameObject} to base the position off of
* @param x - The relative x position
* @param y - The relative y position
*/
const setPositionRelative = function (guideObject: any, x: number, y: number): any {
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;
HTMLCanvasElement.prototype.getContext = () => mockContext; HTMLCanvasElement.prototype.getContext = () => mockContext;
} }

View File

@ -16,6 +16,7 @@ const config = {
entryPointStrategy: "expand", entryPointStrategy: "expand",
exclude: [ exclude: [
"src/polyfills.ts", "src/polyfills.ts",
"src/extensions.ts",
"src/vite.env.d.ts", "src/vite.env.d.ts",
"**/*+.test.ts", "**/*+.test.ts",
"test/test-utils/setup", "test/test-utils/setup",