Skip to content

Commit 9537433

Browse files
committed
Added a Sample Plugin and tested out the Plugin Manager. Working great :)
1 parent fba731e commit 9537433

8 files changed

Lines changed: 631 additions & 891 deletions

File tree

Phaser.sublime-workspace

Lines changed: 123 additions & 813 deletions
Large diffs are not rendered by default.

Plugins/SamplePlugin.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* A Sample Plugin demonstrating how to hook into the Phaser plugin system.
3+
*/
4+
Phaser.Plugin.SamplePlugin = function (game, parent) {
5+
6+
Phaser.Plugin.call(this, game, parent);
7+
8+
this.sprite = null;
9+
10+
};
11+
12+
// Extends the Phaser.Plugin template, setting up values we need
13+
Phaser.Plugin.SamplePlugin.prototype = Object.create(Phaser.Plugin.prototype);
14+
Phaser.Plugin.SamplePlugin.prototype.constructor = Phaser.Plugin.SamplePlugin;
15+
16+
/**
17+
* Add a Sprite reference to this Plugin.
18+
* All this plugin does is move the Sprite across the screen slowly.
19+
* @type {Phaser.Sprite}
20+
*/
21+
Phaser.Plugin.SamplePlugin.prototype.addSprite = function (sprite) {
22+
23+
this.sprite = sprite;
24+
25+
};
26+
27+
/**
28+
* This is run when the plugins update during the core game loop.
29+
*/
30+
Phaser.Plugin.SamplePlugin.prototype.update = function () {
31+
32+
if (this.sprite)
33+
{
34+
this.sprite.x += 0.5;
35+
}
36+
37+
};

examples/plugin1.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<!DOCTYPE HTML>
2+
<html>
3+
<head>
4+
<title>phaser.js - a new beginning</title>
5+
<?php
6+
require('js.php');
7+
?>
8+
<script src="../plugins/SamplePlugin.js"></script>
9+
</head>
10+
<body>
11+
12+
<script type="text/javascript">
13+
14+
(function () {
15+
16+
var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create });
17+
18+
var plugin;
19+
20+
function preload() {
21+
22+
game.load.image('atari1', 'assets/sprites/atari130xe.png');
23+
24+
}
25+
26+
function create() {
27+
28+
// Create our SamplePlugin.
29+
// All the SamplePlugin does is move a sprite across the screen,
30+
// but it shows how to structure a plugin and hook it into key functions
31+
32+
plugin = game.plugins.add(Phaser.Plugin.SamplePlugin);
33+
34+
var tempSprite = game.add.sprite(0, 200, 'atari1');
35+
36+
plugin.addSprite(tempSprite);
37+
38+
}
39+
40+
})();
41+
</script>
42+
43+
</body>
44+
</html>

src/core/Game.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,7 @@ Phaser.Game.prototype = {
394394
this.plugins.update();
395395

396396
this.renderer.render(this.stage._stage);
397+
this.plugins.render();
397398
this.state.render();
398399

399400
this.plugins.postRender();

src/core/Plugin.js

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ Phaser.Plugin = function (game, parent) {
1313

1414
this.hasPreUpdate = false;
1515
this.hasUpdate = false;
16-
this.hasPostUpdate = false;
17-
this.hasPreRender = false;
1816
this.hasRender = false;
1917
this.hasPostRender = false;
2018

@@ -23,42 +21,28 @@ Phaser.Plugin = function (game, parent) {
2321
Phaser.Plugin.prototype = {
2422

2523
/**
26-
* Pre-update is called at the start of the update cycle, before any other updates have taken place.
24+
* Pre-update is called at the start of the update cycle, before any other updates have taken place (including Physics).
2725
* It is only called if active is set to true.
2826
*/
2927
preUpdate: function () {
3028
},
3129

3230
/**
33-
* Pre-update is called at the start of the update cycle, before any other updates have taken place.
31+
* Update is called after all the core subsystems (Input, Tweens, Sound, etc) and the State have updated, but before the render.
3432
* It is only called if active is set to true.
3533
*/
3634
update: function () {
3735
},
3836

3937
/**
40-
* Post-update is called at the end of the objects update cycle, after other update logic has taken place.
41-
* It is only called if active is set to true.
42-
*/
43-
postUpdate: function () {
44-
},
45-
46-
/**
47-
* Pre-render is called right before the Game Renderer starts and before any custom preRender callbacks have been run.
48-
* It is only called if visible is set to true.
49-
*/
50-
preRender: function () {
51-
},
52-
53-
/**
54-
* Pre-render is called right before the Game Renderer starts and before any custom preRender callbacks have been run.
38+
* Render is called right after the Game Renderer completes, but before the State.render.
5539
* It is only called if visible is set to true.
5640
*/
5741
render: function () {
5842
},
5943

6044
/**
61-
* Post-render is called after every camera and game object has been rendered, also after any custom postRender callbacks have been run.
45+
* Post-render is called after the Game Renderer and State.render have run.
6246
* It is only called if visible is set to true.
6347
*/
6448
postRender: function () {

src/core/PluginManager.js

Lines changed: 28 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -36,52 +36,45 @@ Phaser.PluginManager.prototype = {
3636
}
3737

3838
// Check for methods now to avoid having to do this every loop
39-
if (typeof plugin['preUpdate'] === 'function') {
39+
if (typeof plugin['preUpdate'] === 'function')
40+
{
4041
plugin.hasPreUpdate = true;
4142
result = true;
4243
}
4344

44-
if (typeof plugin['update'] === 'function') {
45+
if (typeof plugin['update'] === 'function')
46+
{
4547
plugin.hasUpdate = true;
4648
result = true;
4749
}
4850

49-
if (typeof plugin['postUpdate'] === 'function') {
50-
plugin.hasPostUpdate = true;
51-
result = true;
52-
}
53-
54-
if (typeof plugin['preRender'] === 'function') {
55-
plugin.hasPreRender = true;
56-
result = true;
57-
}
58-
59-
if (typeof plugin['render'] === 'function') {
51+
if (typeof plugin['render'] === 'function')
52+
{
6053
plugin.hasRender = true;
6154
result = true;
6255
}
6356

64-
if (typeof plugin['postRender'] === 'function') {
57+
if (typeof plugin['postRender'] === 'function')
58+
{
6559
plugin.hasPostRender = true;
6660
result = true;
6761
}
6862

6963
// The plugin must have at least one of the above functions to be added to the PluginManager.
70-
if (result) {
71-
72-
if (plugin.hasPreUpdate || plugin.hasUpdate || plugin.hasPostUpdate)
64+
if (result)
65+
{
66+
if (plugin.hasPreUpdate || plugin.hasUpdate)
7367
{
7468
plugin.active = true;
7569
}
7670

77-
if (plugin.hasPreRender || plugin.hasRender || plugin.hasPostRender)
71+
if (plugin.hasRender || plugin.hasPostRender)
7872
{
7973
plugin.visible = true;
8074
}
8175

8276
this._pluginsLength = this.plugins.push(plugin);
8377
return plugin;
84-
8578
}
8679
else
8780
{
@@ -103,8 +96,10 @@ Phaser.PluginManager.prototype = {
10396
return;
10497
}
10598

106-
for (this._p = 0; this._p < this._pluginsLength; this._p++) {
107-
if (this.plugins[this._p].active && this.plugins[this._p].hasPreUpdate) {
99+
for (this._p = 0; this._p < this._pluginsLength; this._p++)
100+
{
101+
if (this.plugins[this._p].active && this.plugins[this._p].hasPreUpdate)
102+
{
108103
this.plugins[this._p].preUpdate();
109104
}
110105
}
@@ -118,39 +113,11 @@ Phaser.PluginManager.prototype = {
118113
return;
119114
}
120115

121-
for (this._p = 0; this._p < this._pluginsLength; this._p++) {
122-
if (this.plugins[this._p].active && this.plugins[this._p].hasUpdate) {
123-
this.plugins[this._p].update();
124-
}
125-
}
126-
127-
},
128-
129-
postUpdate: function () {
130-
131-
if (this._pluginsLength == 0)
116+
for (this._p = 0; this._p < this._pluginsLength; this._p++)
132117
{
133-
return;
134-
}
135-
136-
for (this._p = 0; this._p < this._pluginsLength; this._p++) {
137-
if (this.plugins[this._p].active && this.plugins[this._p].hasPostUpdate) {
138-
this.plugins[this._p].postUpdate();
139-
}
140-
}
141-
142-
},
143-
144-
preRender: function () {
145-
146-
if (this._pluginsLength == 0)
147-
{
148-
return;
149-
}
150-
151-
for (this._p = 0; this._p < this._pluginsLength; this._p++) {
152-
if (this.plugins[this._p].visible && this.plugins[this._p].hasPreRender) {
153-
this.plugins[this._p].preRender();
118+
if (this.plugins[this._p].active && this.plugins[this._p].hasUpdate)
119+
{
120+
this.plugins[this._p].update();
154121
}
155122
}
156123

@@ -163,8 +130,10 @@ Phaser.PluginManager.prototype = {
163130
return;
164131
}
165132

166-
for (this._p = 0; this._p < this._pluginsLength; this._p++) {
167-
if (this.plugins[this._p].visible && this.plugins[this._p].hasRender) {
133+
for (this._p = 0; this._p < this._pluginsLength; this._p++)
134+
{
135+
if (this.plugins[this._p].visible && this.plugins[this._p].hasRender)
136+
{
168137
this.plugins[this._p].render();
169138
}
170139
}
@@ -178,9 +147,10 @@ Phaser.PluginManager.prototype = {
178147
return;
179148
}
180149

181-
for (this._p = 0; this._p < this._pluginsLength; this._p++) {
182-
183-
if (this.plugins[this._p].visible && this.plugins[this._p].hasPostRender) {
150+
for (this._p = 0; this._p < this._pluginsLength; this._p++)
151+
{
152+
if (this.plugins[this._p].visible && this.plugins[this._p].hasPostRender)
153+
{
184154
this.plugins[this._p].postRender();
185155
}
186156
}

0 commit comments

Comments
 (0)