LevelHomeItem.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. const DWTool = require('../utils/DWTool');
  2. const ThemeManager = require("../utils/ThemeManger");
  3. const { RoomState, GameNotificationKey } = require("../utils/GameEnum");
  4. const GameModule = require("../utils/GameModule");
  5. const TapTapTool = require("../utils/TapTapTool");
  6. cc.Class({
  7. extends: cc.Component,
  8. properties: {
  9. // Public Properties
  10. bgNode: cc.Node,
  11. /** 当前显示的图片 */
  12. buildSprite: cc.Sprite,
  13. /** 柱子 */
  14. pillarTop: cc.Sprite,
  15. pillarBottom: cc.Sprite,
  16. pillarRight: cc.Sprite,
  17. pillarLeft: cc.Sprite,
  18. bottomBg: cc.Sprite,
  19. lockBtnFrames: [cc.SpriteFrame],
  20. /** 升级按钮的两种状态图 */
  21. updateBtnFrames: [cc.SpriteFrame],
  22. /** 未解锁状态的节点 */
  23. lockNode: cc.Node,
  24. /** 需要花费所有金币 */
  25. costLabel: cc.Label,
  26. /** 未解锁的建筑名称 */
  27. unLockBuildName: cc.RichText,
  28. /** 未解锁需要花费多少金币 */
  29. unlockRichText: cc.RichText,
  30. /** 未解锁需要总部等级 */
  31. unlockLevelLabel: cc.Label,
  32. /** 建筑昵称 */
  33. buildNameLabel: cc.Label,
  34. /** 这里当做升级建筑按钮 */
  35. updateBtn: cc.Node,
  36. /** 解锁按钮 */
  37. lockBtn: cc.Sprite,
  38. /** 将要解锁建筑的类型图 */
  39. unlockBuildingType: cc.Sprite,
  40. lockBottomNode: cc.Node,
  41. /** 等级进度条 */
  42. levelProgressBar: cc.ProgressBar,
  43. /** 生产了多少金币 */
  44. rateProgressLabel: cc.Label,
  45. /** 下一级生产多少金币 */
  46. nextRateLabel: cc.Label,
  47. openDoorSkeletion: sp.Skeleton,
  48. updateSkeletion: sp.Skeleton,
  49. // 满级提示
  50. maxNode: cc.Node,
  51. //
  52. artistNode: cc.Node,
  53. //
  54. artistMan: cc.Prefab,
  55. awardWrap: cc.Node,
  56. awardPrefab: cc.Prefab,
  57. _itemId: 0,
  58. skillEfcNode: cc.Node,
  59. coupletLeftSrpite: cc.Sprite,
  60. coupletRightSrpite: cc.Sprite,
  61. //对联数组
  62. coupletFrames: [cc.SpriteFrame]
  63. },
  64. // LIFE-CYCLE CALLBACKS:
  65. onLoad () {
  66. this.setupUI();
  67. this.openDoorSkeletion.setCompleteListener(() => {
  68. // 升级建筑
  69. this.updateBuilding();
  70. GameEvent.fire(GameNotificationKey.PlaySuccessAnimation, true);
  71. });
  72. //点击解锁建筑
  73. let self = this;
  74. this.unlockBuildingEvent = _.debounce(() => {
  75. // 如果当前不够钱, 点击不生效
  76. if (!TapTapTool.compare(GameModule.userInfo.gold, self.data.unlockScore)) {
  77. return;
  78. }
  79. // 点完立刻隐藏
  80. self.lockBtn.node.active = false;
  81. let position = self.lockBtn.node.convertToWorldSpace(cc.v2(self.lockBtn.node.width / 2, self.lockBtn.node.height / 2));
  82. GameEvent.fire(GameNotificationKey.PlayUpdateCoinAnimation, position);
  83. self.openDoorSkeletion.setAnimation(1, "changjing_kaiqi2");
  84. self.lockBottomNode.runAction(cc.fadeOut(0.7));
  85. self.lockBottomNode.active = false;
  86. }, 1000, true);
  87. },
  88. setupUI() {
  89. this.humanList = [];
  90. this.humanPool = new cc.NodePool();
  91. for (let i = 0; i < 5; i++) {
  92. let artist = cc.instantiate(this.artistMan);
  93. this.humanPool.put(artist);
  94. this.humanList.push(artist);
  95. }
  96. },
  97. start () {
  98. },
  99. //每帧更新数据
  100. update (dt) {
  101. if (this.data) {
  102. // 不断刷新界面
  103. this.layout(this.data);
  104. }
  105. },
  106. updateItem(data, itemId, resetCallback) {
  107. if (!isNaN(itemId)) {
  108. // if (this._itemId == itemId && itemId != 0) {
  109. // return;
  110. // }
  111. this._itemId = itemId;
  112. }
  113. if (typeof data != 'object') {
  114. return;
  115. }
  116. if (resetCallback) {
  117. this.resetCallback = resetCallback;
  118. }
  119. this.data = data;
  120. this.buildNameLabel.string = `等级${data.level} ${data.name}` ;
  121. this.refreshTheme();
  122. this.layout(data);
  123. // 配置界面上的奖励礼包
  124. this.configAward();
  125. //升级房间时不用刷新人物
  126. if (!isNaN(itemId)) {
  127. this.artistListLayout();
  128. }
  129. },
  130. layout(data) {
  131. // console.log(GameModule.skill.isUsingSkill3);
  132. // 判断是否有下一级, 没有的话就是满级
  133. if (data.hasNext === 1 && data.level < Global.BuildingManager.getLevelCount(data.roomId)) {
  134. // 判断是否已经解锁
  135. if (data.isUnlocked) {
  136. let ratio = data.level % 25;
  137. this.levelProgressBar.progress = ratio / 25;
  138. //
  139. let gold1 = TapTapTool.goldStrToClass(data.gold1);
  140. let gold2 = TapTapTool.goldStrToClass(data.gold2);
  141. let roomMt = TapTapTool.goldStrToClass(data.roomMt);
  142. var secondGold = TapTapTool.multiple(gold1,data.level);
  143. secondGold = TapTapTool.add(secondGold, gold2);
  144. secondGold = TapTapTool.multiple(secondGold,roomMt);
  145. secondGold = TapTapTool.multiple(secondGold, GameModule.skill.multiple);
  146. secondGold = TapTapTool.multiple(secondGold, GameModule.userInfo.perpetualMt);
  147. this.rateProgressLabel.string = `${TapTapTool.parseToString(secondGold)}/秒`;
  148. var nextGold = TapTapTool.multiple(gold1,(data.level+1));
  149. nextGold = TapTapTool.add(nextGold, gold2);
  150. nextGold = TapTapTool.multiple(nextGold,roomMt);
  151. nextGold = TapTapTool.multiple(nextGold, GameModule.skill.multiple);
  152. nextGold = TapTapTool.multiple(nextGold, GameModule.userInfo.perpetualMt);
  153. let nextDiffer = nextGold = TapTapTool.sub(nextGold, secondGold);
  154. this.nextRateLabel.string = `+${TapTapTool.parseToString(nextDiffer)}/秒`;
  155. this.lockNode.active = false;
  156. this.costLabel.string = TapTapTool.parseToString(data.nextUpGold);
  157. // 判断是否有足够的金额解锁
  158. if (TapTapTool.compare(GameModule.userInfo.gold, data.nextUpGold)) {
  159. this.setState(RoomState.Update);
  160. } else {
  161. this.setState(RoomState.UnLock);
  162. }
  163. } else {
  164. this.lockNode.active = true;
  165. this.costLabel.string = 0;
  166. this.unLockBuildName.string = `<b><color=#ffffff>${data.name}</c></b>`;
  167. let unLockData = TapTapTool.goldStrToClass(data.unlockScore);
  168. this.unlockRichText.string = `<img src='coin_small'/><b><color=#ffffff> ${TapTapTool.parseToString(unLockData)}</c><b/>`;
  169. // 判断是否有足够的金额解锁
  170. if (TapTapTool.compare(GameModule.userInfo.gold, unLockData) && GameModule.userInfo.buildingLevel >= data.buildingLevel) {
  171. this.unlockBuildingType.node.active = true;
  172. this.lockBtn.spriteFrame = this.lockBtnFrames[1];
  173. this.lockBtn.node.getComponent(cc.Button).interactable = true;
  174. } else {
  175. this.unlockBuildingType.node.active = false;
  176. this.lockBtn.spriteFrame = this.lockBtnFrames[0];
  177. this.lockBtn.node.getComponent(cc.Button).interactable = false;
  178. }
  179. if (data.buildingLevel > 0) {
  180. this.unlockLevelLabel.node.active = true;
  181. this.unlockLevelLabel.string = `需要总部等级${data.buildingLevel}级`;
  182. } else {
  183. this.unlockLevelLabel.node.active = false;
  184. this.unlockLevelLabel.string = '';
  185. }
  186. this.setState(RoomState.Lock);
  187. }
  188. } else {
  189. this.rate = data.rate;
  190. this.levelProgressBar.progress = 1.0;
  191. this.setState(RoomState.Full);
  192. let gold1 = TapTapTool.goldStrToClass(data.gold1);
  193. let gold2 = TapTapTool.goldStrToClass(data.gold2);
  194. let roomMt = TapTapTool.goldStrToClass(data.roomMt);
  195. var secondGold = TapTapTool.multiple(gold1,data.level);
  196. secondGold = TapTapTool.add(secondGold, gold2);
  197. secondGold = TapTapTool.multiple(secondGold,roomMt);
  198. this.rateProgressLabel.string = `${TapTapTool.parseToString(secondGold)}/秒`;
  199. }
  200. //使用技能3的时候楼层增加对联效果
  201. if (data.isUnlocked && GameModule.skill.isUsingSkill3) {
  202. //大于0为单层显示对联
  203. if (GameModule.skill.skill3Floor > 0) {
  204. if (this.data.roomId % 2 == 0) {
  205. this.skillEfcNode.active = false;
  206. } else {
  207. if (this.skillEfcNode.active == false) {
  208. this.skillEfcNode.active = true;
  209. let arr = this.randNum(0,(this.coupletFrames.length - 1),2);
  210. this.coupletLeftSrpite.spriteFrame = this.coupletFrames[arr[0]];
  211. this.coupletRightSrpite.spriteFrame = this.coupletFrames[arr[1]];
  212. }
  213. }
  214. } else {
  215. if (this.data.roomId % 2 == 0) {
  216. if (this.skillEfcNode.active == false) {
  217. this.skillEfcNode.active = true;
  218. let arr = this.randNum(0,(this.coupletFrames.length - 1),2);
  219. this.coupletLeftSrpite.spriteFrame = this.coupletFrames[arr[0]];
  220. this.coupletRightSrpite.spriteFrame = this.coupletFrames[arr[1]];
  221. }
  222. } else {
  223. this.skillEfcNode.active = false;
  224. }
  225. }
  226. } else {
  227. this.skillEfcNode.active = false;
  228. }
  229. },
  230. randNum(min, max, num) {
  231. var arr = [],
  232. t;
  233. function fn(i) {
  234. for (i; i < num; i++) {
  235. t = parseInt(Math.random() * (max - min + 1) + min);
  236. for(var k in arr) {
  237. if (arr[k] == t) {
  238. fn(i);
  239. break;
  240. }
  241. }
  242. arr[i] = t;
  243. }
  244. }
  245. fn(0);
  246. return arr
  247. },
  248. //根据房间不同状态显示不同界面
  249. setState(state) {
  250. if (this.state === state) { return; }
  251. switch (state) {
  252. case RoomState.Lock:
  253. this.openDoorSkeletion.node.active = true;
  254. this.openDoorSkeletion.clearTracks();
  255. this.openDoorSkeletion.setToSetupPose();
  256. this.lockBtn.node.active = true;
  257. this.lockBottomNode.stopAllActions();
  258. this.lockBottomNode.opacity = 255;
  259. this.lockBottomNode.active = true;
  260. this.lockNode.active = true;
  261. this.updateBtn.active = false;
  262. this.updateBtn.getComponent(cc.Button).interactable = false;
  263. this.updateBtn.getComponent(cc.Sprite).spriteFrame = this.updateBtnFrames[0];
  264. this.maxNode.active = false;
  265. break;
  266. case RoomState.UnLock:
  267. this.openDoorSkeletion.node.active = false;
  268. this.lockBtn.node.active = false;
  269. this.lockBottomNode.active = false;
  270. this.updateBtn.active = true;
  271. this.lockNode.active = false;
  272. this.updateBtn.getComponent(cc.Button).interactable = false;
  273. this.updateBtn.getComponent(cc.Sprite).spriteFrame = this.updateBtnFrames[0];
  274. this.maxNode.active = false;
  275. break;
  276. case RoomState.Update:
  277. this.openDoorSkeletion.node.active = false;
  278. this.lockBtn.node.active = false;
  279. this.lockBottomNode.active = false;
  280. this.updateBtn.active = true;
  281. this.lockNode.active = false;
  282. this.updateBtn.getComponent(cc.Sprite).spriteFrame = this.updateBtnFrames[1];
  283. this.updateBtn.getComponent(cc.Button).interactable = true;
  284. this.maxNode.active = false;
  285. break;
  286. case RoomState.Full:
  287. this.openDoorSkeletion.node.active = false;
  288. this.lockBtn.node.active = false;
  289. this.lockBottomNode.active = false;
  290. this.lockNode.active = false;
  291. this.maxNode.active = true;
  292. this.updateBtn.active = false;
  293. default:
  294. break;
  295. }
  296. this.state = state;
  297. },
  298. refreshTheme() {
  299. ThemeManager.setItemBuildSpriteFrame(this.data.roomId, this.buildSprite);
  300. let lockBottomSprite = this.lockBottomNode.getComponent(cc.Sprite);
  301. ThemeManager.setItemLockDownSpriteFrame(lockBottomSprite);
  302. },
  303. setListViewAdapter(listViewAdapter) {
  304. this._listViewAdapter = listViewAdapter;
  305. },
  306. deleteItem() {
  307. this._listViewAdapter.removeItem(this);
  308. },
  309. // 解锁建筑事件
  310. unlockBuilding() {
  311. this.unlockBuildingEvent();
  312. },
  313. updateBuildingEvent() {
  314. if (this.state === RoomState.Update) {
  315. let position = this.updateBtn.convertToWorldSpace(cc.v2(this.updateBtn.width / 2, this.updateBtn.height / 2));
  316. GameEvent.fire(GameNotificationKey.PlayUpdateCoinAnimation, position);
  317. this.updateBuilding();
  318. if (!this.updateSkeletion.node.active) {
  319. this.updateSkeletion.node.active = true;
  320. this.updateSkeletion.setAnimation(0, "changjing_sj");
  321. this.updateSkeletion.setCompleteListener(() => {
  322. this.updateSkeletion.node.active = false;
  323. });
  324. }
  325. }
  326. },
  327. // 升级建筑
  328. updateBuilding() {
  329. //升级前判断金币是否足够
  330. if (this.data.isUnlocked) {
  331. if (!TapTapTool.compare(GameModule.userInfo.gold, this.data.nextUpGold)) {
  332. return;
  333. }
  334. } else {
  335. if (!TapTapTool.compare(GameModule.userInfo.gold, this.data.unlockScore)) {
  336. return;
  337. }
  338. }
  339. GameModule.audioMng.playUpdateBuilding();
  340. // 从配置文件里获取
  341. let maxLevel = Global.BuildingManager.getLevelCount(this.data.roomId);
  342. let nextLevel = this.data.level + 1;
  343. let level = nextLevel > maxLevel ? maxLevel : nextLevel;
  344. let buildModel = Global.BuildingManager.getBuildingInfo(this.data.roomId, level);
  345. buildModel.roomStars = this.data.roomStars;
  346. buildModel.roomMt = this.data.roomMt;
  347. buildModel.awardCount = this.data.awardCount;
  348. buildModel.isUnlocked = 1;
  349. if (this.resetCallback) {
  350. this.resetCallback(buildModel, this._itemId);
  351. }
  352. if (this.data.isUnlocked) {
  353. // 当前楼层已解锁
  354. GameModule.userInfo.gold = TapTapTool.sub(GameModule.userInfo.gold, this.data.nextUpGold);
  355. GameModule.userInfo.updateRecordModify(buildModel);
  356. } else {
  357. // 当前楼层未解锁
  358. GameModule.userInfo.gold = TapTapTool.sub(GameModule.userInfo.gold, this.data.unlockScore);
  359. GameModule.userInfo.recordUnlockModify.push(buildModel);
  360. // 成功解锁后立刻调用上报,提交数据
  361. GameModule.userInfo.doReport();
  362. GameEvent.fire(GameNotificationKey.UnlockLevelHome,buildModel);
  363. }
  364. this.updateItem(buildModel);
  365. GameEvent.fire(GameNotificationKey.RefreshBuildingData, buildModel);
  366. },
  367. artistListLayout() {
  368. for (let child of this.humanList) {
  369. this.humanPool.put(child);
  370. }
  371. let self = this;
  372. let addHuman = function (artist, index) {
  373. let human = null;
  374. if (self.humanPool.size() > 0) {
  375. human = self.humanPool.get();
  376. } else {
  377. human = cc.instantiate(self.artistMan);
  378. self.humanList.push(human);
  379. }
  380. self.artistNode.addChild(human);
  381. let direction = (index > 0) ? 1 : -1;
  382. human.getComponent('ArtistMan').init(artist, direction);
  383. };
  384. if (this.data.isUnlocked && this.data.roomStars && this.data.roomStars.length > 0) {
  385. let direction = (Math.random() - 0.5) * 2;
  386. for(let i = 0; i < this.data.roomStars.length; ++i) {
  387. let starId = this.data.roomStars[i];
  388. let artist = new Object();
  389. artist.starId = starId;
  390. addHuman(artist, direction);
  391. direction = -direction;
  392. }
  393. }
  394. },
  395. configAward() {
  396. //每25级可以领取一次里程碑加成奖励
  397. let awardCount = this.data.awardCount;
  398. let totalCount = Math.floor(this.data.level / 25);
  399. if (awardCount < totalCount) {
  400. if (!this.isHasAward) {
  401. let awardNode = cc.instantiate(this.awardPrefab);
  402. this.awardScript = awardNode.getComponent('LevelHomeAward');
  403. this.awardWrap.addChild(awardNode);
  404. this.showProp();
  405. } else {
  406. if (parseInt(this.data.level) % 25 == 0) {
  407. this.showProp();
  408. }
  409. }
  410. } else {
  411. if (this.awardScript) {
  412. this.isHasAward = false;
  413. this.awardScript.node.position = cc.v2(150, -10);
  414. this.awardScript.node.active = false;
  415. this.awardScript.isPlay = false;
  416. this.awardScript.isPlaying = false;
  417. }
  418. }
  419. },
  420. hideAward() {
  421. this.isHasAward = false;
  422. this.awardScript.node.position = cc.v2(150, -10);
  423. this.awardScript.node.active = false;
  424. this.awardScript.isPlay = false;
  425. this.awardScript.isPlaying = false;
  426. this.configAward();
  427. },
  428. showProp(isPlayAnimation=true) {
  429. if (!this.isHasAward) {
  430. this.isHasAward = true;
  431. this.awardScript.init(this.data.roomId, (awardCount) => {
  432. // 显示领取动画
  433. this.data.awardCount = awardCount;
  434. this.hideAward();
  435. });
  436. if (isPlayAnimation) {
  437. this.awardScript.showAnimation();
  438. } else {
  439. this.awardScript.node.position = cc.v2(150, -10);
  440. this.awardScript.node.active = true;
  441. }
  442. } else {
  443. this.awardScript.updateAnimation();
  444. }
  445. },
  446. });