Skip to content

Commit 4240f6c

Browse files
committed
Converted to use the new plugin format
1 parent 9fabd00 commit 4240f6c

9 files changed

Lines changed: 243 additions & 54 deletions

File tree

src/camera/local/CameraManager.js

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var Class = require('../../utils/Class');
2+
var PluginManager = require('../../plugins/PluginManager');
23

34
var CameraManager = new Class({
45

@@ -7,9 +8,16 @@ var CameraManager = new Class({
78
function CameraManager (scene)
89
{
910
// The Scene that owns this plugin
10-
this.currentCameraId = 1;
1111
this.scene = scene;
1212

13+
this.systems = scene.sys;
14+
15+
this.mapping = 'cameras';
16+
17+
this.systems.events.on('boot', this.boot, this);
18+
19+
this.currentCameraId = 1;
20+
1321
this.cameras = [];
1422
this.cameraPool = [];
1523

@@ -28,6 +36,15 @@ var CameraManager = new Class({
2836
this.main = this.cameras[0];
2937
},
3038

39+
boot: function ()
40+
{
41+
this.systems.inject(this);
42+
43+
this.systems.events.on('update', this.update, this);
44+
this.systems.events.on('shutdown', this.shutdown, this);
45+
this.systems.events.on('destroy', this.destroy, this);
46+
},
47+
3148
add3D: require('./inc/AddPerspectiveCamera'),
3249
add: require('./inc/Add2DCamera'),
3350
addExisting: require('./inc/AddExisting'),
@@ -42,8 +59,15 @@ var CameraManager = new Class({
4259
remove: require('./inc/RemoveCamera'),
4360
render: require('./inc/Render'),
4461
resetAll: require('./inc/ResetAll'),
45-
update: require('./inc/Update')
62+
update: require('./inc/Update'),
63+
64+
shutdown: function ()
65+
{
66+
67+
}
4668

4769
});
4870

71+
PluginManager.register('cameras', CameraManager);
72+
4973
module.exports = CameraManager;

src/plugins/TestPlugin.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
var Class = require('../utils/Class');
2+
var PluginManager = require('../plugins/PluginManager');
3+
4+
var TestPlugin = new Class({
5+
6+
initialize:
7+
8+
function TestPlugin (scene)
9+
{
10+
this.scene = scene;
11+
12+
this.systems = scene.sys;
13+
14+
this.mapping = 'test';
15+
16+
console.log('TestPlugin is alive');
17+
18+
this.systems.events.on('boot', this.boot, this);
19+
},
20+
21+
boot: function ()
22+
{
23+
console.log('TestPlugin has booted');
24+
25+
this.scene[this.mapping] = this;
26+
},
27+
28+
fire: function (img)
29+
{
30+
console.log('Hello!');
31+
32+
this.systems.add.image(400, 300, img);
33+
}
34+
35+
});
36+
37+
PluginManager.register('test', TestPlugin);
38+
39+
module.exports = TestPlugin;

src/scene/local/Systems.js

Lines changed: 37 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ var Settings = require('./Settings');
1515
var TweenManager = require('../../tweens/manager/TweenManager');
1616
var UpdateList = require('../plugins/UpdateList');
1717

18-
// var PluginManager = require('../../plugins/PluginManager');
18+
var TestPlugin = require('../../plugins/TestPlugin');
1919

2020
var Systems = new Class({
2121

@@ -31,7 +31,7 @@ var Systems = new Class({
3131

3232
this.settings = Settings.create(config);
3333

34-
// Set by the GlobalSceneManager - a reference to the game canvas / context
34+
// Set by the GlobalSceneManager - a reference to the Scene canvas / context
3535

3636
this.canvas;
3737
this.context;
@@ -46,25 +46,14 @@ var Systems = new Class({
4646

4747
// These are core Scene plugins, needed by lots of the global systems (and each other)
4848

49+
this.add;
4950
this.cameras;
5051
this.displayList;
5152
this.events;
53+
this.make;
5254
this.sceneManager;
5355
this.time;
5456
this.updateList;
55-
56-
// Optional Scene plugins - not referenced by core systems, can be overridden with user code
57-
58-
// this.plugins;
59-
60-
this.add;
61-
this.data;
62-
this.dataStore;
63-
this.inputManager;
64-
this.load;
65-
this.make;
66-
this.physicsManager;
67-
this.tweens;
6857
},
6958

7059
init: function (game)
@@ -83,36 +72,42 @@ var Systems = new Class({
8372

8473
// These are core Scene plugins, needed by lots of the global systems (and each other)
8574

86-
this.cameras = new CameraManager(scene);
87-
this.displayList = new DisplayList(scene);
8875
this.events = new EventEmitter();
89-
this.sceneManager = new SceneManager(scene);
90-
this.time = new Clock(scene);
91-
this.updateList = new UpdateList(scene);
76+
77+
game.plugins.install(scene, [ 'displayList', 'updateList', 'sceneManager', 'time', 'cameras', 'add', 'make' ]);
9278

9379
// Optional Scene plugins - not referenced by core systems, can be overridden with user code
9480

95-
this.add = new GameObjectFactory(scene);
81+
// game.plugins.install(scene, [ , 'test' ]);
82+
9683
this.data = new Data(scene);
9784
this.dataStore = new DataStore(scene);
9885
this.inputManager = new InputManager(scene);
9986
this.load = new Loader(scene);
100-
this.make = new GameObjectCreator(scene);
10187
this.physicsManager = new PhysicsManager(scene);
10288
this.tweens = new TweenManager(scene);
10389

104-
// this.plugins = new PluginManager(scene);
105-
10690
// Sometimes the managers need access to a system created after them
107-
this.add.boot(this);
108-
this.make.boot(this);
91+
92+
this.events.emit('boot', this);
93+
10994
this.inputManager.boot();
11095
this.physicsManager.boot();
11196

112-
this.inject();
97+
this.inject2();
98+
},
99+
100+
inject: function (plugin)
101+
{
102+
var map = this.settings.map;
103+
104+
if (plugin.mapping && map.hasOwnProperty(plugin.mapping))
105+
{
106+
this.scene[plugin.mapping] = plugin;
107+
}
113108
},
114109

115-
inject: function ()
110+
inject2: function ()
116111
{
117112
var map = this.settings.map;
118113

@@ -130,31 +125,30 @@ var Systems = new Class({
130125
step: function (time, delta)
131126
{
132127
// Are there any pending SceneManager actions?
128+
// This plugin is a special case, as it can literally modify this Scene, so we update it directly.
133129
this.sceneManager.update();
134130

135131
if (!this.settings.active)
136132
{
137133
return;
138134
}
139135

140-
// Move these into local arrays, so you can control which systems are registered here and their
141-
// execution order
136+
this.events.emit('preupdate', time, delta);
142137

143-
this.updateList.begin(time);
144-
this.time.begin(time);
145138
this.tweens.begin(time);
146139
this.inputManager.begin(time);
147140

141+
this.events.emit('update', time, delta);
142+
148143
this.physicsManager.update(time, delta);
149144

150-
this.updateList.update(time, delta);
151-
this.time.update(time, delta);
152145
this.tweens.update(time, delta);
153-
this.cameras.update(time, delta);
154146
this.inputManager.update(time, delta);
155147

156148
this.scene.update.call(this.scene, time, delta);
157149

150+
this.events.emit('postupdate', time, delta);
151+
158152
this.physicsManager.postUpdate();
159153
},
160154

@@ -252,9 +246,11 @@ var Systems = new Class({
252246
this.settings.active = false;
253247
this.settings.visible = false;
254248

255-
this.displayList.shutdown();
256-
this.updateList.shutdown();
257-
this.time.shutdown();
249+
this.events.emit('shutdown', this);
250+
251+
// this.displayList.shutdown();
252+
// this.updateList.shutdown();
253+
// this.time.shutdown();
258254
this.tweens.shutdown();
259255
this.physicsManager.shutdown();
260256

@@ -267,8 +263,10 @@ var Systems = new Class({
267263
// TODO: Game level nuke
268264
destroy: function ()
269265
{
266+
this.events.emit('destroy', this);
267+
270268
this.add.destroy();
271-
this.time.destroy();
269+
// this.time.destroy();
272270
this.tweens.destroy();
273271
this.physicsManager.destroy();
274272

src/scene/plugins/DisplayList.js

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
var Class = require('../../utils/Class');
22
var StableSort = require('../../utils/array/StableSort');
3+
var PluginManager = require('../../plugins/PluginManager');
34

45
var DisplayList = new Class({
56

@@ -10,6 +11,12 @@ var DisplayList = new Class({
1011
// The Scene that owns this plugin
1112
this.scene = scene;
1213

14+
this.systems = scene.sys;
15+
16+
this.mapping = 'add';
17+
18+
this.systems.events.on('boot', this.boot, this);
19+
1320
// The objects that belong to this collection.
1421
// The equivalent of the old `Sprite.children` array.
1522
this.list = [];
@@ -19,6 +26,14 @@ var DisplayList = new Class({
1926
this.position = 0;
2027
},
2128

29+
boot: function ()
30+
{
31+
this.systems.inject(this);
32+
33+
this.systems.events.on('shutdown', this.shutdown, this);
34+
this.systems.events.on('destroy', this.destroy, this);
35+
},
36+
2237
process: function ()
2338
{
2439
if (this.sortChildrenFlag)
@@ -371,11 +386,6 @@ var DisplayList = new Class({
371386
return this;
372387
},
373388

374-
shutdown: function ()
375-
{
376-
this.removeAll();
377-
},
378-
379389
/**
380390
* Brings the given child to the top of this group so it renders above all other children.
381391
*
@@ -621,6 +631,16 @@ var DisplayList = new Class({
621631
return newParent;
622632
},
623633

634+
shutdown: function ()
635+
{
636+
this.removeAll();
637+
},
638+
639+
destroy: function ()
640+
{
641+
this.shutdown();
642+
},
643+
624644
length: {
625645

626646
get: function ()
@@ -704,4 +724,6 @@ var DisplayList = new Class({
704724

705725
});
706726

727+
PluginManager.register('displayList', DisplayList);
728+
707729
module.exports = DisplayList;

src/scene/plugins/GameObjectCreator.js

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,39 @@
11
var Class = require('../../utils/Class');
2+
var PluginManager = require('../../plugins/PluginManager');
23

34
var GameObjectCreator = new Class({
45

56
initialize:
67

78
function GameObjectCreator (scene)
89
{
10+
// The Scene that owns this plugin
911
this.scene = scene;
1012

13+
this.systems = scene.sys;
14+
15+
this.mapping = 'make';
16+
17+
this.systems.events.on('boot', this.boot, this);
18+
1119
this.displayList;
1220
this.updateList;
1321
},
1422

15-
boot: function (sys)
23+
boot: function ()
24+
{
25+
this.systems.inject(this);
26+
27+
this.displayList = this.systems.displayList;
28+
this.updateList = this.systems.updateList;
29+
30+
this.systems.events.on('shutdown', this.shutdown, this);
31+
this.systems.events.on('destroy', this.destroy, this);
32+
},
33+
34+
shutdown: function ()
1635
{
17-
this.displayList = sys.displayList;
18-
this.updateList = sys.updateList;
36+
1937
},
2038

2139
destroy: function ()
@@ -37,4 +55,6 @@ GameObjectCreator.register = function (type, factoryFunction)
3755
}
3856
};
3957

58+
PluginManager.register('make', GameObjectCreator);
59+
4060
module.exports = GameObjectCreator;

0 commit comments

Comments
 (0)