Skip to content

Commit c65e3c7

Browse files
committed
Added FB Instant Games Plugin to core (will prepare for moving to unique build once feature complete)
1 parent 1abe904 commit c65e3c7

8 files changed

Lines changed: 257 additions & 0 deletions

File tree

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ src/utils/array/StableSort.js
99
src/utils/object/Extend.js
1010
src/structs/RTree.js
1111
src/boot/ScaleManager.js
12+
src/fbinstant/

scripts/copy-to-examples.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,37 @@ let source = './build/phaser.js';
55
let sourceMap = './build/phaser.js.map';
66
let dest = '../phaser3-examples/public/build/dev.js';
77
let destMap = '../phaser3-examples/public/build/phaser.js.map';
8+
let destFB = '../fbtest1/lib/dev.js';
9+
let destFBMap = '../fbtest1/lib/phaser.js.map';
810

911
let sourceCore = './build/phaser-core.js';
1012
let sourceMapCore = './build/phaser-core.js.map';
1113
let destCore = '../phaser3-examples/public/build/phaser-core.js';
1214
let destMapCore = '../phaser3-examples/public/build/phaser-core.js.map';
1315

16+
if (fs.existsSync(destFB))
17+
{
18+
fs.copy(source, destFB, function (err) {
19+
20+
if (err)
21+
{
22+
return console.error(err);
23+
}
24+
25+
console.log('Build copied to ' + destFB);
26+
27+
});
28+
29+
fs.copy(sourceMap, destFBMap, function (err) {
30+
31+
if (err)
32+
{
33+
return console.error(err);
34+
}
35+
36+
});
37+
}
38+
1439
if (fs.existsSync(dest))
1540
{
1641
fs.copy(sourceMapCore, destMapCore, function (err) {

src/boot/Game.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ var DebugHeader = require('./DebugHeader');
1717
var Device = require('../device');
1818
var DOMContentLoaded = require('../dom/DOMContentLoaded');
1919
var EventEmitter = require('eventemitter3');
20+
var FacebookInstantGamesPlugin = require('../fbinstant/FacebookInstantGamesPlugin');
2021
var InputManager = require('../input/InputManager');
2122
var PluginManager = require('../plugins/PluginManager');
2223
var SceneManager = require('../scene/SceneManager');
@@ -247,6 +248,15 @@ var Game = new Class({
247248
*/
248249
this.plugins = new PluginManager(this, this.config);
249250

251+
/**
252+
* An instance of the Facebook Instant Games Manager.
253+
*
254+
* @name Phaser.Game#facebook
255+
* @type {any}
256+
* @since 3.12.0
257+
*/
258+
this.facebook = new FacebookInstantGamesPlugin(this);
259+
250260
/**
251261
* Is this Game pending destruction at the start of the next frame?
252262
*
Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @copyright 2018 Photon Storm Ltd.
4+
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
5+
*/
6+
7+
var Class = require('../utils/Class');
8+
var DataManager = require('../data/DataManager');
9+
var EventEmitter = require('eventemitter3');
10+
var GetValue = require('../utils/object/GetValue');
11+
12+
/**
13+
* @classdesc
14+
* [description]
15+
*
16+
* @class FacebookInstantGamesPlugin
17+
* @memberOf Phaser
18+
* @constructor
19+
* @extends Phaser.Events.EventEmitter
20+
* @since 3.12.0
21+
*
22+
* @param {Phaser.Game} game - A reference to the Phaser.Game instance.
23+
* @param {FBConfig} config
24+
*/
25+
var FacebookInstantGamesPlugin = new Class({
26+
27+
Extends: EventEmitter,
28+
29+
initialize:
30+
31+
function FacebookInstantGamesPlugin (game, config)
32+
{
33+
EventEmitter.call(this);
34+
35+
/**
36+
* A reference to the Phaser.Game instance.
37+
*
38+
* @name Phaser.Boot.FacebookInstantGamesPlugin#game
39+
* @type {Phaser.Game}
40+
* @readOnly
41+
* @since 3.12.0
42+
*/
43+
this.game = game;
44+
45+
this.data = new DataManager(this);
46+
47+
this.on('setdata', this.setDataHandler, this);
48+
this.on('changedata', this.changeDataHandler, this);
49+
50+
this.hasLoaded = false;
51+
this.dataLocked = false;
52+
53+
this.playerID = '';
54+
this.playerName = '';
55+
this.playerPhotoURL = '';
56+
},
57+
58+
setDataHandler: function (parent, key, value)
59+
{
60+
if (this.dataLocked)
61+
{
62+
return;
63+
}
64+
65+
console.log('set data:', key, value);
66+
67+
var data = {};
68+
data[key] = value;
69+
70+
var _this = this;
71+
72+
FBInstant.player.setDataAsync(data).then(function() {
73+
console.log('sdh saved', data);
74+
_this.emit('savedata', data);
75+
});
76+
},
77+
78+
changeDataHandler: function (parent, key, value)
79+
{
80+
if (this.dataLocked)
81+
{
82+
return;
83+
}
84+
85+
console.log('change data:', key, value);
86+
87+
var data = {};
88+
data[key] = value;
89+
90+
var _this = this;
91+
92+
FBInstant.player.setDataAsync(data).then(function() {
93+
console.log('cdh saved', data);
94+
_this.emit('savedata', data);
95+
});
96+
},
97+
98+
showLoadProgress: function (scene)
99+
{
100+
scene.load.on('progress', function (value) {
101+
102+
if (!this.hasLoaded)
103+
{
104+
console.log(value);
105+
FBInstant.setLoadingProgress(value * 100);
106+
}
107+
108+
}, this);
109+
110+
scene.load.on('complete', function () {
111+
112+
this.hasLoaded = true;
113+
114+
console.log('loaded');
115+
116+
FBInstant.startGameAsync().then(this.gameStarted.bind(this));
117+
118+
}, this);
119+
120+
scene.load.start();
121+
122+
return this;
123+
},
124+
125+
gameStarted: function ()
126+
{
127+
console.log('gameStarted');
128+
129+
this.playerID = FBInstant.player.getID();
130+
this.playerName = FBInstant.player.getName();
131+
this.playerPhotoURL = FBInstant.player.getPhoto();
132+
133+
this.emit('startgame');
134+
},
135+
136+
loadPlayerPhoto: function (scene, key)
137+
{
138+
console.log('load');
139+
140+
scene.load.setCORS('anonymous');
141+
142+
scene.load.image(key, this.playerPhotoURL);
143+
144+
scene.load.on('complete', function () {
145+
146+
this.emit('photocomplete', key);
147+
148+
}, this);
149+
150+
scene.load.start();
151+
152+
return this;
153+
},
154+
155+
getData: function (keys)
156+
{
157+
if (!Array.isArray(keys))
158+
{
159+
keys = [ keys ];
160+
}
161+
162+
console.log('getdata', keys);
163+
164+
var _this = this;
165+
166+
FBInstant.player.getDataAsync(keys).then(function(data) {
167+
168+
console.log('getdata req', data);
169+
170+
_this.dataLocked = true;
171+
172+
for (var key in data)
173+
{
174+
_this.data.set(key, data[key]);
175+
}
176+
177+
_this.dataLocked = false;
178+
179+
_this.emit('getdata', data);
180+
181+
});
182+
183+
return this;
184+
},
185+
186+
saveData: function (data)
187+
{
188+
FBInstant.player.setDataAsync(data).then(function() {
189+
console.log('data saved to fb');
190+
this.emit('savedata', data);
191+
});
192+
193+
return this;
194+
},
195+
196+
/**
197+
* Destroys the FacebookInstantGamesPlugin.
198+
*
199+
* @method Phaser.Boot.FacebookInstantGamesPlugin#destroy
200+
* @since 3.12.0
201+
*/
202+
destroy: function ()
203+
{
204+
this.game = null;
205+
}
206+
207+
});
208+
209+
module.exports = FacebookInstantGamesPlugin;

src/phaser.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ var Phaser = {
2626
Display: require('./display'),
2727
DOM: require('./dom'),
2828
Events: require('./events'),
29+
FBInstant: require('./fbinstant/FacebookInstantGamesPlugin'),
2930
Game: require('./boot/Game'),
3031
GameObjects: require('./gameobjects'),
3132
Geom: require('./geom'),

src/plugins/DefaultPlugins.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ var DefaultPlugins = {
2626

2727
'anims',
2828
'cache',
29+
'facebook',
2930
'plugins',
3031
'registry',
3132
'sound',

src/scene/InjectionMap.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ var InjectionMap = {
2424
registry: 'registry',
2525
sound: 'sound',
2626
textures: 'textures',
27+
facebook: 'facebook',
2728

2829
events: 'events',
2930
cameras: 'cameras',

src/scene/Systems.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ var Systems = new Class({
5252
*/
5353
this.game;
5454

55+
/**
56+
* [description]
57+
*
58+
* @name Phaser.Scenes.Systems#facebook
59+
* @type {any}
60+
* @since 3.12.0
61+
*/
62+
this.facebook;
63+
5564
/**
5665
* [description]
5766
*

0 commit comments

Comments
 (0)