Added BuildGameObject and related functions. Allows you to create a game object via a configuration object, rather than having to call all the functions directly. Applied to Images and Sprites so far. Accessed via this.make.sprite or this.make.image.

This commit is contained in:
Richard Davey 2017-04-11 02:47:52 +01:00
parent d08e23f4f4
commit c8d22fe881
6 changed files with 161 additions and 11 deletions

View file

@ -1,4 +1,4 @@
var CHECKSUM = {
build: '6d1ac140-1e01-11e7-abae-c74d411e0c22'
build: '4806a5c0-1e58-11e7-b218-f9a99024fc4e'
};
module.exports = CHECKSUM;

View file

@ -0,0 +1,89 @@
var GetAdvancedValue = require('../utils/object/GetAdvancedValue');
var ScaleModes = require('../renderer/ScaleModes');
var BlendModes = require('../renderer/BlendModes');
var BuildGameObject = function (state, gameObject, config)
{
// Position
gameObject.x = GetAdvancedValue(config, 'x', 0);
gameObject.y = GetAdvancedValue(config, 'y', 0);
gameObject.z = GetAdvancedValue(config, 'z', 0);
// 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);
}
// 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)
{
var ox = GetAdvancedValue(config, 'x', 0.5);
var oy = GetAdvancedValue(config, 'y', 0.5);
gameObject.setOrigin(ox, oy);
}
// ScaleMode
gameObject.scaleMode = GetAdvancedValue(config, 'scaleMode', ScaleModes.DEFAULT);
// BlendMode
gameObject.blendMode = GetAdvancedValue(config, 'blendMode', BlendModes.NORMAL);
// Visible
gameObject.visible = GetAdvancedValue(config, 'visible', true);
// Add to State
var add = GetAdvancedValue(config, 'add', true);
if (add)
{
state.children.add(gameObject);
}
return gameObject;
};
module.exports = BuildGameObject;

View file

@ -0,0 +1,17 @@
var Image = require('./Image');
var GetAdvancedValue = require('../../utils/object/GetAdvancedValue');
var BuildGameObject = require('../BuildGameObject');
var BuildFromConfig = function (state, config)
{
var key = GetAdvancedValue(config, 'key', null);
var frame = GetAdvancedValue(config, 'frame', null);
var image = new Image(state, 0, 0, key, frame);
BuildGameObject(state, image, config);
return image;
};
module.exports = BuildFromConfig;

View file

@ -1,5 +1,6 @@
var Image = require('./Image');
var BuildFromConfig = require('./BuildFromConfig');
var FactoryContainer = require('../../gameobjects/FactoryContainer');
var ImageFactory = {
@ -27,9 +28,9 @@ var ImageFactory = {
return this.children.add(new Image(this.state, x, y, key, frame));
},
make: function (x, y, key, frame)
make: function (config)
{
return new Image(this.state, x, y, key, frame);
return BuildFromConfig(this.state, config);
}
};

View file

@ -0,0 +1,47 @@
var Sprite = require('./Sprite');
var GetAdvancedValue = require('../../utils/object/GetAdvancedValue');
var BuildGameObject = require('../BuildGameObject');
var BuildFromConfig = function (state, config)
{
var key = GetAdvancedValue(config, 'key', null);
var frame = GetAdvancedValue(config, 'frame', null);
var sprite = new Sprite(state, 0, 0, key, frame);
BuildGameObject(state, sprite, config);
// Sprite specific config options:
// { anims: 'key' }
// { anims: {
// key: string
// startFrame: [string|integer]
// }
// }
// delay: Components.Delay,
// delayedPlay: Components.DelayedPlay,
// load: Components.Load,
// pause: Components.Pause,
// paused: Components.Paused,
// play: Components.Play,
// progress: Components.Progress,
// repeat: Components.Repeat,
// repeatDelay: Components.RepeatDelay,
// restart: Components.Restart,
// resume: Components.Resume,
// stop: Components.Stop,
// timeScale: Components.TimeScale,
// totalFrames: Components.TotalFrames,
// totalProgress: Components.TotalProgress,
// update: Components.Update,
// updateFrame: Components.UpdateFrame,
// yoyo: Components.Yoyo
// var anim = GetAdvancedValue(config, 'anims', null);
return sprite;
};
module.exports = BuildFromConfig;

View file

@ -1,11 +1,7 @@
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2016 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
var Sprite = require('./Sprite');
var FactoryContainer = require('../../gameobjects/FactoryContainer');
var BuildFromConfig = require('./BuildFromConfig');
var FactoryContainer = require('../FactoryContainer');
var SpriteFactory = {
@ -31,9 +27,9 @@ var SpriteFactory = {
return this.children.add(new Sprite(this.state, x, y, key, frame));
},
make: function (x, y, key, frame)
make: function (config)
{
return new Sprite(this.state, x, y, key, frame);
return BuildFromConfig(this.state, config);
}
};