GameSkill.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. const GameModule = require('../utils/GameModule');
  2. const GameNotificationKey = require('../utils/GameEnum').GameNotificationKey;
  3. const TapTapTool = require("../utils/TapTapTool");
  4. cc.Class({
  5. extends: cc.Component,
  6. properties: {
  7. },
  8. // LIFE-CYCLE CALLBACKS:
  9. onLoad () {
  10. GameModule.skill = this;
  11. this.multiple = {'n': 1, 'e': 0};
  12. this.skills = GameGlobal.skills;
  13. //判断是否正在使用技能3
  14. this.isUsingSkill3 = false;
  15. this.skill3Floor = Math.random() < 0.5 ? -1 : 1; //初始化在楼层单双数显示对联,正数为单,负数为双
  16. this.getAllSkillInfo();
  17. //先获取技能3的加成倍数
  18. this.getSkill3Ability();
  19. this.updateAutoGetGold();
  20. this.updateClickGold();
  21. this.setupEventListener();
  22. this.schedule(this.updateSkilltime, 1);
  23. },
  24. //添加相关接收通知
  25. setupEventListener() {
  26. GameEvent.on(GameNotificationKey.UpBuildingLevel, this, this.updateClickGold);
  27. GameEvent.on(GameNotificationKey.UpdateFixationSkill, this, (skillInfo) => {
  28. this.updateSkill(skillInfo);
  29. });
  30. //使用实时技能通知
  31. GameEvent.on(GameNotificationKey.UseTimeSkill, this, (skillInfo) => {
  32. this.useTimeSkill(skillInfo);
  33. });
  34. //重置技能CD时间通知
  35. GameEvent.on(GameNotificationKey.ResetSkill, this, () => {
  36. this.resetSkill();
  37. });
  38. GameEvent.on(GameNotificationKey.GameSkillOnHide, this, () => {
  39. this.refreshSkillStatus();
  40. });
  41. //使用好友助力
  42. GameEvent.on(GameNotificationKey.GainFriendHelpClick, this, () => {
  43. this.gainFriendReward();
  44. });
  45. },
  46. //技能列表解锁或升级技能
  47. updateSkill(skillInfo) {
  48. switch (skillInfo.skillId) {
  49. case 4:
  50. this.skill4Info.skillLevel = skillInfo.level;
  51. this.updateAutoGetGold();
  52. break;
  53. case 5:
  54. this.skill5Info.skillLevel = skillInfo.level;
  55. this.updateClickGold();
  56. break;
  57. default:
  58. break;
  59. }
  60. },
  61. //使用实时技能
  62. useTimeSkill(skillInfo) {
  63. switch (skillInfo.skillId) {
  64. case 1:
  65. this.skill1Info.skillLevel = skillInfo.level;
  66. this.skill1Info.tdStarTime = skillInfo.td * 1000;
  67. this.skill1Info.skillStatus = 1;
  68. this.updateAutoGetGold();
  69. break;
  70. case 2:
  71. this.skill2Info = skillInfo;
  72. let add = TapTapTool.multiple(GameModule.userInfo.coinTap, {'n': this.skill2Info.mt, 'e': 0});
  73. GameModule.userInfo.gold = TapTapTool.add(GameModule.userInfo.gold, add);
  74. break;
  75. case 3:
  76. this.skill3Info.skillLevel = skillInfo.level;
  77. this.skill3Info.tdStarTime = skillInfo.td * 1000;
  78. this.skill3Info.skillStatus = 1;
  79. this.getSkill3Ability();
  80. break;
  81. default:
  82. break;
  83. }
  84. },
  85. //使用好友助力奖励
  86. gainFriendReward() {
  87. this.updateAutoGetGold();
  88. },
  89. //重置技能CD时间
  90. resetSkill() {
  91. this.skill1Info.tdStarTime = 0;
  92. this.skill1Info.skillStatus = 0;
  93. this.updateAutoGetGold();
  94. this.skill3Info.tdStarTime = 0;
  95. this.skill3Info.skillStatus = 0;
  96. this.multiple = {'n': 1, 'e': 0};
  97. this.updateClickGold();
  98. GameModule.userInfo.refreshSecondText();
  99. GameEvent.fire(GameNotificationKey.SkillThreeHasDone);
  100. },
  101. //获取所有技能的信息
  102. getAllSkillInfo() {
  103. this.skill1Info = this.skills.find(n => {
  104. return n.skillId == 1;
  105. });
  106. this.skill2Info = this.skills.find(n => {
  107. return n.skillId == 2;
  108. });
  109. this.skill3Info = this.skills.find(n => {
  110. return n.skillId == 3;
  111. });
  112. this.skill4Info = this.skills.find(n => {
  113. return n.skillId == 4;
  114. });
  115. this.skill5Info = this.skills.find(n => {
  116. return n.skillId == 5;
  117. });
  118. },
  119. // start () {
  120. //
  121. // },
  122. /// 刷新技能从后台到前台的状态
  123. refreshSkillStatus() {
  124. let onHideTime = cc.sys.localStorage.getItem('onHideTimestamp');
  125. let curTime = new Date().getTime();
  126. let difference = curTime - onHideTime;
  127. if (this.skill1Info && this.skill1Info.skillStatus == 1) {
  128. //处理游戏使用技能后进入过后台的情况
  129. if (onHideTime) {
  130. if (difference > 0) {
  131. let isPast = difference > this.skill1Info.tdStarTime * 1000; //大于的话就是已超过使用技能的时间戳
  132. if (isPast) {
  133. this.skill1Info.tdStarTime = 0;
  134. this.skill1Info.skillStatus = 2;
  135. this.updateAutoGetGold();
  136. } else {
  137. this.skill1Info.tdStarTime = this.skill1Info.tdStarTime * 1000 - difference;
  138. }
  139. } else {
  140. this.skill1Info.tdStarTime = 0;
  141. this.skill1Info.skillStatus = 2;
  142. this.updateAutoGetGold();
  143. }
  144. }
  145. // console.log('skill 1 ' + this.skill1Info.tdStarTime);
  146. }
  147. if (GameGlobal.friendRewardCdTime > 0) {
  148. if (onHideTime) {
  149. if (difference > 0) {
  150. let isPast = difference > GameGlobal.friendRewardCdTime; //大于的话就是已超过使用技能的时间戳
  151. if (isPast) {
  152. GameGlobal.friendRewardCdTime = 0;
  153. this.updateAutoGetGold();
  154. } else {
  155. GameGlobal.friendRewardCdTime = GameGlobal.friendRewardCdTime - difference;
  156. }
  157. } else {
  158. GameGlobal.friendRewardCdTime = 0;
  159. this.updateAutoGetGold();
  160. }
  161. }
  162. }
  163. if (this.skill3Info && this.skill3Info.skillStatus == 1) {
  164. if (onHideTime) {
  165. if (difference > 0) {
  166. let isPast = difference > this.skill3Info.tdStarTime * 1000; //大于的话就是已超过使用技能的时间戳
  167. if (isPast) {
  168. this.skill3Info.tdStarTime = 0;
  169. this.skill3Info.skillStatus = 2;
  170. this.multiple = {'n': 1, 'e': 0};
  171. this.updateClickGold();
  172. GameModule.userInfo.refreshSecondText();
  173. GameEvent.fire(GameNotificationKey.SkillThreeHasDone);
  174. this.isUsingSkill3 = false;
  175. } else {
  176. this.skill3Info.tdStarTime = this.skill3Info.tdStarTime * 1000 - difference;
  177. }
  178. } else {
  179. this.skill3Info.tdStarTime = 0;
  180. this.skill3Info.skillStatus = 2;
  181. this.multiple = {'n': 1, 'e': 0};
  182. this.updateClickGold();
  183. GameModule.userInfo.refreshSecondText();
  184. GameEvent.fire(GameNotificationKey.SkillThreeHasDone);
  185. this.isUsingSkill3 = false;
  186. }
  187. }
  188. // console.log('skill 3 ' + this.skill3Info.tdStarTime);
  189. }
  190. },
  191. /// 每秒更新实时技能的使用时间
  192. updateSkilltime() {
  193. //技能1使用中倒计时
  194. if (this.skill1Info && this.skill1Info.skillStatus == 1) {
  195. //tdStarTime大于0为正在使用中
  196. if (this.skill1Info.tdStarTime > 0) {
  197. this.skill1Info.tdStarTime -= (1 * 1000 );
  198. if (this.skill1Info.tdStarTime <= 0) {
  199. this.skill1Info.tdStarTime = 0;
  200. this.skill1Info.skillStatus = 2;
  201. this.updateAutoGetGold();
  202. }
  203. } else {
  204. this.skill1Info.tdStarTime = 0;
  205. this.skill1Info.skillStatus = 2;
  206. this.updateAutoGetGold();
  207. }
  208. /// 重新进入前台需要刷新数据
  209. } else if (this.skill1Info && this.skill1Info.isShow != undefined) {
  210. this.skill1Info.isShow = undefined;
  211. this.skill1Info.tdStarTime = 0;
  212. this.updateAutoGetGold();
  213. }
  214. //技能3使用中倒计时
  215. if (this.skill3Info && this.skill3Info.skillStatus == 1) {
  216. if (this.skill3Info.tdStarTime > 0) {
  217. this.skill3Info.tdStarTime -= 1 * 1000;
  218. this.isUsingSkill3 = true;
  219. if (this.skill3Info.tdStarTime <= 0) {
  220. this.skill3Info.tdStarTime = 0;
  221. this.skill3Info.skillStatus = 2;
  222. this.multiple = {'n': 1, 'e': 0};
  223. this.updateClickGold();
  224. GameModule.userInfo.refreshSecondText();
  225. GameEvent.fire(GameNotificationKey.SkillThreeHasDone);
  226. this.isUsingSkill3 = false;
  227. }
  228. } else {
  229. this.skill3Info.tdStarTime = 0;
  230. this.skill3Info.skillStatus = 2;
  231. this.multiple = {'n': 1, 'e': 0};
  232. this.updateClickGold();
  233. GameModule.userInfo.refreshSecondText();
  234. GameEvent.fire(GameNotificationKey.SkillThreeHasDone);
  235. this.isUsingSkill3 = false;
  236. }
  237. } else if (this.skill3Info && this.skill3Info.isShow != undefined) {
  238. this.skill3Info.isShow = undefined;
  239. this.skill3Info.tdStarTime = 0;
  240. this.multiple = {'n': 1, 'e': 0};
  241. this.updateClickGold();
  242. GameModule.userInfo.refreshSecondText();
  243. GameEvent.fire(GameNotificationKey.SkillThreeHasDone);
  244. this.isUsingSkill3 = false;
  245. } else {
  246. this.isUsingSkill3 = false;
  247. }
  248. //好友助力技能使用中
  249. if (GameGlobal.friendRewardCdTime > 0) {
  250. GameGlobal.friendRewardCdTime -= (1 * 1000 );
  251. if (GameGlobal.friendRewardCdTime <= 0) {
  252. GameGlobal.friendRewardCdTime = 0;
  253. this.updateAutoGetGold();
  254. }
  255. }
  256. },
  257. // update (dt) {
  258. // },
  259. //更新每秒自动点击生成金币的速率
  260. updateAutoGetGold() {
  261. var second = 1;
  262. var click = 0;
  263. var skill1 = this.getSkill1Ability();
  264. if (skill1 != null) {
  265. click += skill1.cc / skill1.iv;
  266. }
  267. var skill4 = this.getSkill4Ability();
  268. if (skill4 != null) {
  269. click += skill4.cc / skill4.iv;
  270. }
  271. if (GameGlobal.friendRewardCdTime > 0) {
  272. click += 10;
  273. }
  274. if (click > 0) {
  275. GameModule.userInfo.secondClick = second / click;
  276. } else {
  277. GameModule.userInfo.secondClick = 0;
  278. }
  279. },
  280. //更新每次点击生成金币的数量
  281. updateClickGold() {
  282. var clickGold = this.getBuildingGold();
  283. clickGold = clickGold * this.getSkill5Ability();
  284. let coinTap = TapTapTool.multiple({'n': clickGold, 'e': 0}, this.multiple);
  285. coinTap = TapTapTool.multiple(coinTap, {'n': GameModule.shop.multiple, 'e': 0});
  286. GameModule.userInfo.coinTap = TapTapTool.multiple(coinTap, GameModule.userInfo.perpetualClickMt);
  287. GameModule.userInfo.refreshSecondText();
  288. },
  289. //总部大楼等级技能
  290. getBuildingGold() {
  291. this.userBuildingLevel = GameModule.userInfo.buildingLevel;
  292. let clickGold = 9 + this.userBuildingLevel;
  293. return clickGold;
  294. },
  295. //主动释放技能1,多少分钟内每秒自动点击多少次
  296. getSkill1Ability() {
  297. if (this.skill1Info == undefined || this.skill1Info.skillLevel <= 0 ) {
  298. return null;
  299. }
  300. if (this.skill1Info.skillStatus != 1 || this.skill1Info.tdStarTime == 0) {
  301. return null;
  302. }
  303. let skillInfo = GameGlobal.BuildingManager.getSkillLevelInfo(this.skill1Info.skillId, this.skill1Info.skillLevel);
  304. return skillInfo;
  305. },
  306. //主动释放技能3,增加加成倍数
  307. getSkill3Ability() {
  308. if (this.skill3Info == undefined || this.skill3Info.skillLevel <= 0 ) {
  309. return null;
  310. }
  311. if (this.skill3Info.skillStatus != 1 || this.skill3Info.tdStarTime == 0) {
  312. return null;
  313. }
  314. let skillInfo = GameGlobal.BuildingManager.getSkillLevelInfo(this.skill3Info.skillId, this.skill3Info.skillLevel);
  315. this.multiple = {'n': skillInfo.mt, 'e': 0};
  316. GameModule.userInfo.multiple = this.multiple;
  317. this.updateClickGold();
  318. },
  319. //永久技能1,指定时间内自动点击次数
  320. getSkill4Ability() {
  321. if (this.skill4Info == undefined || this.skill4Info.skillLevel <= 0 ) {
  322. return null;
  323. }
  324. let skillInfo = GameGlobal.BuildingManager.getSkillLevelInfo(this.skill4Info.skillId, this.skill4Info.skillLevel);
  325. return skillInfo;
  326. },
  327. //永久技能2,每次点击产出的金币提升
  328. getSkill5Ability() {
  329. if (this.skill5Info == undefined || this.skill5Info.skillLevel <= 0 ) {
  330. return 1;
  331. }
  332. let skillInfo = GameGlobal.BuildingManager.getSkillLevelInfo(this.skill5Info.skillId, this.skill5Info.skillLevel);
  333. let mt = skillInfo.mt;
  334. return mt;
  335. },
  336. //永久技能3,减少CD时间
  337. getSkill6Ability() {
  338. let skillInfo = this.skills.find(n => {
  339. return n.skillId == 6;
  340. });
  341. skillInfo = GameGlobal.BuildingManager.getSkillLevelInfo(skillInfo.skillId, skillInfo.skillLevel);
  342. let rcd = (1 - skillInfo.rcd);
  343. return rcd;
  344. },
  345. });