mirror of
https://github.com/photonstorm/phaser
synced 2024-11-21 20:23:19 +00:00
Added FB Instant Games Plugin to core (will prepare for moving to unique build once feature complete)
This commit is contained in:
parent
1abe90433f
commit
c65e3c7428
8 changed files with 257 additions and 0 deletions
|
@ -9,3 +9,4 @@ src/utils/array/StableSort.js
|
|||
src/utils/object/Extend.js
|
||||
src/structs/RTree.js
|
||||
src/boot/ScaleManager.js
|
||||
src/fbinstant/
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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?
|
||||
*
|
||||
|
|
209
src/fbinstant/FacebookInstantGamesPlugin.js
Normal file
209
src/fbinstant/FacebookInstantGamesPlugin.js
Normal file
|
@ -0,0 +1,209 @@
|
|||
/**
|
||||
* @author Richard Davey <rich@photonstorm.com>
|
||||
* @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;
|
|
@ -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'),
|
||||
|
|
|
@ -26,6 +26,7 @@ var DefaultPlugins = {
|
|||
|
||||
'anims',
|
||||
'cache',
|
||||
'facebook',
|
||||
'plugins',
|
||||
'registry',
|
||||
'sound',
|
||||
|
|
|
@ -24,6 +24,7 @@ var InjectionMap = {
|
|||
registry: 'registry',
|
||||
sound: 'sound',
|
||||
textures: 'textures',
|
||||
facebook: 'facebook',
|
||||
|
||||
events: 'events',
|
||||
cameras: 'cameras',
|
||||
|
|
|
@ -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]
|
||||
*
|
||||
|
|
Loading…
Reference in a new issue