SkillItem.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. const SkillApi = require('../net/SkillApi');
  2. const DWTool = require("../utils/DWTool");
  3. const GameModule = require("../utils/GameModule");
  4. const buildingLevel = require('../data/buildingLevel');
  5. const {GameNotificationKey, WechatShareType} = require('../utils/GameEnum');
  6. const WeChat = require('../net/WeChat');
  7. const TapTapTool = require("../utils/TapTapTool");
  8. const AlertManager = require('../utils/AlertManager');
  9. const ADVideo = require('../utils/ADVideo');
  10. const HomeApi = require('../net/HomeApi');
  11. // const GameRedDot = require('../utils/GameEnum').GameRedDot;
  12. // WeChat
  13. cc.Class({
  14. extends: cc.Component,
  15. properties: {
  16. userSkillButton: {
  17. tooltip: '使用技能button',
  18. default: null,
  19. type: cc.Button
  20. },
  21. titleRichText: {
  22. tooltip: 'title啦',
  23. default: null,
  24. type: cc.RichText
  25. },
  26. skillIconBg: {
  27. tooltip: '技能icon的背景',
  28. default: null,
  29. type: cc.Sprite
  30. },
  31. ///使用技能的按钮背景图片
  32. useSkillButtonBgSprite: cc.Sprite,
  33. /// 使用技能的text
  34. skillUseTitleRichText: cc.RichText,
  35. /// 描述text
  36. subTitleRichText: cc.RichText,
  37. ///技能icon
  38. skillIcon: cc.Sprite,
  39. ///总部大楼特有,等级 / 25
  40. fillBuldingNode: cc.Node,
  41. fillBuldingSprite: cc.Sprite,
  42. lightSpriteFrame: cc.SpriteFrame,
  43. graySpriteFrame: cc.SpriteFrame,
  44. /// 里程碑奖励显示的button
  45. awardButton: cc.Button,
  46. /// 升级技能的弹窗
  47. alertNode: cc.Node,
  48. ///砖石数
  49. diamondCount: 0,
  50. freeSkillNode: cc.Node,
  51. freeSkillTitleRichText: cc.RichText,
  52. /// 金币数
  53. coinCount: 0,
  54. building: 0,
  55. _awardCount: 0,
  56. _isAlert: false,
  57. /// 0说明是用金币 1 说明是用分享 2 看广告
  58. _adState: 0,
  59. //// 当前使用的广告状态
  60. // _useAdState: 0,
  61. _isInitAd: false
  62. },
  63. // onLoad () {
  64. // },
  65. start () {
  66. this._isAction = false;
  67. },
  68. onDestroy() {
  69. GameEvent.off("skill3_use_begain", this);
  70. GameEvent.off(GameNotificationKey.SkillThreeHasDone, this);
  71. GameEvent.off("skill_five_use", this);
  72. GameEvent.off(GameNotificationKey.UpBuildingLevel, this);
  73. GameEvent.off(GameNotificationKey.ShowShareAction, this);
  74. // GameEvent.off(GameNotificationKey.AdBuildingStateUpdate, this);
  75. },
  76. update (dt) {
  77. /// 如果说是建筑 并且没有处于响应
  78. if (this.fillBuldingNode.active && this._isAction === false) {
  79. let isCanUse = this.judgeIsCanUse();
  80. if (CC_WECHATGAME) {
  81. let isAd = DWTool.checkIsOldUser() && Global._upBuildingInfo.maxCount > Global._upBuildingInfo.count && Global._upBuildingInfo.cdTime <= 0;
  82. /// 如果有广告或者分享
  83. if (!this._isInitAd && isAd) {
  84. this._isInitAd = true;
  85. this.initBuildingAd();
  86. }
  87. if (isAd && !isCanUse) {
  88. this.freeSkillNode.active = true;
  89. this.userSkillButton.node.active = false;
  90. } else {
  91. this.freeSkillNode.active = false;
  92. this.userSkillButton.node.active = true;
  93. }
  94. }
  95. let inter = this.userSkillButton.interactable;
  96. if (isCanUse != inter) {
  97. this.setupGrayBg(!inter);
  98. }
  99. }
  100. },
  101. /// 初始化第一个建筑相关的数据
  102. initBuilding(awardCount) {
  103. this.initAward(awardCount)
  104. this.userSkillButton.interactable = false;
  105. this.updateBuildingData();
  106. /// 第三个技能开始使用
  107. GameEvent.on("skill3_use_begain", this, () => {
  108. this.updateBuildingData();
  109. });
  110. /// 第三个技能结束
  111. GameEvent.on(GameNotificationKey.SkillThreeHasDone, this, () => {
  112. this.updateBuildingData();
  113. });
  114. ////第五个技能开始使用
  115. GameEvent.on("skill_five_use", this, () => {
  116. this.updateBuildingData();
  117. });
  118. // /// cd时间到了
  119. // GameEvent.on(GameNotificationKey.AdBuildingStateUpdate, this, () => {
  120. // this.initBuildingAd();
  121. // });
  122. GameEvent.on(GameNotificationKey.AdUpdateStateNotification, this, (adState, callBack) => {
  123. /// 说明是技能的关闭状态
  124. if (adState === 3 && callBack === 'skill') {
  125. this.finishAction();
  126. }
  127. if (adState === 0 || adState === 1) {
  128. this.initBuildingAd();
  129. }
  130. });
  131. GameModule.homeGuide.on('Fire_state16', this.awardBtnAction, this);
  132. // let buildingLevelInfo = buildingLevel[buildingInfo.buildingLevel - 1];
  133. },
  134. initBuildingAd() {
  135. if (!CC_WECHATGAME) {
  136. return;
  137. }
  138. //// 说明有广告
  139. if (Global._adVideoState == 0) {
  140. this._adState = 2;
  141. this.freeSkillTitleRichText.string = '<color=#ffffff> 看视频\n免费升级' + '</c>';
  142. } else if (Global._adVideoState === 1) {
  143. this._adState = 1;
  144. this.freeSkillTitleRichText.string = '<color=#ffffff> 分享\n免费升级' + '</c>';
  145. }
  146. },
  147. finishAction() {
  148. HomeApi.reportInformation(1, () => {
  149. Global._upBuildingInfo.count += 1;
  150. /// 时间重置为5分钟
  151. Global._upBuildingInfo.cdTime = 5 * 60 * 1000;
  152. this.requestUpBuildingLevel(true);
  153. }, (code, msg) => {
  154. console.log(msg);
  155. })
  156. },
  157. //// 初始化技能数据
  158. init(skillData) {
  159. this.fillBuldingNode.active = false;
  160. this.freeSkillNode.active = false;
  161. this.awardButton.node.active = false;
  162. this.loadSpriteFrame(skillData);
  163. this.skillData = skillData;
  164. this.updateData();
  165. this.setupMaxLevel();
  166. /// 更新技能相关
  167. GameEvent.on('skill_update', this, () => {
  168. this.updateData();
  169. this.setupMaxLevel();
  170. });
  171. },
  172. /// 初始化领取里程碑奖励数据
  173. initAward(awardCount) {
  174. this._awardCount = awardCount;
  175. let needAwardCount = parseInt(GameModule.userInfo.buildingLevel / 25);
  176. if (needAwardCount > this._awardCount) {
  177. this.awardButton.node.active = true;
  178. } else {
  179. this.awardButton.node.active = false;
  180. }
  181. },
  182. /// 更新建筑相关数据
  183. updateBuildingData() {
  184. let currentLevel = GameModule.userInfo.buildingLevel;
  185. let upGet = GameModule.userInfo.perpetualClickMt;
  186. this._upGet = upGet;
  187. if (currentLevel <= 25) {
  188. let buildingObj = buildingLevel.find(n => {
  189. return n.level == currentLevel;
  190. });
  191. this._upGold = {'n': buildingObj.upGold, 'e': 0};
  192. } else {
  193. let mul = TapTapTool.toPower(currentLevel - 26);
  194. this._upGold = TapTapTool.multiple({'n': 1056, 'e': 0}, mul);
  195. }
  196. this.skillUseTitleRichText.string = '<color=#ffffff>+' + TapTapTool.parseToString(this._upGet) + '收益</c>' + `<br/><img src='coin_small'/><color=#ffffff>${TapTapTool.parseToString(this._upGold)}</c>`;
  197. // this.skillUseTitleRichText.string = '<color=#ffffff>+' + TapTapTool.parseToString(upGet) + '收益</c>' + `<br/><img src='coin_small'/><color=#ffffff>${TapTapTool.parseToString(this._upGold)}</c>`;
  198. this.fillBuldingNode.active = true;
  199. this.fillBuldingSprite.fillRange = (currentLevel % 25) / 25;
  200. this.diamondCount = 0;
  201. this.coinCount = this._upGold;
  202. this.building = 0;
  203. this.titleRichText.string = '<color=#df5400>等级' + currentLevel + '</c>' + '<color=#1c1c1c>' + ' 总部大楼' + '</c>';
  204. this.subTitleRichText.string = `<img src='skill_click_coin'/><color=#df5400> ${TapTapTool.parseToString(GameModule.userInfo.coinTap)}收益</c><color=#1c1c1c>/点击</c>`;
  205. },
  206. /// 更新技能相关数据
  207. updateData() {
  208. let levelInfo = Global.BuildingManager.getSkillLevelInfo(this.skillData.skillId, this.skillData.skillLevel);
  209. let skillInfo = Global.BuildingManager.getSkillInfo(this.skillData.skillId);
  210. this._skillInfo = skillInfo;
  211. //// 如果是没有拥有的
  212. if (this.skillData.isHave == 0) {
  213. /// 说明是用其它的来解锁
  214. if (skillInfo.buildingLevel == 0) {
  215. /// 用的砖石 技能4是用砖石或者金币来解锁的
  216. this.showBuySkill();
  217. } else {
  218. /// 说明总部等级不够,还没有解锁
  219. if (GameModule.userInfo.buildingLevel < skillInfo.buildingLevel) {
  220. this.coinCount = {'n': 0, 'e': 0};
  221. this.diamondCount = 0;
  222. this.building = skillInfo.buildingLevel;
  223. this.skillUseTitleRichText.string = "<img src='skill_lock'/><br/><color=#ffffff>总部大楼等级" + skillInfo.buildingLevel + '</c>';
  224. //// 如果是因为建筑锁住的
  225. this._upNoticefacation = false;
  226. GameEvent.on(GameNotificationKey.UpBuildingLevel, this, () => {
  227. if (!this._upNoticefacation && GameModule.userInfo.buildingLevel >= this.building && this.skillData.isHave == 0) {
  228. /// 实时技能直接拥有
  229. this._upNoticefacation = true;
  230. if (this.skillData.skillId < 4) {
  231. /// 直接解锁它
  232. this.unlockedSkill();
  233. } else {
  234. this.showBuySkill();
  235. this.setupGrayBg(this.judgeIsCanUse());
  236. }
  237. }
  238. });
  239. /// 那就需要去购买了
  240. } else {
  241. this.showBuySkill();
  242. }
  243. }
  244. } else {
  245. this.userSkillButton.interactable = true;
  246. let diamondCount = levelInfo.bonus[10002];
  247. /// 用的砖石
  248. if (diamondCount != undefined) {
  249. this.diamondCount = diamondCount;
  250. this.coinCount = {'n': 0, 'e': 0};
  251. this.building = 0;
  252. this.skillUseTitleRichText.string = `<color=#ffffff>升级</c><br/><img src='skill_diamond'/><color=#ffffff>${diamondCount}钻石</c>`;
  253. }
  254. }
  255. this.setupGrayBg(this.judgeIsCanUse());
  256. if (this.skillData.skillLevel > 0) {
  257. this.titleRichText.string = '<color=#df5400>等级' + + this.skillData.skillLevel + ' </c>' + '<color=#1c1c1c>' + skillInfo.name + '</c>';
  258. } else {
  259. this.titleRichText.string = '<color=#df5400>技能' + ' </c>' + '<color=#1c1c1c>' + skillInfo.name + '</c>';
  260. }
  261. if (levelInfo == undefined) {
  262. this.subTitleRichText.string = '<color=#1c1c1c>' + skillInfo.desc + '</c>';
  263. } else {
  264. this.subTitleRichText.string = '<color=#1c1c1c>' + levelInfo.desc + '</c>';
  265. }
  266. },
  267. /// 设置使用技能的背景
  268. setupGrayBg(isActive) {
  269. this.userSkillButton.interactable = isActive;
  270. this.useSkillButtonBgSprite.spriteFrame = isActive ? this.lightSpriteFrame : this.graySpriteFrame;
  271. },
  272. //// 设置技能达到最大值的时候
  273. setupMaxLevel() {
  274. if (this.skillData.skillLevel >= this.skillData.maxLevel) {
  275. this.userSkillButton.interactable = false;
  276. this.skillUseTitleRichText.node.active = false;
  277. DWTool.loadResSpriteFrame('./textures/skill/skill_max')
  278. .then((spriteFrame) => {
  279. this.useSkillButtonBgSprite.spriteFrame = spriteFrame;
  280. });
  281. }
  282. },
  283. /// 加载技能icon图片
  284. loadSpriteFrame(skillData) {
  285. if (skillData.skillId <= 3) {
  286. let bgPath = './textures/skill/400' + skillData.skillId + skillData.skillId;
  287. DWTool.loadResSpriteFrame(bgPath)
  288. .then((spriteFrame) => {
  289. this.skillIconBg.spriteFrame = spriteFrame;
  290. });
  291. }
  292. let iconPath = './textures/skill/4000' + skillData.skillId;
  293. DWTool.loadResSpriteFrame(iconPath)
  294. .then((spriteFrame) => {
  295. this.skillIcon.spriteFrame = spriteFrame;
  296. });
  297. },
  298. ///////////// ******* 网络请求 ****** //////
  299. //// 购买技能
  300. buySkill(skillId) {
  301. return new Promise((resolve, reject) => {
  302. // 获取目标用户的建筑
  303. SkillApi.buySkill(skillId, (respondData) => {
  304. resolve(respondData);
  305. }, (code, msg) => {
  306. reject({code, msg});
  307. });
  308. });
  309. },
  310. /// 升级建筑
  311. upBuildingLevel(upLevel) {
  312. return new Promise((resolve, reject) => {
  313. // 获取目标用户的建筑
  314. SkillApi.upBuildingLevel(upLevel, (respondData) => {
  315. resolve(respondData);
  316. }, (code, msg) => {
  317. reject({code, msg});
  318. });
  319. });
  320. },
  321. /// 上报里程碑奖励
  322. report() {
  323. return new Promise((resolve, reject) => {
  324. // 获取目标用户的建筑
  325. SkillApi.report(3, (respondData) => {
  326. resolve(respondData);
  327. }, (code, msg) => {
  328. reject({code, msg});
  329. });
  330. });
  331. },
  332. ///////////******* 技能升级购买相关 *********/////////////////////////
  333. //// 购买或者升级技能
  334. useSkillButtonAction() {
  335. GameModule.audioMng.playClickButton();
  336. this.userSkillButton.interactable = false;
  337. /// 如果是升级建筑
  338. if (this.fillBuldingNode.active) {
  339. this.upBuildingLevelAction();
  340. } else {
  341. this.skillAction();
  342. }
  343. },
  344. /// 免费升级技能的按钮
  345. freeUpSKillAction() {
  346. GameModule.audioMng.playClickButton();
  347. if (this._adState == 1) {
  348. GameEvent.on(GameNotificationKey.ShowShareAction, this, (type, isOk) => {
  349. if (type == WechatShareType.UpBuilding) {
  350. if (isOk) {
  351. this.finishAction();
  352. }
  353. GameEvent.off(GameNotificationKey.ShowShareAction, this);
  354. }
  355. });
  356. WeChat.shareAction(WechatShareType.UpBuilding, () => {
  357. }, () => {
  358. console.log('分享失败或取消');
  359. });
  360. /// 如果是看视频
  361. } else {
  362. Global._adVideo.showVideo('skill');
  363. }
  364. },
  365. upBuildingLevelAction() {
  366. this._isAction = true;
  367. this.requestUpBuildingLevel(false);
  368. },
  369. requestUpBuildingLevel(isFree) {
  370. this.upBuildingLevel(1).then((respondData) => {
  371. this.userSkillButton.interactable = true;
  372. this._isAction = false;
  373. // Global.userData.buildingLevel = respondData.level;
  374. GameModule.userInfo.buildingLevel = respondData.level;
  375. if (!isFree) {
  376. if (GameModule.userInfo.buildingLevel > 5) {
  377. this.showSkillBuidingAlert(respondData.gold10, respondData.gold100);
  378. }
  379. GameModule.userInfo.gold = TapTapTool.sub(GameModule.userInfo.gold, this._upGold);
  380. }
  381. this.initAward(this._awardCount);
  382. GameEvent.fire(GameNotificationKey.UpBuildingLevel);
  383. Global._gold10 = respondData.gold10;
  384. this.updateBuildingData();
  385. }).catch(({code, msg}) => {
  386. Global.commonAlert.showCommonErrorAlert(msg);
  387. console.log(code, msg);
  388. this._isAction = false;
  389. this.userSkillButton.interactable = true;
  390. });
  391. },
  392. ///点击按钮之后 技能相关的响应
  393. skillAction() {
  394. /// 那就是解锁啦
  395. let skillId = this.skillData.skillId;
  396. /// 说明是购买技能
  397. if (this.skillData.isHave == 0 && skillId > 3) {
  398. /// 购买成功
  399. this.buySkill(skillId).then(() => {
  400. this.updateGloabData();
  401. this.buyFixSkill();
  402. GameEvent.fire('skill_update');
  403. this.userSkillButton.interactable = true;
  404. console.log("技能购买成功");
  405. }).catch(({code, msg}) => {
  406. console.log(code, msg);
  407. Global.commonAlert.showCommonErrorAlert(msg);
  408. this.userSkillButton.interactable = true;
  409. });
  410. /// 升级技能
  411. } else {
  412. let levelInfo = Global.BuildingManager.getSkillLevelInfo(this.skillData.skillId, this.skillData.skillLevel);
  413. levelInfo.name = this._skillInfo.name;
  414. let nextInfo = Global.BuildingManager.getSkillLevelInfo(this.skillData.skillId, this.skillData.skillLevel + 1);
  415. nextInfo.name = this._skillInfo.name;
  416. AlertManager.showSkillBuyAlert(levelInfo, nextInfo, this);
  417. this.userSkillButton.interactable = true;
  418. }
  419. },
  420. //// 购买永久技能
  421. buyFixSkill() {
  422. this.skillData.skillLevel = 1;
  423. this.skillData.isHave = 1;
  424. let skillId = this.skillData.skillId;
  425. /// 如果升级永久技能
  426. let skillLevelInfo = Global.BuildingManager.getSkillLevelInfo(skillId, this.skillData.skillLevel);
  427. GameEvent.fire(GameNotificationKey.UpdateFixationSkill, skillLevelInfo);
  428. if (skillId == 5) {
  429. GameEvent.fire("skill_five_use");
  430. } else if (skillId == 6) {
  431. Global.rcdSkillLevel = 1;
  432. GameEvent.fire("skill_six_use");
  433. }
  434. let iconPath = './textures/skill/4000' + skillId;
  435. AlertManager.showCommonAlert(iconPath, this._skillInfo.desc, this._skillInfo.name);
  436. },
  437. /// 显示购买技能
  438. showBuySkill() {
  439. // let levelInfo = Global.BuildingManager.getSkillLevelInfo(this.skillData.skillId, 1);
  440. /// 使用金币升级
  441. let diamondCount = this._skillInfo.bonus[10002];
  442. /// 用的砖石
  443. if (diamondCount != undefined) {
  444. this.diamondCount = diamondCount;
  445. this.coinCount = {'n': 0, 'e': 0};
  446. this.building = 0;
  447. this.skillUseTitleRichText.string = `<color=#ffffff>购买</c><br/><img src='skill_diamond'/><color=#ffffff>${diamondCount}钻石</c>`;
  448. }
  449. },
  450. /// 每次升级需要弹窗
  451. showSkillBuidingAlert (gold10, gold100) {
  452. this.unschedule(this.hideBuildingAlert, this);
  453. this.scheduleOnce(this.hideBuildingAlert, 2);
  454. this.alertNode.getComponent('SkillBuildingAlert').updateData(gold10, gold100, this);
  455. this.alertNode.active = true;
  456. },
  457. hideBuildingAlert () {
  458. this.alertNode.active = false;
  459. },
  460. hiddenSkillBuidingAlert(gold10, gold100) {
  461. this.initAward(this._awardCount);
  462. this.updateBuildingData();
  463. this.showSkillBuidingAlert(gold10, gold100);
  464. },
  465. /// 解锁某一个技能
  466. unlockedSkill() {
  467. let skillId = this.skillData.skillId;
  468. this.skillData.isHave = 1;
  469. this.skillData.skillLevel = 1;
  470. this.updateData()
  471. if (skillId <= 3) {
  472. GameEvent.fire("TopSkill_unLocked", skillId);
  473. }
  474. console.log("解锁技能成功");
  475. },
  476. //// 里程碑奖励action
  477. awardBtnAction() {
  478. this.awardButton.interactable = false;
  479. GameModule.audioMng.playClickButton();
  480. this.report().then((respondData) => {
  481. this.awardButton.interactable = true;
  482. this._awardCount = respondData.awardCount;
  483. let needAwardCount = parseInt(GameModule.userInfo.buildingLevel / 25);
  484. this.awardButton.node.active = needAwardCount > this._awardCount;
  485. GameModule.userInfo.perpetualClickMt = TapTapTool.multiple(GameModule.userInfo.perpetualClickMt, {'e': 0, 'n': 2});
  486. GameEvent.fire(GameNotificationKey.UpBuildingLevel);
  487. GameEvent.fire(GameNotificationKey.GameShowAdditionTips,"总部大楼",0);
  488. this.updateBuildingData();
  489. let objct = {'cdTime': -6 * 1000, 'infoDesc': '点击提升2倍的金币产出', 'icon': 900001};
  490. Global._fixInformations.push(objct);
  491. GameEvent.fire(GameNotificationKey.GameUpdateMessageList, 1, false);
  492. console.log("领取里程碑奖励成功");
  493. }).catch(({code, msg}) => {
  494. console.log(code, msg);
  495. this.awardButton.interactable = true;
  496. });
  497. },
  498. /// 判断本地是否是解锁状态
  499. judgeIsCanUse() {
  500. if (this.coinCount.n > 0 && TapTapTool.compare(GameModule.userInfo.gold, this.coinCount)) {
  501. return true;
  502. } else if (this.diamondCount > 0 && GameModule.userInfo.diamond >= this.diamondCount) {
  503. return true;
  504. } else if (this.building > 0 && GameModule.userInfo.buildingLevel >= this.building) {
  505. return true;
  506. } else {
  507. return false;
  508. }
  509. },
  510. ///更新全局数据
  511. updateGloabData() {
  512. if (this.coinCount.n > 0) {
  513. GameModule.userInfo.gold = TapTapTool.sub(GameModule.userInfo.gold, this.coinCount);
  514. } else if (this.diamondCount > 0) {
  515. GameModule.userInfo.diamond -= this.diamondCount;
  516. }
  517. },
  518. });