LevelHomeItem.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  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. this.updateBtn.on(cc.Node.EventType.TOUCH_START, () => {
  89. if (!this._isTouch) {
  90. /// 最起码要按住1秒
  91. this._isTouch = true;
  92. this.schedule(this.longUpdateBuilding, 0.3);
  93. }
  94. }, this);
  95. this.updateBtn.on(cc.Node.EventType.TOUCH_CANCEL, () => {
  96. this._isTouch = false;
  97. this.unschedule(this.longUpdateBuilding, this);
  98. }, this);
  99. this.updateBtn.on(cc.Node.EventType.TOUCH_END, () => {
  100. this._isTouch = false;
  101. this.updateBuildingEvent();
  102. this.unschedule(this.longUpdateBuilding, this);
  103. }, this);
  104. },
  105. //长按升级建筑
  106. longUpdateBuilding() {
  107. this.updateBuildingEvent();
  108. },
  109. setupUI() {
  110. this.humanList = [];
  111. this.humanPool = new cc.NodePool();
  112. for (let i = 0; i < 5; i++) {
  113. let artist = cc.instantiate(this.artistMan);
  114. this.humanPool.put(artist);
  115. this.humanList.push(artist);
  116. }
  117. },
  118. start () {
  119. },
  120. //每帧更新数据
  121. update (dt) {
  122. if (this.data) {
  123. // 不断刷新界面
  124. this.layout(this.data);
  125. }
  126. },
  127. updateItem(data, itemId, resetCallback) {
  128. if (!isNaN(itemId)) {
  129. // if (this._itemId == itemId && itemId != 0) {
  130. // return;
  131. // }
  132. this._itemId = itemId;
  133. }
  134. if (typeof data != 'object') {
  135. return;
  136. }
  137. if (resetCallback) {
  138. this.resetCallback = resetCallback;
  139. }
  140. this.data = data;
  141. this.buildNameLabel.string = `等级${data.level} ${data.name}` ;
  142. this.refreshTheme();
  143. this.layout(data);
  144. // 配置界面上的奖励礼包
  145. this.configAward();
  146. //升级房间时不用刷新人物
  147. if (!isNaN(itemId)) {
  148. this.artistListLayout();
  149. }
  150. },
  151. layout(data) {
  152. // console.log(GameModule.skill.isUsingSkill3);
  153. // 判断是否有下一级, 没有的话就是满级
  154. if (data.hasNext === 1 && data.level < GameGlobal.BuildingManager.getLevelCount(data.roomId)) {
  155. // 判断是否已经解锁
  156. if (data.isUnlocked) {
  157. let ratio = data.level % 25;
  158. this.levelProgressBar.progress = ratio / 25;
  159. //
  160. let gold1 = TapTapTool.goldStrToClass(data.gold1);
  161. let gold2 = TapTapTool.goldStrToClass(data.gold2);
  162. let roomMt = TapTapTool.goldStrToClass(data.roomMt);
  163. var secondGold = TapTapTool.multiple(gold1,data.level);
  164. secondGold = TapTapTool.add(secondGold, gold2);
  165. var nextGold = TapTapTool.multiple(gold1,(data.level+1));
  166. nextGold = TapTapTool.add(nextGold, gold2);
  167. let nextDiffer = TapTapTool.sub(nextGold, secondGold);
  168. secondGold = TapTapTool.multiple(secondGold,roomMt);
  169. secondGold = TapTapTool.multiple(secondGold, GameModule.skill.multiple);
  170. secondGold = TapTapTool.multiple(secondGold, GameModule.userInfo.perpetualMt);
  171. secondGold = TapTapTool.multiple(secondGold, GameModule.shop.multiple);
  172. this.rateProgressLabel.string = `${TapTapTool.parseToString(secondGold)}/秒`;
  173. nextDiffer = TapTapTool.multiple(nextDiffer,roomMt);
  174. nextDiffer = TapTapTool.multiple(nextDiffer, GameModule.skill.multiple);
  175. nextDiffer = TapTapTool.multiple(nextDiffer, GameModule.shop.multiple);
  176. nextDiffer = TapTapTool.multiple(nextDiffer, GameModule.userInfo.perpetualMt);
  177. this.nextRateLabel.string = `+${TapTapTool.parseToString(nextDiffer)}/秒`;
  178. this.lockNode.active = false;
  179. this.costLabel.string = TapTapTool.parseToString(data.nextUpGold);
  180. // 判断是否有足够的金额解锁
  181. if (TapTapTool.compare(GameModule.userInfo.gold, data.nextUpGold)) {
  182. this.setState(RoomState.Update);
  183. } else {
  184. this.setState(RoomState.UnLock);
  185. }
  186. } else {
  187. this.lockNode.active = true;
  188. this.costLabel.string = 0;
  189. this.unLockBuildName.string = `<b><color=#ffffff>${data.name}</c></b>`;
  190. let unLockData = TapTapTool.goldStrToClass(data.unlockScore);
  191. this.unlockRichText.string = `<img src='coin_small'/><b><color=#ffffff> ${TapTapTool.parseToString(unLockData)}</c><b/>`;
  192. // 判断是否有足够的金额解锁
  193. if (TapTapTool.compare(GameModule.userInfo.gold, unLockData) && GameModule.userInfo.buildingLevel >= data.buildingLevel) {
  194. this.unlockBuildingType.node.active = true;
  195. this.lockBtn.spriteFrame = this.lockBtnFrames[1];
  196. this.lockBtn.node.getComponent(cc.Button).interactable = true;
  197. } else {
  198. this.unlockBuildingType.node.active = false;
  199. this.lockBtn.spriteFrame = this.lockBtnFrames[0];
  200. this.lockBtn.node.getComponent(cc.Button).interactable = false;
  201. }
  202. if (data.buildingLevel > 0) {
  203. this.unlockLevelLabel.node.active = true;
  204. this.unlockLevelLabel.string = `需要总部等级${data.buildingLevel}级`;
  205. } else {
  206. this.unlockLevelLabel.node.active = false;
  207. this.unlockLevelLabel.string = '';
  208. }
  209. this.setState(RoomState.Lock);
  210. }
  211. } else {
  212. this.rate = data.rate;
  213. this.levelProgressBar.progress = 1.0;
  214. this.setState(RoomState.Full);
  215. let gold1 = TapTapTool.goldStrToClass(data.gold1);
  216. let gold2 = TapTapTool.goldStrToClass(data.gold2);
  217. let roomMt = TapTapTool.goldStrToClass(data.roomMt);
  218. var secondGold = TapTapTool.multiple(gold1,data.level);
  219. secondGold = TapTapTool.add(secondGold, gold2);
  220. secondGold = TapTapTool.multiple(secondGold,roomMt);
  221. this.rateProgressLabel.string = `${TapTapTool.parseToString(secondGold)}/秒`;
  222. }
  223. //使用技能3的时候楼层增加对联效果
  224. if (data.isUnlocked && GameModule.skill.isUsingSkill3) {
  225. //大于0为单层显示对联
  226. if (GameModule.skill.skill3Floor > 0) {
  227. if (this.data.roomId % 2 == 0) {
  228. this.skillEfcNode.active = false;
  229. } else {
  230. if (this.skillEfcNode.active == false) {
  231. this.skillEfcNode.active = true;
  232. let arr = this.randNum(0,(this.coupletFrames.length - 1),2);
  233. this.coupletLeftSrpite.spriteFrame = this.coupletFrames[arr[0]];
  234. this.coupletRightSrpite.spriteFrame = this.coupletFrames[arr[1]];
  235. }
  236. }
  237. } else {
  238. if (this.data.roomId % 2 == 0) {
  239. if (this.skillEfcNode.active == false) {
  240. this.skillEfcNode.active = true;
  241. let arr = this.randNum(0,(this.coupletFrames.length - 1),2);
  242. this.coupletLeftSrpite.spriteFrame = this.coupletFrames[arr[0]];
  243. this.coupletRightSrpite.spriteFrame = this.coupletFrames[arr[1]];
  244. }
  245. } else {
  246. this.skillEfcNode.active = false;
  247. }
  248. }
  249. } else {
  250. this.skillEfcNode.active = false;
  251. }
  252. },
  253. randNum(min, max, num) {
  254. var arr = [],
  255. t;
  256. function fn(i) {
  257. for (i; i < num; i++) {
  258. t = parseInt(Math.random() * (max - min + 1) + min);
  259. for(var k in arr) {
  260. if (arr[k] == t) {
  261. fn(i);
  262. break;
  263. }
  264. }
  265. arr[i] = t;
  266. }
  267. }
  268. fn(0);
  269. return arr
  270. },
  271. //根据房间不同状态显示不同界面
  272. setState(state) {
  273. if (this.state === state) { return; }
  274. switch (state) {
  275. case RoomState.Lock:
  276. this.openDoorSkeletion.node.active = true;
  277. this.openDoorSkeletion.clearTracks();
  278. this.openDoorSkeletion.setToSetupPose();
  279. this.lockBtn.node.active = true;
  280. this.lockBottomNode.stopAllActions();
  281. this.lockBottomNode.opacity = 255;
  282. this.lockBottomNode.active = true;
  283. this.lockNode.active = true;
  284. this.updateBtn.active = false;
  285. this.updateBtn.getComponent(cc.Button).interactable = false;
  286. this.updateBtn.getComponent(cc.Sprite).spriteFrame = this.updateBtnFrames[0];
  287. this.maxNode.active = false;
  288. break;
  289. case RoomState.UnLock:
  290. this.openDoorSkeletion.node.active = false;
  291. this.lockBtn.node.active = false;
  292. this.lockBottomNode.active = false;
  293. this.updateBtn.active = true;
  294. this.lockNode.active = false;
  295. this.updateBtn.getComponent(cc.Button).interactable = false;
  296. this.updateBtn.getComponent(cc.Sprite).spriteFrame = this.updateBtnFrames[0];
  297. this.maxNode.active = false;
  298. break;
  299. case RoomState.Update:
  300. this.openDoorSkeletion.node.active = false;
  301. this.lockBtn.node.active = false;
  302. this.lockBottomNode.active = false;
  303. this.updateBtn.active = true;
  304. this.lockNode.active = false;
  305. this.updateBtn.getComponent(cc.Sprite).spriteFrame = this.updateBtnFrames[1];
  306. this.updateBtn.getComponent(cc.Button).interactable = true;
  307. this.maxNode.active = false;
  308. break;
  309. case RoomState.Full:
  310. this.openDoorSkeletion.node.active = false;
  311. this.lockBtn.node.active = false;
  312. this.lockBottomNode.active = false;
  313. this.lockNode.active = false;
  314. this.maxNode.active = true;
  315. this.updateBtn.active = false;
  316. default:
  317. break;
  318. }
  319. this.state = state;
  320. },
  321. refreshTheme() {
  322. ThemeManager.setItemBuildSpriteFrame(this.data.roomId, this.buildSprite);
  323. let lockBottomSprite = this.lockBottomNode.getComponent(cc.Sprite);
  324. ThemeManager.setItemLockDownSpriteFrame(lockBottomSprite);
  325. },
  326. setListViewAdapter(listViewAdapter) {
  327. this._listViewAdapter = listViewAdapter;
  328. },
  329. deleteItem() {
  330. this._listViewAdapter.removeItem(this);
  331. },
  332. // 解锁建筑事件
  333. unlockBuilding() {
  334. this.unlockBuildingEvent();
  335. },
  336. updateBuildingEvent() {
  337. if (this.state === RoomState.Update) {
  338. let position = this.updateBtn.convertToWorldSpace(cc.v2(this.updateBtn.width / 2, this.updateBtn.height / 2));
  339. GameEvent.fire(GameNotificationKey.PlayUpdateCoinAnimation, position);
  340. this.updateBuilding();
  341. if (!this.updateSkeletion.node.active) {
  342. this.updateSkeletion.node.active = true;
  343. this.updateSkeletion.setAnimation(0, "changjing_sj");
  344. this.updateSkeletion.setCompleteListener(() => {
  345. this.updateSkeletion.node.active = false;
  346. });
  347. }
  348. }
  349. },
  350. // 升级建筑
  351. updateBuilding() {
  352. //升级前判断金币是否足够
  353. if (this.data.isUnlocked) {
  354. if (!TapTapTool.compare(GameModule.userInfo.gold, this.data.nextUpGold)) {
  355. return;
  356. }
  357. } else {
  358. if (!TapTapTool.compare(GameModule.userInfo.gold, this.data.unlockScore)) {
  359. return;
  360. }
  361. }
  362. GameModule.audioMng.playUpdateBuilding();
  363. // 从配置文件里获取
  364. let maxLevel = GameGlobal.BuildingManager.getLevelCount(this.data.roomId);
  365. let nextLevel = this.data.level + 1;
  366. let level = nextLevel > maxLevel ? maxLevel : nextLevel;
  367. let buildModel = GameGlobal.BuildingManager.getBuildingInfo(this.data.roomId, level);
  368. buildModel.roomStars = this.data.roomStars;
  369. buildModel.roomMt = this.data.roomMt;
  370. buildModel.awardCount = this.data.awardCount;
  371. buildModel.isUnlocked = 1;
  372. if (this.resetCallback) {
  373. this.resetCallback(buildModel, this._itemId);
  374. }
  375. if (this.awardScript) {
  376. this.awardScript.roomLevel = level;
  377. }
  378. if (this.data.isUnlocked) {
  379. // 当前楼层已解锁
  380. GameModule.userInfo.gold = TapTapTool.sub(GameModule.userInfo.gold, this.data.nextUpGold);
  381. GameModule.userInfo.updateRecordModify(buildModel);
  382. } else {
  383. // 当前楼层未解锁
  384. GameModule.userInfo.gold = TapTapTool.sub(GameModule.userInfo.gold, this.data.unlockScore);
  385. GameModule.userInfo.recordUnlockModify.push(buildModel);
  386. // 成功解锁后立刻调用上报,提交数据
  387. GameModule.userInfo.doReport();
  388. GameEvent.fire(GameNotificationKey.UnlockLevelHome,buildModel);
  389. }
  390. this.updateItem(buildModel);
  391. GameEvent.fire(GameNotificationKey.RefreshBuildingData, buildModel);
  392. },
  393. artistListLayout() {
  394. for (let child of this.humanList) {
  395. this.humanPool.put(child);
  396. }
  397. let self = this;
  398. let addHuman = function (artist, index) {
  399. let human = null;
  400. if (self.humanPool.size() > 0) {
  401. human = self.humanPool.get();
  402. } else {
  403. human = cc.instantiate(self.artistMan);
  404. self.humanList.push(human);
  405. }
  406. self.artistNode.addChild(human);
  407. let direction = (index > 0) ? 1 : -1;
  408. human.getComponent('ArtistMan').init(artist, direction);
  409. };
  410. if (this.data.isUnlocked && this.data.roomStars && this.data.roomStars.length > 0) {
  411. let direction = (Math.random() - 0.5) * 2;
  412. for(let i = 0; i < this.data.roomStars.length; ++i) {
  413. let starId = this.data.roomStars[i];
  414. let artist = new Object();
  415. artist.starId = starId;
  416. addHuman(artist, direction);
  417. direction = -direction;
  418. }
  419. }
  420. },
  421. configAward() {
  422. //每25级可以领取一次里程碑加成奖励
  423. let awardCount = this.data.awardCount;
  424. let totalCount = Math.floor(this.data.level / 25);
  425. if (awardCount < totalCount) {
  426. if (!this.isHasAward) {
  427. let awardNode = cc.instantiate(this.awardPrefab);
  428. this.awardScript = awardNode.getComponent('LevelHomeAward');
  429. this.awardWrap.addChild(awardNode);
  430. this.showProp();
  431. } else {
  432. if (parseInt(this.data.level) % 25 == 0) {
  433. this.showProp();
  434. }
  435. }
  436. } else {
  437. if (this.awardScript) {
  438. this.isHasAward = false;
  439. this.awardScript.node.position = cc.v2(150, -10);
  440. this.awardScript.node.active = false;
  441. this.awardScript.isPlay = false;
  442. this.awardScript.isPlaying = false;
  443. }
  444. }
  445. },
  446. hideAward() {
  447. this.isHasAward = false;
  448. this.awardScript.node.position = cc.v2(150, -10);
  449. this.awardScript.node.active = false;
  450. this.awardScript.isPlay = false;
  451. this.awardScript.isPlaying = false;
  452. this.configAward();
  453. },
  454. showProp(isPlayAnimation=true) {
  455. if (!this.isHasAward) {
  456. this.isHasAward = true;
  457. this.awardScript.init(this.data.roomId, this.data.level, (awardCount) => {
  458. // 显示领取动画
  459. this.data.awardCount = awardCount;
  460. this.hideAward();
  461. });
  462. if (isPlayAnimation) {
  463. this.awardScript.showAnimation();
  464. } else {
  465. this.awardScript.node.position = cc.v2(150, -10);
  466. this.awardScript.node.active = true;
  467. }
  468. } else {
  469. this.awardScript.updateAnimation();
  470. }
  471. },
  472. });