Skip to content

Commit 28db9a2

Browse files
committed
Lots of work on the Game Object Factory patterns.
1 parent bf6ccc3 commit 28db9a2

6 files changed

Lines changed: 110 additions & 20 deletions

File tree

v3/src/boot/Config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ function Config (config)
8181
callbacks = {};
8282
}
8383

84-
this.preBoot = getValue(callbacks, 'preBoot', NOOP);
85-
this.postBoot = getValue(callbacks, 'postBoot', NOOP);
84+
this.preBoot = getValue(callbacks, 'preBoot', NOOP);
85+
this.postBoot = getValue(callbacks, 'postBoot', NOOP);
8686

8787
}
8888

v3/src/boot/Game.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ var RequestAnimationFrame = require('../dom/RequestAnimationFrame');
1313
var DOMContentLoaded = require('../dom/DOMContentLoaded');
1414
var RandomDataGenerator = require('../math/random-data-generator/RandomDataGenerator');
1515
var CanvasPool = require('../dom/CanvasPool');
16+
var FactoryContainer = require('../gameobjects/FactoryContainer');
17+
var Image = require('../gameobjects/image/ImageFactory');
18+
var GameObjectFactory = require('../state/systems/GameObjectFactory');
1619

1720
var Game = function (config)
1821
{
@@ -64,6 +67,8 @@ var Game = function (config)
6467
*/
6568
// this.state = new Phaser.StateManager(this, stateConfig);
6669

70+
this.add = null;
71+
6772
/**
6873
* @property {Phaser.Device} device - Contains device information and capabilities.
6974
*/
@@ -89,8 +94,19 @@ Game.prototype = {
8994

9095
console.log(CHECKSUM.build);
9196

92-
console.log('pool', CanvasPool.total());
93-
console.log('free', CanvasPool.free());
97+
// console.log('pool', CanvasPool.total());
98+
// console.log('free', CanvasPool.free());
99+
// console.dir(FactoryContainer.getType('image'));
100+
101+
// Create a fake State to test the GF with
102+
103+
var state = {
104+
hello: 'world'
105+
};
106+
107+
this.add = GameObjectFactory(state);
108+
109+
console.log(this.add);
94110

95111
this.config.postBoot();
96112

v3/src/checksum.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
var CHECKSUM = {
2-
build: '1e855860-b380-11e6-ae1d-777159c5bd92'
2+
build: 'f71c8930-b620-11e6-b63f-ab05aca00975'
33
};
44
module.exports = CHECKSUM;
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @copyright 2016 Photon Storm Ltd.
4+
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
5+
*/
6+
7+
/**
8+
* The GameObject Factory is a global level container of Factory instances.
9+
* Factories register themselves with this container.
10+
*
11+
* @class Phaser.GameObject.Factory
12+
* @constructor
13+
* @param {Phaser.Game} game - A reference to the currently running game.
14+
*/
15+
16+
var factories = {};
17+
18+
var FactoryContainer = function ()
19+
{
20+
console.log('FactoryContainer is alive');
21+
22+
this.register = function (factory)
23+
{
24+
if (factories.hasOwnProperty(factory.KEY))
25+
{
26+
console.log('Already registered', factory.KEY);
27+
28+
return this.getType(factory.KEY);
29+
}
30+
31+
console.log('registering', factory.KEY);
32+
33+
factories[factory.KEY] = {
34+
add: factory.add,
35+
make: factory.make
36+
};
37+
38+
return factory;
39+
};
40+
41+
this.getType = function (key)
42+
{
43+
return factories[key];
44+
};
45+
46+
this.load = function (dest)
47+
{
48+
for (var factory in factories)
49+
{
50+
console.log('Testing load of:', factory);
51+
52+
if (factories.hasOwnProperty(factory))
53+
{
54+
console.log('Loading', factory);
55+
dest[factory] = factories[factory].add;
56+
}
57+
}
58+
59+
return dest;
60+
};
61+
62+
return this;
63+
};
64+
65+
module.exports = FactoryContainer();

v3/src/gameobjects/image/ImageFactory.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
55
*/
66

7+
var FactoryContainer = require('../../gameobjects/FactoryContainer');
8+
79
var ImageFactory = {
810

911
KEY: 'image',
@@ -26,9 +28,12 @@ var ImageFactory = {
2628
*/
2729
add: function (x, y, key, frame, group, name)
2830
{
29-
if (group === undefined) { group = this.state; }
31+
console.log('ImageFactory.add', key, x, y, frame, group, name);
32+
console.log('into State', this.state);
33+
34+
// if (group === undefined) { group = this.state; }
3035

31-
return group.children.add(new Image(this.state, x, y, key, frame, name));
36+
// return group.children.add(new Image(this.state, x, y, key, frame, name));
3237
},
3338

3439
/**
@@ -51,4 +56,4 @@ var ImageFactory = {
5156

5257
};
5358

54-
module.exports = ImageFactory;
59+
module.exports = FactoryContainer.register(ImageFactory);
Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
55
*/
66

7+
var FactoryContainer = require('../../gameobjects/FactoryContainer');
8+
79
/**
810
* The GameObject Factory is a quick way to create many common game objects. The Factory is owned by the State.
911
*
@@ -12,25 +14,27 @@
1214
* @param {Phaser.Game} game - A reference to the currently running game.
1315
*/
1416

15-
var GameObjectFactory = function (state)
16-
{
17-
/**
18-
* @property {Phaser.Game} game - A reference to the currently running Game.
19-
* @protected
20-
*/
21-
this.game = state.game;
17+
var GameObjectFactory = {
2218

2319
/**
2420
* @property {Phaser.State} state - The State that owns this Factory
2521
* @protected
2622
*/
27-
this.state = state;
23+
state: null
24+
2825
};
2926

30-
GameObjectFactory.prototype.constructor = GameObjectFactory;
27+
function init (state)
28+
{
29+
console.log('Creating GameObjectFactory instance for State', state);
3130

32-
GameObjectFactory.prototype = {
31+
GameObjectFactory.state = state;
3332

34-
};
33+
// Load the factories into this Object
34+
35+
FactoryContainer.load(GameObjectFactory);
36+
37+
return GameObjectFactory;
38+
}
3539

36-
module.exports = GameObjectFactory;
40+
module.exports = init;

0 commit comments

Comments
 (0)