SkillTopItem.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. const DWTool = require("../utils/DWTool");
  2. const SkillApi = require('../net/SkillApi');
  3. const GameModule = require("../utils/GameModule");
  4. const {GameNotificationKey, WechatShareType } = require('../utils/GameEnum');
  5. const WeChat = require('../net/WeChat');
  6. const TapTapTool = require("../utils/TapTapTool");
  7. const AlertManager = require('../utils/AlertManager');
  8. // const GameRedDot = require('../utils/GameEnum').GameRedDot;
  9. // const ADVideo = require('../utils/ADVideo');
  10. const SkillState = cc.Enum({
  11. /// 使用中
  12. usingState: 1,
  13. /// 技能cd状态
  14. cdState: 2,
  15. /// 技能可以使用的状态
  16. canUseState: 3,
  17. /// 技能锁住的状态
  18. lockedState: 4
  19. });
  20. cc.Class({
  21. extends: cc.Component,
  22. properties: {
  23. titleRichText: {
  24. tooltip: '技能时间time ,锁住状态的text',
  25. default: null,
  26. type: cc.RichText
  27. },
  28. skillBg: {
  29. tooltip: '技能的黑色遮罩',
  30. default: null,
  31. type: cc.Sprite
  32. },
  33. skillBgOutSide: {
  34. tooltip: '技能的黑色外遮罩,这里只有是三个哦,最后一个刷新技能的是没有的',
  35. default: null,
  36. type: cc.Sprite
  37. },
  38. skillBgBottom: {
  39. tooltip: '技能的底部黑色遮罩',
  40. default: null,
  41. type: cc.Sprite
  42. },
  43. resetSkillTitleRichTitle: cc.RichText,
  44. resetSkillAlertNode: cc.Node,
  45. resetSkillAlertTextRich: cc.RichText,
  46. button: cc.Button,
  47. _adState: 0,
  48. },
  49. // LIFE-CYCLE CALLBACKS:
  50. onLoad () {
  51. ///技能解锁啦
  52. GameEvent.on("TopSkill_unLocked", this, (skillId) => {
  53. if (this.skillInfo != undefined && skillId === this.skillInfo.skillId) {
  54. this.setUpSkillState(SkillState.canUseState);
  55. /// 技能等级 1
  56. this.skillInfo.skillLevel = 1;
  57. /// 设置为可以使用
  58. this.skillInfo.skillStatus = SkillState.canUseState;
  59. this.updataData();
  60. let iconPath = './textures/skill/4000' + skillId;
  61. AlertManager.showCommonAlert(iconPath, this._skillData.desc, this._skillData.name);
  62. let iconId = 900005 + skillId
  63. let objct = {'cdTime': 0, 'infoDesc': `${this._skillData.name}可使用`, 'name': this._skillData.name, 'icon': iconId, 'sId': skillId, 'type': 2, 'skillStatus': 0};
  64. Global._timeInformations.push(objct);
  65. GameEvent.fire(GameNotificationKey.GameUpdateMessageList, 1, true);
  66. /// 设置重置技能可用
  67. } else if (this.skillInfo == undefined && this._isAlllLocked) {
  68. this._isAlllLocked = false;
  69. this.initResetAd();
  70. this.setUpSkillState(SkillState.canUseState);
  71. }
  72. });
  73. },
  74. start () {
  75. },
  76. onDestroy() {
  77. GameEvent.off("TopSkill_unLocked", this);
  78. GameEvent.off(GameNotificationKey.ResetSkill, this);
  79. GameEvent.off(GameNotificationKey.UpBuildingLevel, this);
  80. GameEvent.off("skill_six_use", this);
  81. GameEvent.off(GameNotificationKey.ShowShareAction, this);
  82. this.unschedule(this.updateTime, this);
  83. },
  84. init(skillInfo) {
  85. if (this.skillInfo == undefined) {
  86. this.setupTopSkillNotification();
  87. }
  88. this.skillInfo = skillInfo;
  89. let skillData = Global.BuildingManager.getSkillInfo(skillInfo.skillId);
  90. this._skillData = skillData;
  91. if (skillInfo.isHave == 0) {
  92. this.setUpSkillState(SkillState.lockedState);
  93. this.titleRichText.string = "<img src='skill_lock'/><br/><color=#ffffff>等级" + skillData.buildingLevel + '</c>';
  94. return;
  95. }
  96. /// 如果不是第二个技能那么去更新信息流时间
  97. if (skillInfo.skillId != 2) {
  98. for (let i = 0; i < Global._timeInformations.length; ++ i) {
  99. let information = Global._timeInformations[i]
  100. /// 如果是第一个技能或者第三个技能则更新它的时间
  101. if (information.type == 2 && information.sId == skillInfo.skillId) {
  102. /// 技能使用cd中
  103. if (this.skillInfo.skillStatus == 1) {
  104. information.cdTime = skillInfo.cdStarTime;
  105. } else {
  106. information.cdTime = 0;
  107. }
  108. }
  109. }
  110. }
  111. this.updataData();
  112. },
  113. setupTopSkillNotification() {
  114. GameEvent.on(GameNotificationKey.ResetSkill, this, () => {
  115. // console.log(this.skillInfo);
  116. if (this.skillState !== SkillState.lockedState) {
  117. this.setUpSkillState(SkillState.canUseState);
  118. }
  119. let isNew = true;
  120. for (let i = 0; i < Global._timeInformations.length; ++ i) {
  121. let information = Global._timeInformations[i]
  122. /// 如果是第一个技能或者第三个技能则更新它的时间
  123. if (information.type == 2 && information.sId == this.skillInfo.skillId) {
  124. information.cdTime = 0;
  125. /// 可以使用
  126. information.skillStatus = 0;
  127. information.infoDesc = `${this.skillInfo.name}可使用`;
  128. isNew = false;
  129. ///那么就是没有用过嘛 直接固定在那里
  130. }
  131. }
  132. if (isNew) {
  133. let iconId = 900005 + this.skillInfo.skillId
  134. let objct = {'cdTime': 0, 'infoDesc': `${this.skillInfo.name}可使用`, 'name': this.skillInfo.name, 'icon': iconId, 'sId': this.skillInfo.skillId, 'type': 2, 'skillStatus': 0};
  135. Global._timeInformations.push(objct);
  136. GameEvent.fire(GameNotificationKey.GameUpdateMessageList, 1, true);
  137. }
  138. });
  139. /// 第六个技能使用 需要更新cd时间
  140. GameEvent.on("skill_six_use", this, () => {
  141. let lastLevelInfo = Global.BuildingManager.getSkillLevelInfo(6, Global.rcdSkillLevel);
  142. this.cd = this._levelInfo.cd * (1 - lastLevelInfo.rcd / 100);
  143. });
  144. GameEvent.on(GameNotificationKey.UpdateTimeSkill, this, (skillLevelInfo) => {
  145. if (this.skillState === SkillState.canUseState && this.skillInfo.skillId == skillLevelInfo.skillId) {
  146. this.updataData();
  147. }
  148. });
  149. },
  150. updataData() {
  151. let levelInfo = Global.BuildingManager.getSkillLevelInfo(this.skillInfo.skillId, this.skillInfo.skillLevel);
  152. this._levelInfo = levelInfo;
  153. if (Global.rcdSkillLevel == 0) {
  154. this.cd = levelInfo.cd;
  155. } else {
  156. let lastLevelInfo = Global.BuildingManager.getSkillLevelInfo(6, Global.rcdSkillLevel);
  157. this.cd = levelInfo.cd * (1 - lastLevelInfo.rcd / 100);
  158. }
  159. /// 技能cd
  160. /// 持续时间
  161. let td = levelInfo.td;
  162. let hasCd = this.skillInfo.cdStarTime / 1000;
  163. this.td = td;
  164. /// 技能使用cd中
  165. if (this.skillInfo.skillStatus == 2) {
  166. this.setUpSkillState(SkillState.cdState);
  167. this.timeLabelCount = hasCd;
  168. this.titleRichText.string = DWTool.calculateTime(hasCd);
  169. this.skillBg.fillRange = - hasCd / this.cd;
  170. /// 技能使用中
  171. } else if (this.skillInfo.skillStatus == 1) {
  172. this.setUpSkillState(SkillState.usingState);
  173. this.timeLabelCount = hasCd;
  174. this.titleRichText.string = DWTool.calculateTime(hasCd);
  175. this.skillBgOutSide.fillRange = - hasCd / td;
  176. } else {
  177. this.setUpSkillState(SkillState.canUseState);
  178. }
  179. },
  180. /// 初始化刷新技能的item
  181. initUpdateSkill(isAllLocked) {
  182. ///是否所有的技能锁住
  183. this._isAlllLocked = isAllLocked;
  184. // 20分钟
  185. this.cd = 15 * 60;
  186. if (isAllLocked) {
  187. this.setUpSkillState(SkillState.lockedState);
  188. return;
  189. }
  190. this.resetSkillAlertTextRich.string = `<b><color=#6d4a36>是否花费${Global.rsDiamond}钻石重置技能?</color></b>`;
  191. let resetSkillTime = Global.userData.resetSkillTime;
  192. let timestamp = new Date().getTime();
  193. let totalCd = (timestamp - resetSkillTime) / 1000;
  194. if (this.cd <= totalCd) {
  195. this.setUpSkillState(SkillState.canUseState);
  196. } else {
  197. this.setUpSkillState(SkillState.cdState);
  198. this.skillBg.fillRange = totalCd / this.cd - 1;
  199. this.timeLabelCount = this.cd - totalCd;
  200. this.titleRichText.string = DWTool.calculateTime(this.timeLabelCount);
  201. }
  202. GameEvent.on(GameNotificationKey.AdUpdateStateNotification, this, (adState, callBack) => {
  203. /// 说明是技能的关闭状态
  204. if (adState === 3 && callBack === 'resetSkill') {
  205. this.shareActionCallback();
  206. }
  207. if (adState === 0 || adState === 1) {
  208. this.initResetAd();
  209. }
  210. });
  211. this.initResetAd();
  212. },
  213. initResetAd() {
  214. if (!CC_WECHATGAME) {
  215. return;
  216. }
  217. //// 说明有广告
  218. if (Global._adVideoState == 0) {
  219. this._adState = 1;
  220. } else if (Global._adVideoState === 1) {
  221. this._adState = 0;
  222. }
  223. this.updateResetTitle();
  224. },
  225. updateResetTitle() {
  226. if (this.resetSkillTitleRichTitle != null) {
  227. if (this._adState === 0) {
  228. this.resetSkillTitleRichTitle.string = `<outline color=#fd9f00 width=1><b><color=#6e3011>${Global.rsDiamond}钻石\n重置技能</c></b></outline>`;
  229. } else {
  230. this.resetSkillTitleRichTitle.string = `<outline color=#fd9f00 width=1><b><color=#6e3011>观看视频\n重置技能</c></b></outline>`;
  231. }
  232. }
  233. },
  234. updateTime() {
  235. if (this.skillState === SkillState.canUseState || this.skillState === SkillState.lockedState) {
  236. return;
  237. }
  238. /// 使用中的状态
  239. if (this.skillState === SkillState.usingState) {
  240. this.timeLabelCount -= 1;
  241. this.skillBgOutSide.fillRange += 1 / this.td;
  242. if(this.skillBgOutSide.fillRange >= 0 || this.timeLabelCount <= 0) {
  243. this.setUpSkillState(SkillState.cdState);
  244. this.timeLabelCount = this.cd;
  245. this.skillBg.fillRange = - 1;
  246. this.titleRichText.string = DWTool.calculateTime(this.timeLabelCount);
  247. } else {
  248. this.titleRichText.string = DWTool.calculateTime(this.timeLabelCount);
  249. }
  250. /// 技能cd 的状态
  251. } else {
  252. this.skillBg.fillRange += 1 / this.cd;
  253. this.timeLabelCount -= 1;
  254. if (this.skillBg.fillRange >= 0 || this.timeLabelCount <= 0) {
  255. this.setUpSkillState(SkillState.canUseState);
  256. }else {
  257. this.titleRichText.string = DWTool.calculateTime(this.timeLabelCount);
  258. }
  259. }
  260. },
  261. setUpSkillState(skillState) {
  262. this.skillState = skillState;
  263. this.skillBg.node.active = skillState !== SkillState.canUseState;
  264. if (this.skillBgOutSide != null) {
  265. this.skillBgOutSide.node.active = skillState === SkillState.usingState;
  266. }
  267. this.skillBgBottom.node.active = skillState !== SkillState.canUseState;
  268. this.titleRichText.node.active = skillState !== SkillState.canUseState;
  269. this.button.interactable = skillState === SkillState.canUseState;
  270. if (skillState != SkillState.lockedState) {
  271. this.updateResetTitle();
  272. }
  273. },
  274. sendNotification() {
  275. let skillId = this.skillInfo.skillId;
  276. let skillLevelInfo = Global.BuildingManager.getSkillLevelInfo(skillId, this.skillInfo.skillLevel);
  277. GameEvent.fire(GameNotificationKey.UseTimeSkill, skillLevelInfo);
  278. // cdTime 剩余时间 是 [long] 查看
  279. // 5 desc 描述 是 [string] 查看
  280. // 6 name 名称 是 [string] 查看
  281. if (skillId == 2) {
  282. //显示使用技能效果
  283. let gold = TapTapTool.multiple(GameModule.userInfo.coinTap, {'n': skillLevelInfo.mt, 'e': 0});
  284. let text = TapTapTool.parseToString(gold);
  285. AlertManager.showSkill2Efc(text);
  286. let isNew = true;
  287. for (let i = 0; i < Global._timeInformations.length; ++ i) {
  288. let information = Global._timeInformations[i];
  289. if (information.type == 2 && information.sId == 2) {
  290. /// 如果是已经使用过的直接刷新时间就可以啦
  291. information.cdTime = -6 * 1000;
  292. information.skillStatus = 1;
  293. information.infoDesc = `获得金币${text}`;
  294. isNew = false;
  295. break;
  296. }
  297. }
  298. if (isNew) {
  299. let objct = {'cdTime': -6 * 1000, 'infoDesc': `获得金币${text}`, 'name': this.skillInfo.name, 'icon': 900007, 'sId': 2, 'type': 2, 'skillStatus': 1};
  300. Global._fixInformations.push(objct);
  301. GameEvent.fire(GameNotificationKey.GameUpdateMessageList, 1, false);
  302. }
  303. } else {
  304. this.handelSkillTimeInformation(skillLevelInfo, skillId);
  305. }
  306. },
  307. //// 处理第一个和第三个实时技能的信息流数据
  308. handelSkillTimeInformation(skillLevelInfo, skillId) {
  309. let isNew = true;
  310. let infoDesc = this._skillData.infoDesc;
  311. let skillLevel = this.skillInfo.skillLevel;
  312. /// 第一个技能
  313. if (skillId === 1) {
  314. let num = skillLevel > 11 ? (10 + skillLevel - 11) : 10;
  315. infoDesc = infoDesc.replace('{num}', num);
  316. /// 第三个技能
  317. } else {
  318. infoDesc = infoDesc.replace('${num}', skillLevelInfo.mt * 100);
  319. }
  320. for (let i = 0; i < Global._timeInformations.length; ++ i) {
  321. let information = Global._timeInformations[i];
  322. if (information.type == 2 && information.sId == skillId) {
  323. /// 如果是已经使用过的直接刷新时间就可以啦
  324. information.cdTime = skillLevelInfo.td * 1000;
  325. information.skillStatus = 1;
  326. information.infoDesc = infoDesc;
  327. isNew = false;
  328. break;
  329. }
  330. }
  331. if (isNew){
  332. let objct = {'cdTime': skillLevelInfo.td * 1000, 'infoDesc': infoDesc, 'name': this.skillInfo.name, 'icon': 900005 + skillId, 'sId': skillId, 'type': 2, 'skillStatus': 1};
  333. Global._timeInformations.push(objct);
  334. GameEvent.fire(GameNotificationKey.GameUpdateMessageList, 1, true);
  335. }
  336. },
  337. skillAction1() {
  338. GameModule.audioMng.playClickButton();
  339. this.button.interactable = false;
  340. this.useSkill(this.skillInfo.skillId).then(() => {
  341. this.button.interactable = true;
  342. this.setUpSkillState(SkillState.usingState);
  343. this.timeLabelCount = this.td;
  344. this.titleRichText.string = DWTool.calculateTime(this.td);
  345. this.skillBgOutSide.fillRange = -1;
  346. this.skillBg.fillRange = -1;
  347. this.sendNotification();
  348. }).catch(({code, msg}) => {
  349. console.log(code, msg);
  350. this.button.interactable = true;
  351. });
  352. },
  353. skillAction2() {
  354. GameModule.audioMng.playClickButton();
  355. this.button.interactable = false;
  356. this.useSkill(this.skillInfo.skillId).then(() => {
  357. this.button.interactable = true;
  358. this.setUpSkillState(SkillState.cdState);
  359. this.timeLabelCount = this.cd;
  360. this.titleRichText.string = DWTool.calculateTime(this.cd);
  361. this.skillBg.fillRange = -1;
  362. this.sendNotification();
  363. }).catch(({code, msg}) => {
  364. console.log(code, msg);
  365. this.button.interactable = true;
  366. });
  367. },
  368. skillAction3() {
  369. GameModule.audioMng.playClickButton();
  370. this.button.interactable = false;
  371. this.useSkill(this.skillInfo.skillId).then(() => {
  372. this.button.interactable = true;
  373. this.setUpSkillState(SkillState.usingState);
  374. this.timeLabelCount = this.td;
  375. this.titleRichText.string = DWTool.calculateTime(this.td);
  376. this.skillBgOutSide.fillRange = -1;
  377. this.skillBg.fillRange = -1;
  378. this.sendNotification();
  379. GameEvent.fire("skill3_use_begain");
  380. }).catch(({code, msg}) => {
  381. console.log(code, msg);
  382. this.button.interactable = true;
  383. });
  384. },
  385. skillAgainAction() {
  386. GameModule.audioMng.playClickButton();
  387. if (CC_WECHATGAME) {
  388. this.button.interactable = false;
  389. if (this._adState === 0) {
  390. /// 弹出弹窗让它花砖石购买
  391. this.resetSkillAlertNode.active = true;
  392. } else {
  393. Global._adVideo.showVideo('resetSkill');
  394. this.button.interactable = true;
  395. }
  396. }
  397. },
  398. skillResetSureAction() {
  399. GameModule.audioMng.playClickButton();
  400. this.shareActionCallback();
  401. this.resetSkillAlertNode.active = false;
  402. },
  403. skillResetCancelAction() {
  404. this.button.interactable = true;
  405. this.resetSkillAlertNode.active = false;
  406. },
  407. shareActionCallback() {
  408. this.resetSkill().then((respondData) => {
  409. Global.userData.resetSkillTime = respondData.curSystemTime;
  410. this.setUpSkillState(SkillState.cdState);
  411. this.timeLabelCount = this.cd;
  412. this.titleRichText.string = DWTool.calculateTime(this.cd);
  413. this.skillBg.fillRange = -1;
  414. if (this._adState === 0) {
  415. GameModule.userInfo.diamond -= Global.rsDiamond;
  416. this.resetSkillAlertTextRich.string = `<b><color=#6d4a36>是否花费${respondData.rsDiamond}钻石重置技能?</color></b>`;
  417. Global.rsDiamond = respondData.rsDiamond;
  418. this.updateResetTitle();
  419. }
  420. GameEvent.fire(GameNotificationKey.ResetSkill);
  421. }).catch(({code, msg}) => {
  422. console.log(code, msg);
  423. this.button.interactable = true;
  424. });
  425. },
  426. /// 网络请求
  427. useSkill(skillId) {
  428. return new Promise((resolve, reject) => {
  429. // 获取目标用户的建筑
  430. SkillApi.useSkill(skillId, (respondData) => {
  431. /// 如果取消了红点显示
  432. // if (respondData.isCancel) {
  433. // /// 去掉因为技能 可以用的红点条件
  434. // TapTapTool.removeRedDot(GameRedDot.skill);
  435. // }
  436. resolve(respondData);
  437. }, (code, msg) => {
  438. reject({code, msg});
  439. });
  440. });
  441. },
  442. resetSkill() {
  443. return new Promise((resolve, reject) => {
  444. if (this._adState === 1) {
  445. // 获取目标用户的建筑
  446. SkillApi.resetSkill((respondData) => {
  447. resolve(respondData);
  448. }, (code, msg) => {
  449. reject({code, msg});
  450. });
  451. } else {
  452. // 获取目标用户的建筑
  453. SkillApi.resetSkill1((respondData) => {
  454. resolve(respondData);
  455. }, (code, msg) => {
  456. reject({code, msg});
  457. });
  458. }
  459. });
  460. },
  461. });