SkillItem.js 17 KB

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