import { BitmapText } from '../display/bitmap_text.js'; import { Button } from '../display/button.js'; import { Graphics } from '../display/graphics.js'; import { Group } from '../display/group.js'; import { Image } from '../display/image.js'; import { Text } from '../display/text.js'; import type { Game } from './game.js'; import type { SignalListener } from './signal.js'; import type { TextStyle } from '../display/text.js'; export class GameObjectFactory { public game!: Game; /** * Creates a new GameObjectFactory instance. * @param {Game} game - The game instance this factory belongs to. */ public constructor(game: Game) { this.game = game; } /** * Creates a new Image object. * @param {number} x - The x coordinate of the image. * @param {number} y - The y coordinate of the image. * @param {string} key - The texture key for the image. * @param {string|number} frame - The frame to display. * @param {Group} group - The parent group for the image. * @returns {Image} The created Image object. */ public image( x: number, y: number, key: string, frame: string | number | null = null, group: Group | null = null ): Image { const target = group ?? this.game.world; return target.add(new Image(this.game, x, y, key, frame)); } /** * Creates a new Group object. * @param {Group | null} parent - The parent group for the new group. * @param {string | null} name - The name of the group. * @param {boolean} addToStage - Whether to add the group to the stage. * @returns {Group} The created Group object. */ public group(parent: Group | null = null, name: string | null = null, addToStage = false): Group { return new Group(this.game, parent, name, addToStage); } /** * Creates a new Text object. * @param {number} x - The x coordinate of the text. * @param {number} y - The y coordinate of the text. * @param {string} text - The text content to display. * @param {object} style - The style configuration for the text. * @param {Group | null} group - The parent group for the text. * @returns {Text} The created Text object. */ public text(x: number, y: number, text: string | number, style: TextStyle, group: Group | null = null): Text { const parent = group ?? this.game.world; return parent.add(new Text(this.game, x, y, text, style)); } /** * Creates a new Button object. * @param {number} x - The x coordinate of the button. * @param {number} y - The y coordinate of the button. * @param {string} key - The texture key for the button. * @param {Function | null} callback - The function to call when the button is clicked. * @param {object | null} callbackContext - The context to apply when calling the callback. * @param {string | null} overFrame - The frame to display when the mouse is over the button. * @param {string | null} outFrame - The frame to display when the mouse is not over the button. * @param {string | null} downFrame - The frame to display when the button is pressed. * @param {string | null} upFrame - The frame to display when the button is released. * @param {Group | null} group - The parent group for the button. * @returns {Button} The created Button object. */ public button( x: number, y: number, key: string, callback: SignalListener | null = null, callbackContext: unknown = null, overFrame: string | null = null, outFrame: string | null = null, downFrame: string | null = null, upFrame: string | null = null, group: Group | null = null ): Button { const parent = group ?? this.game.world; return parent.add( new Button(this.game, x, y, key, callback, callbackContext, overFrame, outFrame, downFrame, upFrame) ); } /** * Creates a new Graphics object. * @param {number} x - The x coordinate of the graphics object. * @param {number} y - The y coordinate of the graphics object. * @param {Group | null} group - The parent group for the graphics object. * @returns {Graphics} The created Graphics object. */ public graphics(x = 0, y = 0, group: Group | null = null): Graphics { const parent = group ?? this.game.world; return parent.add(new Graphics(this.game, x, y)); } /** * Creates a new BitmapText object. * @param {number} x - The x coordinate of the bitmap text. * @param {number} y - The y coordinate of the bitmap text. * @param {string} font - The font key for the bitmap text. * @param {string} text - The text content to display. * @param {number} size - The font size. * @param {Group | null} group - The parent group for the bitmap text. * @param {string} align - The alignment of the text. * @returns {BitmapText} The created BitmapText object. */ public bitmapText( x: number, y: number, font: string, text: string, size: number, group: Group | null = null, align = 'left' ): BitmapText { const parent = group ?? this.game.world; return parent.add(new BitmapText(this.game, x, y, font, text, size, align)); } }