LevelHomeItem.js 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042
  1. const DWTool = require('../utils/DWTool');
  2. const { RoomState, GameNotificationKey, LevelHomeArtistItemStyle } = require("../utils/GameEnum");
  3. const GameModule = require("../utils/GameModule");
  4. const ThemeManager = require("../utils/ThemeManger");
  5. const HomeApi = require("../net/HomeApi");
  6. // const StateMachine = require('../lib/StateMachine');
  7. // const StateMachineHistory = require('../lib/StateMachineHistory');
  8. cc.Class({
  9. extends: cc.Component,
  10. properties: {
  11. // Public Properties
  12. bgNode: cc.Node,
  13. /** 当前显示的图片 */
  14. buildSprite: cc.Sprite,
  15. /** 柱子 */
  16. pillarTop: cc.Sprite,
  17. pillarBottom: cc.Sprite,
  18. pillarRight: cc.Sprite,
  19. pillarLeft: cc.Sprite,
  20. bottomBg: cc.Sprite,
  21. lockBtnFrames: [cc.SpriteFrame],
  22. /** 升级按钮的两种状态图 */
  23. updateBtnFrames: [cc.SpriteFrame],
  24. /** 未解锁状态的节点 */
  25. lockNode: cc.Node,
  26. /** 需要花费所有金币 */
  27. costLabel: cc.Label,
  28. /** 未解锁的建筑名称 */
  29. unLockBuildName: cc.RichText,
  30. /** 未解锁需要花费多少金币 */
  31. unlockRichText: cc.RichText,
  32. /** 建筑昵称 */
  33. buildNameLabel: cc.Label,
  34. /** 这里当做升级建筑按钮 */
  35. updateBtn: cc.Sprite,
  36. /** 解锁按钮 */
  37. lockBtn: cc.Sprite,
  38. /** 将要解锁建筑的类型图 */
  39. unlockBuildingType: cc.Sprite,
  40. lockBottomNode: cc.Node,
  41. /** 等级节点 */
  42. levelProgressNode: cc.Node,
  43. /** 等级进度条 */
  44. levelProgressBar: cc.ProgressBar,
  45. /** 等级 */
  46. levelProgressLabel: cc.Label,
  47. /** 生产金币节点 */
  48. rateProgressNode: cc.Node,
  49. /** 生产金币进度条 */
  50. rateProgressBar: cc.ProgressBar,
  51. /** 生产了多少金币 */
  52. rateProgressLabel: cc.Label,
  53. /** 倒计时 */
  54. countdownLabel: cc.Label,
  55. /** 金币满了提示 */
  56. coinFullTip: cc.Node,
  57. artistList: cc.Node,
  58. artistListItem: cc.Prefab,
  59. openDoorSkeletion: sp.Skeleton,
  60. updateSkeletion: sp.Skeleton,
  61. // 显示加成的节点
  62. additionNode: cc.Node,
  63. // 显示加成倍数
  64. additionLabel: cc.Label,
  65. // 加成的骨骼
  66. additionSkeleton: sp.Skeleton,
  67. // 满级提示
  68. maxNode: cc.Node,
  69. //入驻艺人提醒
  70. residentTipNode: cc.Node,
  71. // 加成倍数,默认倍率为x1,即无加成
  72. addition: {
  73. get: function () {
  74. if (!this._addition) {
  75. this._addition = 1;
  76. }
  77. return this._addition;
  78. },
  79. set: function (value) {
  80. this._addition = value;
  81. if (this._addition === 1) {
  82. this.additionNode.active = false;
  83. // this.additionSkeleton.setAnimation(0, 'jiasutiao_2', true);
  84. // this.rateProgressBar.barSprite.node.active = true;
  85. } else {
  86. this.additionNode.active = true;
  87. this.additionLabel.string = `X${this._addition}`;
  88. // this.rateProgressBar.barSprite.node.active = false;
  89. }
  90. }
  91. },
  92. coinArrayMax: {
  93. default: 3,
  94. type: cc.Integer,
  95. tooltip: "当前楼层最大存储金币数"
  96. },
  97. coinWrap: cc.Node,
  98. propWrap: cc.Node,
  99. coinPrefab: cc.Prefab,
  100. propPrefab: cc.Prefab,
  101. countDown: {
  102. get: function () {
  103. if (!this._countDown) {
  104. this._countDown = 0;
  105. }
  106. return this._countDown;
  107. },
  108. set: function (value) {
  109. this._countDown = value;
  110. this.countdownLabel.string = DWTool.calculateTime(this._countDown);
  111. this._preCountDown = this._countDown;
  112. }
  113. },
  114. rate: {
  115. get: function () {
  116. if (!this._rate) {
  117. this._rate = 0;
  118. }
  119. return this._rate;
  120. },
  121. set: function (value) {
  122. this._rate = value * this.addition;
  123. this.rateProgressLabel.string = DWTool.coinParse(this._rate);
  124. }
  125. },
  126. notPickupCount: {
  127. get: function () {
  128. if (!this._notPickupCount) {
  129. this._notPickupCount = 0;
  130. }
  131. return this._notPickupCount;
  132. },
  133. set: function (value) {
  134. this._notPickupCount = value;
  135. this.buildingInfo.coinCount = value;
  136. this.isNeedToReport = true;
  137. }
  138. },
  139. },
  140. // LIFE-CYCLE CALLBACKS:
  141. onLoad() {
  142. this.isNeedToReport = false;
  143. this.coinArray = [];
  144. this._currentTime = 0;
  145. this.isFirstLoad = true;
  146. this.humanList = [];
  147. this.isHasProp = false;
  148. this.updateSkeletion.node.active = false;
  149. this._rateProgressWidth = this.rateProgressBar.barSprite.node.width;
  150. this.openDoorSkeletion.setCompleteListener(() => {
  151. // 升级建筑
  152. this.updateBuilding();
  153. GameEvent.fire(GameNotificationKey.PlaySuccessAnimation);
  154. });
  155. let self = this;
  156. this.unlockBuildingEvent = _.debounce(() => {
  157. // 如果当前不够钱, 点击不生效
  158. if (GameModule.userInfo.grossIncome < self.buildingInfo.unlockScore) { return; }
  159. // 点完立刻隐藏
  160. self.lockBtn.node.active = false;
  161. let position = self.lockBtn.node.convertToWorldSpace(cc.v2(self.lockBtn.node.width / 2, self.lockBtn.node.height / 2));
  162. GameEvent.fire(GameNotificationKey.PlayUpdateCoinAnimation, position);
  163. self.openDoorSkeletion.setAnimation(1, "changjing_kaiqi2");
  164. self.lockBottomNode.runAction(cc.fadeOut(0.7));
  165. // this.lockBottomNode.active = false;
  166. }, 1000, true);
  167. // 监听自定义事件
  168. this.setEventListener();
  169. // 升级建筑事件
  170. this.updateBtn.node.on(cc.Node.EventType.TOUCH_END, () => {
  171. this.updateBuildingEvent();
  172. GameModule.audioMng.playUpdateBuilding();
  173. }, this);
  174. this.schedule(() => {
  175. if (this.isNeedToReport) {
  176. this.isNeedToReport = false;
  177. GameModule.userInfo.updateRecordModify(this.buildingInfo);
  178. }
  179. }, 2.0);
  180. this.guideEvent = _.debounce(() => {
  181. GameModule.homeGuide.getComponent('HomeGuide').handleGuideState17(this.artists);
  182. }, 1000, true);
  183. // 配置界面上的金币
  184. this.configCoins();
  185. // 配置界面上的道具
  186. this.configProp();
  187. },
  188. onDestroy() {
  189. GameEvent.off(GameNotificationKey.ResidentArtist, this);
  190. GameEvent.off(GameNotificationKey.RefreshLevelHomeArtistList, this);
  191. GameEvent.off(GameNotificationKey.LevelHomeItemBuildingAllFull, this);
  192. GameEvent.off(GameNotificationKey.ReceiveLevelHomeItemPropUpdate, this);
  193. },
  194. setEventListener() {
  195. // 这个是入驻艺人成功时调用的
  196. GameEvent.on(GameNotificationKey.ResidentArtist, this, (uid, buildingId) => {
  197. if (this.node.active && this.uid === uid && this.buildingInfo.buildingId === buildingId) {
  198. HomeApi.friendGetArtistsInBuilding(this.uid, this.buildingInfo.buildingId, (data) => {
  199. // 为了不让当前的金币出现多种不同金额的币种, 当列表刷新时, 要收取一次
  200. if (this.state === RoomState.Update || this.state === RoomState.Full) {
  201. // 清空当前用来存储金币节点
  202. this.currentCoin = null;
  203. for (let i = 0; i < this.coinArrayMax; i++) {
  204. this.pickupCoin(this.coinArray[i], false);
  205. }
  206. }
  207. }, (code, msg) => {
  208. console.log(msg);
  209. })
  210. }
  211. });
  212. // 这个是召回驱赶艺人时调用的
  213. GameEvent.on(GameNotificationKey.RefreshLevelHomeArtistList, this, (uid, buildingId) => {
  214. if (this.node.active && this.uid === uid && this.buildingInfo.buildingId === buildingId) {
  215. HomeApi.friendGetArtistsInBuilding(this.uid, this.buildingInfo.buildingId, (data) => {
  216. this.artists = data.list || [];
  217. this.artistListLayout();
  218. }, (code, msg) => {
  219. console.log(msg);
  220. })
  221. }
  222. });
  223. // 所有建筑满级之后调用
  224. GameEvent.on(GameNotificationKey.LevelHomeItemBuildingAllFull, this, () => {
  225. if (this.isFirstLoad) { // 代表游戏一打开, 就是满级的
  226. this.currentCoin = null;
  227. this.coinWrap.active = false;
  228. this.artistList.active = false;
  229. this.coinFullTip.active = false;
  230. } else {
  231. GameModule.userInfo.updateRecordModify(this.buildingInfo);
  232. this.currentCoin = null;
  233. for (let i = 0; i < this.coinArrayMax; i++) {
  234. this.pickupCoin(this.coinArray[i]);
  235. }
  236. // 需要把艺人也都清空了
  237. for (let child of this.humanList) { child.destroy(); }
  238. this.coinWrap.active = false;
  239. this.artistList.active = false;
  240. this.coinFullTip.active = false;
  241. }
  242. })
  243. // 离线收益金币动画
  244. // 只显示最底部一层的金币收取
  245. // 其他楼层金币直接隐藏
  246. GameEvent.on(GameNotificationKey.HandleOfflineIncomeAnim, this, () => {
  247. // 清空当前用来存储金币节点
  248. this.currentCoin = null;
  249. let showAnim = this.index == 5 ? true : false
  250. for (let i = 0; i < this.coinArrayMax; i++) {
  251. this.pickupCoinWithNoIncom(this.coinArray[i], showAnim);
  252. }
  253. })
  254. GameEvent.on(GameNotificationKey.ReceiveLevelHomeItemPropUpdate, this, (buildingItem) => {
  255. if (buildingItem.buildingId != this.buildingInfo.buildingId) { return; }
  256. console.log(buildingItem);
  257. this.showProp(buildingItem.item);
  258. });
  259. },
  260. // 解锁建筑事件
  261. unlockBuilding() {
  262. this.unlockBuildingEvent();
  263. },
  264. updateBuildingEvent() {
  265. if (this.state === RoomState.Update) {
  266. // 清空当前用来存储金币节点
  267. this.currentCoin = null;
  268. for (let i = 0; i < this.coinArrayMax; i++) {
  269. this.pickupCoin(this.coinArray[i]);
  270. }
  271. let position = this.updateBtn.node.convertToWorldSpace(cc.v2(this.updateBtn.node.width / 2, this.updateBtn.node.height / 2));
  272. GameEvent.fire(GameNotificationKey.PlayUpdateCoinAnimation, position);
  273. this.updateBuilding();
  274. // if (!this.updateSkeletion.node.active) {
  275. this.updateSkeletion.node.active = true;
  276. this.updateSkeletion.setAnimation(0, "changjing_sj");
  277. this.updateSkeletion.setCompleteListener(() => {
  278. this.updateSkeletion.node.active = false;
  279. });
  280. // }
  281. // 如果好友列表功能已经开放
  282. if (GameModule.tab) {
  283. this.artists = GameModule.tab.getComponent('Tab').artists;
  284. // 尝试触发艺人入驻引导
  285. // this.guideEvent();
  286. }
  287. }
  288. },
  289. configCoins() {
  290. for (let i = 0; i < this.coinArrayMax; i++) {
  291. // 初始化
  292. let coinNode = cc.instantiate(this.coinPrefab)
  293. this.coinWrap.addChild(coinNode);
  294. coinNode = coinNode.getComponent("LevelHomeCoin");
  295. coinNode.index = i;
  296. this.coinHide(coinNode)
  297. this.coinArray[i] = coinNode;
  298. coinNode.node.on(cc.Node.EventType.TOUCH_END, (event) => {
  299. this.pickupCoin(coinNode);
  300. }, this);
  301. }
  302. },
  303. configProp() {
  304. let propNode = cc.instantiate(this.propPrefab)
  305. this.propScript = propNode.getComponent('LevelHomePropItem')
  306. this.propWrap.addChild(propNode);
  307. this.hideProp();
  308. },
  309. hideProp() {
  310. this.isHasProp = false;
  311. this.propScript.node.position = cc.v2(150, -108);
  312. this.propScript.node.active = false;
  313. this.propScript.isPlay = false;
  314. this.propScript.isPlaying = false;
  315. },
  316. showProp(propData, isPlayAnimation=true) {
  317. if (!this.isHasProp) {
  318. this.isHasProp = true;
  319. this.propScript.init(this.buildingInfo.buildingId, propData, (showFrame, showText) => {
  320. // 显示领取动画
  321. this.parent.showActGift(showFrame, showText)
  322. this.hideProp();
  323. });
  324. if (isPlayAnimation) {
  325. this.propScript.showAnimation();
  326. } else {
  327. this.propScript.node.position = cc.v2(150, -28);
  328. this.propScript.node.active = true;
  329. }
  330. } else {
  331. this.propScript.updateProp(propData);
  332. this.propScript.updateAnimation();
  333. }
  334. },
  335. /**
  336. * Public Method, 用来设置建筑背景图
  337. * @param {number} cityId 城市id
  338. * @param {number} index 楼层
  339. */
  340. init(parent, cityId, index) {
  341. if (arguments.length < 1) {
  342. throw new Error("init Missing parameter...");
  343. }
  344. this.parent = parent;
  345. this.cityId = cityId;
  346. this.index = index;
  347. this.isFirstLoad = true;
  348. ThemeManager.setBuildItemColor(this.cityId, this.bgNode);
  349. ThemeManager.setItemBuildSpriteFrame(this.cityId, this.buildSprite, index);
  350. ThemeManager.setItemPillarTopSpriteFrame(this.cityId, this.pillarTop);
  351. ThemeManager.setItemPillarBottomSpriteFrame(this.cityId, this.pillarBottom);
  352. ThemeManager.setItemPillarRightSpriteFrame(this.cityId, this.pillarRight);
  353. ThemeManager.setItemPillarLeftSpriteFrame(this.cityId, this.pillarLeft);
  354. ThemeManager.setItemDownSpriteFrame(this.cityId, this.bottomBg);
  355. let lockBottomSprite = this.lockBottomNode.getComponent(cc.Sprite);
  356. ThemeManager.setItemLockDownSpriteFrame(this.cityId, lockBottomSprite);
  357. },
  358. /**
  359. * Public Method, 配置建筑的内部样式
  360. * @param {*} buildingInfo 建筑信息
  361. * @param {*} uid 当前用户的uid
  362. */
  363. config(buildingInfo, uid, unlockBuildingRecord, resetCallback) {
  364. // console.log(buildingInfo.coinCount);
  365. this.unlockBuildingRecord = unlockBuildingRecord;
  366. if (resetCallback) {
  367. this.resetCallback = resetCallback;
  368. }
  369. if (buildingInfo.buildingId == 1) {
  370. if (buildingInfo.level == 0) {
  371. // 监听完成引导系统state1的事件
  372. GameModule.homeGuide.on('Fire_state1', this.unlockBuildingEvent, this)
  373. // 触发引导系统state1状态
  374. GameModule.homeGuide.getComponent('HomeGuide').handleState('state1')
  375. }
  376. }
  377. this.buildingInfo = buildingInfo;
  378. this.uid = uid;
  379. this.artists = this.buildingInfo.artists;
  380. // 这里设置一些只需要设置一次的数据
  381. this.countDown = buildingInfo.rateUnit;
  382. this.buildNameLabel.string = buildingInfo.name;
  383. this._notPickupCount = buildingInfo.coinCount;
  384. // console.log(buildingInfo.coinCount, this.coinArrayMax);
  385. if (buildingInfo.coinCount === this.coinArrayMax * 10) {
  386. this.rateProgressBar.progress = 1;
  387. this.countdownLabel.string = DWTool.calculateTime(0);
  388. }
  389. this.levelProgressLabel.string = `LV.${buildingInfo.level}`;
  390. if (this.isFirstLoad) {
  391. this.isFirstLoad = false;
  392. this.artistListLayout();
  393. }
  394. //城市开发完毕的时候不显示艺人入驻按钮
  395. if (GameModule.userInfo.levelHomeItemFullCount != 5 && this.cityId === Global.devCityId) {
  396. this.artistList.active = true;
  397. } else {
  398. this.artistList.active = false;
  399. }
  400. this.initializeCoinPosition();
  401. this.layout(buildingInfo);
  402. },
  403. /** 初始化金币的位置 */
  404. initializeCoinPosition() {
  405. // 2018年08月18日15:25, 子奇要求我去好友家园时, 不显示未收取金币
  406. // 城市已经开发完毕不显示金币
  407. if (this.cityId === Global.devCityId && GameModule.userInfo.levelHomeItemFullCount != 5) {
  408. this.coinWrap.active = true;
  409. } else {
  410. this.coinWrap.active = false;
  411. return;
  412. }
  413. if (!this.buildingInfo.coinCount) {
  414. for (let i = 0; i < this.coinArrayMax; i++) {
  415. let coin = this.coinArray[i];
  416. this.coinHide(coin);
  417. }
  418. return;
  419. }
  420. for (let i = 0; i < this.coinArrayMax; i++) {
  421. let coin = this.coinArray[i];
  422. while (this.buildingInfo.coinCount != 0 && coin.coinCount != 10) {
  423. coin.coinCount += 1;
  424. this.buildingInfo.coinCount -= 1;
  425. }
  426. if (coin.coinCount != 0) {
  427. let x = -140 + (85 * i);
  428. coin.isPlay = true;
  429. coin.node.position = cc.v2(x, -28);
  430. coin.node.active = true;
  431. if (this.addition > 1) {
  432. let rate = this.buildingInfo.rate * this.addition;
  433. coin.totalRate = rate * coin.coinCount;
  434. } else {
  435. coin.totalRate = this.buildingInfo.rate * coin.coinCount;
  436. }
  437. coin.initStatic(i);
  438. }
  439. }
  440. },
  441. artistListLayout() {
  442. for (let child of this.artistList.children) { child.destroy(); }
  443. for (let child of this.humanList) { child.destroy(); }
  444. let self = this;
  445. let addAddItemToList = function (showTop = false, showBottom = false) {
  446. let addArtistItem = cc.instantiate(self.artistListItem);
  447. self.artistList.addChild(addArtistItem);
  448. let addArtistScript = addArtistItem.getComponent('LevelHomeArtistItem');
  449. addArtistScript.initWithBuildingInfo(self.buildingInfo, self.uid, true, showTop, showBottom);
  450. }
  451. let addArtistItemToList = function (artist, showTop = false, showBottom = false) {
  452. let artistItem = cc.instantiate(self.artistListItem);
  453. self.artistList.addChild(artistItem);
  454. let artistScript = artistItem.getComponent('LevelHomeArtistItem');
  455. artistScript.initWithArtistData(self.buildingInfo, self.uid, true, artist, showTop, showBottom);
  456. }
  457. let addHuman = function (artist, index) {
  458. DWTool.loadResPrefab("./prefabs/artist_man")
  459. .then((prefab) => {
  460. let human = cc.instantiate(prefab);
  461. human.getComponent('ArtistMan').init(artist);
  462. human.getComponent('ArtistMan').direction = (index > 0) ? 1 : -1;
  463. self.node.addChild(human);
  464. self.humanList.push(human);
  465. });
  466. }
  467. // 当没有自己艺人, 也没有好友艺人入驻时, 这里还得区分主态和客态
  468. if (this.artists.length === 0) {
  469. addAddItemToList(true);
  470. // 没有艺人不显示倍数
  471. this.addition = 1;
  472. } else {
  473. // 有艺人入驻要显示倍数
  474. this.additionNode.active = true;
  475. if (this.artists.length === 1) {
  476. let artist = this.artists[0];
  477. this.addition += artist.stationJobLevel;
  478. // 这里要区分主态和客态, 去别人家园时, 如果有一个是客态自己的艺人, 那么另一个就是可以入驻的按钮
  479. if (artist.role === 2) {
  480. addArtistItemToList(artist, true);
  481. addHuman(artist, (Math.random() - 0.5) * 2);
  482. } else {
  483. addAddItemToList(true, true);
  484. addArtistItemToList(artist);
  485. addHuman(artist, (Math.random() - 0.5) * 2);
  486. }
  487. } else {
  488. for (let i = 0; i < this.artists.length; i++) {
  489. let artist = this.artists[i];
  490. this.addition += artist.stationJobLevel;
  491. if (i === 0) {
  492. addArtistItemToList(artist, true, true);
  493. } else {
  494. addArtistItemToList(artist);
  495. }
  496. addHuman(artist, i);
  497. }
  498. }
  499. }
  500. },
  501. layout(buildingInfo) {
  502. // 判断是否有下一级, 没有的话就是满级
  503. if (buildingInfo.hasNext === 1 && buildingInfo.level <= Global.BuildingManager.getLevelCount(buildingInfo.buildingId)) {
  504. // 判断是否已经解锁
  505. if (buildingInfo.isUnlocked) {
  506. /**
  507. * 2018年09月26日 要求将进度条改成每25级就清空, 重新走, 原来的实现如下:
  508. * this.levelProgressBar.progress = buildingInfo.level / Global.BuildingManager.getLevelCount(buildingInfo.buildingId);
  509. */
  510. let ratio = buildingInfo.level % 25;
  511. this.levelProgressBar.progress = ratio / 25;
  512. this.artistList.active = true;
  513. this.lockNode.active = false;
  514. this.costLabel.string = DWTool.coinParse(buildingInfo.nextUpScore);
  515. this.rate = buildingInfo.rate;
  516. // 判断是否有足够的金额解锁
  517. if (GameModule.userInfo.grossIncome >= buildingInfo.nextUpScore) {
  518. if (buildingInfo.buildingId === 1 && buildingInfo.level === 1) {
  519. // 第三个引导
  520. GameModule.homeGuide.on('Fire_state3', this.updateBuildingEvent, this)
  521. // 触发引导系统state3状态
  522. GameModule.homeGuide.getComponent('HomeGuide').handleState('state3')
  523. }
  524. this.setState(RoomState.Update);
  525. } else {
  526. this.setState(RoomState.UnLock);
  527. }
  528. } else {
  529. this.artistList.active = false;
  530. this.countDown = 0;
  531. this._currentTime = 0;
  532. this.rateProgressBar.progress = 0;
  533. this.totalRate = 0;
  534. this.lockNode.active = true;
  535. this.costLabel.string = 0;
  536. this.hideProp();
  537. this.unLockBuildName.string = `<b><color=#ffffff>${buildingInfo.name}</c></b>`;
  538. this.unlockRichText.string = `<img src='alert_coin'/><b><color=#ffffff> ${DWTool.coinParse(buildingInfo.unlockScore)}</c><b/>`;
  539. // 判断是否有足够的金额解锁
  540. if (GameModule.userInfo.grossIncome >= buildingInfo.unlockScore) {
  541. if (this.unlockBuildingRecord[0] === 1 && this.buildingInfo.buildingId === 2) { // 如果第一层已经解锁, 才能执行下一个引导
  542. setTimeout(() => {
  543. // 监听完成引导系统state4的事件
  544. GameModule.homeGuide.on('Fire_state4', this.unlockBuildingEvent, this)
  545. // 触发引导系统state4状态
  546. GameModule.homeGuide.getComponent('HomeGuide').handleState('state4')
  547. }, 500);
  548. } else if (this.unlockBuildingRecord[1] === 1 && this.buildingInfo.buildingId === 3) {
  549. setTimeout(() => {
  550. // 监听完成引导系统state5的事件
  551. GameModule.homeGuide.on('Fire_state5', this.unlockBuildingEvent, this)
  552. // 触发引导系统state5状态
  553. GameModule.homeGuide.getComponent('HomeGuide').handleState('state5')
  554. }, 500)
  555. }
  556. this.unlockBuildingType.node.active = true;
  557. this.lockBtn.spriteFrame = this.lockBtnFrames[1];
  558. this.lockBtn.node.getComponent(cc.Button).interactable = true;
  559. } else {
  560. this.unlockBuildingType.node.active = false;
  561. this.lockBtn.spriteFrame = this.lockBtnFrames[0];
  562. this.lockBtn.node.getComponent(cc.Button).interactable = false;
  563. }
  564. this.setState(RoomState.Lock);
  565. }
  566. } else {
  567. this.rate = buildingInfo.rate;
  568. this.levelProgressBar.progress = 1.0;
  569. this.setState(RoomState.Full);
  570. }
  571. },
  572. setState(state) {
  573. if (this.state === state) { return; }
  574. switch (state) {
  575. case RoomState.Lock:
  576. this.openDoorSkeletion.node.active = true;
  577. this.openDoorSkeletion.clearTracks();
  578. this.openDoorSkeletion.setToSetupPose();
  579. this.lockBtn.node.active = true;
  580. this.lockBottomNode.stopAllActions();
  581. this.lockBottomNode.opacity = 255;
  582. this.lockBottomNode.active = true;
  583. this.lockNode.active = true;
  584. this.updateBtn.node.active = false;
  585. this.updateBtn.spriteFrame = this.updateBtnFrames[0];
  586. this.updateBtn.node.getComponent(cc.Button).interactable = false;
  587. this.maxNode.active = false;
  588. break;
  589. case RoomState.UnLock:
  590. this.openDoorSkeletion.node.active = false;
  591. this.lockBtn.node.active = false;
  592. this.lockBottomNode.active = false;
  593. this.updateBtn.node.active = true;
  594. this.lockNode.active = false;
  595. this.updateBtn.spriteFrame = this.updateBtnFrames[0];
  596. this.updateBtn.node.getComponent(cc.Button).interactable = false;
  597. this.maxNode.active = false;
  598. break;
  599. case RoomState.Update:
  600. this.openDoorSkeletion.node.active = false;
  601. this.lockBtn.node.active = false;
  602. this.lockBottomNode.active = false;
  603. this.updateBtn.node.active = true;
  604. this.lockNode.active = false;
  605. this.updateBtn.spriteFrame = this.updateBtnFrames[1];
  606. this.updateBtn.node.getComponent(cc.Button).interactable = true;
  607. this.maxNode.active = false;
  608. break;
  609. case RoomState.Full:
  610. this.openDoorSkeletion.node.active = false;
  611. this.lockBtn.node.active = false;
  612. this.lockBottomNode.active = false;
  613. this.lockNode.active = false;
  614. this.maxNode.active = true;
  615. this.updateBtn.node.active = false;
  616. default:
  617. break;
  618. }
  619. this.state = state;
  620. },
  621. // 升级建筑
  622. updateBuilding() {
  623. // 从配置文件里获取
  624. let maxLevel = Global.BuildingManager.getLevelCount(this.buildingInfo.buildingId);
  625. let nextLevel = this.buildingInfo.level + 1;
  626. let level = nextLevel > maxLevel ? maxLevel : nextLevel;
  627. let buildModel = Global.BuildingManager.getBuildingInfo(this.cityId, this.buildingInfo.buildingId, level);
  628. this.isLevelSpeedUp(buildModel);
  629. // 将已有入驻的艺人赋值给新的model, 保持一个引用
  630. buildModel.artists = this.buildingInfo.artists;
  631. if (this.buildingInfo.isUnlocked) {
  632. // 当前楼层已解锁
  633. GameModule.userInfo.grossIncome -= this.buildingInfo.nextUpScore;
  634. GameModule.userInfo.updateRecordModify(buildModel);
  635. } else {
  636. // 当前楼层未解锁
  637. this.unlockBuildingRecord[this.buildingInfo.buildingId - 1] = 1;
  638. GameModule.userInfo.grossIncome -= this.buildingInfo.unlockScore;
  639. GameModule.userInfo.recordUnlockModify.push(buildModel);
  640. // 成功解锁后立刻调用上报,提交ub
  641. GameModule.userInfo.doReport()
  642. }
  643. GameModule.userInfo.stars += 1;
  644. if (this.resetCallback) {
  645. this.resetCallback(buildModel);
  646. }
  647. this.config(buildModel, this.uid);
  648. // 如果满级了, 就发通知告诉userinfo
  649. if (buildModel.hasNext != 1) {
  650. GameEvent.fire(GameNotificationKey.LevelHomeItemBuildingFull);
  651. }
  652. if (buildModel.level === 1) {
  653. GameEvent.fire(GameNotificationKey.LevelHomeItemUnlock);
  654. }
  655. },
  656. /**
  657. * 升级到某个特定等级,根据配置表的金币生产速度和时间,判断是否需要弹出金币x2或者时间-50%的通知。
  658. */
  659. isLevelSpeedUp(buildingInfo) {
  660. if (buildingInfo.level <= 2) {
  661. return;
  662. }
  663. if (buildingInfo.rate >= this.buildingInfo.rate * 2) {
  664. GameEvent.fire(GameNotificationKey.LevelHomeSpeedUp, buildingInfo.name, 1);
  665. } else if (buildingInfo.rateUnit === this.buildingInfo.rateUnit / 2) {
  666. GameEvent.fire(GameNotificationKey.LevelHomeSpeedUp, buildingInfo.name, 2);
  667. }
  668. },
  669. // 收取金币
  670. pickupCoin(coin, isShowAnimation = true) {
  671. if (this.currentCoin === coin) {
  672. this.currentCoin = null;
  673. }
  674. if (isShowAnimation) {
  675. let pos = coin.node.convertToWorldSpace(cc.v2(coin.node.width / 2, coin.node.height / 2));
  676. this.showCollectAnim(pos, coin.coinCount)
  677. }
  678. // 点击一次金币, 就将notPickupCount减去coinNode.coinCount
  679. this.notPickupCount -= coin.coinCount;
  680. // console.log(`收入: ${buildingInfo.rate * coin.coinCount}`);
  681. GameModule.userInfo.grossIncome += (this.buildingInfo.rate * coin.coinCount * this._addition);
  682. this.coinHide(coin);
  683. },
  684. // 收取金币,不增加收益
  685. pickupCoinWithNoIncom(coin, isShowAnimation = true) {
  686. if (this.currentCoin === coin) {
  687. this.currentCoin = null;
  688. }
  689. if (isShowAnimation) {
  690. let pos = coin.node.convertToWorldSpace(cc.v2(coin.node.width / 2, coin.node.height / 2));
  691. this.showCollectAnim(pos, coin.coinCount)
  692. }
  693. // 点击一次金币, 就将notPickupCount减去coinNode.coinCount
  694. this.notPickupCount -= coin.coinCount;
  695. this.coinHide(coin);
  696. },
  697. showCollectAnim(pos, colNums) {
  698. let canvasNode = cc.find("Canvas");
  699. let grossCoin = GameModule.userInfo.grossCoin;
  700. let grossCoinPos = grossCoin.node.convertToWorldSpace(cc.v2(grossCoin.node.width / 2, grossCoin.node.height / 2));
  701. // let colNums = 5
  702. let vSize = cc.view.getVisibleSize();
  703. let target = cc.v2(grossCoinPos.x - vSize.width / 2, grossCoinPos.y - vSize.height / 2)
  704. let i = 0;
  705. let runSt = setInterval(() => {
  706. if (i == colNums) {
  707. clearInterval(runSt)
  708. } else {
  709. let ran = (Math.random() - 0.5) * 2 * 3;
  710. let newCoin = cc.instantiate(this.coinPrefab);
  711. let posX = pos.x - vSize.width / 2;
  712. let posY = pos.y - vSize.height / 2;
  713. canvasNode.addChild(newCoin)
  714. newCoin.x = posX + ran * 15;
  715. newCoin.y = posY + 30 + ran * 15;
  716. newCoin.active = true;
  717. newCoin = newCoin.getComponent("LevelHomeCoin")
  718. newCoin.initAnim()
  719. let cbNotiStart = cc.callFunc(() => {
  720. GameEvent.fire(GameNotificationKey.UserCollectCoin, true);
  721. })
  722. let cbDestroy = cc.callFunc(() => {
  723. newCoin.node.destroy();
  724. })
  725. let act = cc.sequence(cc.moveTo(1, target), cbDestroy, cbNotiStart)
  726. newCoin.node.runAction(act.easing(cc.easeIn(2.1)));
  727. i++
  728. }
  729. }, 100);
  730. },
  731. coinHide(coin) {
  732. // 算出金币的x值, 70是金币宽度, -130是第一个金币的x值
  733. let x = -140 + (85 * coin.index);
  734. // 重置金币状态
  735. coin.node.position = cc.v2(x, -108);
  736. coin.node.active = false;
  737. coin.isPlay = false;
  738. coin.isPlaying = false;
  739. coin.coinCount = 0;
  740. coin.totalRate = 0;
  741. },
  742. getFreeCoin() {
  743. for (let i = 0; i < this.coinArrayMax; i++) {
  744. let coinNode = this.coinArray[i];
  745. if (coinNode.coinCount != 10) {
  746. return coinNode;
  747. }
  748. }
  749. return null;
  750. },
  751. generateCoin() {
  752. if (this.currentCoin && this.currentCoin.coinCount < 10) {
  753. this.currentCoin.totalRate += this.rate;
  754. this.currentCoin.coinCount += 1;
  755. this.currentCoin.updateAnimation();
  756. } else {
  757. this.currentCoin = this.getFreeCoin();
  758. if (this.currentCoin) {
  759. this.currentCoin.totalRate += this.rate;
  760. this.currentCoin.coinCount += 1;
  761. if (this.buildingInfo.buildingId === 1 && this.buildingInfo.level === 1 && this.notPickupCount === 1) {
  762. // 监听完成引导系统state2的事件
  763. GameModule.homeGuide.on('Fire_state2', () => {
  764. this.pickupCoin(this.currentCoin);
  765. }, this);
  766. // 触发引导系统state1状态
  767. GameModule.homeGuide.getComponent('HomeGuide').handleState('state2');
  768. }
  769. if (!this.currentCoin.isPlay) {
  770. this.currentCoin.showAnimation();
  771. } else {
  772. this.currentCoin.updateAnimation();
  773. }
  774. }
  775. }
  776. },
  777. update(dt) {
  778. if (this.buildingInfo) {
  779. // 不断刷新界面
  780. this.layout(this.buildingInfo);
  781. // 只有已经解锁进度条才会走
  782. if (this.buildingInfo.isUnlocked) {
  783. // 进度条走完, 开始生产金币
  784. if (Math.floor(this.rateProgressBar.progress) === 1) {
  785. this.rateProgressBar.progress = 1;
  786. if (GameModule.userInfo.levelHomeItemFullCount != 5 && // 是否满级
  787. this.cityId === Global.devCityId) { // 当前访问的是不是这个city
  788. if (this.notPickupCount >= this.coinArrayMax * 10) {
  789. this.rateProgressLabel.node.active = false;
  790. this.coinFullTip.active = true;
  791. } else {
  792. this.coinFullTip.active = false;
  793. this.notPickupCount += 1;
  794. this.generateCoin();
  795. this.rateProgressBar.progress = 0;
  796. this._currentTime = 0;
  797. }
  798. } else { // 满级后的状态
  799. this.rateProgressBar.progress = 0;
  800. this._currentTime = 0;
  801. this.coinFullTip.active = false;
  802. this.rateProgressLabel.node.active = true;
  803. }
  804. } else {
  805. this.rateProgressLabel.node.active = true;
  806. this.coinFullTip.active = false;
  807. this._currentTime += dt;
  808. this.rateProgressBar.progress = this._currentTime / this.countDown;
  809. if (Math.floor(this.rateProgressBar.progress) === 1) {
  810. this.rateProgressBar.progress = 1;
  811. }
  812. let resultCountDown = this.countDown - Math.floor(this._currentTime);
  813. if (this._preCountDown !== resultCountDown) {
  814. this.countdownLabel.string = DWTool.calculateTime(resultCountDown);
  815. this._preCountDown = resultCountDown;
  816. }
  817. }
  818. }
  819. }
  820. },
  821. setResidentTip(show) {
  822. this.residentTipNode.active = show;
  823. }
  824. });