HomeSence.js 18 KB

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