Fixup mock gameobject methods to match phaser gameobject returns

This commit is contained in:
Sirz Benjie 2025-04-23 09:17:25 -05:00
parent ec3a54273d
commit 1f14325ac7
No known key found for this signature in database
GPG Key ID: 4A524B4D196C759E
7 changed files with 356 additions and 180 deletions

View File

@ -14,11 +14,12 @@ export class PlayerBattleInfo extends BattleInfo {
expBar.setOrigin(0);
this.add(expBar);
const expMaskRect = globalScene.make.graphics({});
expMaskRect.setScale(6);
expMaskRect.fillStyle(0xffffff);
expMaskRect.beginPath();
expMaskRect.fillRect(127, 126, 85, 2);
const expMaskRect = globalScene.make
.graphics({})
.setScale(6)
.fillStyle(0xffffff)
.beginPath()
.fillRect(127, 126, 85, 2);
const expMask = expMaskRect.createGeometryMask();

View File

@ -1,3 +1,4 @@
export interface MockGameObject {
name: string;
destroy?(): void;
}

View File

@ -2,199 +2,252 @@ import type MockTextureManager from "#test/testUtils/mocks/mockTextureManager";
import type { MockGameObject } from "../mockGameObject";
export default class MockContainer implements MockGameObject {
protected x;
protected y;
protected x: number;
protected y: number;
protected scene;
protected width;
protected height;
protected visible;
private alpha;
protected width: number;
protected height: number;
protected visible: boolean;
private alpha: number;
private style;
public frame;
protected textureManager;
public list: MockGameObject[] = [];
public name: string;
constructor(textureManager: MockTextureManager, x, y) {
constructor(textureManager: MockTextureManager, x: number, y: number) {
this.x = x;
this.y = y;
this.frame = {};
this.textureManager = textureManager;
}
setVisible(visible) {
setVisible(visible: boolean): this {
this.visible = visible;
return this;
}
once(_event, _callback, _source) {}
once(_event, _callback, _source): this {
return this;
}
off(_event, _callback, _source) {}
off(_event, _callback, _source): this {
return this;
}
removeFromDisplayList() {
removeFromDisplayList(): this {
// same as remove or destroy
return this;
}
removeBetween(_startIndex, _endIndex, _destroyChild) {
removeBetween(_startIndex, _endIndex, _destroyChild): this {
// Removes multiple children across an index range
return this;
}
addedToScene() {
// This callback is invoked when this Game Object is added to a Scene.
}
setSize(_width, _height) {
setSize(_width: number, _height: number): this {
// Sets the size of this Game Object.
return this;
}
setMask() {
setMask(): this {
/// Sets the mask that this Game Object will use to render with.
return this;
}
setPositionRelative(_source, _x, _y) {
/// Sets the position of this Game Object to be a relative position from the source Game Object.
}
setInteractive = () => null;
setOrigin(x, y) {
this.x = x;
this.y = y;
setInteractive(): this {
return this;
}
setAlpha(alpha) {
setOrigin(x = 0.5, y = x): this {
this.x = x;
this.y = y;
return this;
}
setAlpha(alpha = 1): this {
this.alpha = alpha;
return this;
}
setFrame(_frame, _updateSize?: boolean, _updateOrigin?: boolean) {
setFrame(_frame, _updateSize?: boolean, _updateOrigin?: boolean): this {
// Sets the frame this Game Object will use to render with.
return this;
}
setScale(_scale) {
setScale(_x = 1, _y = _x): this {
// Sets the scale of this Game Object.
return this;
}
setPosition(x, y) {
setPosition(x = 0, y = x, _z = 0, _w = 0): this {
this.x = x;
this.y = y;
return this;
}
setX(x) {
setX(x = 0): this {
this.x = x;
return this;
}
setY(y) {
setY(y = 0): this {
this.y = y;
return this;
}
destroy() {
this.list = [];
}
setShadow(_shadowXpos, _shadowYpos, _shadowColor) {
setShadow(_shadowXpos, _shadowYpos, _shadowColor): this {
// Sets the shadow settings for this Game Object.
return this;
}
setLineSpacing(_lineSpacing) {
setLineSpacing(_lineSpacing): this {
// Sets the line spacing value of this Game Object.
return this;
}
setText(_text) {
setText(_text): this {
// Sets the text this Game Object will display.
return this;
}
setAngle(_angle) {
setAngle(_angle): this {
// Sets the angle of this Game Object.
return this;
}
setShadowOffset(_offsetX, _offsetY) {
setShadowOffset(_offsetX, _offsetY): this {
// Sets the shadow offset values.
return this;
}
setWordWrapWidth(_width) {
// Sets the width (in pixels) to use for wrapping lines.
}
setFontSize(_fontSize) {
setFontSize(_fontSize): this {
// Sets the font size of this Game Object.
return this;
}
getBounds() {
return { width: this.width, height: this.height };
}
setColor(_color) {
setColor(_color): this {
// Sets the tint of this Game Object.
return this;
}
setShadowColor(_color) {
setShadowColor(_color): this {
// Sets the shadow color.
return this;
}
setTint(_color) {
setTint(_color: this) {
// Sets the tint of this Game Object.
return this;
}
setStrokeStyle(_thickness, _color) {
setStrokeStyle(_thickness, _color): this {
// Sets the stroke style for the graphics.
return this;
}
setDepth(_depth) {
// Sets the depth of this Game Object.
setDepth(_depth): this {
// Sets the depth of this Game Object.\
return this;
}
setTexture(_texture) {
// Sets the texture this Game Object will use to render with.
setTexture(_texture): this {
// Sets the texture this Game Object will use to render with.\
return this;
}
clearTint() {
// Clears any previously set tint.
clearTint(): this {
// Clears any previously set tint.\
return this;
}
sendToBack() {
// Sends this Game Object to the back of its parent's display list.
sendToBack(): this {
// Sends this Game Object to the back of its parent's display list.\
return this;
}
moveTo(_obj) {
// Moves this Game Object to the given index in the list.
moveTo(_obj): this {
// Moves this Game Object to the given index in the list.\
return this;
}
moveAbove(_obj) {
moveAbove(_obj): this {
// Moves this Game Object to be above the given Game Object in the display list.
return this;
}
moveBelow(_obj) {
moveBelow(_obj): this {
// Moves this Game Object to be below the given Game Object in the display list.
return this;
}
setName(name: string) {
setName(name: string): this {
this.name = name;
return this;
}
bringToTop(_obj) {
bringToTop(_obj): this {
// Brings this Game Object to the top of its parents display list.
return this;
}
on(_event, _callback, _source) {}
on(_event, _callback, _source): this {
return this;
}
add(obj) {
add(...obj: MockGameObject[]): this {
// Adds a child to this Game Object.
this.list.push(obj);
this.list.push(...obj);
return this;
}
removeAll() {
removeAll(): this {
// Removes all Game Objects from this Container.
this.list = [];
return this;
}
addAt(obj, index) {
addAt(obj: MockGameObject | MockGameObject[], index = 0): this {
// Adds a Game Object to this Container at the given index.
this.list.splice(index, 0, obj);
if (!Array.isArray(obj)) {
obj = [obj];
}
this.list.splice(index, 0, ...obj);
return this;
}
remove(obj) {
const index = this.list.indexOf(obj);
remove(obj: MockGameObject | MockGameObject[], destroyChild = false): this {
if (!Array.isArray(obj)) {
obj = [obj];
}
for (const item of obj) {
const index = this.list.indexOf(item);
if (index !== -1) {
this.list.splice(index, 1);
}
if (destroyChild) {
item.destroy?.();
}
}
return this;
}
getIndex(obj) {
@ -210,15 +263,28 @@ export default class MockContainer implements MockGameObject {
return this.list;
}
getByName(key: string) {
getByName(key: string): MockGameObject | null {
return this.list.find(v => v.name === key) ?? new MockContainer(this.textureManager, 0, 0);
}
disableInteractive = () => null;
disableInteractive(): this {
return this;
}
each(method) {
each(method): this {
for (const item of this.list) {
method(item);
}
return this;
}
copyPosition(source: { x?: number; y?: number }): this {
if (source.x !== undefined) {
this.x = source.x;
}
if (source.y !== undefined) {
this.y = source.y;
}
return this;
}
}

View File

@ -8,34 +8,49 @@ export default class MockGraphics implements MockGameObject {
this.scene = textureManager.scene;
}
fillStyle(_color) {
fillStyle(_color): this {
// Sets the fill style to be used by the fill methods.
return this;
}
beginPath() {
beginPath(): this {
// Starts a new path by emptying the list of sub-paths. Call this method when you want to create a new path.
return this;
}
fillRect(_x, _y, _width, _height) {
fillRect(_x, _y, _width, _height): this {
// Adds a rectangle shape to the path which is filled when you call fill().
return this;
}
createGeometryMask() {
createGeometryMask(): this {
// Creates a geometry mask.
return this;
}
setOrigin(_x, _y) {}
setOrigin(_x, _y): this {
return this;
}
setAlpha(_alpha) {}
setAlpha(_alpha): this {
return this;
}
setVisible(_visible) {}
setVisible(_visible): this {
return this;
}
setName(_name) {}
setName(_name) {
return this;
}
once(_event, _callback, _source) {}
once(_event, _callback, _source) {
return this;
}
removeFromDisplayList() {
removeFromDisplayList(): this {
// same as remove or destroy
return this;
}
addedToScene() {
@ -50,15 +65,18 @@ export default class MockGraphics implements MockGameObject {
this.list = [];
}
setScale(_scale) {
// Sets the scale of this Game Object.
setScale(_scale): this {
return this;
}
off(_event, _callback, _source) {}
off(_event, _callback, _source): this {
return this;
}
add(obj) {
add(obj): this {
// Adds a child to this Game Object.
this.list.push(obj);
return this;
}
removeAll() {
@ -90,4 +108,8 @@ export default class MockGraphics implements MockGameObject {
getAll() {
return this.list;
}
copyPosition(_source): this {
return this;
}
}

View File

@ -10,17 +10,28 @@ export default class MockRectangle implements MockGameObject {
this.fillColor = fillColor;
this.scene = textureManager.scene;
}
setOrigin(_x, _y) {}
setOrigin(_x, _y): this {
return this;
}
setAlpha(_alpha) {}
setVisible(_visible) {}
setAlpha(_alpha): this {
return this;
}
setVisible(_visible): this {
return this;
}
setName(_name) {}
setName(_name): this {
return this;
}
once(_event, _callback, _source) {}
once(_event, _callback, _source): this {
return this;
}
removeFromDisplayList() {
removeFromDisplayList(): this {
// same as remove or destroy
return this;
}
addedToScene() {
@ -35,9 +46,10 @@ export default class MockRectangle implements MockGameObject {
this.list = [];
}
add(obj) {
add(obj): this {
// Adds a child to this Game Object.
this.list.push(obj);
return this;
}
removeAll() {
@ -45,16 +57,18 @@ export default class MockRectangle implements MockGameObject {
this.list = [];
}
addAt(obj, index) {
addAt(obj, index): this {
// Adds a Game Object to this Container at the given index.
this.list.splice(index, 0, obj);
return this;
}
remove(obj) {
remove(obj): this {
const index = this.list.indexOf(obj);
if (index !== -1) {
this.list.splice(index, 1);
}
return this;
}
getIndex(obj) {
@ -69,9 +83,12 @@ export default class MockRectangle implements MockGameObject {
getAll() {
return this.list;
}
setScale(_scale) {
setScale(_scale): this {
// return this.phaserText.setScale(scale);
return this;
}
off() {}
off(): this {
return this;
}
}

View File

@ -22,6 +22,7 @@ export default class MockSprite implements MockGameObject {
// @ts-ignore
Phaser.GameObjects.Sprite.prototype.setTexture = this.setTexture;
Phaser.GameObjects.Sprite.prototype.setSizeToFrame = this.setSizeToFrame;
// @ts-ignore
Phaser.GameObjects.Sprite.prototype.setFrame = this.setFrame;
// Phaser.GameObjects.Sprite.prototype.disable = this.disable;
@ -37,7 +38,7 @@ export default class MockSprite implements MockGameObject {
};
}
setTexture(_key: string, _frame?: string | number) {
setTexture(_key: string, _frame?: string | number): this {
return this;
}
@ -45,38 +46,47 @@ export default class MockSprite implements MockGameObject {
return {} as Sprite;
}
setPipeline(obj) {
setPipeline(obj): this {
// Sets the pipeline of this Game Object.
return this.phaserSprite.setPipeline(obj);
this.phaserSprite.setPipeline(obj);
return this;
}
off(_event, _callback, _source) {}
off(_event, _callback, _source): this {
return this;
}
setTintFill(color) {
setTintFill(color): this {
// Sets the tint fill color.
return this.phaserSprite.setTintFill(color);
this.phaserSprite.setTintFill(color);
return this;
}
setScale(scale) {
return this.phaserSprite.setScale(scale);
setScale(scale): this {
this.phaserSprite.setScale(scale);
return this;
}
setOrigin(x, y) {
return this.phaserSprite.setOrigin(x, y);
setOrigin(x, y): this {
this.phaserSprite.setOrigin(x, y);
return this;
}
setSize(width, height) {
setSize(width, height): this {
// Sets the size of this Game Object.
return this.phaserSprite.setSize(width, height);
this.phaserSprite.setSize(width, height);
return this;
}
once(event, callback, source) {
return this.phaserSprite.once(event, callback, source);
once(event, callback, source): this {
this.phaserSprite.once(event, callback, source);
return this;
}
removeFromDisplayList() {
removeFromDisplayList(): this {
// same as remove or destroy
return this.phaserSprite.removeFromDisplayList();
this.phaserSprite.removeFromDisplayList();
return this;
}
addedToScene() {
@ -84,97 +94,116 @@ export default class MockSprite implements MockGameObject {
return this.phaserSprite.addedToScene();
}
setVisible(visible) {
return this.phaserSprite.setVisible(visible);
setVisible(visible): this {
this.phaserSprite.setVisible(visible);
return this;
}
setPosition(x, y) {
return this.phaserSprite.setPosition(x, y);
setPosition(x, y): this {
this.phaserSprite.setPosition(x, y);
return this;
}
setRotation(radians) {
return this.phaserSprite.setRotation(radians);
setRotation(radians): this {
this.phaserSprite.setRotation(radians);
return this;
}
stop() {
return this.phaserSprite.stop();
stop(): this {
this.phaserSprite.stop();
return this;
}
setInteractive = () => null;
on(event, callback, source) {
return this.phaserSprite.on(event, callback, source);
setInteractive(): this {
return this;
}
setAlpha(alpha) {
return this.phaserSprite.setAlpha(alpha);
on(event, callback, source): this {
this.phaserSprite.on(event, callback, source);
return this;
}
setTint(color) {
setAlpha(alpha): this {
this.phaserSprite.setAlpha(alpha);
return this;
}
setTint(color): this {
// Sets the tint of this Game Object.
return this.phaserSprite.setTint(color);
this.phaserSprite.setTint(color);
return this;
}
setFrame(frame, _updateSize?: boolean, _updateOrigin?: boolean) {
setFrame(frame, _updateSize?: boolean, _updateOrigin?: boolean): this {
// Sets the frame this Game Object will use to render with.
this.frame = frame;
return frame;
return this;
}
setPositionRelative(source, x, y) {
/// Sets the position of this Game Object to be a relative position from the source Game Object.
return this.phaserSprite.setPositionRelative(source, x, y);
this.phaserSprite.setPositionRelative(source, x, y);
}
setY(y) {
return this.phaserSprite.setY(y);
setY(y: number): this {
this.phaserSprite.setY(y);
return this;
}
setCrop(x, y, width, height) {
setCrop(x: number, y: number, width: number, height: number): this {
// Sets the crop size of this Game Object.
return this.phaserSprite.setCrop(x, y, width, height);
this.phaserSprite.setCrop(x, y, width, height);
return this;
}
clearTint() {
clearTint(): this {
// Clears any previously set tint.
return this.phaserSprite.clearTint();
this.phaserSprite.clearTint();
return this;
}
disableInteractive() {
disableInteractive(): this {
// Disables Interactive features of this Game Object.
return null;
return this;
}
apply() {
return this.phaserSprite.apply();
this.phaserSprite.apply();
return this;
}
play() {
play(): this {
// return this.phaserSprite.play();
return this;
}
setPipelineData(key, value) {
setPipelineData(key: string, value: any): this {
this.pipelineData[key] = value;
return this;
}
destroy() {
return this.phaserSprite.destroy();
}
setName(name) {
return this.phaserSprite.setName(name);
setName(name: string): this {
this.phaserSprite.setName(name);
return this;
}
setAngle(angle) {
return this.phaserSprite.setAngle(angle);
setAngle(angle): this {
this.phaserSprite.setAngle(angle);
return this;
}
setMask() {}
setMask(): this {
return this;
}
add(obj) {
add(obj): this {
// Adds a child to this Game Object.
this.list.push(obj);
return this;
}
removeAll() {
@ -182,16 +211,18 @@ export default class MockSprite implements MockGameObject {
this.list = [];
}
addAt(obj, index) {
addAt(obj, index): this {
// Adds a Game Object to this Container at the given index.
this.list.splice(index, 0, obj);
return this;
}
remove(obj) {
remove(obj): this {
const index = this.list.indexOf(obj);
if (index !== -1) {
this.list.splice(index, 1);
}
return this;
}
getIndex(obj) {
@ -206,4 +237,9 @@ export default class MockSprite implements MockGameObject {
getAll() {
return this.list;
}
copyPosition(obj): this {
this.phaserSprite.copyPosition(obj);
return this;
}
}

View File

@ -107,42 +107,51 @@ export default class MockText implements MockGameObject {
}
}
setScale(_scale) {
setScale(_scale): this {
// return this.phaserText.setScale(scale);
return this;
}
setShadow(_shadowXpos, _shadowYpos, _shadowColor) {
setShadow(_shadowXpos, _shadowYpos, _shadowColor): this {
// Sets the shadow settings for this Game Object.
// return this.phaserText.setShadow(shadowXpos, shadowYpos, shadowColor);
return this;
}
setLineSpacing(_lineSpacing) {
setLineSpacing(_lineSpacing): this {
// Sets the line spacing value of this Game Object.
// return this.phaserText.setLineSpacing(lineSpacing);
return this;
}
setOrigin(_x, _y) {
setOrigin(_x, _y): this {
// return this.phaserText.setOrigin(x, y);
return this;
}
once(_event, _callback, _source) {
once(_event, _callback, _source): this {
// return this.phaserText.once(event, callback, source);
return this;
}
off(_event, _callback, _obj) {}
removedFromScene() {}
addToDisplayList() {}
setStroke(_color, _thickness) {
// Sets the stroke color and thickness.
// return this.phaserText.setStroke(color, thickness);
addToDisplayList(): this {
return this;
}
removeFromDisplayList() {
setStroke(_color, _thickness): this {
// Sets the stroke color and thickness.
// return this.phaserText.setStroke(color, thickness);
return this;
}
removeFromDisplayList(): this {
// same as remove or destroy
// return this.phaserText.removeFromDisplayList();
return this;
}
addedToScene() {
@ -154,12 +163,14 @@ export default class MockText implements MockGameObject {
// return this.phaserText.setVisible(visible);
}
setY(_y) {
setY(_y): this {
// return this.phaserText.setY(y);
return this;
}
setX(_x) {
setX(_x): this {
// return this.phaserText.setX(x);
return this;
}
/**
@ -169,17 +180,21 @@ export default class MockText implements MockGameObject {
* @param z The z position of this Game Object. Default 0.
* @param w The w position of this Game Object. Default 0.
*/
setPosition(_x?: number, _y?: number, _z?: number, _w?: number) {}
setPosition(_x?: number, _y?: number, _z?: number, _w?: number): this {
return this;
}
setText(text) {
setText(text): this {
// Sets the text this Game Object will display.
// return this.phaserText.setText\(text);
this.text = text;
return this;
}
setAngle(_angle) {
setAngle(_angle): this {
// Sets the angle of this Game Object.
// return this.phaserText.setAngle(angle);
return this;
}
setPositionRelative(_source, _x, _y) {
@ -187,9 +202,10 @@ export default class MockText implements MockGameObject {
// return this.phaserText.setPositionRelative(source, x, y);
}
setShadowOffset(_offsetX, _offsetY) {
setShadowOffset(_offsetX, _offsetY): this {
// Sets the shadow offset values.
// return this.phaserText.setShadowOffset(offsetX, offsetY);
return this;
}
setWordWrapWidth(width) {
@ -197,9 +213,10 @@ export default class MockText implements MockGameObject {
this.wordWrapWidth = width;
}
setFontSize(_fontSize) {
setFontSize(_fontSize): this {
// Sets the font size of this Game Object.
// return this.phaserText.setFontSize(fontSize);
return this;
}
getBounds() {
@ -209,25 +226,31 @@ export default class MockText implements MockGameObject {
};
}
setColor(color: string) {
setColor(color: string): this {
this.color = color;
return this;
}
setInteractive = () => null;
setInteractive(): this {
return this;
}
setShadowColor(_color) {
setShadowColor(_color): this {
// Sets the shadow color.
// return this.phaserText.setShadowColor(color);
return this;
}
setTint(_color) {
setTint(_color): this {
// Sets the tint of this Game Object.
// return this.phaserText.setTint(color);
return this;
}
setStrokeStyle(_thickness, _color) {
setStrokeStyle(_thickness, _color): this {
// Sets the stroke style for the graphics.
// return this.phaserText.setStrokeStyle(thickness, color);
return this;
}
destroy() {
@ -235,20 +258,24 @@ export default class MockText implements MockGameObject {
this.list = [];
}
setAlpha(_alpha) {
setAlpha(_alpha): this {
// return this.phaserText.setAlpha(alpha);
return this;
}
setName(name: string) {
setName(name: string): this {
this.name = name;
return this;
}
setAlign(_align) {
setAlign(_align): this {
// return this.phaserText.setAlign(align);
return this;
}
setMask() {
setMask(): this {
/// Sets the mask that this Game Object will use to render with.
return this;
}
getBottomLeft() {
@ -265,37 +292,43 @@ export default class MockText implements MockGameObject {
};
}
disableInteractive() {
disableInteractive(): this {
// Disables interaction with this Game Object.
return this;
}
clearTint() {
clearTint(): this {
// Clears tint on this Game Object.
return this;
}
add(obj) {
add(obj): this {
// Adds a child to this Game Object.
this.list.push(obj);
return this;
}
removeAll() {
removeAll(): this {
// Removes all Game Objects from this Container.
this.list = [];
return this;
}
addAt(obj, index) {
addAt(obj, index): this {
// Adds a Game Object to this Container at the given index.
this.list.splice(index, 0, obj);
return this;
}
remove(obj) {
remove(obj): this {
const index = this.list.indexOf(obj);
if (index !== -1) {
this.list.splice(index, 1);
}
return this;
}
getIndex(obj) {
getIndex(obj): number {
const index = this.list.indexOf(obj);
return index || -1;
}