From c8d22fe881a991d530022fd72e199b6ee6a69ac0 Mon Sep 17 00:00:00 2001 From: Richard Davey Date: Tue, 11 Apr 2017 02:47:52 +0100 Subject: [PATCH] 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`. --- v3/src/checksum.js | 2 +- v3/src/gameobjects/BuildGameObject.js | 89 ++++++++++++++++++++ v3/src/gameobjects/image/BuildFromConfig.js | 17 ++++ v3/src/gameobjects/image/ImageFactory.js | 5 +- v3/src/gameobjects/sprite/BuildFromConfig.js | 47 +++++++++++ v3/src/gameobjects/sprite/SpriteFactory.js | 12 +-- 6 files changed, 161 insertions(+), 11 deletions(-) create mode 100644 v3/src/gameobjects/BuildGameObject.js create mode 100644 v3/src/gameobjects/image/BuildFromConfig.js create mode 100644 v3/src/gameobjects/sprite/BuildFromConfig.js diff --git a/v3/src/checksum.js b/v3/src/checksum.js index 593193e1e..25864e8d9 100644 --- a/v3/src/checksum.js +++ b/v3/src/checksum.js @@ -1,4 +1,4 @@ var CHECKSUM = { -build: '6d1ac140-1e01-11e7-abae-c74d411e0c22' +build: '4806a5c0-1e58-11e7-b218-f9a99024fc4e' }; module.exports = CHECKSUM; \ No newline at end of file diff --git a/v3/src/gameobjects/BuildGameObject.js b/v3/src/gameobjects/BuildGameObject.js new file mode 100644 index 000000000..acca5c19a --- /dev/null +++ b/v3/src/gameobjects/BuildGameObject.js @@ -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; diff --git a/v3/src/gameobjects/image/BuildFromConfig.js b/v3/src/gameobjects/image/BuildFromConfig.js new file mode 100644 index 000000000..75df49c79 --- /dev/null +++ b/v3/src/gameobjects/image/BuildFromConfig.js @@ -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; diff --git a/v3/src/gameobjects/image/ImageFactory.js b/v3/src/gameobjects/image/ImageFactory.js index 90ee83199..d182365a0 100644 --- a/v3/src/gameobjects/image/ImageFactory.js +++ b/v3/src/gameobjects/image/ImageFactory.js @@ -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); } }; diff --git a/v3/src/gameobjects/sprite/BuildFromConfig.js b/v3/src/gameobjects/sprite/BuildFromConfig.js new file mode 100644 index 000000000..ea434e93f --- /dev/null +++ b/v3/src/gameobjects/sprite/BuildFromConfig.js @@ -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; diff --git a/v3/src/gameobjects/sprite/SpriteFactory.js b/v3/src/gameobjects/sprite/SpriteFactory.js index b30cf6b5d..747c228ed 100644 --- a/v3/src/gameobjects/sprite/SpriteFactory.js +++ b/v3/src/gameobjects/sprite/SpriteFactory.js @@ -1,11 +1,7 @@ -/** -* @author Richard Davey -* @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); } };