SkillTopItem.js 17 KB

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