Make setPositionRelative return this

This commit is contained in:
Sirz Benjie 2025-05-27 15:16:22 -05:00
parent ebb0135c0e
commit 559dce794e
No known key found for this signature in database
GPG Key ID: 4A524B4D196C759E
10 changed files with 43 additions and 31 deletions

View File

@ -29,7 +29,7 @@ window.addEventListener("unhandledrejection", event => {
const setPositionRelative = function (guideObject: Phaser.GameObjects.GameObject, x: number, y: number) { const setPositionRelative = function (guideObject: Phaser.GameObjects.GameObject, x: number, y: number) {
const offsetX = guideObject.width * (-0.5 + (0.5 - guideObject.originX)); const offsetX = guideObject.width * (-0.5 + (0.5 - guideObject.originX));
const offsetY = guideObject.height * (-0.5 + (0.5 - guideObject.originY)); const offsetY = guideObject.height * (-0.5 + (0.5 - guideObject.originY));
this.setPosition(guideObject.x + offsetX + x, guideObject.y + offsetY + y); return this.setPosition(guideObject.x + offsetX + x, guideObject.y + offsetY + y);
}; };
Phaser.GameObjects.Container.prototype.setPositionRelative = setPositionRelative; Phaser.GameObjects.Container.prototype.setPositionRelative = setPositionRelative;

View File

@ -20,37 +20,37 @@ declare module "phaser" {
/** /**
* Sets this object's position relative to another object with a given offset * Sets this object's position relative to another object with a given offset
*/ */
setPositionRelative(guideObject: any, x: number, y: number): self; setPositionRelative(guideObject: any, x: number, y: number): this;
} }
interface Sprite { interface Sprite {
/** /**
* Sets this object's position relative to another object with a given offset * Sets this object's position relative to another object with a given offset
*/ */
setPositionRelative(guideObject: any, x: number, y: number): self; setPositionRelative(guideObject: any, x: number, y: number): this;
} }
interface Image { interface Image {
/** /**
* Sets this object's position relative to another object with a given offset * Sets this object's position relative to another object with a given offset
*/ */
setPositionRelative(guideObject: any, x: number, y: number): self; setPositionRelative(guideObject: any, x: number, y: number): this;
} }
interface NineSlice { interface NineSlice {
/** /**
* Sets this object's position relative to another object with a given offset * Sets this object's position relative to another object with a given offset
*/ */
setPositionRelative(guideObject: any, x: number, y: number): self; setPositionRelative(guideObject: any, x: number, y: number): this;
} }
interface Text { interface Text {
/** /**
* Sets this object's position relative to another object with a given offset * Sets this object's position relative to another object with a given offset
*/ */
setPositionRelative(guideObject: any, x: number, y: number): self; setPositionRelative(guideObject: any, x: number, y: number): this;
} }
interface Rectangle { interface Rectangle {
/** /**
* Sets this object's position relative to another object with a given offset * Sets this object's position relative to another object with a given offset
*/ */
setPositionRelative(guideObject: any, x: number, y: number): self; setPositionRelative(guideObject: any, x: number, y: number): this;
} }
} }

View File

@ -98,8 +98,8 @@ export default abstract class BattleInfo extends Phaser.GameObjects.Container {
.setVisible(false) .setVisible(false)
.setOrigin(0) .setOrigin(0)
.setScale(0.5) .setScale(0.5)
.setInteractive(hitArea, hitCallback); .setInteractive(hitArea, hitCallback)
this.teraIcon.setPositionRelative(this.nameText, 0, 2); .setPositionRelative(this.nameText, 0, 2);
this.shinyIcon = globalScene.add this.shinyIcon = globalScene.add
.sprite(0, 0, "shiny_star") .sprite(0, 0, "shiny_star")
@ -107,8 +107,8 @@ export default abstract class BattleInfo extends Phaser.GameObjects.Container {
.setVisible(false) .setVisible(false)
.setOrigin(0) .setOrigin(0)
.setScale(0.5) .setScale(0.5)
.setInteractive(hitArea, hitCallback); .setInteractive(hitArea, hitCallback)
this.shinyIcon.setPositionRelative(this.nameText, 0, 2); .setPositionRelative(this.nameText, 0, 2);
this.fusionShinyIcon = globalScene.add this.fusionShinyIcon = globalScene.add
.sprite(0, 0, "shiny_star_2") .sprite(0, 0, "shiny_star_2")
@ -124,8 +124,8 @@ export default abstract class BattleInfo extends Phaser.GameObjects.Container {
.setVisible(false) .setVisible(false)
.setOrigin(0) .setOrigin(0)
.setScale(0.5) .setScale(0.5)
.setInteractive(hitArea, hitCallback); .setInteractive(hitArea, hitCallback)
this.splicedIcon.setPositionRelative(this.nameText, 0, 2); .setPositionRelative(this.nameText, 0, 2);
this.add([this.teraIcon, this.shinyIcon, this.fusionShinyIcon, this.splicedIcon]); this.add([this.teraIcon, this.shinyIcon, this.fusionShinyIcon, this.splicedIcon]);
} }
@ -235,8 +235,10 @@ export default abstract class BattleInfo extends Phaser.GameObjects.Container {
.setOrigin(0); .setOrigin(0);
this.add(this.nameText); this.add(this.nameText);
this.genderText = addTextObject(0, 0, "", TextStyle.BATTLE_INFO).setName("text_gender").setOrigin(0); this.genderText = addTextObject(0, 0, "", TextStyle.BATTLE_INFO)
this.genderText.setPositionRelative(this.nameText, 0, 2); .setName("text_gender")
.setOrigin(0)
.setPositionRelative(this.nameText, 0, 2);
this.add(this.genderText); this.add(this.genderText);
this.constructIcons(); this.constructIcons();
@ -245,8 +247,8 @@ export default abstract class BattleInfo extends Phaser.GameObjects.Container {
.sprite(0, 0, getLocalizedSpriteKey("statuses")) .sprite(0, 0, getLocalizedSpriteKey("statuses"))
.setName("icon_status") .setName("icon_status")
.setVisible(false) .setVisible(false)
.setOrigin(0); .setOrigin(0)
this.statusIndicator.setPositionRelative(this.nameText, 0, 11.5); .setPositionRelative(this.nameText, 0, 11.5);
this.add(this.statusIndicator); this.add(this.statusIndicator);
this.levelContainer = globalScene.add this.levelContainer = globalScene.add

View File

@ -56,15 +56,19 @@ export class EnemyBattleInfo extends BattleInfo {
super(140, -141, false, posParams); super(140, -141, false, posParams);
this.ownedIcon = globalScene.add.sprite(0, 0, "icon_owned").setName("icon_owned").setVisible(false).setOrigin(0, 0); this.ownedIcon = globalScene.add
this.ownedIcon.setPositionRelative(this.nameText, 0, 11.75); .sprite(0, 0, "icon_owned")
.setName("icon_owned")
.setVisible(false)
.setOrigin(0, 0)
.setPositionRelative(this.nameText, 0, 11.75);
this.championRibbon = globalScene.add this.championRibbon = globalScene.add
.sprite(0, 0, "champion_ribbon") .sprite(0, 0, "champion_ribbon")
.setName("icon_champion_ribbon") .setName("icon_champion_ribbon")
.setVisible(false) .setVisible(false)
.setOrigin(0, 0); .setOrigin(0, 0)
this.championRibbon.setPositionRelative(this.nameText, 8, 11.75); .setPositionRelative(this.nameText, 8, 11.75);
// Ensure these two icons are positioned below the stats container // Ensure these two icons are positioned below the stats container
this.addAt([this.ownedIcon, this.championRibbon], this.getIndex(this.statsContainer)); this.addAt([this.ownedIcon, this.championRibbon], this.getIndex(this.statsContainer));
@ -73,8 +77,10 @@ export class EnemyBattleInfo extends BattleInfo {
this.moveBelow<Phaser.GameObjects.GameObject>(this.flyoutMenu, this.box); this.moveBelow<Phaser.GameObjects.GameObject>(this.flyoutMenu, this.box);
this.effectivenessContainer = globalScene.add.container(0, 0).setVisible(false); this.effectivenessContainer = globalScene.add
this.effectivenessContainer.setPositionRelative(this.type1Icon, 22, 4); .container(0, 0)
.setVisible(false)
.setPositionRelative(this.type1Icon, 22, 4);
this.add(this.effectivenessContainer); this.add(this.effectivenessContainer);
this.effectivenessText = addTextObject(5, 4.5, "", TextStyle.BATTLE_INFO); this.effectivenessText = addTextObject(5, 4.5, "", TextStyle.BATTLE_INFO);

View File

@ -58,8 +58,9 @@ export default class MockContainer implements MockGameObject {
return this; return this;
} }
setPositionRelative(_source, _x, _y) { setPositionRelative(_source, _x, _y): this {
/// Sets the position of this Game Object to be a relative position from the source Game Object. /// Sets the position of this Game Object to be a relative position from the source Game Object.
return this;
} }
setInteractive(): this { setInteractive(): this {

View File

@ -57,8 +57,9 @@ export default class MockGraphics implements MockGameObject {
// This callback is invoked when this Game Object is added to a Scene. // This callback is invoked when this Game Object is added to a Scene.
} }
setPositionRelative(_source, _x, _y) { setPositionRelative(_source, _x, _y): this {
/// Sets the position of this Game Object to be a relative position from the source Game Object. /// Sets the position of this Game Object to be a relative position from the source Game Object.
return this;
} }
destroy() { destroy() {

View File

@ -38,8 +38,9 @@ export default class MockRectangle implements MockGameObject {
// This callback is invoked when this Game Object is added to a Scene. // This callback is invoked when this Game Object is added to a Scene.
} }
setPositionRelative(_source, _x, _y) { setPositionRelative(_source, _x, _y): this {
/// Sets the position of this Game Object to be a relative position from the source Game Object. /// Sets the position of this Game Object to be a relative position from the source Game Object.
return this;
} }
destroy() { destroy() {

View File

@ -140,9 +140,9 @@ export default class MockSprite implements MockGameObject {
return this; return this;
} }
setPositionRelative(source, x, y) { setPositionRelative(source, x, y): this {
/// Sets the position of this Game Object to be a relative position from the source Game Object. /// Sets the position of this Game Object to be a relative position from the source Game Object.
this.phaserSprite.setPositionRelative(source, x, y); return this.phaserSprite.setPositionRelative(source, x, y);
} }
setY(y: number): this { setY(y: number): this {

View File

@ -197,9 +197,10 @@ export default class MockText implements MockGameObject {
return this; return this;
} }
setPositionRelative(_source, _x, _y) { setPositionRelative(_source, _x, _y): this {
/// Sets the position of this Game Object to be a relative position from the source Game Object. /// Sets the position of this Game Object to be a relative position from the source Game Object.
// return this.phaserText.setPositionRelative(source, x, y); // return this.phaserText.setPositionRelative(source, x, y);
return this;
} }
setShadowOffset(_offsetX, _offsetY): this { setShadowOffset(_offsetX, _offsetY): this {

View File

@ -70,10 +70,10 @@ export function initTestFile() {
* @param x The relative x position * @param x The relative x position
* @param y The relative y position * @param y The relative y position
*/ */
const setPositionRelative = function (guideObject: any, x: number, y: number) { const setPositionRelative = function (guideObject: any, x: number, y: number): any {
const offsetX = guideObject.width * (-0.5 + (0.5 - guideObject.originX)); const offsetX = guideObject.width * (-0.5 + (0.5 - guideObject.originX));
const offsetY = guideObject.height * (-0.5 + (0.5 - guideObject.originY)); const offsetY = guideObject.height * (-0.5 + (0.5 - guideObject.originY));
this.setPosition(guideObject.x + offsetX + x, guideObject.y + offsetY + y); return this.setPosition(guideObject.x + offsetX + x, guideObject.y + offsetY + y);
}; };
Phaser.GameObjects.Container.prototype.setPositionRelative = setPositionRelative; Phaser.GameObjects.Container.prototype.setPositionRelative = setPositionRelative;