SkillTopItem.js 20 KB

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