UNPKG

@petkoneo/phaser3-rex-plugins

Version:
1,625 lines (1,337 loc) 148 kB
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : typeof define === 'function' && define.amd ? define(factory) : (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.rexperspectiveimageplugin = factory()); })(this, (function () { 'use strict'; const Mesh = Phaser.GameObjects.Mesh; class MeshBase extends Mesh { get tint() { if (this.vertices.length === 0) { return 0xffffff; } else { return this.vertices[0].color; } } forceUpdate() { this.dirtyCache[10] = 1; return this; } } const Vector3 = Phaser.Math.Vector3; const Matrix4 = Phaser.Math.Matrix4; var tempPosition = new Vector3(); var tempRotation = new Vector3(); var tempMatrix = new Matrix4(); var TransformVerts = function (mesh, x, y, z, rotateX, rotateY, rotateZ) { if (x === undefined) { x = 0; } if (y === undefined) { y = 0; } if (z === undefined) { z = 0; } if (rotateX === undefined) { rotateX = 0; } if (rotateY === undefined) { rotateY = 0; } if (rotateZ === undefined) { rotateZ = 0; } tempPosition.set(x, y, z); tempRotation.set(rotateX, rotateY, rotateZ); tempMatrix.fromRotationXYTranslation(tempRotation, tempPosition, true); for (var i = 0, cnt = mesh.vertices.length; i < cnt; i++) { mesh.vertices[i].transformMat4(tempMatrix); } }; const IsPlainObject$6 = Phaser.Utils.Objects.IsPlainObject; const GetValue$h = Phaser.Utils.Objects.GetValue; const GenerateGridVerts = Phaser.Geom.Mesh.GenerateGridVerts; const RadToDeg$4 = Phaser.Math.RadToDeg; const DegToRad$6 = Phaser.Math.DegToRad; const FOV = 45; const PanZ = 1 + (1 / Math.sin(DegToRad$6(FOV))); class Image extends MeshBase { constructor(scene, x, y, key, frame, config) { if (IsPlainObject$6(x)) { config = x; x = GetValue$h(config, 'x', 0); y = GetValue$h(config, 'y', 0); key = GetValue$h(config, 'key', null); frame = GetValue$h(config, 'frame', null); } super(scene, x, y, key, frame); this.type = 'rexPerspectiveImage'; this.setSizeToFrame(); this.resetPerspective(); this.panZ(PanZ); this.hideCCW = GetValue$h(config, 'hideCCW', true); var gridWidth = GetValue$h(config, 'gridWidth', 0); var gridHeight = GetValue$h(config, 'gridHeight', gridWidth); this.resetVerts(gridWidth, gridHeight); this.prevFrame = this.frame; } preUpdate(time, delta) { // Reset size and vertex if frame is changed if (this.prevFrame !== this.frame) { this.prevFrame = this.frame; this.syncSize(); } super.preUpdate(time, delta); } get originX() { return 0.5; } get originY() { return 0.5; } resetPerspective() { this.setPerspective(this.width, this.height, FOV); return this; } resetVerts(gridWidth, gridHeight) { if (gridWidth !== undefined) { this.gridWidth = gridWidth; } if (gridHeight !== undefined) { this.gridHeight = gridHeight; } // Clear faces and vertices this.clear(); this.dirtyCache[9] = -1; if ((this.width === 0) || (this.height === 0)) { return this; } // Generate faces and vertices var frameWidth = this.frame.cutWidth, frameHeight = this.frame.cutHeight; var gridWidth, gridHeight; if (this.gridWidth === 0) { gridWidth = Math.max(frameWidth / 8, 32); } else { gridHeight = this.gridWidth; } if (this.gridHeight === 0) { gridHeight = Math.max(frameHeight / 8, 32); } else { gridHeight = this.gridHeight; } GenerateGridVerts({ mesh: this, width: frameWidth / this.height, height: frameHeight / this.height, widthSegments: Math.ceil(frameWidth / gridWidth), heightSegments: Math.ceil(frameHeight / gridHeight), }); // Recover vertices transform var transformInfo = this.transformInfo; if (transformInfo) { this.transformVerts( transformInfo.x, transformInfo.y, transformInfo.z, transformInfo.rotateX, transformInfo.rotateY, transformInfo.rotateZ ); } return this; } syncSize() { this.setSizeToFrame(); // Reset size this.resetPerspective(); // Reset perspective this.resetVerts(); // Reset verts return this; } get rotationX() { return this.modelRotation.x; } set rotationX(value) { this.modelRotation.x = value; } get angleX() { return RadToDeg$4(this.rotationX); } set angleX(value) { this.rotationX = DegToRad$6(value); } get rotationY() { return this.modelRotation.y; } set rotationY(value) { this.modelRotation.y = value; } get angleY() { return RadToDeg$4(this.rotationY); } set angleY(value) { this.rotationY = DegToRad$6(value); } get rotationZ() { return this.modelRotation.z; } set rotationZ(value) { this.modelRotation.z = value; } get angleZ() { return RadToDeg$4(this.rotationZ); } set angleZ(value) { this.rotationZ = DegToRad$6(value); } transformVerts(x, y, z, rotateX, rotateY, rotateZ) { if (x === undefined) { x = 0; } if (y === undefined) { y = 0; } if (z === undefined) { z = 0; } if (rotateX === undefined) { rotateX = 0; } if (rotateY === undefined) { rotateY = 0; } if (rotateZ === undefined) { rotateZ = 0; } if (!this.transformInfo) { this.transformInfo = {}; } this.transformInfo.x = x; this.transformInfo.y = y; this.transformInfo.rotateX = rotateX; this.transformInfo.rotateY = rotateY; this.transformInfo.rotateZ = rotateZ; TransformVerts(this, x, y, z, rotateX, rotateY, rotateZ); return this; } } function PerspectiveImageFactory (x, y, texture, frame, config) { var gameObject = new Image(this.scene, x, y, texture, frame, config); this.scene.add.existing(gameObject); return gameObject; } const GetAdvancedValue$3 = Phaser.Utils.Objects.GetAdvancedValue; const BuildGameObject$5 = Phaser.GameObjects.BuildGameObject; function PerspectiveImageCreator (config, addToScene) { if (config === undefined) { config = {}; } if (addToScene !== undefined) { config.add = addToScene; } var key = GetAdvancedValue$3(config, 'key', null); var frame = GetAdvancedValue$3(config, 'frame', null); var gameObject = new Image(this.scene, 0, 0, key, frame, config); BuildGameObject$5(this.scene, gameObject, config); return gameObject; } const DynamicTexture = Phaser.Textures.DynamicTexture; var CreateDynamicTexture = function (scene, width, height) { if (width === undefined) { width = 2; } if (height === undefined) { height = 2; } var dt = new DynamicTexture(scene.sys.textures, null, width, height); return dt; }; var GetDisplayWidth = function (gameObject) { if (gameObject.displayWidth !== undefined) { return gameObject.displayWidth; } else { return gameObject.width; } }; var GetDisplayHeight = function (gameObject) { if (gameObject.displayHeight !== undefined) { return gameObject.displayHeight; } else { return gameObject.height; } }; const Rectangle$1 = Phaser.Geom.Rectangle; const Vector2 = Phaser.Math.Vector2; const RotateAround$1 = Phaser.Math.RotateAround; const P3Container$1 = Phaser.GameObjects.Container; var GetBounds = function (gameObject, output) { if (output === undefined) { output = new Rectangle$1(); } else if (output === true) { if (GlobRect$1 === undefined) { GlobRect$1 = new Rectangle$1(); } output = GlobRect$1; } if (gameObject.getBounds && !(gameObject instanceof P3Container$1)) { return gameObject.getBounds(output); } // We can use the output object to temporarily store the x/y coords in: var TLx, TLy, TRx, TRy, BLx, BLy, BRx, BRy; // Instead of doing a check if parent container is // defined per corner we only do it once. if (gameObject.parentContainer) { var parentMatrix = gameObject.parentContainer.getBoundsTransformMatrix(); GetTopLeft(gameObject, output); parentMatrix.transformPoint(output.x, output.y, output); TLx = output.x; TLy = output.y; GetTopRight(gameObject, output); parentMatrix.transformPoint(output.x, output.y, output); TRx = output.x; TRy = output.y; GetBottomLeft(gameObject, output); parentMatrix.transformPoint(output.x, output.y, output); BLx = output.x; BLy = output.y; GetBottomRight(gameObject, output); parentMatrix.transformPoint(output.x, output.y, output); BRx = output.x; BRy = output.y; } else { GetTopLeft(gameObject, output); TLx = output.x; TLy = output.y; GetTopRight(gameObject, output); TRx = output.x; TRy = output.y; GetBottomLeft(gameObject, output); BLx = output.x; BLy = output.y; GetBottomRight(gameObject, output); BRx = output.x; BRy = output.y; } output.x = Math.min(TLx, TRx, BLx, BRx); output.y = Math.min(TLy, TRy, BLy, BRy); output.width = Math.max(TLx, TRx, BLx, BRx) - output.x; output.height = Math.max(TLy, TRy, BLy, BRy) - output.y; return output; }; var GlobRect$1 = undefined; var GetTopLeft = function (gameObject, output, includeParent) { if (output === undefined) { output = new Vector2(); } else if (output === true) { if (GlobVector === undefined) { GlobVector = new Vector2(); } output = GlobVector; } if (gameObject.getTopLeft) { return gameObject.getTopLeft(output); } output.x = gameObject.x - (GetDisplayWidth(gameObject) * gameObject.originX); output.y = gameObject.y - (GetDisplayHeight(gameObject) * gameObject.originY); return PrepareBoundsOutput(gameObject, output, includeParent); }; var GetTopRight = function (gameObject, output, includeParent) { if (output === undefined) { output = new Vector2(); } else if (output === true) { if (GlobVector === undefined) { GlobVector = new Vector2(); } output = GlobVector; } if (gameObject.getTopRight) { return gameObject.getTopRight(output); } output.x = (gameObject.x - (GetDisplayWidth(gameObject) * gameObject.originX)) + GetDisplayWidth(gameObject); output.y = gameObject.y - (GetDisplayHeight(gameObject) * gameObject.originY); return PrepareBoundsOutput(gameObject, output, includeParent); }; var GetBottomLeft = function (gameObject, output, includeParent) { if (output === undefined) { output = new Vector2(); } else if (output === true) { if (GlobVector === undefined) { GlobVector = new Vector2(); } output = GlobVector; } if (gameObject.getBottomLeft) { return gameObject.getBottomLeft(output); } output.x = gameObject.x - (GetDisplayWidth(gameObject) * gameObject.originX); output.y = (gameObject.y - (GetDisplayHeight(gameObject) * gameObject.originY)) + GetDisplayHeight(gameObject); return PrepareBoundsOutput(gameObject, output, includeParent); }; var GetBottomRight = function (gameObject, output, includeParent) { if (output === undefined) { output = new Vector2(); } else if (output === true) { if (GlobVector === undefined) { GlobVector = new Vector2(); } output = GlobVector; } if (gameObject.getBottomRight) { return gameObject.getBottomRight(output); } output.x = (gameObject.x - (GetDisplayWidth(gameObject) * gameObject.originX)) + GetDisplayWidth(gameObject); output.y = (gameObject.y - (GetDisplayHeight(gameObject) * gameObject.originY)) + GetDisplayHeight(gameObject); return PrepareBoundsOutput(gameObject, output, includeParent); }; var GlobVector = undefined; var PrepareBoundsOutput = function (gameObject, output, includeParent) { if (includeParent === undefined) { includeParent = false; } if (gameObject.rotation !== 0) { RotateAround$1(output, gameObject.x, gameObject.y, gameObject.rotation); } if (includeParent && gameObject.parentContainer) { var parentMatrix = gameObject.parentContainer.getBoundsTransformMatrix(); parentMatrix.transformPoint(output.x, output.y, output); } return output; }; const Rectangle = Phaser.Geom.Rectangle; const Union = Phaser.Geom.Rectangle.Union; var GetBoundsOfGameObjects = function (gameObjects, out) { if (out === undefined) { out = new Rectangle(); } else if (out === true) { if (GlobRect === undefined) { GlobRect = new Rectangle(); } out = GlobRect; } out.setTo(0, 0, 0, 0); var gameObject; var firstClone = true; for (var i = 0, cnt = gameObjects.length; i < cnt; i++) { gameObject = gameObjects[i]; if (!gameObject.getBounds) { continue; } var boundsRect = GetBounds(gameObject, true); if (firstClone) { out.setTo(boundsRect.x, boundsRect.y, boundsRect.width, boundsRect.height); firstClone = false; } else { Union(boundsRect, out, out); } } return out; }; var GlobRect; var Clear = function (obj) { if ((typeof (obj) !== 'object') || (obj === null)) { return obj; } if (Array.isArray(obj)) { obj.length = 0; } else { for (var key in obj) { delete obj[key]; } } return obj; }; /** * Shallow Object Clone. Will not out nested objects. * @param {object} obj JSON object * @param {object} ret JSON object to return, set null to return a new object * @returns {object} this object */ var Clone = function (obj, out) { var objIsArray = Array.isArray(obj); if (out === undefined) { out = (objIsArray) ? [] : {}; } else { Clear(out); } if (objIsArray) { out.length = obj.length; for (var i = 0, cnt = obj.length; i < cnt; i++) { out[i] = obj[i]; } } else { for (var key in obj) { out[key] = obj[key]; } } return out; }; var SortGameObjectsByDepth = function (gameObjects, descending) { if (gameObjects.length <= 1) { return gameObjects; } if (descending === undefined) { descending = false; } var itemList; for (var i = 0, cnt = gameObjects.length; i < cnt; i++) { var gameObject = gameObjects[i]; if (gameObject.displayList) { // Inside a scene or a layer itemList = gameObject.displayList; // displayList } else if (gameObject.parentContainer) { // Inside a container itemList = gameObject.parentContainer.list; // array } if (itemList) { break; } } if (!itemList) { itemList = gameObject.scene.sys.displayList; // displayList // ?? } if (itemList.depthSort) { // Is a displayList object itemList.depthSort(); itemList = itemList.list; // itemList is an array now } // itemList is an array if (descending) { gameObjects.sort(function (childA, childB) { return itemList.indexOf(childB) - itemList.indexOf(childA); }); } else { gameObjects.sort(function (childA, childB) { return itemList.indexOf(childA) - itemList.indexOf(childB); }); } return gameObjects; }; const GameObjectClass = Phaser.GameObjects.GameObject; const LayerClass$1 = Phaser.GameObjects.Layer; var IsGameObject = function (object) { return (object instanceof GameObjectClass) || (object instanceof LayerClass$1); }; var GetValue$g = Phaser.Utils.Objects.GetValue; var Snapshot = function (config) { if (!config) { return; } var gameObjects = config.gameObjects; var renderTexture = config.renderTexture; // renderTexture, or dynamicTexture var saveTexture = config.saveTexture; var x = GetValue$g(config, 'x', undefined); var y = GetValue$g(config, 'y', undefined); var width = GetValue$g(config, 'width', undefined); var height = GetValue$g(config, 'height', undefined); var originX = GetValue$g(config, 'originX', 0); var originY = GetValue$g(config, 'originY', 0); var padding = GetValue$g(config, 'padding', 0); var scrollX, scrollY; if ((width === undefined) || (height === undefined) || (x === undefined) || (y === undefined)) { // Union bounds of gameObjects var bounds = GetBoundsOfGameObjects(gameObjects, true); var isCenterOrigin = (x !== undefined) && (y !== undefined); if (isCenterOrigin) { width = Math.max((x - bounds.left), (bounds.right - x)) * 2; height = Math.max((y - bounds.top), (bounds.bottom - y)) * 2; originX = 0.5; originY = 0.5; } else { x = bounds.x; y = bounds.y; width = bounds.width; height = bounds.height; originX = 0; originY = 0; } scrollX = bounds.x; scrollY = bounds.y; } else { scrollX = x + ((0 - originX) * width); scrollY = y + ((0 - originY) * height); } scrollX -= padding; scrollY -= padding; width += (padding * 2); height += (padding * 2); var scene = gameObjects[0].scene; var textureManager = scene.sys.textures; // Snapshot on dynamicTexture directly if (saveTexture && !renderTexture) { renderTexture = textureManager.addDynamicTexture(saveTexture, width, height); } // Return a renderTexture if (!renderTexture) { renderTexture = scene.add.renderTexture(0, 0, width, height); } if (renderTexture.setPosition) { renderTexture.setPosition(x, y); } if ((renderTexture.width !== width) || (renderTexture.height !== height)) { renderTexture.setSize(width, height); } if (renderTexture.setOrigin) { renderTexture.setOrigin(originX, originY); } renderTexture.camera.setScroll(scrollX, scrollY); // Draw gameObjects gameObjects = SortGameObjectsByDepth(Clone(gameObjects)); renderTexture.draw(gameObjects); // Save render result to texture if (saveTexture) { if (IsGameObject(renderTexture)) { renderTexture.saveTexture(saveTexture); } else if (renderTexture.key !== saveTexture) { textureManager.renameTexture(renderTexture.key, key); } } return renderTexture; }; const IsPlainObject$5 = Phaser.Utils.Objects.IsPlainObject; const GetValue$f = Phaser.Utils.Objects.GetValue; let RenderTexture$1 = class RenderTexture extends Image { constructor(scene, x, y, width, height, config) { if (IsPlainObject$5(x)) { config = x; x = GetValue$f(config, 'x', 0); y = GetValue$f(config, 'y', 0); width = GetValue$f(config, 'width', 32); height = GetValue$f(config, 'height', 32); } // dynamic-texture -> quad-image var texture = CreateDynamicTexture(scene, width, height); super(scene, x, y, texture, null, config); this.type = 'rexPerspectiveRenderTexture'; this.rt = this.texture; } destroy(fromScene) { // This Game Object has already been destroyed if (!this.scene || this.ignoreDestroy) { return; } super.destroy(fromScene); this.rt.destroy(); this.rt = null; } snapshot(gameObjects, config) { if (config === undefined) { config = {}; } config.gameObjects = gameObjects; config.renderTexture = this.rt; Snapshot(config); if ((this.width !== this.frame.realWidth) || (this.height !== this.frame.realHeight)) { this.syncSize(); } return this; } }; function PerspectiveRenderTextureFactory (x, y, width, height, config) { var gameObject = new RenderTexture$1(this.scene, x, y, width, height, config); this.scene.add.existing(gameObject); return gameObject; } const GetAdvancedValue$2 = Phaser.Utils.Objects.GetAdvancedValue; const BuildGameObject$4 = Phaser.GameObjects.BuildGameObject; function PerspectiveRenderTextureCreator (config, addToScene) { if (config === undefined) { config = {}; } if (addToScene !== undefined) { config.add = addToScene; } var width = GetAdvancedValue$2(config, 'width', 32); var height = GetAdvancedValue$2(config, 'height', 32); var gameObject = new RenderTexture$1(this.scene, 0, 0, width, height, config); BuildGameObject$4(this.scene, gameObject, config); return gameObject; } const AnimationState = Phaser.Animations.AnimationState; const IsPlainObject$4 = Phaser.Utils.Objects.IsPlainObject; const GetValue$e = Phaser.Utils.Objects.GetValue; class Sprite extends Image { constructor(scene, x, y, key, frame, config) { if (IsPlainObject$4(x)) { config = x; x = GetValue$e(config, 'x', 0); y = GetValue$e(config, 'y', 0); key = GetValue$e(config, 'key', null); frame = GetValue$e(config, 'frame', null); } super(scene, x, y, key, frame, config); this.type = 'rexPerspectiveSprite'; this.anims = new AnimationState(this); } preDestroy() { super.preDestroy(); this.anims.destroy(); this.anims = undefined; } preUpdate(time, delta) { this.anims.update(time, delta); super.preUpdate(time, delta); } play(key, ignoreIfPlaying, startFrame) { return this.anims.play(key, ignoreIfPlaying, startFrame); } playReverse(key, ignoreIfPlaying) { return this.anims.playReverse(key, ignoreIfPlaying); } playAfterDelay(key, delay) { return this.anims.playAfterDelay(key, delay); } playAfterRepeat(key, repeatCount) { return this.anims.playAfterRepeat(key, repeatCount); } chain(key) { return this.anims.chain(key); } stop() { return this.anims.stop(); } stopAfterDelay(delay) { return this.anims.stopAfterDelay(delay); } stopAfterRepeat(repeatCount) { return this.anims.stopAfterRepeat(repeatCount); } stopOnFrame(frame) { return this.anims.stopOnFrame(frame); } } function PerspectiveSpriteFactory (x, y, texture, frame, config) { var gameObject = new Sprite(this.scene, x, y, texture, frame, config); this.scene.add.existing(gameObject); return gameObject; } const GetAdvancedValue$1 = Phaser.Utils.Objects.GetAdvancedValue; const BuildGameObject$3 = Phaser.GameObjects.BuildGameObject; function PerspectiveSpriteCreator (config, addToScene) { if (config === undefined) { config = {}; } if (addToScene !== undefined) { config.add = addToScene; } var key = GetAdvancedValue$1(config, 'key', null); var frame = GetAdvancedValue$1(config, 'frame', null); var gameObject = new Sprite(this.scene, 0, 0, key, frame, config); BuildGameObject$3(this.scene, gameObject, config); return gameObject; } const MinVersion = 60; var IsChecked = false; var CheckP3Version = function (minVersion) { if (IsChecked) { return; } if (minVersion === undefined) { minVersion = MinVersion; } var version = Phaser.VERSION.split('.'); var mainVersion = parseInt(version[0]); if (mainVersion === 3) { var currentVersion = parseInt(version[1]); if (currentVersion < minVersion) { console.error(`Minimum supported version : ${mainVersion}.${currentVersion}`); } } else { console.error(`Can't supported version : ${mainVersion}`); } IsChecked = true; }; CheckP3Version(); const Zone = Phaser.GameObjects.Zone; const AddItem = Phaser.Utils.Array.Add; const RemoveItem = Phaser.Utils.Array.Remove; class Base extends Zone { constructor(scene, x, y, width, height) { if (x === undefined) { x = 0; } if (y === undefined) { y = 0; } if (width === undefined) { width = 1; } if (height === undefined) { height = 1; } super(scene, x, y, width, height); this.children = []; } destroy(fromScene) { // This Game Object has already been destroyed if (!this.scene || this.ignoreDestroy) { return; } if (fromScene) { // Stop scene var child; for (var i = this.children.length - 1; i >= 0; i--) { child = this.children[i]; if (!child.parentContainer && // Not in container !child.displayList // Not in scene, neither in layer ) { // Destroy child which is not in scene, container, or layer manually child.destroy(fromScene); } } } // Destroy/remove children this.clear(!fromScene); super.destroy(fromScene); } contains(gameObject) { return (this.children.indexOf(gameObject) !== -1); } add(gameObjects) { var parent = this; AddItem(this.children, gameObjects, 0, // Callback of item added function (gameObject) { gameObject.once('destroy', parent.onChildDestroy, parent); }, this); return this; } remove(gameObjects, destroyChild) { var parent = this; RemoveItem(this.children, gameObjects, // Callback of item removed function (gameObject) { gameObject.off('destroy', parent.onChildDestroy, parent); if (destroyChild) { gameObject.destroy(); } } ); return this; } onChildDestroy(child, fromScene) { // Only remove reference this.remove(child, false); } clear(destroyChild) { var parent = this; var gameObject; for (var i = 0, cnt = this.children.length; i < cnt; i++) { gameObject = this.children[i]; gameObject.off('destroy', parent.onChildDestroy, parent); if (destroyChild) { gameObject.destroy(); } } this.children.length = 0; return this; } } const Components = Phaser.GameObjects.Components; Phaser.Class.mixin(Base, [ Components.Alpha, Components.Flip ] ); var GetParent = function (gameObject, name) { var parent; if (name === undefined) { if (gameObject.hasOwnProperty('rexContainer')) { parent = gameObject.rexContainer.parent; } } else { parent = GetParent(gameObject); while (parent) { if (parent.name === name) { break; } parent = GetParent(parent); } } return parent; }; var GetTopmostParent = function (gameObject) { var parent = GetParent(gameObject); while (parent) { gameObject = parent; parent = GetParent(parent); } return gameObject; }; const DegToRad$5 = Phaser.Math.DegToRad; const RadToDeg$3 = Phaser.Math.RadToDeg; var GetLocalState = function (gameObject) { if (!gameObject.hasOwnProperty('rexContainer')) { var rexContainer = { parent: null, self: null, layer: null, x: 0, y: 0, syncPosition: true, rotation: 0, syncRotation: true, scaleX: 0, scaleY: 0, syncScale: true, alpha: 0, syncAlpha: true, syncScrollFactor: true, syncCameraFilter: true, syncDisplayList: true, visible: true, active: true, }; Object.defineProperty(rexContainer, 'angle', { get: function () { return RadToDeg$3(this.rotation); }, set: function (value) { this.rotation = DegToRad$5(value); } }); Object.defineProperty(rexContainer, 'displayWidth', { get: function () { return gameObject.width * this.scaleX; }, set: function (width) { this.scaleX = width / gameObject.width; } }); Object.defineProperty(rexContainer, 'displayHeight', { get: function () { return gameObject.height * this.scaleY; }, set: function (height) { this.scaleY = height / gameObject.height; } }); gameObject.rexContainer = rexContainer; } return gameObject.rexContainer; }; var Parent = { setParent(gameObject, parent) { if (parent === undefined) { parent = this; } var localState = GetLocalState(gameObject); if (parent) { // Add to parent localState.parent = parent; localState.self = gameObject; } else { // Remove from parent localState.parent = null; localState.self = null; } return this; }, getParent(gameObject, name) { if (typeof (gameObject) === 'string') { name = gameObject; gameObject = undefined; } if (gameObject === undefined) { gameObject = this; } return GetParent(gameObject, name); }, getTopmostParent(gameObject) { if (gameObject === undefined) { gameObject = this; } return GetTopmostParent(gameObject); } }; const GetValue$d = Phaser.Utils.Objects.GetValue; const BaseAdd = Base.prototype.add; var Add = function (gameObject, config) { this.setParent(gameObject); var state = GetLocalState(gameObject); SetupSyncFlags(state, config); this .resetChildState(gameObject) // Reset local state of child .updateChildVisible(gameObject) // Apply parent's visible to child .updateChildActive(gameObject) // Apply parent's active to child .updateChildScrollFactor(gameObject) // Apply parent's scroll factor to child .updateChildMask(gameObject) // Apply parent's mask to child .updateCameraFilter(gameObject); // Apply parent's cameraFilter to child BaseAdd.call(this, gameObject); SyncDisplayList.call(this, gameObject, state); return this; }; var AddLocal = function (gameObject, config) { this.setParent(gameObject); // Set local state from child directly var state = GetLocalState(gameObject); SetupSyncFlags(state, config); // Position state.x = gameObject.x; state.y = gameObject.y; state.rotation = gameObject.rotation; state.scaleX = gameObject.scaleX; state.scaleY = gameObject.scaleY; // Alpha state.alpha = gameObject.alpha; // Visible state.visible = gameObject.visible; // Active state.active = gameObject.active; this .updateChildPosition(gameObject) .updateChildAlpha(gameObject) .updateChildVisible(gameObject) // Apply parent's visible to child .updateChildActive(gameObject) // Apply parent's active to child .updateChildScrollFactor(gameObject) // Apply parent's scroll factor to child .updateChildMask(gameObject); // Apply parent's mask to child BaseAdd.call(this, gameObject); SyncDisplayList.call(this, gameObject, state); return this; }; var SetupSyncFlags = function (state, config) { if (config === undefined) { config = true; } if (typeof (config) === 'boolean') { state.syncPosition = config; state.syncRotation = config; state.syncScale = config; state.syncAlpha = config; state.syncScrollFactor = config; state.syncCameraFilter = config; state.syncDisplayList = config; } else { state.syncPosition = GetValue$d(config, 'syncPosition', true); state.syncRotation = GetValue$d(config, 'syncRotation', true); state.syncScale = GetValue$d(config, 'syncScale', true); state.syncAlpha = GetValue$d(config, 'syncAlpha', true); state.syncScrollFactor = GetValue$d(config, 'syncScrollFactor', true); state.syncCameraFilter = GetValue$d(config, 'syncCameraFilter', true); state.syncDisplayList = GetValue$d(config, 'syncDisplayList', true); } }; var SyncDisplayList = function (gameObject, state) { this.addToParentContainer(gameObject); // Sync parent's container to child if (state.syncDisplayList) { this.addToPatentLayer(gameObject); // Sync parent's layer to child } this.addToRenderLayer(gameObject); // Sync parent's render-layer }; var AddChild = { // Can override this method add(gameObject) { if (Array.isArray(gameObject)) { this.addMultiple(gameObject); } else { Add.call(this, gameObject); } return this; }, // Don't override this method pin(gameObject, config) { if (Array.isArray(gameObject)) { this.addMultiple(gameObject, config); } else { Add.call(this, gameObject, config); } return this; }, // Can override this method addMultiple(gameObjects) { var args = Array.from(arguments); for (var i = 0, cnt = gameObjects.length; i < cnt; i++) { args[0] = gameObjects[i]; this.add.apply(this, args); } return this; }, addLocal(gameObject) { if (Array.isArray(gameObject)) { this.addMultiple(gameObject); } else { AddLocal.call(this, gameObject); } return this; }, // Don't override this method pinLocal(gameObject, config) { if (Array.isArray(gameObject)) { this.addMultiple(gameObject, config); } else { AddLocal.call(this, gameObject, config); } return this; }, addLocalMultiple(gameObjects) { for (var i = 0, cnt = gameObjects.length; i < cnt; i++) { AddLocal.call(this, gameObjects[i]); } return this; } }; const BaseRemove = Base.prototype.remove; const BaseClear = Base.prototype.clear; var RemoveChild = { // Can override this method remove(gameObject, destroyChild) { if (GetParent(gameObject) !== this) { return this; } this.setParent(gameObject, null); if (!destroyChild) { this.removeFromRenderLayer(gameObject); } BaseRemove.call(this, gameObject, destroyChild); return this; }, // Don't override this method unpin(gameObject, destroyChild) { if (GetParent(gameObject) !== this) { return this; } this.setParent(gameObject, null); if (!destroyChild) { this.removeFromRenderLayer(gameObject); } BaseRemove.call(this, gameObject, destroyChild); return this; }, clear(destroyChild) { var children = this.children; for (var i = 0, cnt = children.length; i < cnt; i++) { var child = children[i]; this.setParent(child, null); if (!destroyChild) { this.removeFromRenderLayer(child); } } BaseClear.call(this, destroyChild); return this; }, }; var ChildState = { getLocalState(gameObject) { return GetLocalState(gameObject); }, resetChildState(gameObject) { this .resetChildPositionState(gameObject) .resetChildVisibleState(gameObject) .resetChildAlphaState(gameObject) .resetChildActiveState(gameObject); return this; }, resetChildrenState(gameObjects) { for (var i = 0, cnt = gameObjects.length; i < cnt; i++) { this.resetChildState(gameObjects[i]); } return this; }, syncProperties() { this .syncPosition() .syncVisible() .syncAlpha() .syncActive() .syncScrollFactor() .syncMask(); return this; } }; var Transform = { worldToLocal(point) { // Transform point.x -= this.x; point.y -= this.y; // Rotate var c = Math.cos(-this.rotation); var s = Math.sin(-this.rotation); var tx = point.x; var ty = point.y; point.x = tx * c - ty * s; point.y = tx * s + ty * c; // Scale point.x /= this.scaleX; point.y /= this.scaleY; return point; }, localToWorld(point) { // Scale point.x *= this.scaleX; point.y *= this.scaleY; // Rotate var c = Math.cos(this.rotation); var s = Math.sin(this.rotation); var tx = point.x; var ty = point.y; point.x = tx * c - ty * s; point.y = tx * s + ty * c; // Transform point.x += this.x; point.y += this.y; return point; } }; var GetScale = function (a, b) { if (a === b) { return 1; } else { return a / b; } }; var Position = { updateChildPosition(child) { if (child.isRexContainerLite) { child.syncChildrenEnable = false; } var localState = GetLocalState(child); var parent = localState.parent; if (localState.syncPosition) { child.x = localState.x; child.y = localState.y; parent.localToWorld(child); } if (localState.syncRotation) { child.rotation = localState.rotation + parent.rotation; } if (localState.syncScale) { child.scaleX = localState.scaleX * parent.scaleX; child.scaleY = localState.scaleY * parent.scaleY; } if (child.isRexContainerLite) { child.syncChildrenEnable = true; child.syncPosition(); } return this; }, syncPosition() { if (this.syncChildrenEnable) { this.children.forEach(this.updateChildPosition, this); } return this; }, resetChildPositionState(child) { var localState = GetLocalState(child); var parent = localState.parent; localState.x = child.x; localState.y = child.y; parent.worldToLocal(localState); localState.scaleX = GetScale(child.scaleX, parent.scaleX); localState.scaleY = GetScale(child.scaleY, parent.scaleY); localState.rotation = child.rotation - parent.rotation; return this; }, setChildPosition(child, x, y) { child.x = x; child.y = y; this.resetChildPositionState(child); return this; }, setChildLocalPosition(child, x, y) { var localState = GetLocalState(child); localState.x = x; localState.y = y; this.updateChildPosition(child); return this; }, resetLocalPositionState() { var parent = GetLocalState(this).parent; if (parent) { parent.resetChildPositionState(this); } return this; }, getChildLocalX(child) { var localState = GetLocalState(child); return localState.x; }, getChildLocalY(child) { var localState = GetLocalState(child); return localState.y; }, }; const DegToRad$4 = Phaser.Math.DegToRad; var Rotation = { updateChildRotation(child) { var localState = GetLocalState(child); var parent = localState.parent; if (localState.syncRotation) { child.rotation = parent.rotation + localState.rotation; } return this; }, syncRotation() { if (this.syncChildrenEnable) { this.children.forEach(this.updateChildRotation, this); } return this; }, resetChildRotationState(child) { var localState = GetLocalState(child); var parent = localState.parent; localState.rotation = child.rotation - parent.rotation; return this; }, setChildRotation(child, rotation) { child.rotation = rotation; this.resetChildRotationState(child); return this; }, setChildAngle(child, angle) { child.angle = angle; this.resetChildRotationState(child); return this; }, setChildLocalRotation(child, rotation) { var localState = GetLocalState(child); localState.rotation = rotation; this.updateChildRotation(child); return this; }, setChildLocalAngle(child, angle) { var localState = GetLocalState(child); localState.rotation = DegToRad$4(angle); this.updateChildRotation(child); return this; }, resetLocalRotationState() { var parent = GetLocalState(this).parent; if (parent) { parent.resetChildRotationState(this); } return this; }, getChildLocalRotation(child) { var localState = GetLocalState(child); return localState.rotation; }, }; var Scale = { updateChildScale(child) { var state = GetLocalState(child); var parent = state.parent; if (state.syncScale) { child.scaleX = parent.scaleX * state.scaleX; child.scaleY = parent.scaleY * state.scaleY; } return this; }, syncScale() { if (this.syncChildrenEnable) { this.children.forEach(this.updateChildScale, this); } return this; }, resetChildScaleState(child) { var state = GetLocalState(child); var parent = state.parent; state.scaleX = GetScale(child.scaleX, parent.scaleX); state.scaleY = GetScale(child.scaleY, parent.scaleY); return this; }, setChildScale(child, scaleX, scaleY) { if (scaleY === undefined) { scaleY = scaleX; } child.scaleX = scaleX; child.scaleY = scaleY; this.resetChildScaleState(child); return this; }, setChildLocalScale(child, scaleX, scaleY) { if (scaleY === undefined) { scaleY = scaleX; } var state = GetLocalState(child); state.scaleX = scaleX; state.scaleY = scaleY; this.updateChildScale(child); return this; }, setChildDisplaySize(child, width, height) { child.setDisplaySize(width, height); this.resetChildScaleState(child); return this; }, resetLocalScaleState() { var parent = GetLocalState(this).parent; if (parent) { parent.resetChildScaleState(this); } return this; }, getChildLocalScaleX(child) { var localState = GetLocalState(child); return localState.scaleX; }, getChildLocalScaleY(child) { var localState = GetLocalState(child); return localState.scaleY; }, }; /* Visible in localState: - visible: original visible of child - maskVisible: invisible by parent mask, see MaskChildren.js - undefined (not in masking) : Equal to mask visible - true (mask visible) : Inside, or across parent's visible area - false (maske invisible) : Out of parent's visible area Visible result of child = (parent visible) && (child visible) && (mask visible) */ var V