UNPKG

jsge

Version:

Javascript Game Engine

268 lines (234 loc) 10.6 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="expires" content="Wen, 05 Jun 2024 07:01:00 GMT"> <title>JSDoc: Source: base/DrawObjectFactory.js</title> <script src="scripts/prettify/prettify.js"> </script> <script src="scripts/prettify/lang-css.js"> </script> <!--[if lt IE 9]> <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css"> <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css"> </head> <body> <div id="main"> <h1 class="stage-title">Source: base/DrawObjectFactory.js</h1> <section> <article> <pre class="prettyprint source linenums"><code>import { DrawRectObject } from "./2d/DrawRectObject.js"; import { DrawTextObject } from "./2d/DrawTextObject.js"; import { DrawConusObject } from "./2d/DrawConusObject.js"; import { DrawImageObject } from "./2d/DrawImageObject.js"; import { DrawLineObject } from "./2d/DrawLineObject.js"; import { DrawPolygonObject } from "./2d/DrawPolygonObject.js"; import { DrawCircleObject } from "./2d/DrawCircleObject.js"; import { DrawTiledLayer } from "./2d/DrawTiledLayer.js"; import { DrawShapeObject } from "./2d/DrawShapeObject.js"; import { GameStageData } from "./GameStageData.js"; import { Exception, Warning } from "./Exception.js"; import { ERROR_CODES, WARNING_CODES } from "../constants.js"; import AssetsManager from "../../modules/assetsm/src/AssetsManager.js"; /** * Creates drawObjects instances.&lt;br> * accessible via GameStage.draw &lt;br> * Attach images for image objects and tilemaps &lt;br> * Adds drawObjects to current GameStage.stageData * @see {@link GameStage} a part of GameStage */ export class DrawObjectFactory { /** * @type {AssetsManager} */ #iLoader; /** * @type {GameStageData | null} */ #currentPageData; /** * @hideconstructor */ constructor(iLoader) { this.#iLoader = iLoader; } get stageData() { return this.#currentPageData; } /** * * @param {*} renderObject * @returns {void} */ #addObjectToPageData(renderObject) { this.#currentPageData._renderObject = renderObject; } /** * @param {number} x * @param {number} y * @param {number} width * @param {number} height * @param {string} backgroundColor - rgba(r,g,b,a) * @returns {DrawRectObject} */ rect(x, y, width, height, backgroundColor) { const renderObject = new DrawRectObject(x, y, width, height, backgroundColor); this.#addObjectToPageData(renderObject); return renderObject; } /** * @param {number} x * @param {number} y * @param {string} text * @param {string} font - size fontFamily * @param {string} color - rgba(r,g,b,a) * @returns {DrawTextObject} */ text(x, y, text, font, color) { const renderObject = new DrawTextObject(x, y, text, font, color); this.#addObjectToPageData(renderObject); return renderObject; } /** * * @param {number} radius * @param {string} bgColor - rgba(r,g,b,a) * @param {number=} angle * @param {number=} [fade=0] (0 - 1) * @returns {DrawConusObject} */ conus(x, y, radius, bgColor, angle, fade = 0) { const renderObject = new DrawConusObject(x, y, radius, bgColor, angle, fade); this.#addObjectToPageData(renderObject); return renderObject; } /** * * @param {number} radius * @param {string} bgColor - rgba(r,g,b,a) * @returns {DrawCircleObject} */ circle(x, y, radius, bgColor) { const renderObject = new DrawCircleObject(x, y, radius, bgColor); this.#addObjectToPageData(renderObject); return renderObject; } /** * @param {number} x * @param {number} y * @param {number} width * @param {number} height * @param {string} key * @param {number} [imageIndex = 0] * @param {Array&lt;{x:Number, y:Number}> | {r:number}=} boundaries - boundaries as polygon, or circle * @param {number} [spacing = 0] - for tilesets.spacing > 0 * @param {number} [margin = 0] - for tilesets.margin > 0 * @returns {DrawImageObject} */ image(x, y, width, height, key, imageIndex = 0, boundaries, spacing = 0, margin = 0) { const image = this.#iLoader.getImage(key); if (!image) { Exception(ERROR_CODES.CANT_GET_THE_IMAGE, "iLoader can't get the image with key: " + key); } const renderObject = new DrawImageObject(x, y, width, height, key, imageIndex, boundaries, image, spacing, margin); this.#addObjectToPageData(renderObject); return renderObject; } /** * @param {Array&lt;number>} vertices * @param {string} color - rgba(r,g,b,a) * @returns {DrawLineObject} */ line(vertices, color) { const renderObject = new DrawLineObject(vertices, color); this.#addObjectToPageData(renderObject); return renderObject; } /** * @param {Array&lt;{x:number, y:number}>} vertices - should go in anticlockwise order * @param {string} bgColor - rgba(r,g,b,a) * @returns {DrawPolygonObject} */ polygon(vertices, bgColor) { const renderObject = new DrawPolygonObject(vertices, bgColor); this.#addObjectToPageData(renderObject); return renderObject; } /** * * @param {string} layerKey * @param {string} tileMapKey * @param {boolean=} setBoundaries * @param {DrawShapeObject=} shapeMask * @returns {DrawTiledLayer} */ tiledLayer(layerKey, tileMapKey, setBoundaries, shapeMask) { const tilemap = this.#iLoader.getTileMap(tileMapKey), layerData = Object.assign({}, tilemap.layers.find((layer) => layer.name === layerKey)), // copy to avoid change same tilemap instance in different tiledLayers tilesetIds = Array.from(new Set(layerData.data.filter((id) => id !== 0))).sort((a, b) => a - b), tilesets = tilemap.tilesets.map((tileset) => Object.assign({}, tileset)).filter((tileset) => { const tilesetStartI = tileset.firstgid, tilesetLastI = tilesetStartI + tileset.tilecount; if (tilesetIds.find((id) => ((id >= tilesetStartI) &amp;&amp; (id &lt; tilesetLastI)))) { return true; } else { return false; } }), // copy to avoid change same tilemap instance in different tiledLayers tilesetImages = tilesets.map((tileset) => this.#iLoader.getImage(tileset.name)), renderObject = new DrawTiledLayer(layerKey, tileMapKey, tilemap, tilesets, tilesetImages, layerData, setBoundaries, shapeMask); if (tilesetImages.length > 1) { Warning(WARNING_CODES.MULTIPLE_IMAGE_TILESET, " tileset " + layerKey + " includes multiple images, it can case performance issues!"); } //console.log(layerKey); //console.log(tilesetIds); this.#addObjectToPageData(renderObject); return renderObject; } /** * @ignore * @param {string} methodKey * @param {Function} createObjectInstance */ _registerNewObjectMethod = (methodKey, createObjectInstance) => { this[methodKey] = (...args) => this.#createObjectAndAddToPageData(createObjectInstance, ...args); }; /** * @ignore * @param {Function} createInstance * @param {Array&lt;any>} args */ #createObjectAndAddToPageData = (createInstance, ...args) => { const instance = createInstance(...args); this.#addObjectToPageData(instance); return instance; }; /** * @ignore * @param {GameStageData} pageData; */ _attachPageData = (pageData) => { this.#currentPageData = pageData; }; /** * @ignore */ _detachPageData = () => { this.#currentPageData = null; }; }</code></pre> </article> </section> </div> <nav> <div class="switcher"><ul><li class="active"><a href="/">1.5.9</a></li></div> <h2><a href="/">Home</a></h2><h3>Classes</h3><ul><li><a href="DrawCircleObject.html">DrawCircleObject</a></li><li><a href="DrawConusObject.html">DrawConusObject</a></li><li><a href="DrawImageObject.html">DrawImageObject</a></li><li><a href="DrawLineObject.html">DrawLineObject</a></li><li><a href="DrawObjectFactory.html">DrawObjectFactory</a></li><li><a href="DrawPolygonObject.html">DrawPolygonObject</a></li><li><a href="DrawRectObject.html">DrawRectObject</a></li><li><a href="DrawShapeObject.html">DrawShapeObject</a></li><li><a href="DrawTextObject.html">DrawTextObject</a></li><li><a href="DrawTiledLayer.html">DrawTiledLayer</a></li><li><a href="GameStage.html">GameStage</a></li><li><a href="GameStageData.html">GameStageData</a></li><li><a href="IExtension.html">IExtension</a></li><li><a href="INetwork.html">INetwork</a></li><li><a href="IRender.html">IRender</a></li><li><a href="ISystem.html">ISystem</a></li><li><a href="ISystemAudio.html">ISystemAudio</a></li><li><a href="RenderLoop.html">RenderLoop</a></li><li><a href="RenderLoopDebug.html">RenderLoopDebug</a></li><li><a href="System.html">System</a></li><li><a href="SystemSettings.html">SystemSettings</a></li></ul><h3>Tutorials</h3><ul><li><a href="tutorial-application_scheme.html">Application Scheme</a></li><li><a href="tutorial-assets_manager.html">Assets Manager</a></li><li><a href="tutorial-common_issues.html">Common Issues</a></li><li><a href="tutorial-how_to_add_and_use_audio.html">How to add and use audio</a></li><li><a href="tutorial-how_to_do_animations.html">How to Create Animations</a></li><li><a href="tutorial-how_to_load_and_use_tilemaps.html">How to Load and Use Tilemaps</a></li><li><a href="tutorial-quick_start.html">Quick Start</a></li><li><a href="tutorial-spine_animations.html">How to Add Spine Animations</a></li><li><a href="tutorial-stages_lifecycle.html">Stages Lifecycle</a></li></ul> </nav> <br class="clear"> <footer> Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.4</a> on Wed Jun 18 2025 06:53:54 GMT+0000 (Coordinated Universal Time) </footer> <script> prettyPrint(); </script> <script src="scripts/linenumber.js"> </script> </body> </html>