SkillItem.js 18 KB

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