SkillTopItem.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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. button: cc.Button,
  44. },
  45. // LIFE-CYCLE CALLBACKS:
  46. onLoad () {
  47. ///技能解锁啦
  48. GameEvent.on("TopSkill_unLocked", this, (skillId) => {
  49. if (this.skillInfo != undefined && skillId === this.skillInfo.skillId) {
  50. this.setUpSkillState(SkillState.canUseState);
  51. /// 技能等级 1
  52. this.skillInfo.skillLevel = 1;
  53. /// 设置为可以使用
  54. this.skillInfo.skillStatus = SkillState.canUseState;
  55. this.updataData();
  56. let iconPath = './textures/skill/4000' + skillId;
  57. AlertManager.showCommonAlert(iconPath, this._skillData.desc, this._skillData.name);
  58. let iconId = 900005 + skillId
  59. let objct = {'cdTime': 0, 'infoDesc': `${this._skillData.name}可使用`, 'name': this._skillData.name, 'icon': iconId, 'sId': skillId, 'type': 2, 'skillStatus': 0};
  60. Global._timeInformations.push(objct);
  61. GameEvent.fire(GameNotificationKey.GameUpdateMessageList, 1, true);
  62. /// 设置重置技能可用
  63. } else if (this.skillInfo == undefined && this._isAlllLocked) {
  64. this._isAlllLocked = false;
  65. this.setUpSkillState(SkillState.canUseState);
  66. }
  67. });
  68. },
  69. start () {
  70. },
  71. onDestroy() {
  72. GameEvent.off("TopSkill_unLocked", this);
  73. GameEvent.off(GameNotificationKey.ResetSkill, this);
  74. GameEvent.off(GameNotificationKey.UpBuildingLevel, this);
  75. GameEvent.off("skill_six_use", this);
  76. GameEvent.off(GameNotificationKey.ShowShareAction, this);
  77. this.unschedule(this.updateTime, this);
  78. },
  79. init(skillInfo) {
  80. if (this.skillInfo == undefined) {
  81. this.setupTopSkillNotification();
  82. }
  83. this.skillInfo = skillInfo;
  84. let skillData = Global.BuildingManager.getSkillInfo(skillInfo.skillId);
  85. this._skillData = skillData;
  86. if (skillInfo.isHave == 0) {
  87. this.setUpSkillState(SkillState.lockedState);
  88. this.titleRichText.string = "<img src='skill_lock'/><br/><color=#ffffff>等级" + skillData.buildingLevel + '</c>';
  89. return;
  90. }
  91. /// 如果不是第二个技能那么去更新信息流时间
  92. if (skillInfo.skillId != 2) {
  93. for (let i = 0; i < Global._timeInformations.length; ++ i) {
  94. let information = Global._timeInformations[i]
  95. /// 如果是第一个技能或者第三个技能则更新它的时间
  96. if (information.type == 2 && information.sId == skillInfo.skillId) {
  97. /// 技能使用cd中
  98. if (this.skillInfo.skillStatus == 1) {
  99. information.cdTime = skillInfo.cdStarTime;
  100. } else {
  101. information.cdTime = 0;
  102. }
  103. }
  104. }
  105. }
  106. this.updataData();
  107. },
  108. setupTopSkillNotification() {
  109. GameEvent.on(GameNotificationKey.ResetSkill, this, () => {
  110. // console.log(this.skillInfo);
  111. if (this.skillState !== SkillState.lockedState) {
  112. this.setUpSkillState(SkillState.canUseState);
  113. }
  114. let isNew = true;
  115. for (let i = 0; i < Global._timeInformations.length; ++ i) {
  116. let information = Global._timeInformations[i]
  117. /// 如果是第一个技能或者第三个技能则更新它的时间
  118. if (information.type == 2 && information.sId == this.skillInfo.skillId) {
  119. information.cdTime = 0;
  120. /// 可以使用
  121. information.skillStatus = 0;
  122. information.infoDesc = `${this.skillInfo.name}可使用`;
  123. isNew = false;
  124. ///那么就是没有用过嘛 直接固定在那里
  125. }
  126. }
  127. if (isNew) {
  128. let iconId = 900005 + this.skillInfo.skillId
  129. let objct = {'cdTime': 0, 'infoDesc': `${this.skillInfo.name}可使用`, 'name': this.skillInfo.name, 'icon': iconId, 'sId': this.skillInfo.skillId, 'type': 2, 'skillStatus': 0};
  130. Global._timeInformations.push(objct);
  131. GameEvent.fire(GameNotificationKey.GameUpdateMessageList, 1, true);
  132. }
  133. });
  134. /// 第六个技能使用 需要更新cd时间
  135. GameEvent.on("skill_six_use", this, () => {
  136. let lastLevelInfo = Global.BuildingManager.getSkillLevelInfo(6, Global.rcdSkillLevel);
  137. this.cd = this._levelInfo.cd * (1 - lastLevelInfo.rcd / 100);
  138. });
  139. GameEvent.on(GameNotificationKey.UpdateTimeSkill, this, (skillLevelInfo) => {
  140. if (this.skillState === SkillState.canUseState && this.skillInfo.skillId == skillLevelInfo.skillId) {
  141. this.updataData();
  142. }
  143. });
  144. },
  145. updataData() {
  146. let levelInfo = Global.BuildingManager.getSkillLevelInfo(this.skillInfo.skillId, this.skillInfo.skillLevel);
  147. this._levelInfo = levelInfo;
  148. if (Global.rcdSkillLevel == 0) {
  149. this.cd = levelInfo.cd;
  150. } else {
  151. let lastLevelInfo = Global.BuildingManager.getSkillLevelInfo(6, Global.rcdSkillLevel);
  152. this.cd = levelInfo.cd * (1 - lastLevelInfo.rcd / 100);
  153. }
  154. /// 技能cd
  155. /// 持续时间
  156. let td = levelInfo.td;
  157. let hasCd = this.skillInfo.cdStarTime / 1000;
  158. this.td = td;
  159. /// 技能使用cd中
  160. if (this.skillInfo.skillStatus == 2) {
  161. this.setUpSkillState(SkillState.cdState);
  162. this.timeLabelCount = hasCd;
  163. this.titleRichText.string = DWTool.calculateTime(hasCd);
  164. this.skillBg.fillRange = - hasCd / this.cd;
  165. /// 技能使用中
  166. } else if (this.skillInfo.skillStatus == 1) {
  167. this.setUpSkillState(SkillState.usingState);
  168. this.timeLabelCount = hasCd;
  169. this.titleRichText.string = DWTool.calculateTime(hasCd);
  170. this.skillBgOutSide.fillRange = - hasCd / td;
  171. } else {
  172. this.setUpSkillState(SkillState.canUseState);
  173. }
  174. },
  175. /// 初始化刷新技能的item
  176. initUpdateSkill(isAllLocked) {
  177. ///是否所有的技能锁住
  178. this._isAlllLocked = isAllLocked;
  179. // 20分钟
  180. this.cd = 15 * 60;
  181. if (isAllLocked) {
  182. this.setUpSkillState(SkillState.lockedState);
  183. return;
  184. }
  185. let resetSkillTime = Global.userData.resetSkillTime;
  186. let timestamp = new Date().getTime();
  187. let totalCd = (timestamp - resetSkillTime) / 1000;
  188. if (this.cd <= totalCd) {
  189. this.setUpSkillState(SkillState.canUseState);
  190. } else {
  191. this.setUpSkillState(SkillState.cdState);
  192. this.skillBg.fillRange = totalCd / this.cd - 1;
  193. this.timeLabelCount = this.cd - totalCd;
  194. this.titleRichText.string = DWTool.calculateTime(this.timeLabelCount);
  195. }
  196. },
  197. updateTime() {
  198. if (this.skillState === SkillState.canUseState || this.skillState === SkillState.lockedState) {
  199. return;
  200. }
  201. /// 使用中的状态
  202. if (this.skillState === SkillState.usingState) {
  203. this.timeLabelCount -= 1;
  204. this.skillBgOutSide.fillRange += 1 / this.td;
  205. if(this.skillBgOutSide.fillRange >= 0 || this.timeLabelCount <= 0) {
  206. this.setUpSkillState(SkillState.cdState);
  207. this.timeLabelCount = this.cd;
  208. this.skillBg.fillRange = - 1;
  209. this.titleRichText.string = DWTool.calculateTime(this.timeLabelCount);
  210. } else {
  211. this.titleRichText.string = DWTool.calculateTime(this.timeLabelCount);
  212. }
  213. /// 技能cd 的状态
  214. } else {
  215. this.skillBg.fillRange += 1 / this.cd;
  216. this.timeLabelCount -= 1;
  217. if (this.skillBg.fillRange >= 0 || this.timeLabelCount <= 0) {
  218. this.setUpSkillState(SkillState.canUseState);
  219. }else {
  220. this.titleRichText.string = DWTool.calculateTime(this.timeLabelCount);
  221. }
  222. }
  223. },
  224. setUpSkillState(skillState) {
  225. this.skillState = skillState;
  226. this.skillBg.node.active = skillState !== SkillState.canUseState;
  227. if (this.skillBgOutSide != null) {
  228. this.skillBgOutSide.node.active = skillState === SkillState.usingState;
  229. }
  230. this.skillBgBottom.node.active = skillState !== SkillState.canUseState;
  231. this.titleRichText.node.active = skillState !== SkillState.canUseState;
  232. this.button.interactable = skillState === SkillState.canUseState;
  233. },
  234. sendNotification() {
  235. let skillId = this.skillInfo.skillId;
  236. let skillLevelInfo = Global.BuildingManager.getSkillLevelInfo(skillId, this.skillInfo.skillLevel);
  237. GameEvent.fire(GameNotificationKey.UseTimeSkill, skillLevelInfo);
  238. // cdTime 剩余时间 是 [long] 查看
  239. // 5 desc 描述 是 [string] 查看
  240. // 6 name 名称 是 [string] 查看
  241. if (skillId == 2) {
  242. //显示使用技能效果
  243. let gold = TapTapTool.multiple(GameModule.userInfo.coinTap, {'n': skillLevelInfo.mt, 'e': 0});
  244. let text = TapTapTool.parseToString(gold);
  245. AlertManager.showSkill2Efc(text);
  246. let isNew = true;
  247. for (let i = 0; i < Global._timeInformations.length; ++ i) {
  248. let information = Global._timeInformations[i];
  249. if (information.type == 2 && information.sId == 2) {
  250. /// 如果是已经使用过的直接刷新时间就可以啦
  251. information.cdTime = -6 * 1000;
  252. information.skillStatus = 1;
  253. information.infoDesc = `获得金币${text}`;
  254. isNew = false;
  255. break;
  256. }
  257. }
  258. if (isNew) {
  259. let objct = {'cdTime': -6 * 1000, 'infoDesc': `获得金币${text}`, 'name': this.skillInfo.name, 'icon': 900007, 'sId': 2, 'type': 2, 'skillStatus': 1};
  260. Global._fixInformations.push(objct);
  261. GameEvent.fire(GameNotificationKey.GameUpdateMessageList, 1, false);
  262. }
  263. } else {
  264. this.handelSkillTimeInformation(skillLevelInfo, skillId);
  265. }
  266. },
  267. //// 处理第一个和第三个实时技能的信息流数据
  268. handelSkillTimeInformation(skillLevelInfo, skillId) {
  269. let isNew = true;
  270. let infoDesc = this._skillData.infoDesc;
  271. let skillLevel = this.skillInfo.skillLevel;
  272. /// 第一个技能
  273. if (skillId === 1) {
  274. let num = skillLevel > 11 ? (10 + skillLevel - 11) : 10;
  275. infoDesc = infoDesc.replace('{num}', num);
  276. /// 第三个技能
  277. } else {
  278. infoDesc = infoDesc.replace('${num}', skillLevelInfo.mt * 100);
  279. }
  280. for (let i = 0; i < Global._timeInformations.length; ++ i) {
  281. let information = Global._timeInformations[i];
  282. if (information.type == 2 && information.sId == skillId) {
  283. /// 如果是已经使用过的直接刷新时间就可以啦
  284. information.cdTime = skillLevelInfo.td * 1000;
  285. information.skillStatus = 1;
  286. information.infoDesc = infoDesc;
  287. isNew = false;
  288. break;
  289. }
  290. }
  291. if (isNew){
  292. let objct = {'cdTime': skillLevelInfo.td * 1000, 'infoDesc': infoDesc, 'name': this.skillInfo.name, 'icon': 900005 + skillId, 'sId': skillId, 'type': 2, 'skillStatus': 1};
  293. Global._timeInformations.push(objct);
  294. GameEvent.fire(GameNotificationKey.GameUpdateMessageList, 1, true);
  295. }
  296. },
  297. skillAction1() {
  298. GameModule.audioMng.playClickButton();
  299. this.button.interactable = false;
  300. this.useSkill(this.skillInfo.skillId).then(() => {
  301. this.button.interactable = true;
  302. this.setUpSkillState(SkillState.usingState);
  303. this.timeLabelCount = this.td;
  304. this.titleRichText.string = DWTool.calculateTime(this.td);
  305. this.skillBgOutSide.fillRange = -1;
  306. this.skillBg.fillRange = -1;
  307. this.sendNotification();
  308. }).catch(({code, msg}) => {
  309. console.log(code, msg);
  310. this.button.interactable = true;
  311. });
  312. },
  313. skillAction2() {
  314. GameModule.audioMng.playClickButton();
  315. this.button.interactable = false;
  316. this.useSkill(this.skillInfo.skillId).then(() => {
  317. this.button.interactable = true;
  318. this.setUpSkillState(SkillState.cdState);
  319. this.timeLabelCount = this.cd;
  320. this.titleRichText.string = DWTool.calculateTime(this.cd);
  321. this.skillBg.fillRange = -1;
  322. this.sendNotification();
  323. }).catch(({code, msg}) => {
  324. console.log(code, msg);
  325. this.button.interactable = true;
  326. });
  327. },
  328. skillAction3() {
  329. GameModule.audioMng.playClickButton();
  330. this.button.interactable = false;
  331. this.useSkill(this.skillInfo.skillId).then(() => {
  332. this.button.interactable = true;
  333. this.setUpSkillState(SkillState.usingState);
  334. this.timeLabelCount = this.td;
  335. this.titleRichText.string = DWTool.calculateTime(this.td);
  336. this.skillBgOutSide.fillRange = -1;
  337. this.skillBg.fillRange = -1;
  338. this.sendNotification();
  339. GameEvent.fire("skill3_use_begain");
  340. }).catch(({code, msg}) => {
  341. console.log(code, msg);
  342. this.button.interactable = true;
  343. });
  344. },
  345. skillAgainAction() {
  346. GameModule.audioMng.playClickButton();
  347. if (CC_WECHATGAME) {
  348. this.button.interactable = false;
  349. ADVideo.wxPlayADVideo('adunit-72f6285169164939', () => {
  350. this.button.interactable = true;
  351. }, (isFinished) => {
  352. if (isFinished) {
  353. this.shareActionCallback();
  354. } else {
  355. this.button.interactable = true;
  356. }
  357. });
  358. }
  359. },
  360. shareActionCallback() {
  361. this.resetSkill().then((respondData) => {
  362. Global.userData.resetSkillTime = respondData.curSystemTime;
  363. this.setUpSkillState(SkillState.cdState);
  364. this.timeLabelCount = this.cd;
  365. this.titleRichText.string = DWTool.calculateTime(this.cd);
  366. this.skillBg.fillRange = -1;
  367. GameEvent.fire(GameNotificationKey.ResetSkill);
  368. }).catch(({code, msg}) => {
  369. console.log(code, msg);
  370. this.button.interactable = true;
  371. });
  372. GameEvent.off(GameNotificationKey.ShowShareAction, this);
  373. },
  374. /// 网络请求
  375. useSkill(skillId) {
  376. return new Promise((resolve, reject) => {
  377. // 获取目标用户的建筑
  378. SkillApi.useSkill(skillId, (respondData) => {
  379. /// 如果取消了红点显示
  380. // if (respondData.isCancel) {
  381. // /// 去掉因为技能 可以用的红点条件
  382. // TapTapTool.removeRedDot(GameRedDot.skill);
  383. // }
  384. resolve(respondData);
  385. }, (code, msg) => {
  386. reject({code, msg});
  387. });
  388. });
  389. },
  390. resetSkill() {
  391. return new Promise((resolve, reject) => {
  392. // 获取目标用户的建筑
  393. SkillApi.resetSkill((respondData) => {
  394. resolve(respondData);
  395. }, (code, msg) => {
  396. reject({code, msg});
  397. });
  398. });
  399. },
  400. });