SkillItem.js 21 KB

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