phaser/src/physics/impact/ImpactPhysics.js

211 lines
5.1 KiB
JavaScript
Raw Normal View History

2018-02-12 16:01:20 +00:00
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2018 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
var Class = require('../../utils/Class');
var Factory = require('./Factory');
var GetFastValue = require('../../utils/object/GetFastValue');
var Merge = require('../../utils/object/Merge');
var PluginCache = require('../../plugins/PluginCache');
var World = require('./World');
2018-02-09 16:04:43 +00:00
/**
* @classdesc
* [description]
*
* @class ImpactPhysics
* @memberOf Phaser.Physics.Impact
* @constructor
* @since 3.0.0
*
* @param {Phaser.Scene} scene - [description]
*/
var ImpactPhysics = new Class({
initialize:
function ImpactPhysics (scene)
{
2018-02-09 16:04:43 +00:00
/**
* [description]
*
* @name Phaser.Physics.Impact.ImpactPhysics#scene
* @type {Phaser.Scene}
* @since 3.0.0
*/
this.scene = scene;
2018-02-09 16:04:43 +00:00
/**
* [description]
*
* @name Phaser.Physics.Impact.ImpactPhysics#systems
* @type {Phaser.Scenes.Systems}
* @since 3.0.0
*/
this.systems = scene.sys;
2018-02-09 16:04:43 +00:00
/**
* [description]
*
* @name Phaser.Physics.Impact.ImpactPhysics#config
* @type {object}
* @since 3.0.0
*/
this.config = this.getConfig();
2018-02-09 16:04:43 +00:00
/**
* [description]
*
* @name Phaser.Physics.Impact.ImpactPhysics#world
* @type {Phaser.Physics.Impact.World}
* @since 3.0.0
*/
this.world;
2018-02-09 16:04:43 +00:00
/**
* [description]
*
* @name Phaser.Physics.Impact.ImpactPhysics#add
* @type {Phaser.Physics.Impact.Factory}
* @since 3.0.0
*/
this.add;
scene.sys.events.once('boot', this.boot, this);
scene.sys.events.on('start', this.start, this);
},
/**
* This method is called automatically, only once, when the Scene is first created.
* Do not invoke it directly.
*
* @method Phaser.Physics.Impact.ImpactPhysics#boot
* @private
* @since 3.5.1
*/
boot: function ()
{
this.world = new World(this.scene, this.config);
this.add = new Factory(this.world);
this.systems.events.once('destroy', this.destroy, this);
},
/**
* This method is called automatically by the Scene when it is starting up.
* It is responsible for creating local systems, properties and listening for Scene events.
* Do not invoke it directly.
*
* @method Phaser.Physics.Impact.ImpactPhysics#start
* @private
* @since 3.5.0
*/
start: function ()
{
if (!this.world)
{
this.world = new World(this.scene, this.config);
this.add = new Factory(this.world);
}
var eventEmitter = this.systems.events;
eventEmitter.on('update', this.world.update, this.world);
eventEmitter.once('shutdown', this.shutdown, this);
},
2018-02-09 16:04:43 +00:00
/**
* [description]
*
* @method Phaser.Physics.Impact.ImpactPhysics#getConfig
* @since 3.0.0
*
* @return {object} [description]
*/
getConfig: function ()
{
var gameConfig = this.systems.game.config.physics;
var sceneConfig = this.systems.settings.physics;
var config = Merge(
GetFastValue(sceneConfig, 'impact', {}),
GetFastValue(gameConfig, 'impact', {})
);
return config;
},
2018-02-09 16:52:08 +00:00
/**
* [description]
*
* @method Phaser.Physics.Impact.ImpactPhysics#pause
* @since 3.0.0
*
2018-02-12 13:48:38 +00:00
* @return {Phaser.Physics.Impact.World} The Impact World object.
2018-02-09 16:52:08 +00:00
*/
pause: function ()
{
return this.world.pause();
},
/**
* [description]
*
* @method Phaser.Physics.Impact.ImpactPhysics#resume
* @since 3.0.0
*
2018-02-12 13:48:38 +00:00
* @return {Phaser.Physics.Impact.World} The Impact World object.
2018-02-09 16:52:08 +00:00
*/
resume: function ()
{
return this.world.resume();
},
2018-02-09 16:04:43 +00:00
/**
* The Scene that owns this plugin is shutting down.
* We need to kill and reset all internal properties as well as stop listening to Scene events.
2018-02-09 16:04:43 +00:00
*
* @method Phaser.Physics.Impact.ImpactPhysics#shutdown
* @private
2018-02-09 16:04:43 +00:00
* @since 3.0.0
*/
shutdown: function ()
{
var eventEmitter = this.systems.events;
eventEmitter.off('update', this.world.update, this.world);
eventEmitter.off('shutdown', this.shutdown, this);
this.add.destroy();
this.world.destroy();
this.add = null;
this.world = null;
},
2018-02-09 16:04:43 +00:00
/**
* The Scene that owns this plugin is being destroyed.
* We need to shutdown and then kill off all external references.
2018-02-09 16:04:43 +00:00
*
* @method Phaser.Physics.Impact.ImpactPhysics#destroy
* @private
2018-02-09 16:04:43 +00:00
* @since 3.0.0
*/
destroy: function ()
{
this.shutdown();
this.scene.sys.events.off('start', this.start, this);
this.scene = null;
this.systems = null;
}
});
PluginCache.register('ImpactPhysics', ImpactPhysics, 'impactPhysics');
module.exports = ImpactPhysics;