GameSkill.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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 = Global.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. //技能列表解锁或升级技能
  43. updateSkill(skillInfo) {
  44. switch (skillInfo.skillId) {
  45. case 4:
  46. this.skill4Info.skillLevel = skillInfo.level;
  47. this.updateAutoGetGold();
  48. break;
  49. case 5:
  50. this.skill5Info.skillLevel = skillInfo.level;
  51. this.updateClickGold();
  52. break;
  53. default:
  54. break;
  55. }
  56. },
  57. //技能实时技能
  58. useTimeSkill(skillInfo) {
  59. switch (skillInfo.skillId) {
  60. case 1:
  61. this.skill1Info.skillLevel = skillInfo.level;
  62. this.skill1Info.tdStarTime = skillInfo.td * 1000;
  63. this.skill1Info.skillStatus = 1;
  64. this.updateAutoGetGold();
  65. break;
  66. case 2:
  67. this.skill2Info = skillInfo;
  68. let add = TapTapTool.multiple(GameModule.userInfo.coinTap, {'n': this.skill2Info.mt, 'e': 0});
  69. GameModule.userInfo.gold = TapTapTool.add(GameModule.userInfo.gold, add);
  70. break;
  71. case 3:
  72. this.skill3Info.skillLevel = skillInfo.level;
  73. this.skill3Info.tdStarTime = skillInfo.td * 1000;
  74. this.skill3Info.skillStatus = 1;
  75. this.getSkill3Ability();
  76. break;
  77. default:
  78. break;
  79. }
  80. },
  81. //重置技能CD时间
  82. resetSkill() {
  83. this.skill1Info.tdStarTime = 0;
  84. this.skill1Info.skillStatus = 0;
  85. this.updateAutoGetGold();
  86. this.skill3Info.tdStarTime = 0;
  87. this.skill3Info.skillStatus = 0;
  88. this.multiple = {'n': 1, 'e': 0};
  89. this.updateClickGold();
  90. GameModule.userInfo.refreshSecondText();
  91. GameEvent.fire(GameNotificationKey.SkillThreeHasDone);
  92. },
  93. //获取所有技能的信息
  94. getAllSkillInfo() {
  95. this.skill1Info = this.skills.find(n => {
  96. return n.skillId == 1;
  97. });
  98. this.skill2Info = this.skills.find(n => {
  99. return n.skillId == 2;
  100. });
  101. this.skill3Info = this.skills.find(n => {
  102. return n.skillId == 3;
  103. });
  104. this.skill4Info = this.skills.find(n => {
  105. return n.skillId == 4;
  106. });
  107. this.skill5Info = this.skills.find(n => {
  108. return n.skillId == 5;
  109. });
  110. },
  111. // start () {
  112. //
  113. // },
  114. /// 刷新技能从后台到前台的状态
  115. refreshSkillStatus() {
  116. let onHideTime = cc.sys.localStorage.getItem('onHideTimestamp');
  117. let curTime = new Date().getTime();
  118. let difference = curTime - onHideTime;
  119. if (this.skill1Info && this.skill1Info.skillStatus == 1) {
  120. //处理游戏使用技能后进入过后台的情况
  121. if (onHideTime) {
  122. if (difference > 0) {
  123. let isPast = difference > this.skill1Info.tdStarTime * 1000; //大于的话就是已超过使用技能的时间戳
  124. if (isPast) {
  125. this.skill1Info.tdStarTime = 0;
  126. this.skill1Info.skillStatus = 2;
  127. this.updateAutoGetGold();
  128. } else {
  129. this.skill1Info.tdStarTime = this.skill1Info.tdStarTime * 1000 - difference;
  130. }
  131. } else {
  132. this.skill1Info.tdStarTime = 0;
  133. this.skill1Info.skillStatus = 2;
  134. this.updateAutoGetGold();
  135. }
  136. }
  137. // console.log('skill 1 ' + this.skill1Info.tdStarTime);
  138. }
  139. if (this.skill3Info && this.skill3Info.skillStatus == 1) {
  140. if (onHideTime) {
  141. if (difference > 0) {
  142. let isPast = difference > this.skill3Info.tdStarTime * 1000; //大于的话就是已超过使用技能的时间戳
  143. if (isPast) {
  144. this.skill3Info.tdStarTime = 0;
  145. this.skill3Info.skillStatus = 2;
  146. this.multiple = {'n': 1, 'e': 0};
  147. this.updateClickGold();
  148. GameModule.userInfo.refreshSecondText();
  149. GameEvent.fire(GameNotificationKey.SkillThreeHasDone);
  150. this.isUsingSkill3 = false;
  151. } else {
  152. this.skill3Info.tdStarTime = this.skill3Info.tdStarTime * 1000 - difference;
  153. }
  154. } else {
  155. this.skill3Info.tdStarTime = 0;
  156. this.skill3Info.skillStatus = 2;
  157. this.multiple = {'n': 1, 'e': 0};
  158. this.updateClickGold();
  159. GameModule.userInfo.refreshSecondText();
  160. GameEvent.fire(GameNotificationKey.SkillThreeHasDone);
  161. this.isUsingSkill3 = false;
  162. }
  163. }
  164. // console.log('skill 3 ' + this.skill3Info.tdStarTime);
  165. }
  166. },
  167. /// 每秒更新实时技能的使用时间
  168. updateSkilltime() {
  169. if (this.skill1Info && this.skill1Info.skillStatus == 1) {
  170. //tdStarTime大于0为正在使用中
  171. if (this.skill1Info.tdStarTime > 0) {
  172. this.skill1Info.tdStarTime -= (1 * 1000 );
  173. if (this.skill1Info.tdStarTime <= 0) {
  174. this.skill1Info.tdStarTime = 0;
  175. this.skill1Info.skillStatus = 2;
  176. this.updateAutoGetGold();
  177. }
  178. } else {
  179. this.skill1Info.tdStarTime = 0;
  180. this.skill1Info.skillStatus = 2;
  181. this.updateAutoGetGold();
  182. }
  183. /// 重新进入前台需要刷新数据
  184. } else if (this.skill1Info && this.skill1Info.isShow != undefined) {
  185. this.skill1Info.isShow = undefined;
  186. this.skill1Info.tdStarTime = 0;
  187. this.updateAutoGetGold();
  188. }
  189. if (this.skill3Info && this.skill3Info.skillStatus == 1) {
  190. if (this.skill3Info.tdStarTime > 0) {
  191. this.skill3Info.tdStarTime -= 1 * 1000;
  192. this.isUsingSkill3 = true;
  193. if (this.skill3Info.tdStarTime <= 0) {
  194. this.skill3Info.tdStarTime = 0;
  195. this.skill3Info.skillStatus = 2;
  196. this.multiple = {'n': 1, 'e': 0};
  197. this.updateClickGold();
  198. GameModule.userInfo.refreshSecondText();
  199. GameEvent.fire(GameNotificationKey.SkillThreeHasDone);
  200. this.isUsingSkill3 = false;
  201. }
  202. } else {
  203. this.skill3Info.tdStarTime = 0;
  204. this.skill3Info.skillStatus = 2;
  205. this.multiple = {'n': 1, 'e': 0};
  206. this.updateClickGold();
  207. GameModule.userInfo.refreshSecondText();
  208. GameEvent.fire(GameNotificationKey.SkillThreeHasDone);
  209. this.isUsingSkill3 = false;
  210. }
  211. } else if (this.skill3Info && this.skill3Info.isShow != undefined) {
  212. this.skill3Info.isShow = undefined;
  213. this.skill3Info.tdStarTime = 0;
  214. this.multiple = {'n': 1, 'e': 0};
  215. this.updateClickGold();
  216. GameModule.userInfo.refreshSecondText();
  217. GameEvent.fire(GameNotificationKey.SkillThreeHasDone);
  218. this.isUsingSkill3 = false;
  219. } else {
  220. this.isUsingSkill3 = false;
  221. }
  222. },
  223. // update (dt) {
  224. // },
  225. //更新每秒自动点击生成金币的速率
  226. updateAutoGetGold() {
  227. var second = 1;
  228. var click = 0;
  229. var skill1 = this.getSkill1Ability();
  230. if (skill1 != null) {
  231. click += skill1.cc / skill1.iv;
  232. }
  233. var skill4 = this.getSkill4Ability();
  234. if (skill4 != null) {
  235. click += skill4.cc / skill4.iv;
  236. }
  237. if (click > 0) {
  238. GameModule.userInfo.secondClick = second / click;
  239. } else {
  240. GameModule.userInfo.secondClick = 0;
  241. }
  242. },
  243. //更新每次点击生成金币的数量
  244. updateClickGold() {
  245. var clickGold = this.getBuildingGold();
  246. clickGold = clickGold * this.getSkill5Ability();
  247. let coinTap = TapTapTool.multiple({'n': clickGold, 'e': 0}, this.multiple);
  248. coinTap = TapTapTool.multiple(coinTap, GameModule.shop.multiple);
  249. GameModule.userInfo.coinTap = TapTapTool.multiple(coinTap, GameModule.userInfo.perpetualClickMt);
  250. GameModule.userInfo.refreshSecondText();
  251. },
  252. //总部大楼等级技能
  253. getBuildingGold() {
  254. this.userBuildingLevel = GameModule.userInfo.buildingLevel;
  255. let clickGold = 9 + this.userBuildingLevel;
  256. return clickGold;
  257. },
  258. //主动释放技能1,多少分钟内每秒自动点击多少次
  259. getSkill1Ability() {
  260. if (this.skill1Info == undefined || this.skill1Info.skillLevel <= 0 ) {
  261. return null;
  262. }
  263. if (this.skill1Info.skillStatus != 1 || this.skill1Info.tdStarTime == 0) {
  264. return null;
  265. }
  266. let skillInfo = Global.BuildingManager.getSkillLevelInfo(this.skill1Info.skillId, this.skill1Info.skillLevel);
  267. return skillInfo;
  268. },
  269. //主动释放技能3,增加加成倍数
  270. getSkill3Ability() {
  271. if (this.skill3Info == undefined || this.skill3Info.skillLevel <= 0 ) {
  272. return null;
  273. }
  274. if (this.skill3Info.skillStatus != 1 || this.skill3Info.tdStarTime == 0) {
  275. return null;
  276. }
  277. let skillInfo = Global.BuildingManager.getSkillLevelInfo(this.skill3Info.skillId, this.skill3Info.skillLevel);
  278. this.multiple = {'n': skillInfo.mt, 'e': 0};
  279. GameModule.userInfo.multiple = this.multiple;
  280. this.updateClickGold();
  281. },
  282. //永久技能1,指定时间内自动点击次数
  283. getSkill4Ability() {
  284. if (this.skill4Info == undefined || this.skill4Info.skillLevel <= 0 ) {
  285. return null;
  286. }
  287. let skillInfo = Global.BuildingManager.getSkillLevelInfo(this.skill4Info.skillId, this.skill4Info.skillLevel);
  288. return skillInfo;
  289. },
  290. //永久技能2,每次点击产出的金币提升
  291. getSkill5Ability() {
  292. if (this.skill5Info == undefined || this.skill5Info.skillLevel <= 0 ) {
  293. return 1;
  294. }
  295. let skillInfo = Global.BuildingManager.getSkillLevelInfo(this.skill5Info.skillId, this.skill5Info.skillLevel);
  296. let mt = skillInfo.mt;
  297. return mt;
  298. },
  299. //永久技能3,减少CD时间
  300. getSkill6Ability() {
  301. let skillInfo = this.skills.find(n => {
  302. return n.skillId == 6;
  303. });
  304. skillInfo = Global.BuildingManager.getSkillLevelInfo(skillInfo.skillId, skillInfo.skillLevel);
  305. let rcd = (1 - skillInfo.rcd);
  306. return rcd;
  307. },
  308. });