diff --git a/.eslintignore b/.eslintignore index 8618d2af7..78cc9ea67 100644 --- a/.eslintignore +++ b/.eslintignore @@ -9,3 +9,4 @@ src/utils/array/StableSort.js src/utils/object/Extend.js src/structs/RTree.js src/boot/ScaleManager.js +src/fbinstant/ diff --git a/scripts/copy-to-examples.js b/scripts/copy-to-examples.js index aaace8d25..7e2cf2427 100644 --- a/scripts/copy-to-examples.js +++ b/scripts/copy-to-examples.js @@ -5,12 +5,37 @@ let source = './build/phaser.js'; let sourceMap = './build/phaser.js.map'; let dest = '../phaser3-examples/public/build/dev.js'; let destMap = '../phaser3-examples/public/build/phaser.js.map'; +let destFB = '../fbtest1/lib/dev.js'; +let destFBMap = '../fbtest1/lib/phaser.js.map'; let sourceCore = './build/phaser-core.js'; let sourceMapCore = './build/phaser-core.js.map'; let destCore = '../phaser3-examples/public/build/phaser-core.js'; let destMapCore = '../phaser3-examples/public/build/phaser-core.js.map'; +if (fs.existsSync(destFB)) +{ + fs.copy(source, destFB, function (err) { + + if (err) + { + return console.error(err); + } + + console.log('Build copied to ' + destFB); + + }); + + fs.copy(sourceMap, destFBMap, function (err) { + + if (err) + { + return console.error(err); + } + + }); +} + if (fs.existsSync(dest)) { fs.copy(sourceMapCore, destMapCore, function (err) { diff --git a/src/boot/Game.js b/src/boot/Game.js index 82a2383e4..ee159933f 100644 --- a/src/boot/Game.js +++ b/src/boot/Game.js @@ -17,6 +17,7 @@ var DebugHeader = require('./DebugHeader'); var Device = require('../device'); var DOMContentLoaded = require('../dom/DOMContentLoaded'); var EventEmitter = require('eventemitter3'); +var FacebookInstantGamesPlugin = require('../fbinstant/FacebookInstantGamesPlugin'); var InputManager = require('../input/InputManager'); var PluginManager = require('../plugins/PluginManager'); var SceneManager = require('../scene/SceneManager'); @@ -247,6 +248,15 @@ var Game = new Class({ */ this.plugins = new PluginManager(this, this.config); + /** + * An instance of the Facebook Instant Games Manager. + * + * @name Phaser.Game#facebook + * @type {any} + * @since 3.12.0 + */ + this.facebook = new FacebookInstantGamesPlugin(this); + /** * Is this Game pending destruction at the start of the next frame? * diff --git a/src/fbinstant/FacebookInstantGamesPlugin.js b/src/fbinstant/FacebookInstantGamesPlugin.js new file mode 100644 index 000000000..b10412fa6 --- /dev/null +++ b/src/fbinstant/FacebookInstantGamesPlugin.js @@ -0,0 +1,209 @@ +/** + * @author Richard Davey + * @copyright 2018 Photon Storm Ltd. + * @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License} + */ + +var Class = require('../utils/Class'); +var DataManager = require('../data/DataManager'); +var EventEmitter = require('eventemitter3'); +var GetValue = require('../utils/object/GetValue'); + +/** + * @classdesc + * [description] + * + * @class FacebookInstantGamesPlugin + * @memberOf Phaser + * @constructor + * @extends Phaser.Events.EventEmitter + * @since 3.12.0 + * + * @param {Phaser.Game} game - A reference to the Phaser.Game instance. + * @param {FBConfig} config + */ +var FacebookInstantGamesPlugin = new Class({ + + Extends: EventEmitter, + + initialize: + + function FacebookInstantGamesPlugin (game, config) + { + EventEmitter.call(this); + + /** + * A reference to the Phaser.Game instance. + * + * @name Phaser.Boot.FacebookInstantGamesPlugin#game + * @type {Phaser.Game} + * @readOnly + * @since 3.12.0 + */ + this.game = game; + + this.data = new DataManager(this); + + this.on('setdata', this.setDataHandler, this); + this.on('changedata', this.changeDataHandler, this); + + this.hasLoaded = false; + this.dataLocked = false; + + this.playerID = ''; + this.playerName = ''; + this.playerPhotoURL = ''; + }, + + setDataHandler: function (parent, key, value) + { + if (this.dataLocked) + { + return; + } + + console.log('set data:', key, value); + + var data = {}; + data[key] = value; + + var _this = this; + + FBInstant.player.setDataAsync(data).then(function() { + console.log('sdh saved', data); + _this.emit('savedata', data); + }); + }, + + changeDataHandler: function (parent, key, value) + { + if (this.dataLocked) + { + return; + } + + console.log('change data:', key, value); + + var data = {}; + data[key] = value; + + var _this = this; + + FBInstant.player.setDataAsync(data).then(function() { + console.log('cdh saved', data); + _this.emit('savedata', data); + }); + }, + + showLoadProgress: function (scene) + { + scene.load.on('progress', function (value) { + + if (!this.hasLoaded) + { + console.log(value); + FBInstant.setLoadingProgress(value * 100); + } + + }, this); + + scene.load.on('complete', function () { + + this.hasLoaded = true; + + console.log('loaded'); + + FBInstant.startGameAsync().then(this.gameStarted.bind(this)); + + }, this); + + scene.load.start(); + + return this; + }, + + gameStarted: function () + { + console.log('gameStarted'); + + this.playerID = FBInstant.player.getID(); + this.playerName = FBInstant.player.getName(); + this.playerPhotoURL = FBInstant.player.getPhoto(); + + this.emit('startgame'); + }, + + loadPlayerPhoto: function (scene, key) + { + console.log('load'); + + scene.load.setCORS('anonymous'); + + scene.load.image(key, this.playerPhotoURL); + + scene.load.on('complete', function () { + + this.emit('photocomplete', key); + + }, this); + + scene.load.start(); + + return this; + }, + + getData: function (keys) + { + if (!Array.isArray(keys)) + { + keys = [ keys ]; + } + + console.log('getdata', keys); + + var _this = this; + + FBInstant.player.getDataAsync(keys).then(function(data) { + + console.log('getdata req', data); + + _this.dataLocked = true; + + for (var key in data) + { + _this.data.set(key, data[key]); + } + + _this.dataLocked = false; + + _this.emit('getdata', data); + + }); + + return this; + }, + + saveData: function (data) + { + FBInstant.player.setDataAsync(data).then(function() { + console.log('data saved to fb'); + this.emit('savedata', data); + }); + + return this; + }, + + /** + * Destroys the FacebookInstantGamesPlugin. + * + * @method Phaser.Boot.FacebookInstantGamesPlugin#destroy + * @since 3.12.0 + */ + destroy: function () + { + this.game = null; + } + +}); + +module.exports = FacebookInstantGamesPlugin; diff --git a/src/phaser.js b/src/phaser.js index 69002f963..4903673f6 100644 --- a/src/phaser.js +++ b/src/phaser.js @@ -26,6 +26,7 @@ var Phaser = { Display: require('./display'), DOM: require('./dom'), Events: require('./events'), + FBInstant: require('./fbinstant/FacebookInstantGamesPlugin'), Game: require('./boot/Game'), GameObjects: require('./gameobjects'), Geom: require('./geom'), diff --git a/src/plugins/DefaultPlugins.js b/src/plugins/DefaultPlugins.js index 049c0f636..d5f88c867 100644 --- a/src/plugins/DefaultPlugins.js +++ b/src/plugins/DefaultPlugins.js @@ -26,6 +26,7 @@ var DefaultPlugins = { 'anims', 'cache', + 'facebook', 'plugins', 'registry', 'sound', diff --git a/src/scene/InjectionMap.js b/src/scene/InjectionMap.js index c364ab012..f2cbe5406 100644 --- a/src/scene/InjectionMap.js +++ b/src/scene/InjectionMap.js @@ -24,6 +24,7 @@ var InjectionMap = { registry: 'registry', sound: 'sound', textures: 'textures', + facebook: 'facebook', events: 'events', cameras: 'cameras', diff --git a/src/scene/Systems.js b/src/scene/Systems.js index 512293b22..abbae42c1 100644 --- a/src/scene/Systems.js +++ b/src/scene/Systems.js @@ -52,6 +52,15 @@ var Systems = new Class({ */ this.game; + /** + * [description] + * + * @name Phaser.Scenes.Systems#facebook + * @type {any} + * @since 3.12.0 + */ + this.facebook; + /** * [description] *