123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576 |
- var WxCommon = require("../WxCommon")
- var cfg = require("Configure")
- // @TODO 引入sdk代码
- import DwSdk from "../duowansdk/DwSdk";
- const debug = false;
- const objSdk = new DwSdk("布丁弹一弹", debug);
- // 注册sdk的update事件
- cc.director.on(cc.Director.EVENT_AFTER_DRAW, objSdk.update);
- cc.Class({
- extends: cc.Component,
- properties: {
- gamebgMusic: {
- default: null,
- type: cc.AudioClip
- },
- buttonAudio: {
- default: null,
- type: cc.AudioClip
- },
- SignAtlas: {
- default: null,
- type: cc.SpriteAtlas,
- },
- ShareStrategyNode: { //游戏攻略图片节点
- default: null,
- type: cc.Node
- },
- GuideNode: {
- default: null,
- type: cc.Node
- },
- shareNumBg: {
- default: null,
- type: cc.Node
- },
- shareLayer: {
- default: null,
- type: cc.Node
- },
- mask: {
- default: null,
- type: cc.Node
- },
- shareNumSprites: [cc.SpriteFrame],
- feiendtex: null,
- otherGamesIndex: 0,
- skinIndex: 0,
- showGuideValue: true
- },
- // LIFE-CYCLE CALLBACKS:
- onLoad() {
- cc.audioEngine.playMusic(this.gamebgMusic, true);
- if (CC_WECHATGAME) {
- this.WxLogin();
- this.initShare();
- // wx.setPreferredFramesPerSecond(30);
- }
- this.listenGame();
- objSdk.showAdIcon(-360, -240, this.node);
- //SDK接入
- if (CC_WECHATGAME) {
- let { screenWidth, screenHeight } = wx.getSystemInfoSync();
- let style = {
- left: 60,
- top: screenHeight - 100,
- width: screenWidth - 120,
- height: 100
- }
- if (screenWidth <= 360) {
- style.left = (screenWidth - 320) / 2;
- }
- let indexBannerId = 'adunit-de205564802e4627',
- deadBannerId = 'adunit-7a574e7546c7062d',
- accountBannerId = 'adunit-1e3576e27d61175a';
- window.indexBanner = wx.createBannerAd({
- adUnitId: indexBannerId,
- style: style
- })
- indexBanner.onError(err => {
- console.log('indexBanner 拉取失败', err)
- })
- indexBanner.onResize(res => {
- console.log(res.width, res.height)
- indexBanner.style.left = (screenWidth - res.width) / 2;
- indexBanner.style.top = screenHeight - res.height;
- })
- indexBanner.onLoad(() => {
- indexBanner.show()
- })
- window.deadBanner = wx.createBannerAd({
- adUnitId: deadBannerId,
- style: style
- })
- deadBanner.onResize(res => {
- deadBanner.style.left = (screenWidth - res.width) / 2;
- deadBanner.style.top = screenHeight - res.height;
- })
- deadBanner.onError(err => {
- console.log('deadBanner 拉取失败', err)
- })
- window.accountBanner = wx.createBannerAd({
- adUnitId: accountBannerId,
- style: style
- })
- accountBanner.onResize(res => {
- accountBanner.style.left = (screenWidth - res.width) / 2;
- accountBanner.style.top = screenHeight - res.height;
- })
- accountBanner.onError(err => {
- console.log('accountBanner 拉取失败', err)
- })
- } else {
- let advers = ['indexBanner', 'deadBanner', 'accountBanner'];
- advers.forEach(ad => {
- window[ad] = {
- show: function () { },
- hide: function () { }
- }
- })
- }
- },
- start() {
- this.feiendtex = new cc.Texture2D();
- this.PHB = cc.find("Canvas/PHB")
- this.rankScene = this.PHB.getComponent("RankScene");
- this.rankNode = this.PHB.getChildByName("Rank")
- this.myRankNode = this.PHB.getChildByName('MyRank')
- this.worldContentNode = this.rankNode.getChildByName('WorldContent')
- this.friendContentNode = this.rankNode.getChildByName('FriendContent')
- this.shareLabel = this.shareNumBg.getChildByName('text').getComponent(cc.Label);
- this.shareNumSprite = this.shareLayer.getChildByName('sharenum').getComponent(cc.Sprite);
- //更新护盾信息 后期写到获取到数据的回调里
- this.updateShareBtn();
- },
- WxLogin() {
- if (UserInfo.platform != 1) {
- return;
- }
- wx.login({
- success: function (res) {
- if (res.code) {
- var js_code = res.code;
- } else {
- console.log('登录失败!' + res.errMsg)
- }
- }
- });
- },
- initShare(data) {
- let datavalue = UserInfo.getString('allscore');
- let key = UserInfo.getCurWeekScoreValue();
- if (UserInfo.IsEmpty(datavalue)) {
- UserInfo.setString('allscore', key);
- return;
- }
- let datalist = datavalue.split('*');
- let removeIndex = -1;
- for (let index = 0; index < datalist.length; ++index) {
- if (key == datalist[index]) {
- // UserInfo.removeString(datalist[index]);
- removeIndex = index;
- }
- }
- if (removeIndex != -1) {
- datalist.splice(removeIndex, 1);
- }
- WxCommon.RemoveUserCloudStorage(datalist, this, this.RemoveScoreKey);
- // this.RemoveScoreKey(datalist, true);
- },
- listenGame() {
- if (CC_WECHATGAME) {
- // var date = UserInfo.GetNowFormatDate();
- this.OnUpdateGame();
- WxCommon.SetEnableDebug(false);
- WxCommon.OnShow(null, this, null);
- WxCommon.ShowShareMenu();
- WxCommon.OnShareAppMessage(null, this, this.shareCallback);
- WxCommon.GetSystemInfo(null, this, null);
- }
- },
- OnUpdateGame() {
- const updateManager = wx.getUpdateManager()
- updateManager.onCheckForUpdate(function (res) {
- // 请求完新版本信息的回调
- console.log(res.hasUpdate)
- })
- updateManager.onUpdateReady(function () {
- wx.showModal({
- title: '更新提示',
- content: '新版本已经准备好,是否重启应用?',
- success: function (res) {
- if (res.confirm) {
- // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
- updateManager.applyUpdate()
- }
- }
- })
- })
- updateManager.onUpdateFailed(function () {
- // 新的版本下载失败
- })
- },
- onRankBtn: function (event, optData) {
- console.log('onrankbtn:', optData)
- if (UserInfo.platform != 1 || !cc.vv.isRank) {
- return;
- }
- wx.postMessage({
- method: 'showFriendRank',
- data: optData
- });
- cc.find("Canvas/PHB").getChildByName("BtnPHBClose").active = (optData == "true");
- cc.find("Canvas/PHB").getChildByName("emptyBtn").active = (optData == "true");
- cc.find("Canvas/PHB").getChildByName("bg").active = (optData == "true");
- if (this.node.active) {
- if ("true" == optData) {
- window.indexBanner.hide();
- this.rankScene.status = 1;
- this.showRank();
- }
- if ("false" == optData) {
- window.indexBanner.show();
- wx.postMessage({
- method: 'hideRank',
- data: optData
- });
- cc.find("Canvas/PHB").active = false;
- }
- } else {
- this.rankScene.status = 3;
- window.accountBanner.show();
- }
- },
- showRank() {
- if (UserInfo.platform == 1 && cc.vv.isRank) {
- this.myRankNode.active = false;
- this.rankNode.active = true;
- if (this.rankScene.status == 1) {
- let openDataContext = wx.getOpenDataContext();
- let sharedCanvas = openDataContext.canvas;
- this.feiendtex.initWithElement(sharedCanvas);
- this.feiendtex.handleLoadedTexture();
- this.friendContentNode.getComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(this.feiendtex);
- var scale = Math.min(750 / this.feiendtex.width, 1334 / this.feiendtex.height);
- this.friendContentNode.setContentSize(this.feiendtex.width * scale, this.feiendtex.height * scale);
- }
- this.PHB.active = true;
- }
- },
- update: function () {
- if (cc.find("Canvas/PHB").active) {
- this.showRank();
- }
- },
- onGameStartBtn: function () {
- this.node.parent.getChildByName("SkinNode").active = false;
- window.indexBanner.hide();
- if (this.showGuide()) {
- this.GuideNode.active = true;
- } else {
- if (this.skinIndex != UserInfo.getSkinIndex()) {
- UserInfo.setSkinIndex(this.skinIndex);
- }
- if (this.node.active) {
- cc.audioEngine.stopMusic(this.gamebgMusic);
- this.node.parent.getComponent("SenceManager").enterSence("Home", "Game");
- } else {
- this.node.parent.getChildByName("Game").getComponent("GameSence").onRestartBtn();
- }
- }
- },
- showGuide() {
- if (!this.showGuideValue) return false;
- console.log('show guide');
- var getStorage = function (key) {
- return window.wx ? window.wx.getStorageSync(key) : localStorage.getItem(key);
- }
- var setStorage = function (key, value) {
- return window.wx ? window.wx.setStorageSync(key, value) : localStorage.setItem(key, value);
- }
- var key = 'guideTime';
- var guideTime = getStorage(key);
- var now = new Date();
- var year = now.getFullYear();
- var month = now.getMonth() + 1;
- var date = now.getDate();
- var guideTimeValue = [year, month, date].join('/');
- if (!guideTime || guideTime != guideTimeValue) {
- setStorage(key, guideTimeValue);
- return true;
- } else {
- this.GuideNode && this.GuideNode.destroy();
- this.showGuideValue = false;
- return false;
- }
- },
- onSkinBtn: function (event, optData) {
- if (optData == "true") {
- this.node.parent.getChildByName("SkinNode").active = true;
- this.skinIndex = UserInfo.getSkinIndex();
- var sumScoreLabel = this.node.parent.getChildByName("SkinNode").getChildByName("SumScoreSp").getChildByName("SumScoreLabel");
- sumScoreLabel.getComponent("cc.Label").string = UserInfo.getTotalGold();
- this.updateSkinNode();
- window.indexBanner.hide();
- } else if (optData == "false") {
- this.node.parent.getChildByName("SkinNode").active = false;
- window.indexBanner.show();
- }
- },
- updateSkinNode: function () {
- var skinNode = this.node.parent.getChildByName("SkinNode");
- if (this.skinIndex == 0) {
- skinNode.getChildByName("RightBtn").active = true;
- skinNode.getChildByName("LeftBtn").active = false;
- } else if (this.skinIndex == cfg.skinTypePath.length - 1) {
- skinNode.getChildByName("RightBtn").active = false;
- skinNode.getChildByName("LeftBtn").active = true;
- } else {
- skinNode.getChildByName("RightBtn").active = true;
- skinNode.getChildByName("LeftBtn").active = true;
- }
- var skSpine = skinNode.getChildByName("skSprite");
- skSpine.getComponent("sp.Skeleton").setSkin("p" + (this.skinIndex + 1));
- //var self = this;
- //cc.loader.loadRes(cfg.skinTypePath[self.skinIndex],cc.SpriteFrame,function(err,spriteFrame){
- //var skinsp = skinNode.getChildByName("SkinSp");
- //skinsp.getComponent("cc.Sprite").spriteFrame = spriteFrame;
- var condition1 = skinNode.getChildByName("Condition1");
- var condition2 = skinNode.getChildByName("Condition2");
- var lockerSp = skinNode.getChildByName("LockerSp");
- var playBtn = skinNode.getChildByName("PlayBtn");
- var unlockSp = skinNode.getChildByName("UnlockSp");
- if (this.skinIndex > 0) {
- if (cfg.skinCondition[this.skinIndex].scoreType == 0) {
- var historyScore = UserInfo.getHistoryScore();
- if (historyScore >= cfg.skinCondition[this.skinIndex].score) {
- condition1.active = false;
- condition2.active = false;
- unlockSp.active = true;
- lockerSp.active = false;
- playBtn.active = true;
- } else {
- condition1.active = true;
- condition2.active = false;
- unlockSp.active = false;
- lockerSp.active = true;
- playBtn.active = false;
- condition1.getChildByName("ConditionLabel").getComponent("cc.Label").string = historyScore + "." + cfg.skinCondition[this.skinIndex].score;
- }
- } else if (cfg.skinCondition[this.skinIndex].scoreType == 1) {
- var sumScore = UserInfo.getTotalGold();
- if (sumScore >= cfg.skinCondition[this.skinIndex].score) {
- condition1.active = false;
- condition2.active = false;
- unlockSp.active = true;
- lockerSp.active = false;
- playBtn.active = true;
- } else {
- condition1.active = false;
- condition2.active = true;
- unlockSp.active = false;
- lockerSp.active = true;
- playBtn.active = false;
- condition2.getChildByName("ConditionLabel").getComponent("cc.Label").string = cfg.skinCondition[this.skinIndex].score;
- }
- }
- } else {
- condition1.active = false;
- condition2.active = false;
- lockerSp.active = false;
- playBtn.active = true;
- unlockSp.active = true;
- }
- //})
- },
- onGuideBtn: function (event) {
- this.node.parent.getChildByName("Guide").active = false;
- window.indexBanner.show();
- },
- onNextBtn: function (event, optData) {
- if (optData == "right") {
- this.skinIndex++;
- } else if (optData = "left") {
- this.skinIndex--;
- }
- this.updateSkinNode();
- },
- onHonourBtn: function () {
- },
- onMoreBtn: function (event, optData) {
- if (CC_WECHATGAME) {
- // wx.navigateToMiniProgram({
- // appId: "wx582548bc3a843fed",
- // path: "pages/index",
- // extraData: {
- // fromGame: "budinggame"
- // }
- // })
- objSdk.openGameCenter();
- } else {
- if (this.node.active) {
- this.node.getChildByName("GameStartBtn").getComponent("cc.Button").interactable = (optData == "false");
- this.node.getChildByName("SkinBtn").getComponent("cc.Button").interactable = (optData == "false");
- this.node.getChildByName("HonourBtn").getComponent("cc.Button").interactable = (optData == "false");
- this.node.getChildByName("RankBtn").getComponent("cc.Button").interactable = (optData == "false");
- this.node.getChildByName("MoreBtn").getComponent("cc.Button").interactable = (optData == "false");
- } else {
- this.node.parent.getChildByName("Game").getComponent("GameSence").onMoreGameBtn(optData);
- }
- this.ShareStrategyNode.active = (optData == "true");
- if (optData == "true") {
- this.createTimer();
- }
- }
- },
- //更新share上方的护盾信息
- updateShareBtn() {
- let shield = UserInfo.shield;
- if (shield.count == 0) {
- this.shareNumBg.active = false;
- } else {
- let shareNumSprite = this.shareNumSprite,
- shareLabel = this.shareLabel;
- shareNumSprite.spriteFrame = this.shareNumSprites[shield.count];
- shareLabel.string = shield.count;
- }
- },
- //点击分享按钮,弹出提示
- onShareBtnBefore() {
- this.shareLayer.setLocalZOrder(999999);
- this.shareLayer.active = true;
- this.mask.setLocalZOrder(999998);
- this.mask.active = true;
- },
- //微信分享
- onShareBtn: function () {
- if (CC_WECHATGAME) {
- var data = {
- titleData: UserInfo.shareDesc,
- imgurlData: UserInfo.shareUrl
- }
- WxCommon.ShareAppMessage(data, this, null);
- }
- this.shareLayer.active = false;
- this.mask.active = false;
- },
- createTimer: function () {
- var self = this;
- var updateOtherGames = function () {
- self.otherGamesIndex = (self.otherGamesIndex + 1 + WxCommon.otherGameCount) % WxCommon.otherGameCount;
- self.ShareStrategyNode.getChildByName("OtherGameBtn").getComponent(cc.Sprite).spriteFrame = self.SignAtlas.getSpriteFrame('games' + self.otherGamesIndex);
- self.createTimer();
- };
- if (this.ShareStrategyNode.active == true) {
- this.timerid = setTimeout(updateOtherGames, 2000);
- }
- },
- onShowOtherGame: function () {
- if (CC_WECHATGAME) {
- var path = WxCommon.otherGameMap.get(this.otherGamesIndex);
- WxCommon.PreviewImage(path, this, this.hideNode);
- }
- },
- killTimer: function () {
- if (this.timerid != null) {
- clearTimeout(this.timerid);
- this.timerid = null;
- }
- },
- hideNode: function () {
- if (this.ShareStrategyNode.active) {
- this.killTimer();
- this.ShareStrategyNode.active = false;
- if (this.node.active) {
- this.node.getChildByName("GameStartBtn").getComponent("cc.Button").interactable = true;
- this.node.getChildByName("SkinBtn").getComponent("cc.Button").interactable = true;
- this.node.getChildByName("HonourBtn").getComponent("cc.Button").interactable = true;
- this.node.getChildByName("RankBtn").getComponent("cc.Button").interactable = true;
- this.node.getChildByName("MoreBtn").getComponent("cc.Button").interactable = true;
- } else {
- this.node.parent.getChildByName("Game").getComponent("GameSence").onMoreGameBtn("false");
- }
- }
- },
- onBtnAudio: function () {
- cc.audioEngine.playEffect(this.buttonAudio);
- }
- // update (dt) {},
- });
|