diff --git a/.gitattributes b/.gitattributes index 83a12b0..b5bdadc 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1 +1 @@ -dist/*.js binary \ No newline at end of file +dist/*.* binary \ No newline at end of file diff --git a/.gitignore b/.gitignore index 0ac92e1..5daf0a6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .idea .DS_Store node_modules +sandbox/node_modules \ No newline at end of file diff --git a/README.md b/README.md index f35f401..6c204b4 100644 --- a/README.md +++ b/README.md @@ -12,10 +12,7 @@ Currently the switch between states is really static and a game should be able t https://codepen.io/cristianbote/full/GjgVxg ## How to use it -You have several options here - -### Straight -Just download the `dist/phaser-state-transition.min.js` file and you're done +You have several options here including es6 imports. ### Npm @@ -23,44 +20,47 @@ Just download the `dist/phaser-state-transition.min.js` file and you're done npm install phaser-state-transition --save ``` +And then import it in your project + +```js +import "phaser-state-transition"; +``` + +The plugin needs the `Phaser` framework to work, therefore you should make sure that this is included before the plugin's import. + +#### Straight but nor recommended +Just download the `dist/phaser-state-transition.umd.js` file and you're done, but this is not the recommended way. You should use it via npm. You have better control on what version you're keeping locally. + ## Usage -Since we're talking about v2, there's been some changes. Now, the plugin basically overrides the create state method, so you could keep you're code the same, and just add transition configs where you see fit. +The easiest way to use it, is by just passing a transition for entering. ```js -const MenuState = { - create: () => { - let game = this; - let playButton; - - // Let's assume when the user taps on the window, we wanna change the state - this.game.input.onTap.addOnce(() => { - game.state.start( - 'playState', // The play state - Phaser.Plugin.StateTransition.In.SlideLeft, - Phaser.Plugin.StateTransition.Out.SlideLeft - ); - }) - } -}; - -const PlayState = { - create: () => { - let game = this; - let playButton; - - // Let's assume when the user taps on the window, we wanna change the state - this.game.input.onTap.addOnce(() => { - game.state.start( - 'menuState', // The menu state - Phaser.Plugin.StateTransition.In.SlideLeft, - Phaser.Plugin.StateTransition.Out.SlideLeft - ); - }) +import { createTransition } from "phaser-state-transition"; + +const EnteringTransition = createTransition({ + props: { + x: game => game.width } -}; +}); + +game.state.start("stateName", EnteringTransition); ``` -Notice the 2 optional params, that are transition config instances. There are several available by default, you should run this: `console.log(Phaser.Plugin.StateTransition.Out);` and `console.log(Phaser.Plugin.StateTransition.In);`. Obviously you could easily add your own nice transition as well. +The transition options to pass in are basically just some instructions for the plugin, to handle the _how_ of the transition. You'll find there are other properties inside, like ease, duration and other properties that are not that important to have nice transitions. + +## API + +### StateTransitionPlugin +The plugin class. Normally you should not work on this class, but you could extend it if needed. The plugin does not need a class to be working. + +### createTransition(options) +This helper function, generates a transition object to be passed along the `game.state.start` method. + +The default duration would be `500ms` and the ease function `Phaser.Easing.Exponential.InOut` + +* `@param {object} options` The options to create a transition object +* `@returns {object}` The transition object to be passed along the `game.start.state` + ## Feedback If there's something you think it could be improved let me know, or create a pr. diff --git a/dist/phaser-state-transition.js b/dist/phaser-state-transition.js index d418bc1..6d5c12b 100644 --- a/dist/phaser-state-transition.js +++ b/dist/phaser-state-transition.js @@ -1,491 +1,2 @@ -(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o PHASER_LEGACY) { - this._texture.renderXY(game.world, game.camera.position.x * -1, game.camera.position.y * -1); - } else { - this._texture.renderXY(game.world, game.width / 2 - game.camera.position.x, game.height / 2 - game.camera.position.y); - } - - // Get the image - Phaser.Image.call(this, game, x || 0, y || 0, this._texture); - - // Capture all input events - this.inputEnabled = true; - } - - ContentSnapshot.prototype = Object.create(Phaser.Image.prototype); - ContentSnapshot.constructor = ContentSnapshot; - - module.exports = ContentSnapshot; -}()); - -},{}],2:[function(require,module,exports){ -(function() { - "use strict"; - - var Transition = require('../transition/Transition'), - ContentSnapshot = require('./ContentSnapshot'); - - /** - * Slide Class - * @constructor - * @name Slide - * @version 0.1.0 - * @author Cristian Bote - * @param {object} game Phaser.Game instance - * @param {boolean} noStage No stage flag - */ - function Slide(game, noStage) { - this.game = game; - this._contentSnapshot = new ContentSnapshot(game, 0, 0, noStage); - this._transition = new Transition(game); - } - - /** - * Start sliding - * @method - * @name go - * @version 0.1.0 - * @author Cristian Bote - * @param {object} options Transition options - */ - Slide.prototype.go = function(options) { - this.game.stage.addChildAt(this._contentSnapshot, this.game.stage.children.length); - this._transition.start(this._contentSnapshot, options); - }; - - module.exports = Slide; -}()); - -},{"../transition/Transition":7,"./ContentSnapshot":1}],3:[function(require,module,exports){ -/*global - Phaser: true - window: true -*/ - -(function() { - "use strict"; - - var StateManagerCachedStart = Phaser.StateManager.prototype.start, - Slide = require('./Slide'), - ContentSnapshot = require('./ContentSnapshot'); - - function cleanup(children) { - var i = 0, - l = children.length; - - for(; i < l; i += 1) { - if (children[i] && (children[i] instanceof ContentSnapshot)) { - children[i].destroy(); - } - } - } - - function StateManagerStart(stateId, slideOutOptions, slideInOptions) { - var _slide, - _introSlide, - _stateManager = this, - _state = _stateManager.states[stateId], - _args = [].slice.call(arguments), - _cachedStateCreate = _state.create; - - _stateManager.game.stage && cleanup(_stateManager.game.stage.children); - - if (_stateManager.game.isBooted && slideOutOptions) { - _slide = new Slide(this.game, slideOutOptions.noStage); - - (function (_state, slideOutOptions, slideInOptions) { - _state.create = function () { - _cachedStateCreate.call(this); - - // Slide in intro - if (slideInOptions) { - _introSlide = new Slide(_stateManager.game, slideInOptions.noStage); - _stateManager._created = false; - _introSlide.go(slideInOptions); - - _introSlide._transition.onComplete = function () { - _stateManager._created = true; - cleanup(_stateManager.game.stage.children); - }; - } - - _slide.go(slideOutOptions); - - // Put the original create back - _state.create = _cachedStateCreate; - }; - }(_state, slideOutOptions, slideInOptions)); - } - - // Start the cached state with the params for it - StateManagerCachedStart.apply(this, [stateId].concat(_args.slice(3))); - } - - module.exports = StateManagerStart; -}()); - -},{"./ContentSnapshot":1,"./Slide":2}],4:[function(require,module,exports){ -/*global - Phaser: true - window: true - */ - -(function() { - "use strict"; - - var DefaultTransition = require('../transition/DefaultTransition'); - - /** - * Phaser State Plugin Class - * @constructor - * @version 0.1.0 - * @author Cristian Bote - * @param {object} game Phaser.Game instance - * @param {object} parent Parent element - * @extend {Phaser.Plugin} - * @example: Usage - * var plugin = this.game.plugins.add(Phaser.Pluging.StateTransition); - */ - function StateTransition(game, parent) { - Phaser.Plugin.call(this, game, parent); - } - - StateTransition.prototype = Object.create(Phaser.Plugin.prototype); - StateTransition.constructor = StateTransition; - - /** - * Create custom transition - * @param {object} options Transition object - * @param {boolean} [options.intro] Is this a introduction transition - * @param {object|function} options.props Properties to transition to - */ - StateTransition.createTransition = function(options) { - return DefaultTransition(options); - }; - - /** - * Intro transition list - * @type {object} - */ - StateTransition.In = { - - SlideLeft: DefaultTransition({ - intro: true, - props: { - x: function(game) { - return game.width - } - } - }), - - SlideRight: DefaultTransition({ - intro: true, - props: { - x: function(game) { - return -game.width - } - } - }), - - SlideTop: DefaultTransition({ - intro: true, - props: { - y: function(game) { - return game.height - } - } - }), - - SlideBottom: DefaultTransition({ - intro: true, - props: { - y: function(game) { - return -game.height - } - } - }), - - ScaleUp: DefaultTransition({ - intro: true, - props: { - alpha: 0.4, - scale: new Phaser.Point({ - x: 2 - }) - } - }) - }; - - /** - * Exit transition list - * @type {object} - */ - StateTransition.Out = { - - SlideLeft: DefaultTransition({ - props: { - x: function(game) { - return -game.width - } - } - }), - - SlideRight: DefaultTransition({ - props: { - x: function(game) { - return game.width - } - } - }), - - SlideTop: DefaultTransition({ - props: { - y: function(game) { - return -game.height - } - } - }), - - SlideBottom: DefaultTransition({ - props: function(game) { - return { - y: game.height - } - } - }), - - ScaleUp: DefaultTransition({ - props: { - x: function(game) { - return game.width / 2 - }, - scale: { - x: 0 - } - } - }) - }; - - module.exports = StateTransition; -}()); - -},{"../transition/DefaultTransition":6}],5:[function(require,module,exports){ -(function() { - "use strict"; - - var Slide = require('./core/Slide'), - StateManagerStart = require('./core/StateManagerStart'), - DefaultTransition = require('./transition/DefaultTransition'); - - - // Define the Plugin Class - Phaser.Plugin.StateTransition = require('./core/StateTransition'); - - // Override the default state.start - Phaser.StateManager.prototype.start = StateManagerStart; - - module.exports = window.StateTransition = Phaser.Plugin.StateTransition; -}()); - -},{"./core/Slide":2,"./core/StateManagerStart":3,"./core/StateTransition":4,"./transition/DefaultTransition":6}],6:[function(require,module,exports){ -(function(){ - "use strict"; - - module.exports = function(options) { - return { - ease: options.ease || Phaser.Easing.Exponential.InOut, - duration: options.duration || 500, - intro: options.intro || false, - props: options.props || {} - } - }; -}()); - -},{}],7:[function(require,module,exports){ -(function(){ - "use strict"; - - var raf = window.requestAnimationFrame; - - /** - * Transition Class - * @constructor - * @name Transition - * @param {object} game Game instance - */ - function Transition(game) { - this.game = game; - this.onComplete = null; - this._tweens = []; - } - - /** - * Start the transition with a given target and options - * @name start - * @param target - * @param options - */ - Transition.prototype.start = function(target, options) { - var prop, - _props = options.props, - _isIntro = !!options.intro, - _tweenTarget, - _tweenInstance, - _queue = { - '_': {} - }; - - // Store the currentTarget - this.currentTarget = target; - - // If we need to compile the output - if (typeof _props === 'function') { - _props = _props(this.game); - } - - // Make sure the proper values for props are there - _isIntro && this._prepareTargetForTweening(_props); - - // Parse the options.props and generate the tweens options - for (prop in _props) { - if (_props.hasOwnProperty(prop)) { - // If we need to compile the output - if (typeof _props[prop] === 'function') { - _props[prop] = _props[prop](this.game); - } - - // If the original value is an object - // we need a separate tween - if (typeof target[prop] === 'object') { - _queue[prop] = _props[prop]; - } else { - _queue['_'][prop] = _props[prop]; - } - } - } - - for (prop in _queue) { - if (_queue.hasOwnProperty(prop)) { - _tweenTarget = prop === '_' ? target : target[prop]; - - this._tweens.push( - this.game.add.tween(_tweenTarget) - [_isIntro ? 'from' : 'to']( - _queue[prop], - options.duration, - options.ease, - true, - options.delay - ) - ); - - _tweenInstance = this._tweens[this._tweens.length - 1]; - _tweenInstance.onComplete.addOnce(this._checkForComplete, this); - } - } - }; - - /** - * Verify complete state for transition - * @param target - * @param tween - * @private - */ - Transition.prototype._checkForComplete = function(target, tween) { - var i = 0, - l = this._tweens.length, - _currentTween, - completed = 0; - - for(; i < l; i++) { - _currentTween = this._tweens[i]; - if (_currentTween.isRunning === false) { - completed++; - this.game.tweens.remove(tween); - } - } - - if (completed === l) { - this.onComplete && this.onComplete(); - this.currentTarget.destroy(); - } - }; - - /** - * Makes sure, before the transition starts, that we're doing fine - * property wise. - * @param props - * @private - */ - Transition.prototype._prepareTargetForTweening = function(props) { - if (props.hasOwnProperty('alpha')) { - this.currentTarget.alpha = 0; - } - }; - - /** - * Destroy handler - * @param target - */ - Transition.prototype.destroy = function(target) { - target.destroy(); - }; - - /** - * Stop handler - */ - Transition.prototype.stop = function() { - this._active = false; - this.update(); - }; - - /** - * Returns a unique identifier based in Date.now() stamp. - * Not that reliable. - * @returns {string} - * @private - */ - function _getIdentifier() { - return Date.now().toString(22).substr(-4, 4); - } - - module.exports = Transition; -}()); - -},{}]},{},[5]) -//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIm5vZGVfbW9kdWxlcy9icm93c2VyLXBhY2svX3ByZWx1ZGUuanMiLCJzcmMvY29yZS9Db250ZW50U25hcHNob3QuanMiLCJzcmMvY29yZS9TbGlkZS5qcyIsInNyYy9jb3JlL1N0YXRlTWFuYWdlclN0YXJ0LmpzIiwic3JjL2NvcmUvU3RhdGVUcmFuc2l0aW9uLmpzIiwic3JjL2luZGV4LmpzIiwic3JjL3RyYW5zaXRpb24vRGVmYXVsdFRyYW5zaXRpb24uanMiLCJzcmMvdHJhbnNpdGlvbi9UcmFuc2l0aW9uLmpzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBO0FDQUE7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUN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lQ3JlYXRlLmNhbGwodGhpcyk7XG5cbiAgICAgICAgICAgICAgICAgICAgLy8gU2xpZGUgaW4gaW50cm9cbiAgICAgICAgICAgICAgICAgICAgaWYgKHNsaWRlSW5PcHRpb25zKSB7XG4gICAgICAgICAgICAgICAgICAgICAgICBfaW50cm9TbGlkZSA9IG5ldyBTbGlkZShfc3RhdGVNYW5hZ2VyLmdhbWUsIHNsaWRlSW5PcHRpb25zLm5vU3RhZ2UpO1xuICAgICAgICAgICAgICAgICAgICAgICAgX3N0YXRlTWFuYWdlci5fY3JlYXRlZCA9IGZhbHNlO1xuICAgICAgICAgICAgICAgICAgICAgICAgX2ludHJvU2xpZGUuZ28oc2xpZGVJbk9wdGlvbnMpO1xuXG4gICAgICAgICAgICAgICAgICAgICAgICBfaW50cm9TbGlkZS5fdHJhbnNpdGlvbi5vbkNvbXBsZXRlID0gZnVuY3Rpb24gKCkge1xuICAgICAgICAgICAgICAgICAgICAgICAgICAgIF9zdGF0ZU1hbmFnZXIuX2NyZWF0ZWQgPSB0cnVlO1xuICAgICAgICAgICAgICAgICAgICAgICAgICAgIGNsZWFudXAoX3N0YXRlTWFuYWdlci5nYW1lLnN0YWdlLmNoaWxkcmVuKTtcbiAgICAgICAgICAgICAgICAgICAgICAgIH07XG4gICAgICAgICAgICAgICAgICAgIH1cblxuICAgICAgICAgICAgICAgICAgICBfc2xpZGUuZ28oc2xpZGVPdXRPcHRpb25zKTtcblxuICAgICAgICAgICAgICAgICAgICAvLyBQdXQgdGhlIG9yaWdpbmFsIGNyZWF0ZSBiYWNrXG4gICAgICAgICAgICAgICAgICAgIF9zdGF0ZS5jcmVhdGUgPSBfY2FjaGVkU3RhdGVDcmVhdGU7XG4gICAgICAgICAgICAgICAgfTtcbiAgICAgICAgICAgIH0oX3N0YXRlLCBzbGlkZU91dE9wdGlvbnMsIHNsaWRlSW5PcHRpb25zKSk7XG4gICAgICAgIH1cblxuICAgICAgICAvLyBTdGFydCB0aGUgY2FjaGVkIHN0Y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gICAgICBTbGlkZVJpZ2h0OiBEZWZhdWx0VHJhbnNpdGlvbih7XG4gICAgICAgICAgICBpbnRybzogdHJ1ZSxcbiAgICAgICAgICAgIHByb3BzOiB7XG4gICAgICAgICAgICAgICAgeDogZnVuY3Rpb24oZ2FtZSkge1xuICAgICAgICAgICAgICAgICAgICByZXR1cm4gLWdhbWUud2lkdGhcbiAgICAgICAgICAgICAgICB9XG4gICAgICAgICAgICB9XG4gICAgICAgIH0pLFxuXG4gICAgICAgIFNsaWRlVG9wOiBEZWZhdWx0VHJhbnNpdGlvbih7XG4gICAgICAgICAgICBpbnRybzogdHJ1ZSxcbiAgICAgICAgICAgIHByb3BzOiB7XG4gICAgICAgICAgICAgICAgeTogZnVuY3Rpb24oZ2FtZSkge1xuICAgICAgICAgICAgICAgICAgICByZXR1cm4gZ2FtZS5oZWlnaHRcbiAgICAgICAgICAgICAgICB9XG4gICAgICAgICAgICB9XG4gICAgICAgIH0pLFxuXG4gICAgICAgIFNsaWRlQm90dG9tOiBEZWZhdWx0VHJhbnNpdGlvbih7XG4gICAgICAgICAgICBpbnRybzogdHJ1ZSxcbiAgICAgICAgICAgIHByb3BzOiB7XG4gICAgICAgICAgICAgICAgeTogZnVuY3Rpb24oZ2FtZSkge1xuICAgICAgICAgICAgICAgICAgICByZXR1cm4gLWdhbWUuaGVpZ2h0XG4gICAgICAgICAgICAgICAgfVxuICAgICAgICAgICAgfVxuICAgICAgICB9KSxcblxuICAgICAgICBTY2FsZVVwOiBEZWZhdWx0VHJhbnNpdGlvbih7XG4gICAgICAgICAgICBpbnRybzogdHJ1ZSxcbiAgICAgICAgICAgIHByb3BzOiB7XG4gICAgICAgICAgICAgICAgYWxwaGE6IDAuNCxcbiAgICAgICAgICAgICAgICBzY2FsZTogbmV3IFBoYXNlci5Qb2ludCh7XG4gICAgICAgICAgICAgICAgICAgIHg6IDJcbiAgICAgICAgICAgICAgICB9KVxuICAgICAgICAgICAgfVxuICAgICAgICB9KVxuICAgIH07XG5cbiAgICAvKipcbiAgICAgKiBFeGl0IHRyYW5zaXRpb24gbGlzdFxuICAgICAqIEB0eXBlIHtvYmplY3R9XG4gICAgICovXG4gICAgU3RhdGVUcmFuc2l0aW9uLk91dCA9IHtcblxuICAgICAgICBTbGlkZUxlZnQ6IERlZmF1bHRUcmFuc2l0aW9uKHtcbiAgICAgICAgICAgIHByb3BzOiB7XG4gICAgICAgICAgICAgICAgeDogZnVuY3Rpb24oZ2FtZSkge1xuICAgICAgICAgICAgICAgICAgICByZXR1cm4gLWdhbWUud2lkdGhcbiAgICAgICAgICAgICAgICB9XG4gICAgICAgICAgICB9XG4gICAgICAgIH0pLFxuXG4gICAgICAgIFNsaWRlUmlnaHQ6IERlZmF1bHRUcmFuc2l0aW9uKHtcbiAgICAgICAgICAgIHByb3BzOiB7XG4gICAgICAgICAgICAgICAgeDogZnVuY3Rpb24oZ2FtZSkge1xuICAgICAgICAgICAgICAgICAgICByZXR1cm4gZ2FtZS53aWR0aFxuICAgICAgICAgICAgICAgIH1cbiAgICAgICAgICAgIH1cbiAgICAgICAgfSksXG5cbiAgICAgICAgU2xpZGVUb3A6IERlZmF1bHRUcmFuc2l0aW9uKHtcbiAgICAgICAgICAgIHByb3BzOiB7XG4gICAgICAgICAgICAgICAgeTogZnVuY3Rpb24oZ2FtZSkge1xuICAgICAgICAgICAgICAgICAgICByZXR1cm4gLWdhbWUuaGVpZ2h0XG4gICAgICAgICAgICAgICAgfVxuICAgICAgICAgICAgfVxuICAgICAgICB9KSxcblxuICAgICAgICBTbGlkZUJvdHRvbTogRGVmYXVsdFRyYW5zaXRpb24oe1xuICAgICAgICAgICAgcHJvcHM6IGZ1bmN0aW9uKGdhbWUpIHtcbiAgICAgICAgICAgICAgICByZXR1cm4ge1xuICAgICAgICAgICAgICAgICAgICB5OiBnYW1lLmhlaWdodFxuICAgICAgICAgICAgICAgIH1cbiAgICAgICAgICAgIH1cbiAgICAgICAgfSksXG5cbiAgICAgICAgU2NhbGVVcDogRGVmYX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1bmN0aW9uJykge1xuICAgICAgICAgICAgICAgICAgICBfcHJvcHNbcHJvcF0gPSBfcHJvcHNbcHJvcF0odGhpcy5nYW1lKTtcbiAgICAgICAgICAgICAgICB9XG5cbiAgICAgICAgICAgICAgICAvLyBJZiB0aGUgb3JpZ2luYWwgdmFsdWUgaXMgYW4gb2JqZWN0XG4gICAgICAgICAgICAgICAgLy8gd2UgbmVlZCBhIHNlcGFyYXRlIHR3ZWVuXG4gICAgICAgICAgICAgICAgaWYgKHR5cGVvZiB0YXJnZXRbcHJvcF0gPT09ICdvYmplY3QnKSB7XG4gICAgICAgICAgICAgICAgICAgIF9xdWV1ZVtwcm9wXSA9IF9wcm9wc1twcm9wXTtcbiAgICAgICAgICAgICAgICB9IGVsc2Uge1xuICAgICAgICAgICAgICAgICAgICBfcXVldWVbJ18nXVtwcm9wXSA9IF9wcm9wc1twcm9wXTtcbiAgICAgICAgICAgICAgICB9XG4gICAgICAgICAgICB9XG4gICAgICAgIH1cblxuICAgICAgICBmb3IgKHByb3AgaW4gX3F1ZXVlKSB7XG4gICAgICAgICAgICBpZiAoX3F1ZXVlLmhhc093blByb3BlcnR5KHByb3ApKSB7XG4gICAgICAgICAgICAgICAgX3R3ZWVuVGFyZ2V0ID0gcHJvcCA9PT0gJ18nID8gdGFyZ2V0IDogdGFyZ2V0W3Byb3BdO1xuXG4gICAgICAgICAgICAgICAgdGhpcy5fdHdlZW5zLnB1c2goXG4gICAgICAgICAgICAgICAgICAgIHRoaXMuZ2FtZS5hZGQudHdlZW4oX3R3ZWVuVGFyZ2V0KVxuICAgICAgICAgICAgICAgICAgICAgICAgW19pc0ludHJvID8gJ2Zyb20nIDogJ3RvJ10oXG4gICAgICAgICAgICAgICAgICAgICAgICAgICAgX3F1ZXVlW3Byb3BdLFxuICAgICAgICAgICAgICAgICAgICAgICAgICAgIG9wdGlvbnMuZHVyYXRpb24sXG4gICAgICAgICAgICAgICAgICAgICAgICAgICAgb3B0aW9ucy5lYXNlLFxuICAgICAgICAgICAgICAgICAgICAgICAgICAgIHRydWUsXG4gICAgICAgICAgICAgICAgICAgICAgICAgICAgb3B0aW9ucy5kZWxheVxuICAgICAgICAgICAgICAgICAgICAgICAgKVxuICAgICAgICAgICAgICAgICk7XG5cbiAgICAgICAgICAgICAgICBfdHdlZW5JbnN0YW5jZSA9IHRoaXMuX3R3ZWVuc1t0aGlzLl90d2VlbnMubGVuZ3RoIC0gMV07XG4gICAgICAgICAgICAgICAgX3R3ZWVuSW5zdGFuY2Uub25Db21wbGV0ZS5hZGRPbmNlKHRoaXMuX2NoZWNrRm9yQ29tcGxldGUsIHRoaXMpO1xuICAgICAgICAgICAgfVxuICAgICAgICB9XG4gICAgfTtcblxuICAgIC8qKlxuICAgICAqIFZlcmlmeSBjb21wbGV0ZSBzdGF0ZSBmb3IgdHJhbnNpdGlvblxuICAgICAqIEBwYXJhbSB0YXJnZXRcbiAgICAgKiBAcGFyYW0gdHdlZW5cbiAgICAgKiBAcHJpdmF0ZVxuICAgICAqL1xuICAgIFRyYW5zaXRpb24ucHJvdG90eXBlLl9jaGVja0ZvckNvbXBsZXRlID0gZnVuY3Rpb24odGFyZ2V0LCB0d2Vlbikge1xuICAgICAgICB2YXIgaSA9IDAsXG4gICAgICAgICAgICBsID0gdGhpcy5fdHdlZW5zLmxlbmd0aCxcbiAgICAgICAgICAgIF9jdXJyZW50VHdlZW4sXG4gICAgICAgICAgICBjb21wbGV0ZWQgPSAwO1xuXG4gICAgICAgIGZvcig7IGkgPCBsOyBpKyspIHtcbiAgICAgICAgICAgIF9jdXJyZW50VHdlZW4gPSB0aGlzLl90d2VlbnNbaV07XG4gICAgICAgICAgICBpZiAoX2N1cnJlbnRUd2Vlbi5pc1J1bm5pbmcgPT09IGZhbHNlKSB7XG4gICAgICAgICAgICAgICAgY29tcGxldGVkKys7XG4gICAgICAgICAgICAgICAgdGhpcy5nYW1lLnR3ZWVucy5yZW1vdmUodHdlZW4pO1xuICAgICAgICAgICAgfVxuICAgICAgICB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ZW50aWZpZXIgYmFzZWQgaW4gRGF0ZS5ub3coKSBzdGFtcC5cbiAgICAgKiBOb3QgdGhhdCByZWxpYWJsZS5cbiAgICAgKiBAcmV0dXJucyB7c3RyaW5nfVxuICAgICAqIEBwcml2YXRlXG4gICAgICovXG4gICAgZnVuY3Rpb24gX2dldElkZW50aWZpZXIoKSB7XG4gICAgICAgIHJldHVybiBEYXRlLm5vdygpLnRvU3RyaW5nKDIyKS5zdWJzdHIoLTQsIDQpO1xuICAgIH1cblxuICAgIG1vZHVsZS5leHBvcnRzID0gVHJhbnNpdGlvbjtcbn0oKSk7XG4iXX0= +var t=function(t){this.game=t,this.onComplete=null,this._tweens=[]};t.prototype.start=function(t,e){var n,o,r=e.props,a=!!e.intro,i={_:{}};for(n in this.currentTarget=t,"function"==typeof r&&(r=r(this.game)),a&&this._prepareTargetForTweening(r),r)r.hasOwnProperty(n)&&("function"==typeof r[n]&&(r[n]=r[n](this.game)),"object"==typeof t[n]?i[n]=r[n]:i._[n]=r[n]);for(n in i)i.hasOwnProperty(n)&&(this._tweens.push(this.game.add.tween("_"===n?t:t[n])[a?"from":"to"](i[n],e.duration,e.ease,!0,e.delay)),(o=this._tweens[this._tweens.length-1]).onComplete.addOnce(this._checkForComplete,this),o.onLoop.addOnce(function(){console.log("progress",this)},this))},t.prototype._checkForComplete=function(t,e){for(var n=0,o=this._tweens.length,r=0;n248},n=function(t){function n(n,o,r,a){var i=new Phaser.RenderTexture(n,n.width,n.height);if(t.call(this,n,o||0,r||0,i),!a){var s=new Phaser.Graphics(n,0,0);s.beginFill(n.stage.backgroundColor),s.drawRect(0,0,n.width,n.height),s.endFill(),n.stage.addChildAt(s,0),i.renderXY(s,0,0),n.stage.removeChild(s)}e()?i.renderXY(n.world,-1*n.camera.position.x,-1*n.camera.position.y):i.renderXY(n.world,n.width/2-n.camera.position.x,n.height/2-n.camera.position.y),this.inputEnabled=!0}return t&&(n.__proto__=t),(n.prototype=Object.create(t&&t.prototype)).constructor=n,n}(Phaser.Image),o=function(e,o){this.game=e,this._contentSnapshot=new n(e,0,0,o),this._transition=new t(e)};o.prototype.go=function(t){this.game.stage.addChildAt(this._contentSnapshot,this.game.stage.children.length),this._transition.start(this._contentSnapshot,t)};var r=function(t){for(var e=0,o=t.length;e0;)i[s]=arguments[s+3];(function(t,e,n){var a,i,s=this,h=s.states[t],p=h.create;s.game.stage&&r(s.game.stage.children),s.game.isBooted&&e&&(a=new o(s.game,e.noStage),function(t,e,n){t.create=function(){p.call(this),n&&(i=new o(s.game,n.noStage),s._created=!1,i.go(n),i._transition.onComplete=function(){s._created=!0,r(s.game.stage.children)}),a.go(e),t.create=p}}(h,e,n))}).call(this,t,e,n),a.call.apply(a,[this,t].concat(i))};var i=function(t){function e(){t.apply(this,arguments)}return t&&(e.__proto__=t),(e.prototype=Object.create(t&&t.prototype)).constructor=e,e}(Phaser.Plugin);i.createTransition=function(t){return{ease:t.ease||Phaser.Easing.Exponential.InOut,duration:t.duration||500,intro:t.intro||!1,props:t.props||{}}},module.exports=i; +//# sourceMappingURL=phaser-state-transition.js.map diff --git a/dist/phaser-state-transition.js.map b/dist/phaser-state-transition.js.map new file mode 100644 index 0000000..cd77f81 --- /dev/null +++ b/dist/phaser-state-transition.js.map @@ -0,0 +1 @@ +{"version":3,"file":"phaser-state-transition.js","sources":["../src/transition/transition.js","../src/core/content-snapshot.js","../src/core/slide.js","../src/core/state-manager-start.js","../src/index.js"],"sourcesContent":["/**\n * Transition Class\n * @constructor\n * @name Transition\n * @param {object} game Game instance\n */\nexport class Transition {\n\n constructor(game) {\n this.game = game;\n this.onComplete = null;\n this._tweens = [];\n }\n\n start(target, options) {\n let prop,\n _props = options.props,\n _isIntro = !!options.intro,\n _tweenTarget,\n _tweenInstance,\n _queue = {\n '_': {}\n };\n\n // Store the currentTarget\n this.currentTarget = target;\n\n // If we need to compile the output\n if (typeof _props === 'function') {\n _props = _props(this.game);\n }\n\n // Make sure the proper values for props are there\n _isIntro && this._prepareTargetForTweening(_props);\n\n // Parse the options.props and generate the tweens options\n for (prop in _props) {\n if (_props.hasOwnProperty(prop)) {\n // If we need to compile the output\n if (typeof _props[prop] === 'function') {\n _props[prop] = _props[prop](this.game);\n }\n\n // If the original value is an object\n // we need a separate tween\n if (typeof target[prop] === 'object') {\n _queue[prop] = _props[prop];\n } else {\n _queue['_'][prop] = _props[prop];\n }\n }\n }\n\n for (prop in _queue) {\n if (_queue.hasOwnProperty(prop)) {\n _tweenTarget = prop === '_' ? target : target[prop];\n\n this._tweens.push(\n this.game.add.tween(_tweenTarget)\n [_isIntro ? 'from' : 'to'](\n _queue[prop],\n options.duration,\n options.ease,\n true,\n options.delay\n )\n );\n\n _tweenInstance = this._tweens[this._tweens.length - 1];\n _tweenInstance.onComplete.addOnce(this._checkForComplete, this);\n _tweenInstance.onLoop.addOnce(function () {\n console.log('progress', this);\n }, this);\n }\n }\n }\n\n _checkForComplete(target, tween) {\n var i = 0,\n l = this._tweens.length,\n _currentTween,\n completed = 0;\n\n for(; i < l; i++) {\n _currentTween = this._tweens[i];\n if (_currentTween.isRunning === false) {\n completed++;\n this.game.tweens.remove(tween);\n }\n }\n\n if (completed === l) {\n this.onComplete && this.onComplete();\n this.currentTarget.destroy();\n }\n }\n\n _prepareTargetForTweening(props) {\n if (props.hasOwnProperty('alpha')) {\n this.currentTarget.alpha = 0;\n }\n }\n\n destroy(target) {\n target.destroy();\n }\n\n stop() {\n this._active = false;\n this.update();\n }\n\n}\n","const PHASER_LEGACY = 248;\n\nconst shouldRenderBasedOnCameraPosition = () => {\n const currentVersion = parseInt(Phaser.VERSION.replace(/\\./g, ''), 10);\n return currentVersion > PHASER_LEGACY;\n};\n\nexport class ContentSnapshot extends Phaser.Image {\n\n /**\n * Content Snapshot Class\n * @constructor\n * @name ContentSnapshot\n * @param {object} game Game object instance\n * @param {number} x Offset of x\n * @param {number} y Offset of y\n * @param {boolean} noStage Flag do skip rendering the stage for slider\n * @extend Phaser.Image\n */\n constructor(game, x, y, noStage) {\n const texture = new Phaser.RenderTexture(game, game.width, game.height);\n\n super(game, x || 0, y || 0, texture);\n\n if (!noStage) {\n // Create the game background fill\n const backgroundFill = new Phaser.Graphics(game, 0, 0);\n backgroundFill.beginFill(game.stage.backgroundColor);\n backgroundFill.drawRect(0, 0, game.width, game.height);\n backgroundFill.endFill();\n\n // Add the graphicFill object temporary to the stage at the base\n game.stage.addChildAt(backgroundFill, 0);\n texture.renderXY(backgroundFill, 0, 0);\n // After this is rendered to the texture, remove it\n game.stage.removeChild(backgroundFill);\n }\n\n // After 2.4.8 (0,0) it's basically middle\n if (shouldRenderBasedOnCameraPosition()) {\n texture.renderXY(game.world, game.camera.position.x * -1, game.camera.position.y * -1);\n } else {\n texture.renderXY(game.world, game.width / 2 - game.camera.position.x, game.height / 2 - game.camera.position.y);\n }\n\n // Capture all input events\n this.inputEnabled = true;\n }\n}\n","import { Transition } from \"../transition/transition\";\nimport { ContentSnapshot } from \"./content-snapshot\";\n\nexport class Slide {\n\n constructor(game, noStage) {\n this.game = game;\n this._contentSnapshot = new ContentSnapshot(game, 0, 0, noStage);\n this._transition = new Transition(game);\n }\n\n go(options) {\n this.game.stage.addChildAt(this._contentSnapshot, this.game.stage.children.length);\n this._transition.start(this._contentSnapshot, options);\n }\n\n}\n","import { Slide } from \"./slide\";\nimport { ContentSnapshot } from \"./content-snapshot\";\n\nconst cleanup = (children) => {\n let i = 0;\n let l = children.length;\n\n for(; i < l; i += 1) {\n if (children[i] && (children[i] instanceof ContentSnapshot)) {\n children[i].destroy();\n }\n }\n};\n\nexport function stateManagerStart(stateId, slideOutOptions, slideInOptions) {\n let _slide,\n _introSlide,\n _stateManager = this,\n _state = _stateManager.states[stateId],\n _cachedStateCreate = _state.create;\n\n _stateManager.game.stage && cleanup(_stateManager.game.stage.children);\n\n if (_stateManager.game.isBooted && slideOutOptions) {\n _slide = new Slide(_stateManager.game, slideOutOptions.noStage);\n\n (function (_state, slideOutOptions, slideInOptions) {\n _state.create = function () {\n _cachedStateCreate.call(this);\n\n // Slide in intro\n if (slideInOptions) {\n _introSlide = new Slide(_stateManager.game, slideInOptions.noStage);\n _stateManager._created = false;\n _introSlide.go(slideInOptions);\n\n _introSlide._transition.onComplete = function () {\n _stateManager._created = true;\n cleanup(_stateManager.game.stage.children);\n };\n }\n\n _slide.go(slideOutOptions);\n\n // Put the original create back\n _state.create = _cachedStateCreate;\n };\n }(_state, slideOutOptions, slideInOptions));\n }\n}\n","import { stateManagerStart } from \"./core/state-manager-start\";\n\nconst cachedStart = Phaser.StateManager.prototype.start;\nPhaser.StateManager.prototype.start = function start(stateId, slideInOption, slideOut, ...args) {\n stateManagerStart.call(this, stateId, slideInOption, slideOut);\n cachedStart.call(this, stateId, ...args);\n};\n\n/**\n * Creates a transition object\n * @param options\n * @returns {{ease: *, duration: number, intro: boolean, props: {}}}\n */\nexport const createTransition = (options) => {\n return {\n ease: options.ease || Phaser.Easing.Exponential.InOut,\n duration: options.duration || 500,\n intro: options.intro || false,\n props: options.props || {}\n }\n};\n\nexport default class StateTransition extends Phaser.Plugin {}\n\n// Expose the createTransition function\nStateTransition.createTransition = createTransition;"],"names":["Transition","game","onComplete","_tweens","start","target","options","prop","_tweenInstance","_props","props","_isIntro","intro","_queue","currentTarget","this","_prepareTargetForTweening","hasOwnProperty","push","add","tween","duration","ease","delay","length","addOnce","_checkForComplete","onLoop","log","i","l","completed","isRunning","tweens","remove","destroy","alpha","stop","_active","update","const","shouldRenderBasedOnCameraPosition","parseInt","Phaser","VERSION","replace","ContentSnapshot","x","y","noStage","texture","RenderTexture","width","height","super","backgroundFill","Graphics","beginFill","stage","backgroundColor","drawRect","endFill","addChildAt","renderXY","removeChild","world","camera","position","inputEnabled","Image","Slide","_contentSnapshot","_transition","go","children","cleanup","let","cachedStart","StateManager","prototype","stateId","slideInOption","slideOut","slideOutOptions","slideInOptions","_slide","_introSlide","_stateManager","_state","states","_cachedStateCreate","create","isBooted","call","_created","args","StateTransition","Plugin","createTransition","Easing","Exponential","InOut"],"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AgBxE,KAChByE,EAASD,EAAcE,OAAOT,GAC9BU,EAAqBF,EAAOG,OAEhCJ,EAActF,KAAKyD,OAASiB,EAAQY,EAActF,KAAKyD,MAAMgB,UAEzDa,EAActF,KAAK2F,UAAYT,IAC/BE,EAAS,IAAIf,EAAMiB,EAActF,KAAMkF,EAAgBlC,kBAE5CuC,EAAQL,EAAiBC,GAChCI,EAAOG,OAAS,WACZD,EAAmBG,KAAK9E,MAGpBqE,IACAE,EAAc,IAAIhB,EAAMiB,EAActF,KAAMmF,EAAenC,SAC3DsC,EAAcO,UAAW,EACzBR,EAAYb,GAAGW,GAEfE,EAAYd,YAAYtE,WAAa,WACjCqF,EAAcO,UAAW,EACzBnB,EAAQY,EAActF,KAAKyD,MAAMgB,YAIzCW,EAAOZ,GAAGU,GAGVK,EAAOG,OAASD,IAEtBF,EAAQL,EAAiBC,MC3CbS,KAAK9E,KAAMiE,EAASC,EAAeC,GACrDL,EAAYgB,cAAK9E,KAAMiE,UAAYe,KAQvC,IASqBC,0IAAwBrD,OAAOsD,UAGpCC,0BAZiB5F,UAEzBgB,KAAMhB,EAAQgB,MAAQqB,OAAOwD,OAAOC,YAAYC,MAChDhF,SAAUf,EAAQe,UAAY,IAC9BT,MAAON,EAAQM,QAAS,EACxBF,MAAOJ,EAAQI"} \ No newline at end of file diff --git a/dist/phaser-state-transition.m.js b/dist/phaser-state-transition.m.js new file mode 100644 index 0000000..22af578 --- /dev/null +++ b/dist/phaser-state-transition.m.js @@ -0,0 +1,2 @@ +var t=function(t){this.game=t,this.onComplete=null,this._tweens=[]};t.prototype.start=function(t,e){var n,o,r=e.props,a=!!e.intro,i={_:{}};for(n in this.currentTarget=t,"function"==typeof r&&(r=r(this.game)),a&&this._prepareTargetForTweening(r),r)r.hasOwnProperty(n)&&("function"==typeof r[n]&&(r[n]=r[n](this.game)),"object"==typeof t[n]?i[n]=r[n]:i._[n]=r[n]);for(n in i)i.hasOwnProperty(n)&&(this._tweens.push(this.game.add.tween("_"===n?t:t[n])[a?"from":"to"](i[n],e.duration,e.ease,!0,e.delay)),(o=this._tweens[this._tweens.length-1]).onComplete.addOnce(this._checkForComplete,this),o.onLoop.addOnce(function(){console.log("progress",this)},this))},t.prototype._checkForComplete=function(t,e){for(var n=0,o=this._tweens.length,r=0;n248},n=function(t){function n(n,o,r,a){var i=new Phaser.RenderTexture(n,n.width,n.height);if(t.call(this,n,o||0,r||0,i),!a){var s=new Phaser.Graphics(n,0,0);s.beginFill(n.stage.backgroundColor),s.drawRect(0,0,n.width,n.height),s.endFill(),n.stage.addChildAt(s,0),i.renderXY(s,0,0),n.stage.removeChild(s)}e()?i.renderXY(n.world,-1*n.camera.position.x,-1*n.camera.position.y):i.renderXY(n.world,n.width/2-n.camera.position.x,n.height/2-n.camera.position.y),this.inputEnabled=!0}return t&&(n.__proto__=t),(n.prototype=Object.create(t&&t.prototype)).constructor=n,n}(Phaser.Image),o=function(e,o){this.game=e,this._contentSnapshot=new n(e,0,0,o),this._transition=new t(e)};o.prototype.go=function(t){this.game.stage.addChildAt(this._contentSnapshot,this.game.stage.children.length),this._transition.start(this._contentSnapshot,t)};var r=function(t){for(var e=0,o=t.length;e0;)i[s]=arguments[s+3];(function(t,e,n){var a,i,s=this,h=s.states[t],p=h.create;s.game.stage&&r(s.game.stage.children),s.game.isBooted&&e&&(a=new o(s.game,e.noStage),function(t,e,n){t.create=function(){p.call(this),n&&(i=new o(s.game,n.noStage),s._created=!1,i.go(n),i._transition.onComplete=function(){s._created=!0,r(s.game.stage.children)}),a.go(e),t.create=p}}(h,e,n))}).call(this,t,e,n),a.call.apply(a,[this,t].concat(i))};var i=function(t){return{ease:t.ease||Phaser.Easing.Exponential.InOut,duration:t.duration||500,intro:t.intro||!1,props:t.props||{}}},s=function(t){function e(){t.apply(this,arguments)}return t&&(e.__proto__=t),(e.prototype=Object.create(t&&t.prototype)).constructor=e,e}(Phaser.Plugin);s.createTransition=i;export default s;export{i as createTransition}; +//# sourceMappingURL=phaser-state-transition.m.js.map diff --git a/dist/phaser-state-transition.m.js.map b/dist/phaser-state-transition.m.js.map new file mode 100644 index 0000000..d6cb338 --- /dev/null +++ b/dist/phaser-state-transition.m.js.map @@ -0,0 +1 @@ +{"version":3,"file":"phaser-state-transition.m.js","sources":["../src/transition/transition.js","../src/core/content-snapshot.js","../src/core/slide.js","../src/core/state-manager-start.js","../src/index.js"],"sourcesContent":["/**\n * Transition Class\n * @constructor\n * @name Transition\n * @param {object} game Game instance\n */\nexport class Transition {\n\n constructor(game) {\n this.game = game;\n this.onComplete = null;\n this._tweens = [];\n }\n\n start(target, options) {\n let prop,\n _props = options.props,\n _isIntro = !!options.intro,\n _tweenTarget,\n _tweenInstance,\n _queue = {\n '_': {}\n };\n\n // Store the currentTarget\n this.currentTarget = target;\n\n // If we need to compile the output\n if (typeof _props === 'function') {\n _props = _props(this.game);\n }\n\n // Make sure the proper values for props are there\n _isIntro && this._prepareTargetForTweening(_props);\n\n // Parse the options.props and generate the tweens options\n for (prop in _props) {\n if (_props.hasOwnProperty(prop)) {\n // If we need to compile the output\n if (typeof _props[prop] === 'function') {\n _props[prop] = _props[prop](this.game);\n }\n\n // If the original value is an object\n // we need a separate tween\n if (typeof target[prop] === 'object') {\n _queue[prop] = _props[prop];\n } else {\n _queue['_'][prop] = _props[prop];\n }\n }\n }\n\n for (prop in _queue) {\n if (_queue.hasOwnProperty(prop)) {\n _tweenTarget = prop === '_' ? target : target[prop];\n\n this._tweens.push(\n this.game.add.tween(_tweenTarget)\n [_isIntro ? 'from' : 'to'](\n _queue[prop],\n options.duration,\n options.ease,\n true,\n options.delay\n )\n );\n\n _tweenInstance = this._tweens[this._tweens.length - 1];\n _tweenInstance.onComplete.addOnce(this._checkForComplete, this);\n _tweenInstance.onLoop.addOnce(function () {\n console.log('progress', this);\n }, this);\n }\n }\n }\n\n _checkForComplete(target, tween) {\n var i = 0,\n l = this._tweens.length,\n _currentTween,\n completed = 0;\n\n for(; i < l; i++) {\n _currentTween = this._tweens[i];\n if (_currentTween.isRunning === false) {\n completed++;\n this.game.tweens.remove(tween);\n }\n }\n\n if (completed === l) {\n this.onComplete && this.onComplete();\n this.currentTarget.destroy();\n }\n }\n\n _prepareTargetForTweening(props) {\n if (props.hasOwnProperty('alpha')) {\n this.currentTarget.alpha = 0;\n }\n }\n\n destroy(target) {\n target.destroy();\n }\n\n stop() {\n this._active = false;\n this.update();\n }\n\n}\n","const PHASER_LEGACY = 248;\n\nconst shouldRenderBasedOnCameraPosition = () => {\n const currentVersion = parseInt(Phaser.VERSION.replace(/\\./g, ''), 10);\n return currentVersion > PHASER_LEGACY;\n};\n\nexport class ContentSnapshot extends Phaser.Image {\n\n /**\n * Content Snapshot Class\n * @constructor\n * @name ContentSnapshot\n * @param {object} game Game object instance\n * @param {number} x Offset of x\n * @param {number} y Offset of y\n * @param {boolean} noStage Flag do skip rendering the stage for slider\n * @extend Phaser.Image\n */\n constructor(game, x, y, noStage) {\n const texture = new Phaser.RenderTexture(game, game.width, game.height);\n\n super(game, x || 0, y || 0, texture);\n\n if (!noStage) {\n // Create the game background fill\n const backgroundFill = new Phaser.Graphics(game, 0, 0);\n backgroundFill.beginFill(game.stage.backgroundColor);\n backgroundFill.drawRect(0, 0, game.width, game.height);\n backgroundFill.endFill();\n\n // Add the graphicFill object temporary to the stage at the base\n game.stage.addChildAt(backgroundFill, 0);\n texture.renderXY(backgroundFill, 0, 0);\n // After this is rendered to the texture, remove it\n game.stage.removeChild(backgroundFill);\n }\n\n // After 2.4.8 (0,0) it's basically middle\n if (shouldRenderBasedOnCameraPosition()) {\n texture.renderXY(game.world, game.camera.position.x * -1, game.camera.position.y * -1);\n } else {\n texture.renderXY(game.world, game.width / 2 - game.camera.position.x, game.height / 2 - game.camera.position.y);\n }\n\n // Capture all input events\n this.inputEnabled = true;\n }\n}\n","import { Transition } from \"../transition/transition\";\nimport { ContentSnapshot } from \"./content-snapshot\";\n\nexport class Slide {\n\n constructor(game, noStage) {\n this.game = game;\n this._contentSnapshot = new ContentSnapshot(game, 0, 0, noStage);\n this._transition = new Transition(game);\n }\n\n go(options) {\n this.game.stage.addChildAt(this._contentSnapshot, this.game.stage.children.length);\n this._transition.start(this._contentSnapshot, options);\n }\n\n}\n","import { Slide } from \"./slide\";\nimport { ContentSnapshot } from \"./content-snapshot\";\n\nconst cleanup = (children) => {\n let i = 0;\n let l = children.length;\n\n for(; i < l; i += 1) {\n if (children[i] && (children[i] instanceof ContentSnapshot)) {\n children[i].destroy();\n }\n }\n};\n\nexport function stateManagerStart(stateId, slideOutOptions, slideInOptions) {\n let _slide,\n _introSlide,\n _stateManager = this,\n _state = _stateManager.states[stateId],\n _cachedStateCreate = _state.create;\n\n _stateManager.game.stage && cleanup(_stateManager.game.stage.children);\n\n if (_stateManager.game.isBooted && slideOutOptions) {\n _slide = new Slide(_stateManager.game, slideOutOptions.noStage);\n\n (function (_state, slideOutOptions, slideInOptions) {\n _state.create = function () {\n _cachedStateCreate.call(this);\n\n // Slide in intro\n if (slideInOptions) {\n _introSlide = new Slide(_stateManager.game, slideInOptions.noStage);\n _stateManager._created = false;\n _introSlide.go(slideInOptions);\n\n _introSlide._transition.onComplete = function () {\n _stateManager._created = true;\n cleanup(_stateManager.game.stage.children);\n };\n }\n\n _slide.go(slideOutOptions);\n\n // Put the original create back\n _state.create = _cachedStateCreate;\n };\n }(_state, slideOutOptions, slideInOptions));\n }\n}\n","import { stateManagerStart } from \"./core/state-manager-start\";\n\nconst cachedStart = Phaser.StateManager.prototype.start;\nPhaser.StateManager.prototype.start = function start(stateId, slideInOption, slideOut, ...args) {\n stateManagerStart.call(this, stateId, slideInOption, slideOut);\n cachedStart.call(this, stateId, ...args);\n};\n\n/**\n * Creates a transition object\n * @param options\n * @returns {{ease: *, duration: number, intro: boolean, props: {}}}\n */\nexport const createTransition = (options) => {\n return {\n ease: options.ease || Phaser.Easing.Exponential.InOut,\n duration: options.duration || 500,\n intro: options.intro || false,\n props: options.props || {}\n }\n};\n\nexport default class StateTransition extends Phaser.Plugin {}\n\n// Expose the createTransition function\nStateTransition.createTransition = createTransition;"],"names":["Transition","game","onComplete","_tweens","start","target","options","prop","_tweenInstance","_props","props","_isIntro","intro","_queue","currentTarget","this","_prepareTargetForTweening","hasOwnProperty","push","add","tween","duration","ease","delay","length","addOnce","_checkForComplete","onLoop","log","i","l","completed","isRunning","tweens","remove","destroy","alpha","stop","_active","update","const","shouldRenderBasedOnCameraPosition","parseInt","Phaser","VERSION","replace","ContentSnapshot","x","y","noStage","texture","RenderTexture","width","height","super","backgroundFill","Graphics","beginFill","stage","backgroundColor","drawRect","endFill","addChildAt","renderXY","removeChild","world","camera","position","inputEnabled","Image","Slide","_contentSnapshot","_transition","go","children","cleanup","let","cachedStart","StateManager","prototype","stateId","slideInOption","slideOut","slideOutOptions","slideInOptions","_slide","_introSlide","_stateManager","_state","states","_cachedStateCreate","create","isBooted","call","_created","args","createTransition","Easing","Exponential","InOut","StateTransition","Plugin"],"mappings":"AAMO,IAAMA,EAET,SAAYC,QACHA,KAAOA,OACPC,WAAa,UACbC,wBAGTC,eAAMC,EAAQC,OACNC,EAIAC,EAHAC,EAASH,EAAQI,MACjBC,IAAaL,EAAQM,MAGrBC,aAgBCN,UAXAO,cAAgBT,EAGC,mBAAXI,MACEA,EAAOM,KAAKd,UAIbc,KAAKC,0BAA0BP,GAG9BA,EACLA,EAAOQ,eAAeV,KAEM,mBAAjBE,EAAOF,OACPA,GAAQE,EAAOF,QAAWN,OAKT,iBAAjBI,EAAOE,KACPA,GAAQE,EAAOF,OAEVA,GAAQE,EAAOF,QAKlCA,KAAQM,EACLA,EAAOI,eAAeV,UAGjBJ,QAAQe,UACJjB,KAAKkB,IAAIC,MAHM,MAATb,EAAeF,EAASA,EAAOE,IAIrCI,EAAW,OAAS,MACrBE,EAAON,GACPD,EAAQe,SACRf,EAAQgB,MACR,EACAhB,EAAQiB,gBAIMpB,aAAaA,QAAQqB,OAAS,IACrCtB,WAAWuB,aAAaC,0BACxBC,OAAOF,QAAQ,mBAClBG,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B,EAAQgB,MAAQqB,OAAOsD,OAAOC,YAAYC,MAChD9E,SAAUf,EAAQe,UAAY,IAC9BT,MAAON,EAAQM,QAAS,EACxBF,MAAOJ,EAAQI,YAIF0F,0IAAwBzD,OAAO0D,UAGpCL,iBAAmBA"} \ No newline at end of file diff --git a/dist/phaser-state-transition.min.js b/dist/phaser-state-transition.min.js deleted file mode 100644 index 512078a..0000000 --- a/dist/phaser-state-transition.min.js +++ /dev/null @@ -1 +0,0 @@ -(function t(e,n,i){function r(a,s){if(!n[a]){if(!e[a]){var h=typeof require=="function"&&require;if(!s&&h)return h(a,!0);if(o)return o(a,!0);var c=new Error("Cannot find module '"+a+"'");throw c.code="MODULE_NOT_FOUND",c}var u=n[a]={exports:{}};e[a][0].call(u.exports,function(t){var n=e[a][1][t];return r(n?n:t)},u,u.exports,t,e,n,i)}return n[a].exports}var o=typeof require=="function"&&require;for(var a=0;at){this._texture.renderXY(e.world,e.camera.position.x*-1,e.camera.position.y*-1)}else{this._texture.renderXY(e.world,e.width/2-e.camera.position.x,e.height/2-e.camera.position.y)}Phaser.Image.call(this,e,n||0,i||0,this._texture);this.inputEnabled=true}n.prototype=Object.create(Phaser.Image.prototype);n.constructor=n;e.exports=n})()},{}],2:[function(t,e,n){(function(){"use strict";var n=t("../transition/Transition"),i=t("./ContentSnapshot");function r(t,e){this.game=t;this._contentSnapshot=new i(t,0,0,e);this._transition=new n(t)}r.prototype.go=function(t){this.game.stage.addChildAt(this._contentSnapshot,this.game.stage.children.length);this._transition.start(this._contentSnapshot,t)};e.exports=r})()},{"../transition/Transition":7,"./ContentSnapshot":1}],3:[function(t,e,n){(function(){"use strict";var n=Phaser.StateManager.prototype.start,i=t("./Slide"),r=t("./ContentSnapshot");function o(t){var e=0,n=t.length;for(;e248},n=function(t){function n(n,o,r,a){var i=new Phaser.RenderTexture(n,n.width,n.height);if(t.call(this,n,o||0,r||0,i),!a){var s=new Phaser.Graphics(n,0,0);s.beginFill(n.stage.backgroundColor),s.drawRect(0,0,n.width,n.height),s.endFill(),n.stage.addChildAt(s,0),i.renderXY(s,0,0),n.stage.removeChild(s)}e()?i.renderXY(n.world,-1*n.camera.position.x,-1*n.camera.position.y):i.renderXY(n.world,n.width/2-n.camera.position.x,n.height/2-n.camera.position.y),this.inputEnabled=!0}return t&&(n.__proto__=t),(n.prototype=Object.create(t&&t.prototype)).constructor=n,n}(Phaser.Image),o=function(e,o){this.game=e,this._contentSnapshot=new n(e,0,0,o),this._transition=new t(e)};o.prototype.go=function(t){this.game.stage.addChildAt(this._contentSnapshot,this.game.stage.children.length),this._transition.start(this._contentSnapshot,t)};var r=function(t){for(var e=0,o=t.length;e0;)i[s]=arguments[s+3];(function(t,e,n){var a,i,s=this,h=s.states[t],p=h.create;s.game.stage&&r(s.game.stage.children),s.game.isBooted&&e&&(a=new o(s.game,e.noStage),function(t,e,n){t.create=function(){p.call(this),n&&(i=new o(s.game,n.noStage),s._created=!1,i.go(n),i._transition.onComplete=function(){s._created=!0,r(s.game.stage.children)}),a.go(e),t.create=p}}(h,e,n))}).call(this,t,e,n),a.call.apply(a,[this,t].concat(i))};var i=function(t){function e(){t.apply(this,arguments)}return t&&(e.__proto__=t),(e.prototype=Object.create(t&&t.prototype)).constructor=e,e}(Phaser.Plugin);return i.createTransition=function(t){return{ease:t.ease||Phaser.Easing.Exponential.InOut,duration:t.duration||500,intro:t.intro||!1,props:t.props||{}}},i}); +//# sourceMappingURL=phaser-state-transition.umd.js.map diff --git a/dist/phaser-state-transition.umd.js.map b/dist/phaser-state-transition.umd.js.map new file mode 100644 index 0000000..e3f4166 --- /dev/null +++ b/dist/phaser-state-transition.umd.js.map @@ -0,0 +1 @@ +{"version":3,"file":"phaser-state-transition.umd.js","sources":["../src/transition/transition.js","../src/core/content-snapshot.js","../src/core/slide.js","../src/core/state-manager-start.js","../src/index.js"],"sourcesContent":["/**\n * Transition Class\n * @constructor\n * @name Transition\n * @param {object} game Game instance\n */\nexport class Transition {\n\n constructor(game) {\n this.game = game;\n this.onComplete = null;\n this._tweens = [];\n }\n\n start(target, options) {\n let prop,\n _props = options.props,\n _isIntro = !!options.intro,\n _tweenTarget,\n _tweenInstance,\n _queue = {\n '_': {}\n };\n\n // Store the currentTarget\n this.currentTarget = target;\n\n // If we need to compile the output\n if (typeof _props === 'function') {\n _props = _props(this.game);\n }\n\n // Make sure the proper values for props are there\n _isIntro && this._prepareTargetForTweening(_props);\n\n // Parse the options.props and generate the tweens options\n for (prop in _props) {\n if (_props.hasOwnProperty(prop)) {\n // If we need to compile the output\n if (typeof _props[prop] === 'function') {\n _props[prop] = _props[prop](this.game);\n }\n\n // If the original value is an object\n // we need a separate tween\n if (typeof target[prop] === 'object') {\n _queue[prop] = _props[prop];\n } else {\n _queue['_'][prop] = _props[prop];\n }\n }\n }\n\n for (prop in _queue) {\n if (_queue.hasOwnProperty(prop)) {\n _tweenTarget = prop === '_' ? target : target[prop];\n\n this._tweens.push(\n this.game.add.tween(_tweenTarget)\n [_isIntro ? 'from' : 'to'](\n _queue[prop],\n options.duration,\n options.ease,\n true,\n options.delay\n )\n );\n\n _tweenInstance = this._tweens[this._tweens.length - 1];\n _tweenInstance.onComplete.addOnce(this._checkForComplete, this);\n _tweenInstance.onLoop.addOnce(function () {\n console.log('progress', this);\n }, this);\n }\n }\n }\n\n _checkForComplete(target, tween) {\n var i = 0,\n l = this._tweens.length,\n _currentTween,\n completed = 0;\n\n for(; i < l; i++) {\n _currentTween = this._tweens[i];\n if (_currentTween.isRunning === false) {\n completed++;\n this.game.tweens.remove(tween);\n }\n }\n\n if (completed === l) {\n this.onComplete && this.onComplete();\n this.currentTarget.destroy();\n }\n }\n\n _prepareTargetForTweening(props) {\n if (props.hasOwnProperty('alpha')) {\n this.currentTarget.alpha = 0;\n }\n }\n\n destroy(target) {\n target.destroy();\n }\n\n stop() {\n this._active = false;\n this.update();\n }\n\n}\n","const PHASER_LEGACY = 248;\n\nconst shouldRenderBasedOnCameraPosition = () => {\n const currentVersion = parseInt(Phaser.VERSION.replace(/\\./g, ''), 10);\n return currentVersion > PHASER_LEGACY;\n};\n\nexport class ContentSnapshot extends Phaser.Image {\n\n /**\n * Content Snapshot Class\n * @constructor\n * @name ContentSnapshot\n * @param {object} game Game object instance\n * @param {number} x Offset of x\n * @param {number} y Offset of y\n * @param {boolean} noStage Flag do skip rendering the stage for slider\n * @extend Phaser.Image\n */\n constructor(game, x, y, noStage) {\n const texture = new Phaser.RenderTexture(game, game.width, game.height);\n\n super(game, x || 0, y || 0, texture);\n\n if (!noStage) {\n // Create the game background fill\n const backgroundFill = new Phaser.Graphics(game, 0, 0);\n backgroundFill.beginFill(game.stage.backgroundColor);\n backgroundFill.drawRect(0, 0, game.width, game.height);\n backgroundFill.endFill();\n\n // Add the graphicFill object temporary to the stage at the base\n game.stage.addChildAt(backgroundFill, 0);\n texture.renderXY(backgroundFill, 0, 0);\n // After this is rendered to the texture, remove it\n game.stage.removeChild(backgroundFill);\n }\n\n // After 2.4.8 (0,0) it's basically middle\n if (shouldRenderBasedOnCameraPosition()) {\n texture.renderXY(game.world, game.camera.position.x * -1, game.camera.position.y * -1);\n } else {\n texture.renderXY(game.world, game.width / 2 - game.camera.position.x, game.height / 2 - game.camera.position.y);\n }\n\n // Capture all input events\n this.inputEnabled = true;\n }\n}\n","import { Transition } from \"../transition/transition\";\nimport { ContentSnapshot } from \"./content-snapshot\";\n\nexport class Slide {\n\n constructor(game, noStage) {\n this.game = game;\n this._contentSnapshot = new ContentSnapshot(game, 0, 0, noStage);\n this._transition = new Transition(game);\n }\n\n go(options) {\n this.game.stage.addChildAt(this._contentSnapshot, this.game.stage.children.length);\n this._transition.start(this._contentSnapshot, options);\n }\n\n}\n","import { Slide } from \"./slide\";\nimport { ContentSnapshot } from \"./content-snapshot\";\n\nconst cleanup = (children) => {\n let i = 0;\n let l = children.length;\n\n for(; i < l; i += 1) {\n if (children[i] && (children[i] instanceof ContentSnapshot)) {\n children[i].destroy();\n }\n }\n};\n\nexport function stateManagerStart(stateId, slideOutOptions, slideInOptions) {\n let _slide,\n _introSlide,\n _stateManager = this,\n _state = _stateManager.states[stateId],\n _cachedStateCreate = _state.create;\n\n _stateManager.game.stage && cleanup(_stateManager.game.stage.children);\n\n if (_stateManager.game.isBooted && slideOutOptions) {\n _slide = new Slide(_stateManager.game, slideOutOptions.noStage);\n\n (function (_state, slideOutOptions, slideInOptions) {\n _state.create = function () {\n _cachedStateCreate.call(this);\n\n // Slide in intro\n if (slideInOptions) {\n _introSlide = new Slide(_stateManager.game, slideInOptions.noStage);\n _stateManager._created = false;\n _introSlide.go(slideInOptions);\n\n _introSlide._transition.onComplete = function () {\n _stateManager._created = true;\n cleanup(_stateManager.game.stage.children);\n };\n }\n\n _slide.go(slideOutOptions);\n\n // Put the original create back\n _state.create = _cachedStateCreate;\n };\n }(_state, slideOutOptions, slideInOptions));\n }\n}\n","import { stateManagerStart } from \"./core/state-manager-start\";\n\nconst cachedStart = Phaser.StateManager.prototype.start;\nPhaser.StateManager.prototype.start = function start(stateId, slideInOption, slideOut, ...args) {\n stateManagerStart.call(this, stateId, slideInOption, slideOut);\n cachedStart.call(this, stateId, ...args);\n};\n\n/**\n * Creates a transition object\n * @param options\n * @returns {{ease: *, duration: number, intro: boolean, props: {}}}\n */\nexport const createTransition = (options) => {\n return {\n ease: options.ease || Phaser.Easing.Exponential.InOut,\n duration: options.duration || 500,\n intro: options.intro || false,\n props: options.props || {}\n }\n};\n\nexport default class StateTransition extends Phaser.Plugin {}\n\n// Expose the createTransition function\nStateTransition.createTransition = createTransition;"],"names":["Transition","game","onComplete","_tweens","start","target","options","prop","_tweenInstance","_props","props","_isIntro","intro","_queue","currentTarget","this","_prepareTargetForTweening","hasOwnProperty","push","add","tween","duration","ease","delay","length","addOnce","_checkForComplete","onLoop","log","i","l","completed","isRunning","tweens","remove","destroy","alpha","stop","_active","update","const","shouldRenderBasedOnCameraPosition","parseInt","Phaser","VERSION","replace","ContentSnapshot","x","y","noStage","texture","RenderTexture","width","height","super","backgroundFill","Graphics","beginFill","stage","backgroundColor","drawRect","endFill","addChildAt","renderXY","removeChild","world","camera","position","inputEnabled","Image","Slide","_contentSnapshot","_transition","go","children","cleanup","let","cachedStart","StateManager","prototype","stateId","slideInOption","slideOut","slideOutOptions","slideInOptions","_slide","_introSlide","_stateManager","_state","states","_cachedStateCreate","create","isBooted","call","_created","args","StateTransition","Plugin","createTransition","Easing","Exponential","InOut"],"mappings":"8KAMO,IAAMA,EAET,SAAYC,QACHA,KAAOA,OACPC,WAAa,UACbC,wBAGTC,eAAMC,EAAQC,OACNC,EAIAC,EAHAC,EAASH,EAAQI,MACjBC,IAAaL,EAAQM,MAGrBC,aAgBCN,UAXAO,cAAgBT,EAGC,mBAAXI,MACEA,EAAOM,KAAKd,UAIbc,KAAKC,0BAA0BP,GAG9BA,EACLA,EAAOQ,eAAeV,KAEM,mBAAjBE,EAAOF,OACPA,GAAQE,EAAOF,QAAWN,OAKT,iBAAjBI,EAAOE,KACPA,GAAQE,EAAOF,OAEVA,GAAQE,EAAOF,QAKlCA,KAAQM,EACLA,EAAOI,eAAeV,UAGjBJ,QAAQe,UACJjB,KAAKkB,IAAIC,MAHM,MAATb,EAAeF,EAASA,EAAOE,IAIrCI,EAAW,OAAS,MACrBE,EAAON,GACPD,EAAQe,SACRf,EAAQgB,MACR,EACAhB,EAAQiB,gBAIMpB,aAAaA,QAAQqB,OAAS,IACrCtB,WAAWuB,aAAaC,0BACxBC,OAAOF,QAAQ,mBAClBG,IAAI,WAAYb,2BAMxCW,2BAAkBrB,EAAQe,WAClBS,EAAI,EACJC,EAAIf,KAAKZ,QAAQqB,OAEjBO,EAAY,EAEVF,EAAIC,EAAGD,KAEuB,SADX1B,QAAQ0B,GACXG,qBAET/B,KAAKgC,OAAOC,OAAOd,IAI5BW,IAAcD,SACT5B,YAAca,KAAKb,kBACnBY,cAAcqB,wBAI3BnB,mCAA0BN,GAClBA,EAAMO,eAAe,gBAChBH,cAAcsB,MAAQ,gBAInCD,iBAAQ9B,KACG8B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AAIC,EAAGD,GAAK,EACV6C,EAAS7C,IAAO6C,EAAS7C,aAAciB,GACvC4B,EAAS7C,GAAGM,WCPxBK,IAAMqC,EAAclC,OAAOmC,aAAaC,UAAU3E,MAClDuC,OAAOmC,aAAaC,UAAU3E,MAAQ,SAAe4E,EAASC,EAAeC,kEDW7E,SAAkCF,EAASG,EAAiBC,GACxDR,IAAIS,EACAC,EACAC,EAAgBxE,KAChByE,EAASD,EAAcE,OAAOT,GAC9BU,EAAqBF,EAAOG,OAEhCJ,EAActF,KAAKyD,OAASiB,EAAQY,EAActF,KAAKyD,MAAMgB,UAEzDa,EAActF,KAAK2F,UAAYT,IAC/BE,EAAS,IAAIf,EAAMiB,EAActF,KAAMkF,EAAgBlC,kBAE5CuC,EAAQL,EAAiBC,GAChCI,EAAOG,OAAS,WACZD,EAAmBG,KAAK9E,MAGpBqE,IACAE,EAAc,IAAIhB,EAAMiB,EAActF,KAAMmF,EAAenC,SAC3DsC,EAAcO,UAAW,EACzBR,EAAYb,GAAGW,GAEfE,EAAYd,YAAYtE,WAAa,WACjCqF,EAAcO,UAAW,EACzBnB,EAAQY,EAActF,KAAKyD,MAAMgB,YAIzCW,EAAOZ,GAAGU,GAGVK,EAAOG,OAASD,IAEtBF,EAAQL,EAAiBC,MC3CbS,KAAK9E,KAAMiE,EAASC,EAAeC,GACrDL,EAAYgB,cAAK9E,KAAMiE,UAAYe,KAQvC,IASqBC,0IAAwBrD,OAAOsD,iBAGpCC,0BAZiB5F,UAEzBgB,KAAMhB,EAAQgB,MAAQqB,OAAOwD,OAAOC,YAAYC,MAChDhF,SAAUf,EAAQe,UAAY,IAC9BT,MAAON,EAAQM,QAAS,EACxBF,MAAOJ,EAAQI"} \ No newline at end of file diff --git a/example.html b/example.html new file mode 100644 index 0000000..ba7a413 --- /dev/null +++ b/example.html @@ -0,0 +1,70 @@ + + + + + + + + + + + + + diff --git a/index.js b/index.js index 3a39bcd..c95dbd1 100644 --- a/index.js +++ b/index.js @@ -1 +1 @@ -module.exports = require('./dist/phaser-state-transition.min.js'); +module.exports = require('./dist/phaser-state-transition.js'); diff --git a/package.json b/package.json index aff8439..aadbcaa 100644 --- a/package.json +++ b/package.json @@ -1,36 +1,33 @@ { "name": "phaser-state-transition", - "version": "2.4.3", + "version": "2.5.0", "description": "Phaser state transition plugin", - "main": "index.js", + "main": "dist/phaser-state-transition.js", + "module": "dist/phaser-state-transition.m.js", + "umd:main": "dist/phaser-state-transition.umd.js", "repository": { "type": "git", "url": "git@github.com:cristianbote/phaser-state-transition.git" }, "files": [ "index.js", - "dist/phaser-state-transition.js", - "dist/phaser-state-transition.min.js", + "dist", "README.md", "LICENSE.md" ], "keywords": [ "phaser", + "plugin", "state", "transition" ], "scripts": { - "compile": "npm run build & npm run uglify", - "uglify": "uglifyjs dist/phaser-state-transition.js -o dist/phaser-state-transition.min.js -m", - "build": "browserify src/index.js -d -o dist/phaser-state-transition.js -v", - "watch": "watchify src/index.js -d -o dist/phaser-state-transition.js -v" + "build": "rm -rf dist; microbundle build --output dist --target browser --name StateTransition", + "watch": "microbundle watch --output dist --target browser --name StateTransition --entry src/index.js" }, "author": "Cristian Bote ", "license": "ISC", "devDependencies": { - "browserify": "^13.1.0", - "phaser-ce": "^2.7.2", - "uglify-js": "^2.7.3", - "watchify": "^3.7.0" + "microbundle": "^0.4.3" } } diff --git a/sandbox/index.html b/sandbox/index.html deleted file mode 100644 index 67c6f92..0000000 --- a/sandbox/index.html +++ /dev/null @@ -1,82 +0,0 @@ - - - - - - - - - - - - \ No newline at end of file diff --git a/src/core/ContentSnapshot.js b/src/core/ContentSnapshot.js deleted file mode 100644 index e1c84bf..0000000 --- a/src/core/ContentSnapshot.js +++ /dev/null @@ -1,53 +0,0 @@ -(function() { - "use strict"; - - var PHASER_LEGACY = '2.4.8'; - - /** - * Content Snapshot Class - * @constructor - * @name ContentSnapshot - * @param {object} game Game object instance - * @param {number} x Offset of x - * @param {number} y Offset of y - * @param {boolean} noStage Flag do skip rendering the stage for slider - * @extend Phaser.Image - */ - function ContentSnapshot(game, x, y, noStage) { - - // Create the game texture - this._texture = new Phaser.RenderTexture(game, game.width, game.height); - - if (!noStage) { - // Create the game background fill - this._graphicFill = new Phaser.Graphics(game, 0, 0); - this._graphicFill.beginFill(game.stage.backgroundColor); - this._graphicFill.drawRect(0, 0, game.width, game.height); - this._graphicFill.endFill(); - - // Add the graphicFill object temporary to the stage at the base - game.stage.addChildAt(this._graphicFill, 0); - this._texture.renderXY(this._graphicFill, 0, 0); - // After this is rendered to the texture, remove it - game.stage.removeChild(this._graphicFill); - } - - // After 2.4.8 (0,0) it's basically middle - if (Phaser.VERSION > PHASER_LEGACY) { - this._texture.renderXY(game.world, game.camera.position.x * -1, game.camera.position.y * -1); - } else { - this._texture.renderXY(game.world, game.width / 2 - game.camera.position.x, game.height / 2 - game.camera.position.y); - } - - // Get the image - Phaser.Image.call(this, game, x || 0, y || 0, this._texture); - - // Capture all input events - this.inputEnabled = true; - } - - ContentSnapshot.prototype = Object.create(Phaser.Image.prototype); - ContentSnapshot.constructor = ContentSnapshot; - - module.exports = ContentSnapshot; -}()); diff --git a/src/core/Slide.js b/src/core/Slide.js deleted file mode 100644 index f8392d3..0000000 --- a/src/core/Slide.js +++ /dev/null @@ -1,36 +0,0 @@ -(function() { - "use strict"; - - var Transition = require('../transition/Transition'), - ContentSnapshot = require('./ContentSnapshot'); - - /** - * Slide Class - * @constructor - * @name Slide - * @version 0.1.0 - * @author Cristian Bote - * @param {object} game Phaser.Game instance - * @param {boolean} noStage No stage flag - */ - function Slide(game, noStage) { - this.game = game; - this._contentSnapshot = new ContentSnapshot(game, 0, 0, noStage); - this._transition = new Transition(game); - } - - /** - * Start sliding - * @method - * @name go - * @version 0.1.0 - * @author Cristian Bote - * @param {object} options Transition options - */ - Slide.prototype.go = function(options) { - this.game.stage.addChildAt(this._contentSnapshot, this.game.stage.children.length); - this._transition.start(this._contentSnapshot, options); - }; - - module.exports = Slide; -}()); diff --git a/src/core/StateManagerStart.js b/src/core/StateManagerStart.js deleted file mode 100644 index 2651365..0000000 --- a/src/core/StateManagerStart.js +++ /dev/null @@ -1,66 +0,0 @@ -/*global - Phaser: true - window: true -*/ - -(function() { - "use strict"; - - var StateManagerCachedStart = Phaser.StateManager.prototype.start, - Slide = require('./Slide'), - ContentSnapshot = require('./ContentSnapshot'); - - function cleanup(children) { - var i = 0, - l = children.length; - - for(; i < l; i += 1) { - if (children[i] && (children[i] instanceof ContentSnapshot)) { - children[i].destroy(); - } - } - } - - function StateManagerStart(stateId, slideOutOptions, slideInOptions) { - var _slide, - _introSlide, - _stateManager = this, - _state = _stateManager.states[stateId], - _args = [].slice.call(arguments), - _cachedStateCreate = _state.create; - - _stateManager.game.stage && cleanup(_stateManager.game.stage.children); - - if (_stateManager.game.isBooted && slideOutOptions) { - _slide = new Slide(this.game, slideOutOptions.noStage); - - (function (_state, slideOutOptions, slideInOptions) { - _state.create = function () { - _cachedStateCreate.call(this); - - // Slide in intro - if (slideInOptions) { - _introSlide = new Slide(_stateManager.game, slideInOptions.noStage); - _stateManager._created = false; - _introSlide.go(slideInOptions); - - _introSlide._transition.onComplete = function () { - _stateManager._created = true; - cleanup(_stateManager.game.stage.children); - }; - } - - _slide.go(slideOutOptions); - - // Put the original create back - _state.create = _cachedStateCreate; - }; - }(_state, slideOutOptions, slideInOptions)); - } - - // Start the cached state with the params for it - StateManagerCachedStart.apply(this, [stateId].concat(_args.slice(3))); - } - - module.exports = StateManagerStart; -}()); diff --git a/src/core/content-snapshot.js b/src/core/content-snapshot.js new file mode 100644 index 0000000..e12b8ec --- /dev/null +++ b/src/core/content-snapshot.js @@ -0,0 +1,49 @@ +const PHASER_LEGACY = 248; + +const shouldRenderBasedOnCameraPosition = () => { + const currentVersion = parseInt(Phaser.VERSION.replace(/\./g, ''), 10); + return currentVersion > PHASER_LEGACY; +}; + +export class ContentSnapshot extends Phaser.Image { + + /** + * Content Snapshot Class + * @constructor + * @name ContentSnapshot + * @param {object} game Game object instance + * @param {number} x Offset of x + * @param {number} y Offset of y + * @param {boolean} noStage Flag do skip rendering the stage for slider + * @extend Phaser.Image + */ + constructor(game, x, y, noStage) { + const texture = new Phaser.RenderTexture(game, game.width, game.height); + + super(game, x || 0, y || 0, texture); + + if (!noStage) { + // Create the game background fill + const backgroundFill = new Phaser.Graphics(game, 0, 0); + backgroundFill.beginFill(game.stage.backgroundColor); + backgroundFill.drawRect(0, 0, game.width, game.height); + backgroundFill.endFill(); + + // Add the graphicFill object temporary to the stage at the base + game.stage.addChildAt(backgroundFill, 0); + texture.renderXY(backgroundFill, 0, 0); + // After this is rendered to the texture, remove it + game.stage.removeChild(backgroundFill); + } + + // After 2.4.8 (0,0) it's basically middle + if (shouldRenderBasedOnCameraPosition()) { + texture.renderXY(game.world, game.camera.position.x * -1, game.camera.position.y * -1); + } else { + texture.renderXY(game.world, game.width / 2 - game.camera.position.x, game.height / 2 - game.camera.position.y); + } + + // Capture all input events + this.inputEnabled = true; + } +} diff --git a/src/core/slide.js b/src/core/slide.js new file mode 100644 index 0000000..a80b1f9 --- /dev/null +++ b/src/core/slide.js @@ -0,0 +1,17 @@ +import { Transition } from "../transition/transition"; +import { ContentSnapshot } from "./content-snapshot"; + +export class Slide { + + constructor(game, noStage) { + this.game = game; + this._contentSnapshot = new ContentSnapshot(game, 0, 0, noStage); + this._transition = new Transition(game); + } + + go(options) { + this.game.stage.addChildAt(this._contentSnapshot, this.game.stage.children.length); + this._transition.start(this._contentSnapshot, options); + } + +} diff --git a/src/core/state-manager-start.js b/src/core/state-manager-start.js new file mode 100644 index 0000000..8760cf3 --- /dev/null +++ b/src/core/state-manager-start.js @@ -0,0 +1,50 @@ +import { Slide } from "./slide"; +import { ContentSnapshot } from "./content-snapshot"; + +const cleanup = (children) => { + let i = 0; + let l = children.length; + + for(; i < l; i += 1) { + if (children[i] && (children[i] instanceof ContentSnapshot)) { + children[i].destroy(); + } + } +}; + +export function stateManagerStart(stateId, slideOutOptions, slideInOptions) { + let _slide, + _introSlide, + _stateManager = this, + _state = _stateManager.states[stateId], + _cachedStateCreate = _state.create; + + _stateManager.game.stage && cleanup(_stateManager.game.stage.children); + + if (_stateManager.game.isBooted && slideOutOptions) { + _slide = new Slide(_stateManager.game, slideOutOptions.noStage); + + (function (_state, slideOutOptions, slideInOptions) { + _state.create = function () { + _cachedStateCreate.call(this); + + // Slide in intro + if (slideInOptions) { + _introSlide = new Slide(_stateManager.game, slideInOptions.noStage); + _stateManager._created = false; + _introSlide.go(slideInOptions); + + _introSlide._transition.onComplete = function () { + _stateManager._created = true; + cleanup(_stateManager.game.stage.children); + }; + } + + _slide.go(slideOutOptions); + + // Put the original create back + _state.create = _cachedStateCreate; + }; + }(_state, slideOutOptions, slideInOptions)); + } +} diff --git a/src/index.js b/src/index.js index e596332..6296376 100644 --- a/src/index.js +++ b/src/index.js @@ -1,16 +1,26 @@ -(function() { - "use strict"; +import { stateManagerStart } from "./core/state-manager-start"; - var Slide = require('./core/Slide'), - StateManagerStart = require('./core/StateManagerStart'), - DefaultTransition = require('./transition/DefaultTransition'); +const cachedStart = Phaser.StateManager.prototype.start; +Phaser.StateManager.prototype.start = function start(stateId, slideInOption, slideOut, ...args) { + stateManagerStart.call(this, stateId, slideInOption, slideOut); + cachedStart.call(this, stateId, ...args); +}; +/** + * Creates a transition object + * @param options + * @returns {{ease: *, duration: number, intro: boolean, props: {}}} + */ +export const createTransition = (options) => { + return { + ease: options.ease || Phaser.Easing.Exponential.InOut, + duration: options.duration || 500, + intro: options.intro || false, + props: options.props || {} + } +}; - // Define the Plugin Class - Phaser.Plugin.StateTransition = require('./core/StateTransition'); +export default class StateTransition extends Phaser.Plugin {} - // Override the default state.start - Phaser.StateManager.prototype.start = StateManagerStart; - - module.exports = window.StateTransition = Phaser.Plugin.StateTransition; -}()); +// Expose the createTransition function +StateTransition.createTransition = createTransition; \ No newline at end of file diff --git a/src/transition/DefaultTransition.js b/src/transition/DefaultTransition.js deleted file mode 100644 index 917bd26..0000000 --- a/src/transition/DefaultTransition.js +++ /dev/null @@ -1,12 +0,0 @@ -(function(){ - "use strict"; - - module.exports = function(options) { - return { - ease: options.ease || Phaser.Easing.Exponential.InOut, - duration: options.duration || 500, - intro: options.intro || false, - props: options.props || {} - } - }; -}()); diff --git a/src/transition/Transition.js b/src/transition/transition.js similarity index 60% rename from src/transition/Transition.js rename to src/transition/transition.js index 937d685..501962a 100644 --- a/src/transition/Transition.js +++ b/src/transition/transition.js @@ -1,28 +1,19 @@ -(function(){ - "use strict"; - - var raf = window.requestAnimationFrame; - - /** - * Transition Class - * @constructor - * @name Transition - * @param {object} game Game instance - */ - function Transition(game) { +/** + * Transition Class + * @constructor + * @name Transition + * @param {object} game Game instance + */ +export class Transition { + + constructor(game) { this.game = game; this.onComplete = null; this._tweens = []; } - /** - * Start the transition with a given target and options - * @name start - * @param target - * @param options - */ - Transition.prototype.start = function(target, options) { - var prop, + start(target, options) { + let prop, _props = options.props, _isIntro = !!options.intro, _tweenTarget, @@ -67,27 +58,24 @@ this._tweens.push( this.game.add.tween(_tweenTarget) [_isIntro ? 'from' : 'to']( - _queue[prop], - options.duration, - options.ease, - true, - options.delay - ) + _queue[prop], + options.duration, + options.ease, + true, + options.delay + ) ); _tweenInstance = this._tweens[this._tweens.length - 1]; _tweenInstance.onComplete.addOnce(this._checkForComplete, this); + _tweenInstance.onLoop.addOnce(function () { + console.log('progress', this); + }, this); } } - }; - - /** - * Verify complete state for transition - * @param target - * @param tween - * @private - */ - Transition.prototype._checkForComplete = function(target, tween) { + } + + _checkForComplete(target, tween) { var i = 0, l = this._tweens.length, _currentTween, @@ -105,45 +93,21 @@ this.onComplete && this.onComplete(); this.currentTarget.destroy(); } - }; - - /** - * Makes sure, before the transition starts, that we're doing fine - * property wise. - * @param props - * @private - */ - Transition.prototype._prepareTargetForTweening = function(props) { + } + + _prepareTargetForTweening(props) { if (props.hasOwnProperty('alpha')) { this.currentTarget.alpha = 0; } - }; + } - /** - * Destroy handler - * @param target - */ - Transition.prototype.destroy = function(target) { + destroy(target) { target.destroy(); - }; + } - /** - * Stop handler - */ - Transition.prototype.stop = function() { + stop() { this._active = false; this.update(); - }; - - /** - * Returns a unique identifier based in Date.now() stamp. - * Not that reliable. - * @returns {string} - * @private - */ - function _getIdentifier() { - return Date.now().toString(22).substr(-4, 4); } - module.exports = Transition; -}()); +}