2018-02-12 16:01:20 +00:00
|
|
|
/**
|
2024-02-19 17:12:18 +00:00
|
|
|
* @author Richard Davey <rich@phaser.io>
|
|
|
|
* @copyright 2013-2024 Phaser Studio Inc.
|
2019-05-10 15:15:04 +00:00
|
|
|
* @license {@link https://opensource.org/licenses/MIT|MIT License}
|
2018-02-12 16:01:20 +00:00
|
|
|
*/
|
|
|
|
|
2018-02-12 17:03:53 +00:00
|
|
|
var BlendModes = require('../renderer/BlendModes');
|
2017-04-11 01:47:52 +00:00
|
|
|
var GetAdvancedValue = require('../utils/object/GetAdvancedValue');
|
|
|
|
|
2018-02-12 17:03:53 +00:00
|
|
|
/**
|
|
|
|
* Builds a Game Object using the provided configuration object.
|
|
|
|
*
|
2018-03-16 00:46:39 +00:00
|
|
|
* @function Phaser.GameObjects.BuildGameObject
|
2018-02-12 17:03:53 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-06-01 10:31:37 +00:00
|
|
|
* @param {Phaser.Scene} scene - A reference to the Scene.
|
|
|
|
* @param {Phaser.GameObjects.GameObject} gameObject - The initial GameObject.
|
2019-05-09 11:01:00 +00:00
|
|
|
* @param {Phaser.Types.GameObjects.GameObjectConfig} config - The config to build the GameObject with.
|
2018-02-12 17:03:53 +00:00
|
|
|
*
|
|
|
|
* @return {Phaser.GameObjects.GameObject} The built Game Object.
|
|
|
|
*/
|
2017-07-14 13:30:20 +00:00
|
|
|
var BuildGameObject = function (scene, gameObject, config)
|
2017-04-11 01:47:52 +00:00
|
|
|
{
|
|
|
|
// Position
|
|
|
|
|
|
|
|
gameObject.x = GetAdvancedValue(config, 'x', 0);
|
|
|
|
gameObject.y = GetAdvancedValue(config, 'y', 0);
|
2017-09-14 02:12:00 +00:00
|
|
|
gameObject.depth = GetAdvancedValue(config, 'depth', 0);
|
2017-04-11 01:47:52 +00:00
|
|
|
|
|
|
|
// Flip
|
|
|
|
|
|
|
|
gameObject.flipX = GetAdvancedValue(config, 'flipX', false);
|
|
|
|
gameObject.flipY = GetAdvancedValue(config, 'flipY', false);
|
|
|
|
|
|
|
|
// Scale
|
|
|
|
// Either: { scale: 2 } or { scale: { x: 2, y: 2 }}
|
|
|
|
|
|
|
|
var scale = GetAdvancedValue(config, 'scale', null);
|
|
|
|
|
|
|
|
if (typeof scale === 'number')
|
|
|
|
{
|
|
|
|
gameObject.setScale(scale);
|
|
|
|
}
|
|
|
|
else if (scale !== null)
|
|
|
|
{
|
|
|
|
gameObject.scaleX = GetAdvancedValue(scale, 'x', 1);
|
|
|
|
gameObject.scaleY = GetAdvancedValue(scale, 'y', 1);
|
|
|
|
}
|
|
|
|
|
2017-06-26 12:17:42 +00:00
|
|
|
// ScrollFactor
|
|
|
|
// Either: { scrollFactor: 2 } or { scrollFactor: { x: 2, y: 2 }}
|
|
|
|
|
|
|
|
var scrollFactor = GetAdvancedValue(config, 'scrollFactor', null);
|
|
|
|
|
|
|
|
if (typeof scrollFactor === 'number')
|
|
|
|
{
|
|
|
|
gameObject.setScrollFactor(scrollFactor);
|
|
|
|
}
|
|
|
|
else if (scrollFactor !== null)
|
|
|
|
{
|
|
|
|
gameObject.scrollFactorX = GetAdvancedValue(scrollFactor, 'x', 1);
|
|
|
|
gameObject.scrollFactorY = GetAdvancedValue(scrollFactor, 'y', 1);
|
|
|
|
}
|
|
|
|
|
2017-04-11 01:47:52 +00:00
|
|
|
// Rotation
|
|
|
|
|
|
|
|
gameObject.rotation = GetAdvancedValue(config, 'rotation', 0);
|
|
|
|
|
|
|
|
var angle = GetAdvancedValue(config, 'angle', null);
|
|
|
|
|
|
|
|
if (angle !== null)
|
|
|
|
{
|
|
|
|
gameObject.angle = angle;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Alpha
|
|
|
|
|
|
|
|
gameObject.alpha = GetAdvancedValue(config, 'alpha', 1);
|
|
|
|
|
|
|
|
// Origin
|
|
|
|
// Either: { origin: 0.5 } or { origin: { x: 0.5, y: 0.5 }}
|
|
|
|
|
|
|
|
var origin = GetAdvancedValue(config, 'origin', null);
|
|
|
|
|
|
|
|
if (typeof origin === 'number')
|
|
|
|
{
|
|
|
|
gameObject.setOrigin(origin);
|
|
|
|
}
|
|
|
|
else if (origin !== null)
|
|
|
|
{
|
2017-11-29 03:57:52 +00:00
|
|
|
var ox = GetAdvancedValue(origin, 'x', 0.5);
|
|
|
|
var oy = GetAdvancedValue(origin, 'y', 0.5);
|
2017-04-11 01:47:52 +00:00
|
|
|
|
|
|
|
gameObject.setOrigin(ox, oy);
|
|
|
|
}
|
|
|
|
|
|
|
|
// BlendMode
|
|
|
|
|
|
|
|
gameObject.blendMode = GetAdvancedValue(config, 'blendMode', BlendModes.NORMAL);
|
|
|
|
|
|
|
|
// Visible
|
|
|
|
|
|
|
|
gameObject.visible = GetAdvancedValue(config, 'visible', true);
|
|
|
|
|
2017-07-14 13:30:20 +00:00
|
|
|
// Add to Scene
|
2017-04-11 01:47:52 +00:00
|
|
|
|
|
|
|
var add = GetAdvancedValue(config, 'add', true);
|
|
|
|
|
|
|
|
if (add)
|
|
|
|
{
|
2017-07-14 13:30:20 +00:00
|
|
|
scene.sys.displayList.add(gameObject);
|
2017-11-16 16:47:13 +00:00
|
|
|
}
|
2017-09-13 14:34:03 +00:00
|
|
|
|
2017-11-16 16:47:13 +00:00
|
|
|
if (gameObject.preUpdate)
|
|
|
|
{
|
|
|
|
scene.sys.updateList.add(gameObject);
|
2017-04-11 01:47:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return gameObject;
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = BuildGameObject;
|