SkillItem.js 18 KB

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