LevelHomeItem.js 19 KB

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