HomeSence.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. var WxCommon = require("../WxCommon")
  2. var cfg = require("Configure")
  3. // @TODO 引入sdk代码
  4. import DwSdk from "../duowansdk/DwSdk";
  5. const debug = false;
  6. const objSdk = new DwSdk("布丁弹一弹", debug);
  7. // 注册sdk的update事件
  8. cc.director.on(cc.Director.EVENT_AFTER_DRAW, objSdk.update);
  9. cc.Class({
  10. extends: cc.Component,
  11. properties: {
  12. gamebgMusic: {
  13. default: null,
  14. type: cc.AudioClip
  15. },
  16. buttonAudio: {
  17. default: null,
  18. type: cc.AudioClip
  19. },
  20. SignAtlas: {
  21. default: null,
  22. type: cc.SpriteAtlas,
  23. },
  24. ShareStrategyNode: { //游戏攻略图片节点
  25. default: null,
  26. type: cc.Node
  27. },
  28. GuideNode: {
  29. default: null,
  30. type: cc.Node
  31. },
  32. shareNumBg: {
  33. default: null,
  34. type: cc.Node
  35. },
  36. shareLayer: {
  37. default: null,
  38. type: cc.Node
  39. },
  40. mask: {
  41. default: null,
  42. type: cc.Node
  43. },
  44. shareNumSprites: [cc.SpriteFrame],
  45. feiendtex: null,
  46. otherGamesIndex: 0,
  47. skinIndex: 0,
  48. showGuideValue: true
  49. },
  50. // LIFE-CYCLE CALLBACKS:
  51. onLoad() {
  52. cc.audioEngine.playMusic(this.gamebgMusic, true);
  53. if (CC_WECHATGAME) {
  54. this.WxLogin();
  55. this.initShare();
  56. // wx.setPreferredFramesPerSecond(30);
  57. }
  58. this.listenGame();
  59. objSdk.showAdIcon(-360, -240, this.node);
  60. //SDK接入
  61. if (CC_WECHATGAME) {
  62. let { screenWidth, screenHeight } = wx.getSystemInfoSync();
  63. let style = {
  64. left: 60,
  65. top: screenHeight - 100,
  66. width: screenWidth - 120,
  67. height: 100
  68. }
  69. if (screenWidth <= 360) {
  70. style.left = (screenWidth - 320) / 2;
  71. }
  72. let indexBannerId = 'adunit-de205564802e4627',
  73. deadBannerId = 'adunit-7a574e7546c7062d',
  74. accountBannerId = 'adunit-1e3576e27d61175a';
  75. window.indexBanner = wx.createBannerAd({
  76. adUnitId: indexBannerId,
  77. style: style
  78. })
  79. indexBanner.onError(err => {
  80. console.log('indexBanner 拉取失败', err)
  81. })
  82. indexBanner.onResize(res => {
  83. console.log(res.width, res.height)
  84. indexBanner.style.left = (screenWidth - res.width) / 2;
  85. indexBanner.style.top = screenHeight - res.height;
  86. })
  87. indexBanner.onLoad(() => {
  88. indexBanner.show()
  89. })
  90. window.deadBanner = wx.createBannerAd({
  91. adUnitId: deadBannerId,
  92. style: style
  93. })
  94. deadBanner.onResize(res => {
  95. deadBanner.style.left = (screenWidth - res.width) / 2;
  96. deadBanner.style.top = screenHeight - res.height;
  97. })
  98. deadBanner.onError(err => {
  99. console.log('deadBanner 拉取失败', err)
  100. })
  101. window.accountBanner = wx.createBannerAd({
  102. adUnitId: accountBannerId,
  103. style: style
  104. })
  105. accountBanner.onResize(res => {
  106. accountBanner.style.left = (screenWidth - res.width) / 2;
  107. accountBanner.style.top = screenHeight - res.height;
  108. })
  109. accountBanner.onError(err => {
  110. console.log('accountBanner 拉取失败', err)
  111. })
  112. } else {
  113. let advers = ['indexBanner', 'deadBanner', 'accountBanner'];
  114. advers.forEach(ad => {
  115. window[ad] = {
  116. show: function () { },
  117. hide: function () { }
  118. }
  119. })
  120. }
  121. },
  122. start() {
  123. this.feiendtex = new cc.Texture2D();
  124. this.PHB = cc.find("Canvas/PHB")
  125. this.rankScene = this.PHB.getComponent("RankScene");
  126. this.rankNode = this.PHB.getChildByName("Rank")
  127. this.myRankNode = this.PHB.getChildByName('MyRank')
  128. this.worldContentNode = this.rankNode.getChildByName('WorldContent')
  129. this.friendContentNode = this.rankNode.getChildByName('FriendContent')
  130. this.shareLabel = this.shareNumBg.getChildByName('text').getComponent(cc.Label);
  131. this.shareNumSprite = this.shareLayer.getChildByName('sharenum').getComponent(cc.Sprite);
  132. //更新护盾信息 后期写到获取到数据的回调里
  133. this.updateShareBtn();
  134. },
  135. WxLogin() {
  136. if (UserInfo.platform != 1) {
  137. return;
  138. }
  139. wx.login({
  140. success: function (res) {
  141. if (res.code) {
  142. var js_code = res.code;
  143. } else {
  144. console.log('登录失败!' + res.errMsg)
  145. }
  146. }
  147. });
  148. },
  149. initShare(data) {
  150. let datavalue = UserInfo.getString('allscore');
  151. let key = UserInfo.getCurWeekScoreValue();
  152. if (UserInfo.IsEmpty(datavalue)) {
  153. UserInfo.setString('allscore', key);
  154. return;
  155. }
  156. let datalist = datavalue.split('*');
  157. let removeIndex = -1;
  158. for (let index = 0; index < datalist.length; ++index) {
  159. if (key == datalist[index]) {
  160. // UserInfo.removeString(datalist[index]);
  161. removeIndex = index;
  162. }
  163. }
  164. if (removeIndex != -1) {
  165. datalist.splice(removeIndex, 1);
  166. }
  167. WxCommon.RemoveUserCloudStorage(datalist, this, this.RemoveScoreKey);
  168. // this.RemoveScoreKey(datalist, true);
  169. },
  170. listenGame() {
  171. if (CC_WECHATGAME) {
  172. // var date = UserInfo.GetNowFormatDate();
  173. this.OnUpdateGame();
  174. WxCommon.SetEnableDebug(false);
  175. WxCommon.OnShow(null, this, null);
  176. WxCommon.ShowShareMenu();
  177. WxCommon.OnShareAppMessage(null, this, this.shareCallback);
  178. WxCommon.GetSystemInfo(null, this, null);
  179. }
  180. },
  181. OnUpdateGame() {
  182. const updateManager = wx.getUpdateManager()
  183. updateManager.onCheckForUpdate(function (res) {
  184. // 请求完新版本信息的回调
  185. console.log(res.hasUpdate)
  186. })
  187. updateManager.onUpdateReady(function () {
  188. wx.showModal({
  189. title: '更新提示',
  190. content: '新版本已经准备好,是否重启应用?',
  191. success: function (res) {
  192. if (res.confirm) {
  193. // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
  194. updateManager.applyUpdate()
  195. }
  196. }
  197. })
  198. })
  199. updateManager.onUpdateFailed(function () {
  200. // 新的版本下载失败
  201. })
  202. },
  203. onRankBtn: function (event, optData) {
  204. console.log('onrankbtn:', optData)
  205. if (UserInfo.platform != 1 || !cc.vv.isRank) {
  206. return;
  207. }
  208. wx.postMessage({
  209. method: 'showFriendRank',
  210. data: optData
  211. });
  212. cc.find("Canvas/PHB").getChildByName("BtnPHBClose").active = (optData == "true");
  213. cc.find("Canvas/PHB").getChildByName("emptyBtn").active = (optData == "true");
  214. cc.find("Canvas/PHB").getChildByName("bg").active = (optData == "true");
  215. if (this.node.active) {
  216. if ("true" == optData) {
  217. window.indexBanner.hide();
  218. this.rankScene.status = 1;
  219. this.showRank();
  220. }
  221. if ("false" == optData) {
  222. window.indexBanner.show();
  223. wx.postMessage({
  224. method: 'hideRank',
  225. data: optData
  226. });
  227. cc.find("Canvas/PHB").active = false;
  228. }
  229. } else {
  230. this.rankScene.status = 3;
  231. window.accountBanner.show();
  232. }
  233. },
  234. showRank() {
  235. if (UserInfo.platform == 1 && cc.vv.isRank) {
  236. this.myRankNode.active = false;
  237. this.rankNode.active = true;
  238. if (this.rankScene.status == 1) {
  239. let openDataContext = wx.getOpenDataContext();
  240. let sharedCanvas = openDataContext.canvas;
  241. this.feiendtex.initWithElement(sharedCanvas);
  242. this.feiendtex.handleLoadedTexture();
  243. this.friendContentNode.getComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(this.feiendtex);
  244. var scale = Math.min(750 / this.feiendtex.width, 1334 / this.feiendtex.height);
  245. this.friendContentNode.setContentSize(this.feiendtex.width * scale, this.feiendtex.height * scale);
  246. }
  247. this.PHB.active = true;
  248. }
  249. },
  250. update: function () {
  251. if (cc.find("Canvas/PHB").active) {
  252. this.showRank();
  253. }
  254. },
  255. onGameStartBtn: function () {
  256. this.node.parent.getChildByName("SkinNode").active = false;
  257. window.indexBanner.hide();
  258. if (this.showGuide()) {
  259. this.GuideNode.active = true;
  260. } else {
  261. if (this.skinIndex != UserInfo.getSkinIndex()) {
  262. UserInfo.setSkinIndex(this.skinIndex);
  263. }
  264. if (this.node.active) {
  265. cc.audioEngine.stopMusic(this.gamebgMusic);
  266. this.node.parent.getComponent("SenceManager").enterSence("Home", "Game");
  267. } else {
  268. this.node.parent.getChildByName("Game").getComponent("GameSence").onRestartBtn();
  269. }
  270. }
  271. },
  272. showGuide() {
  273. if (!this.showGuideValue) return false;
  274. console.log('show guide');
  275. var getStorage = function (key) {
  276. return window.wx ? window.wx.getStorageSync(key) : localStorage.getItem(key);
  277. }
  278. var setStorage = function (key, value) {
  279. return window.wx ? window.wx.setStorageSync(key, value) : localStorage.setItem(key, value);
  280. }
  281. var key = 'guideTime';
  282. var guideTime = getStorage(key);
  283. var now = new Date();
  284. var year = now.getFullYear();
  285. var month = now.getMonth() + 1;
  286. var date = now.getDate();
  287. var guideTimeValue = [year, month, date].join('/');
  288. if (!guideTime || guideTime != guideTimeValue) {
  289. setStorage(key, guideTimeValue);
  290. return true;
  291. } else {
  292. this.GuideNode && this.GuideNode.destroy();
  293. this.showGuideValue = false;
  294. return false;
  295. }
  296. },
  297. onSkinBtn: function (event, optData) {
  298. if (optData == "true") {
  299. this.node.parent.getChildByName("SkinNode").active = true;
  300. this.skinIndex = UserInfo.getSkinIndex();
  301. var sumScoreLabel = this.node.parent.getChildByName("SkinNode").getChildByName("SumScoreSp").getChildByName("SumScoreLabel");
  302. sumScoreLabel.getComponent("cc.Label").string = UserInfo.getTotalGold();
  303. this.updateSkinNode();
  304. window.indexBanner.hide();
  305. } else if (optData == "false") {
  306. this.node.parent.getChildByName("SkinNode").active = false;
  307. window.indexBanner.show();
  308. }
  309. },
  310. updateSkinNode: function () {
  311. var skinNode = this.node.parent.getChildByName("SkinNode");
  312. if (this.skinIndex == 0) {
  313. skinNode.getChildByName("RightBtn").active = true;
  314. skinNode.getChildByName("LeftBtn").active = false;
  315. } else if (this.skinIndex == cfg.skinTypePath.length - 1) {
  316. skinNode.getChildByName("RightBtn").active = false;
  317. skinNode.getChildByName("LeftBtn").active = true;
  318. } else {
  319. skinNode.getChildByName("RightBtn").active = true;
  320. skinNode.getChildByName("LeftBtn").active = true;
  321. }
  322. var skSpine = skinNode.getChildByName("skSprite");
  323. skSpine.getComponent("sp.Skeleton").setSkin("p" + (this.skinIndex + 1));
  324. //var self = this;
  325. //cc.loader.loadRes(cfg.skinTypePath[self.skinIndex],cc.SpriteFrame,function(err,spriteFrame){
  326. //var skinsp = skinNode.getChildByName("SkinSp");
  327. //skinsp.getComponent("cc.Sprite").spriteFrame = spriteFrame;
  328. var condition1 = skinNode.getChildByName("Condition1");
  329. var condition2 = skinNode.getChildByName("Condition2");
  330. var lockerSp = skinNode.getChildByName("LockerSp");
  331. var playBtn = skinNode.getChildByName("PlayBtn");
  332. var unlockSp = skinNode.getChildByName("UnlockSp");
  333. if (this.skinIndex > 0) {
  334. if (cfg.skinCondition[this.skinIndex].scoreType == 0) {
  335. var historyScore = UserInfo.getHistoryScore();
  336. if (historyScore >= cfg.skinCondition[this.skinIndex].score) {
  337. condition1.active = false;
  338. condition2.active = false;
  339. unlockSp.active = true;
  340. lockerSp.active = false;
  341. playBtn.active = true;
  342. } else {
  343. condition1.active = true;
  344. condition2.active = false;
  345. unlockSp.active = false;
  346. lockerSp.active = true;
  347. playBtn.active = false;
  348. condition1.getChildByName("ConditionLabel").getComponent("cc.Label").string = historyScore + "." + cfg.skinCondition[this.skinIndex].score;
  349. }
  350. } else if (cfg.skinCondition[this.skinIndex].scoreType == 1) {
  351. var sumScore = UserInfo.getTotalGold();
  352. if (sumScore >= cfg.skinCondition[this.skinIndex].score) {
  353. condition1.active = false;
  354. condition2.active = false;
  355. unlockSp.active = true;
  356. lockerSp.active = false;
  357. playBtn.active = true;
  358. } else {
  359. condition1.active = false;
  360. condition2.active = true;
  361. unlockSp.active = false;
  362. lockerSp.active = true;
  363. playBtn.active = false;
  364. condition2.getChildByName("ConditionLabel").getComponent("cc.Label").string = cfg.skinCondition[this.skinIndex].score;
  365. }
  366. }
  367. } else {
  368. condition1.active = false;
  369. condition2.active = false;
  370. lockerSp.active = false;
  371. playBtn.active = true;
  372. unlockSp.active = true;
  373. }
  374. //})
  375. },
  376. onGuideBtn: function (event) {
  377. this.node.parent.getChildByName("Guide").active = false;
  378. window.indexBanner.show();
  379. },
  380. onNextBtn: function (event, optData) {
  381. if (optData == "right") {
  382. this.skinIndex++;
  383. } else if (optData = "left") {
  384. this.skinIndex--;
  385. }
  386. this.updateSkinNode();
  387. },
  388. onHonourBtn: function () {
  389. },
  390. onMoreBtn: function (event, optData) {
  391. if (CC_WECHATGAME) {
  392. // wx.navigateToMiniProgram({
  393. // appId: "wx582548bc3a843fed",
  394. // path: "pages/index",
  395. // extraData: {
  396. // fromGame: "budinggame"
  397. // }
  398. // })
  399. objSdk.openGameCenter();
  400. } else {
  401. if (this.node.active) {
  402. this.node.getChildByName("GameStartBtn").getComponent("cc.Button").interactable = (optData == "false");
  403. this.node.getChildByName("SkinBtn").getComponent("cc.Button").interactable = (optData == "false");
  404. this.node.getChildByName("HonourBtn").getComponent("cc.Button").interactable = (optData == "false");
  405. this.node.getChildByName("RankBtn").getComponent("cc.Button").interactable = (optData == "false");
  406. this.node.getChildByName("MoreBtn").getComponent("cc.Button").interactable = (optData == "false");
  407. } else {
  408. this.node.parent.getChildByName("Game").getComponent("GameSence").onMoreGameBtn(optData);
  409. }
  410. this.ShareStrategyNode.active = (optData == "true");
  411. if (optData == "true") {
  412. this.createTimer();
  413. }
  414. }
  415. },
  416. //更新share上方的护盾信息
  417. updateShareBtn() {
  418. let shield = UserInfo.shield;
  419. if (shield.count == 0) {
  420. this.shareNumBg.active = false;
  421. } else {
  422. let shareNumSprite = this.shareNumSprite,
  423. shareLabel = this.shareLabel;
  424. shareNumSprite.spriteFrame = this.shareNumSprites[shield.count];
  425. shareLabel.string = shield.count;
  426. }
  427. },
  428. //点击分享按钮,弹出提示
  429. onShareBtnBefore() {
  430. this.shareLayer.setLocalZOrder(999999);
  431. this.shareLayer.active = true;
  432. this.mask.setLocalZOrder(999998);
  433. this.mask.active = true;
  434. },
  435. //微信分享
  436. onShareBtn: function () {
  437. if (CC_WECHATGAME) {
  438. var data = {
  439. titleData: UserInfo.shareDesc,
  440. imgurlData: UserInfo.shareUrl
  441. }
  442. WxCommon.ShareAppMessage(data, this, null);
  443. }
  444. this.shareLayer.active = false;
  445. this.mask.active = false;
  446. },
  447. createTimer: function () {
  448. var self = this;
  449. var updateOtherGames = function () {
  450. self.otherGamesIndex = (self.otherGamesIndex + 1 + WxCommon.otherGameCount) % WxCommon.otherGameCount;
  451. self.ShareStrategyNode.getChildByName("OtherGameBtn").getComponent(cc.Sprite).spriteFrame = self.SignAtlas.getSpriteFrame('games' + self.otherGamesIndex);
  452. self.createTimer();
  453. };
  454. if (this.ShareStrategyNode.active == true) {
  455. this.timerid = setTimeout(updateOtherGames, 2000);
  456. }
  457. },
  458. onShowOtherGame: function () {
  459. if (CC_WECHATGAME) {
  460. var path = WxCommon.otherGameMap.get(this.otherGamesIndex);
  461. WxCommon.PreviewImage(path, this, this.hideNode);
  462. }
  463. },
  464. killTimer: function () {
  465. if (this.timerid != null) {
  466. clearTimeout(this.timerid);
  467. this.timerid = null;
  468. }
  469. },
  470. hideNode: function () {
  471. if (this.ShareStrategyNode.active) {
  472. this.killTimer();
  473. this.ShareStrategyNode.active = false;
  474. if (this.node.active) {
  475. this.node.getChildByName("GameStartBtn").getComponent("cc.Button").interactable = true;
  476. this.node.getChildByName("SkinBtn").getComponent("cc.Button").interactable = true;
  477. this.node.getChildByName("HonourBtn").getComponent("cc.Button").interactable = true;
  478. this.node.getChildByName("RankBtn").getComponent("cc.Button").interactable = true;
  479. this.node.getChildByName("MoreBtn").getComponent("cc.Button").interactable = true;
  480. } else {
  481. this.node.parent.getChildByName("Game").getComponent("GameSence").onMoreGameBtn("false");
  482. }
  483. }
  484. },
  485. onBtnAudio: function () {
  486. cc.audioEngine.playEffect(this.buttonAudio);
  487. }
  488. // update (dt) {},
  489. });