Lots of work on the Game Object Factory patterns.

This commit is contained in:
photonstorm 2016-11-29 10:46:35 +00:00
parent bf6ccc3d8d
commit 28db9a2550
6 changed files with 112 additions and 22 deletions

View file

@ -13,6 +13,9 @@ var RequestAnimationFrame = require('../dom/RequestAnimationFrame');
var DOMContentLoaded = require('../dom/DOMContentLoaded');
var RandomDataGenerator = require('../math/random-data-generator/RandomDataGenerator');
var CanvasPool = require('../dom/CanvasPool');
var FactoryContainer = require('../gameobjects/FactoryContainer');
var Image = require('../gameobjects/image/ImageFactory');
var GameObjectFactory = require('../state/systems/GameObjectFactory');
var Game = function (config)
{
@ -64,6 +67,8 @@ var Game = function (config)
*/
// this.state = new Phaser.StateManager(this, stateConfig);
this.add = null;
/**
* @property {Phaser.Device} device - Contains device information and capabilities.
*/
@ -89,8 +94,19 @@ Game.prototype = {
console.log(CHECKSUM.build);
console.log('pool', CanvasPool.total());
console.log('free', CanvasPool.free());
// console.log('pool', CanvasPool.total());
// console.log('free', CanvasPool.free());
// console.dir(FactoryContainer.getType('image'));
// Create a fake State to test the GF with
var state = {
hello: 'world'
};
this.add = GameObjectFactory(state);
console.log(this.add);
this.config.postBoot();

View file

@ -1,4 +1,4 @@
var CHECKSUM = {
build: '1e855860-b380-11e6-ae1d-777159c5bd92'
build: 'f71c8930-b620-11e6-b63f-ab05aca00975'
};
module.exports = CHECKSUM;

View file

@ -0,0 +1,65 @@
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2016 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
/**
* The GameObject Factory is a global level container of Factory instances.
* Factories register themselves with this container.
*
* @class Phaser.GameObject.Factory
* @constructor
* @param {Phaser.Game} game - A reference to the currently running game.
*/
var factories = {};
var FactoryContainer = function ()
{
console.log('FactoryContainer is alive');
this.register = function (factory)
{
if (factories.hasOwnProperty(factory.KEY))
{
console.log('Already registered', factory.KEY);
return this.getType(factory.KEY);
}
console.log('registering', factory.KEY);
factories[factory.KEY] = {
add: factory.add,
make: factory.make
};
return factory;
};
this.getType = function (key)
{
return factories[key];
};
this.load = function (dest)
{
for (var factory in factories)
{
console.log('Testing load of:', factory);
if (factories.hasOwnProperty(factory))
{
console.log('Loading', factory);
dest[factory] = factories[factory].add;
}
}
return dest;
};
return this;
};
module.exports = FactoryContainer();

View file

@ -4,6 +4,8 @@
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
var FactoryContainer = require('../../gameobjects/FactoryContainer');
var ImageFactory = {
KEY: 'image',
@ -26,9 +28,12 @@ var ImageFactory = {
*/
add: function (x, y, key, frame, group, name)
{
if (group === undefined) { group = this.state; }
console.log('ImageFactory.add', key, x, y, frame, group, name);
console.log('into State', this.state);
return group.children.add(new Image(this.state, x, y, key, frame, name));
// if (group === undefined) { group = this.state; }
// return group.children.add(new Image(this.state, x, y, key, frame, name));
},
/**
@ -51,4 +56,4 @@ var ImageFactory = {
};
module.exports = ImageFactory;
module.exports = FactoryContainer.register(ImageFactory);

View file

@ -4,6 +4,8 @@
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
var FactoryContainer = require('../../gameobjects/FactoryContainer');
/**
* The GameObject Factory is a quick way to create many common game objects. The Factory is owned by the State.
*
@ -12,25 +14,27 @@
* @param {Phaser.Game} game - A reference to the currently running game.
*/
var GameObjectFactory = function (state)
{
/**
* @property {Phaser.Game} game - A reference to the currently running Game.
* @protected
*/
this.game = state.game;
var GameObjectFactory = {
/**
* @property {Phaser.State} state - The State that owns this Factory
* @protected
*/
this.state = state;
};
GameObjectFactory.prototype.constructor = GameObjectFactory;
GameObjectFactory.prototype = {
state: null
};
module.exports = GameObjectFactory;
function init (state)
{
console.log('Creating GameObjectFactory instance for State', state);
GameObjectFactory.state = state;
// Load the factories into this Object
FactoryContainer.load(GameObjectFactory);
return GameObjectFactory;
}
module.exports = init;