SkillTopItem.js 18 KB

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