LevelHomeItem.js 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  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. addition: {
  70. get: function () {
  71. if (!this._addition) {
  72. this._addition = 0;
  73. }
  74. return this._addition;
  75. },
  76. set: function (value) {
  77. this._addition = value;
  78. if (this._addition === 0) {
  79. this.additionNode.active = false;
  80. this.additionSkeleton.setAnimation(0, 'jiasutiao_2', true);
  81. this.rateProgressBar.barSprite.node.active = true;
  82. } else {
  83. this.additionNode.active = true;
  84. this.additionLabel.string = `X${this._addition}`;
  85. this.additionSkeleton.setAnimation(0, 'jiasutiao_1', true);
  86. this.rateProgressBar.barSprite.node.active = false;
  87. }
  88. }
  89. },
  90. coinArrayMax: {
  91. default: 3,
  92. type: cc.Integer,
  93. tooltip: "当前楼层最大存储金币数"
  94. },
  95. coinWrap: cc.Node,
  96. coinPrefab: cc.Prefab,
  97. countDown: {
  98. get: function () {
  99. if (!this._countDown) {
  100. this._countDown = 0;
  101. }
  102. return this._countDown;
  103. },
  104. set: function (value) {
  105. this._countDown = value;
  106. this.countdownLabel.string = DWTool.calculateTime(this._countDown);
  107. this._preCountDown = this._countDown;
  108. }
  109. },
  110. rate: {
  111. get: function () {
  112. if (!this._rate) {
  113. this._rate = 0;
  114. }
  115. return this._rate;
  116. },
  117. set: function (value) {
  118. this._rate = value;
  119. if (this.addition > 0) {
  120. this._rate = this._rate * this.addition;
  121. }
  122. this.rateProgressLabel.string = DWTool.coinParse(this._rate);
  123. }
  124. },
  125. notPickupCount: {
  126. get: function () {
  127. if (!this._notPickupCount) {
  128. this._notPickupCount = 0;
  129. }
  130. return this._notPickupCount;
  131. },
  132. set: function (value) {
  133. this._notPickupCount = value;
  134. this.buildingInfo.coinCount = value;
  135. this.isNeedToReport = true;
  136. }
  137. },
  138. },
  139. // LIFE-CYCLE CALLBACKS:
  140. onLoad() {
  141. this.isNeedToReport = false;
  142. this.coinArray = [];
  143. this._currentTime = 0;
  144. this.isFirstLoad = true;
  145. this.humanList = [];
  146. this.updateSkeletion.node.active = false;
  147. this._rateProgressWidth = this.rateProgressBar.barSprite.node.width;
  148. this.openDoorSkeletion.setCompleteListener(() => {
  149. // 升级建筑
  150. this.updateBuilding();
  151. GameEvent.fire(GameNotificationKey.PlaySuccessAnimation);
  152. });
  153. let self = this;
  154. this.unlockBuildingEvent = _.debounce(() => {
  155. // 如果当前不够钱, 点击不生效
  156. if (GameModule.userInfo.grossIncome < self.buildingInfo.unlockScore) { return; }
  157. // 点完立刻隐藏
  158. self.lockBtn.node.active = false;
  159. let position = self.lockBtn.node.convertToWorldSpace(cc.v2(self.lockBtn.node.width/2, self.lockBtn.node.height/2));
  160. GameEvent.fire(GameNotificationKey.PlayUpdateCoinAnimation, position);
  161. self.openDoorSkeletion.setAnimation(1, "changjing_kaiqi2");
  162. self.lockBottomNode.runAction(cc.fadeOut(0.7));
  163. // this.lockBottomNode.active = false;
  164. }, 1000, true);
  165. // 监听自定义事件
  166. this.setEventListener();
  167. // 升级建筑事件
  168. this.updateBtn.node.on(cc.Node.EventType.TOUCH_END, () => {
  169. this.updateBuildingEvent();
  170. }, this);
  171. this.schedule(() => {
  172. if (this.isNeedToReport) {
  173. this.isNeedToReport = false;
  174. GameModule.userInfo.updateRecordModify(this.buildingInfo);
  175. }
  176. }, 2.0);
  177. // 配置界面上的金币
  178. this.configCoins();
  179. },
  180. onDestroy() {
  181. GameEvent.off(GameNotificationKey.ResidentArtist, this);
  182. GameEvent.off(GameNotificationKey.RefreshLevelHomeArtistList, this);
  183. GameEvent.off(GameNotificationKey.LevelHomeItemBuildingAllFull, this);
  184. },
  185. setEventListener() {
  186. // 这个是入驻艺人成功时调用的
  187. GameEvent.on(GameNotificationKey.ResidentArtist, this, (uid, buildingId) => {
  188. if (this.node.active && this.uid === uid && this.buildingInfo.buildingId === buildingId) {
  189. HomeApi.friendGetArtistsInBuilding(this.uid, this.buildingInfo.buildingId, (data) => {
  190. // 为了不让当前的金币出现多种不同金额的币种, 当列表刷新时, 要收取一次
  191. if (this.state === RoomState.Update || this.state === RoomState.Full) {
  192. // 清空当前用来存储金币节点
  193. this.currentCoin = null;
  194. for (let i = 0; i < this.coinArrayMax; i++) {
  195. this.pickupCoin(this.coinArray[i], false);
  196. }
  197. }
  198. }, (code, msg) => {
  199. console.log(msg);
  200. })
  201. }
  202. });
  203. // 这个是召回驱赶艺人时调用的
  204. GameEvent.on(GameNotificationKey.RefreshLevelHomeArtistList, this, (uid, buildingId) => {
  205. if (this.node.active && this.uid === uid && this.buildingInfo.buildingId === buildingId) {
  206. HomeApi.friendGetArtistsInBuilding(this.uid, this.buildingInfo.buildingId, (data) => {
  207. this.artists = data.list || [];
  208. this.artistListLayout();
  209. }, (code, msg) => {
  210. console.log(msg);
  211. })
  212. }
  213. });
  214. // 所有建筑满级之后调用
  215. GameEvent.on(GameNotificationKey.LevelHomeItemBuildingAllFull, this, () => {
  216. if (this.isFirstLoad) { // 代表游戏一打开, 就是满级的
  217. this.currentCoin = null;
  218. this.coinWrap.active = false;
  219. this.artistList.active = false;
  220. this.coinFullTip.active = false;
  221. } else {
  222. GameModule.userInfo.updateRecordModify(this.buildingInfo);
  223. this.currentCoin = null;
  224. for (let i = 0; i < this.coinArrayMax; i++) {
  225. this.pickupCoin(this.coinArray[i]);
  226. }
  227. // 需要把艺人也都清空了
  228. for (let child of this.humanList) { child.destroy(); }
  229. this.coinWrap.active = false;
  230. this.artistList.active = false;
  231. this.coinFullTip.active = false;
  232. }
  233. })
  234. },
  235. // 解锁建筑事件
  236. unlockBuilding() {
  237. this.unlockBuildingEvent();
  238. },
  239. updateBuildingEvent() {
  240. if (this.state === RoomState.Update) {
  241. // 清空当前用来存储金币节点
  242. this.currentCoin = null;
  243. for (let i = 0; i < this.coinArrayMax; i++) {
  244. this.pickupCoin(this.coinArray[i]);
  245. }
  246. let position = this.updateBtn.node.convertToWorldSpace(cc.v2(this.updateBtn.node.width/2, this.updateBtn.node.height/2));
  247. GameEvent.fire(GameNotificationKey.PlayUpdateCoinAnimation, position);
  248. this.updateBuilding();
  249. if (!this.updateSkeletion.node.active) {
  250. this.updateSkeletion.node.active = true;
  251. this.updateSkeletion.setAnimation(0, "changjing_sj");
  252. this.updateSkeletion.setCompleteListener(() => {
  253. this.updateSkeletion.node.active = false;
  254. });
  255. }
  256. // 如果好友列表功能已经开放
  257. if(GameModule.tab) {
  258. let artists = GameModule.tab.getComponent('Tab').artists
  259. // 尝试触发艺人入驻引导
  260. GameModule.homeGuide.getComponent('HomeGuide').handleGuideState17(artists)
  261. }
  262. }
  263. },
  264. configCoins() {
  265. for (let i = 0; i < this.coinArrayMax; i++) {
  266. // 初始化
  267. let coinNode = cc.instantiate(this.coinPrefab)
  268. this.coinWrap.addChild(coinNode);
  269. coinNode = coinNode.getComponent("LevelHomeCoin");
  270. coinNode.index = i;
  271. this.coinHide(coinNode)
  272. this.coinArray[i] = coinNode;
  273. coinNode.node.on(cc.Node.EventType.TOUCH_END, (event) => {
  274. this.pickupCoin(coinNode);
  275. }, this);
  276. }
  277. },
  278. /**
  279. * Public Method, 用来设置建筑背景图
  280. * @param {*} index
  281. */
  282. init(cityId, index) {
  283. if (arguments.length < 1) {
  284. throw new Error("init Missing parameter...");
  285. }
  286. this.cityId = cityId;
  287. this.index = index;
  288. this.isFirstLoad = true;
  289. ThemeManager.setBuildItemColor(this.cityId, this.bgNode);
  290. ThemeManager.setItemBuildSpriteFrame(this.cityId, this.buildSprite, index);
  291. ThemeManager.setItemPillarTopSpriteFrame(this.cityId, this.pillarTop);
  292. ThemeManager.setItemPillarBottomSpriteFrame(this.cityId, this.pillarBottom);
  293. ThemeManager.setItemPillarRightSpriteFrame(this.cityId, this.pillarRight);
  294. ThemeManager.setItemPillarLeftSpriteFrame(this.cityId, this.pillarLeft);
  295. let lockBottomSprite = this.lockBottomNode.getComponent(cc.Sprite);
  296. ThemeManager.setItemLockDownSpriteFrame(this.cityId, lockBottomSprite);
  297. },
  298. /**
  299. * Public Method, 配置建筑的内部样式
  300. * @param {*} buildingInfo 建筑信息
  301. * @param {*} uid 当前用户的uid
  302. */
  303. config(buildingInfo, uid, unlockBuildingRecord, resetCallback) {
  304. this.unlockBuildingRecord = unlockBuildingRecord;
  305. if (resetCallback) {
  306. this.resetCallback = resetCallback;
  307. }
  308. if(buildingInfo.buildingId == 1) {
  309. if(buildingInfo.level == 0) {
  310. // 监听完成引导系统state1的事件
  311. GameModule.homeGuide.on('Fire_state1', this.unlockBuildingEvent, this)
  312. // 触发引导系统state1状态
  313. GameModule.homeGuide.getComponent('HomeGuide').handleState('state1')
  314. }
  315. }
  316. this.buildingInfo = buildingInfo;
  317. this.uid = uid;
  318. this.artists = this.buildingInfo.artists;
  319. // 这里设置一些只需要设置一次的数据
  320. this.countDown = buildingInfo.rateUnit;
  321. this.buildNameLabel.string = buildingInfo.name;
  322. this._notPickupCount = buildingInfo.coinCount;
  323. if (buildingInfo.coinCount === this.coinArrayMax * 10) {
  324. this.rateProgressBar.progress = 1;
  325. this.countdownLabel.string = DWTool.calculateTime(0);
  326. }
  327. this.levelProgressBar.progress = buildingInfo.level / Global.BuildingManager.getLevelCount(buildingInfo.buildingId);
  328. this.levelProgressLabel.string = `LV.${buildingInfo.level}`;
  329. if (this.isFirstLoad) {
  330. this.isFirstLoad = false;
  331. this.artistListLayout();
  332. }
  333. //城市开发完毕的时候不显示艺人入驻按钮
  334. if (GameModule.userInfo.levelHomeItemFullCount != 5 && this.cityId === Global.devCityId) {
  335. this.artistList.active = true;
  336. } else {
  337. this.artistList.active = false;
  338. }
  339. this.initializeCoinPosition();
  340. this.layout(buildingInfo);
  341. },
  342. /** 初始化金币的位置 */
  343. initializeCoinPosition() {
  344. // 2018年08月18日15:25, 子奇要求我去好友家园时, 不显示未收取金币
  345. // 城市已经开发完毕不显示金币
  346. if (this.cityId === Global.devCityId && GameModule.userInfo.levelHomeItemFullCount != 5) {
  347. this.coinWrap.active = true;
  348. } else {
  349. this.coinWrap.active = false;
  350. return;
  351. }
  352. if (!this.buildingInfo.coinCount) {
  353. for (let i = 0; i < this.coinArrayMax; i++) {
  354. let coin = this.coinArray[i];
  355. this.coinHide(coin);
  356. }
  357. return;
  358. }
  359. for (let i = 0; i < this.coinArrayMax; i++) {
  360. let coin = this.coinArray[i];
  361. while (this.buildingInfo.coinCount != 0 && coin.coinCount != 10) {
  362. coin.coinCount += 1;
  363. this.buildingInfo.coinCount -= 1;
  364. }
  365. if (coin.coinCount != 0) {
  366. let x = -140 + (85 * i);
  367. coin.isPlay = true;
  368. coin.node.position = cc.v2(x, -28);
  369. coin.node.active = true;
  370. if (this.addition > 0) {
  371. let rate = this.buildingInfo.rate * this.addition;
  372. coin.totalRate = rate * coin.coinCount;
  373. } else {
  374. coin.totalRate = this.buildingInfo.rate * coin.coinCount;
  375. }
  376. coin.initStatic(i);
  377. }
  378. }
  379. },
  380. artistListLayout() {
  381. for (let child of this.artistList.children) { child.destroy(); }
  382. for (let child of this.humanList) { child.destroy(); }
  383. let self = this;
  384. let addAddItemToList = function (showTop = false, showBottom = false) {
  385. let addArtistItem = cc.instantiate(self.artistListItem);
  386. self.artistList.addChild(addArtistItem);
  387. let addArtistScript = addArtistItem.getComponent('LevelHomeArtistItem');
  388. addArtistScript.initWithBuildingInfo(self.buildingInfo, self.uid, true, showTop, showBottom);
  389. }
  390. let addArtistItemToList = function (artist, showTop = false, showBottom = false) {
  391. let artistItem = cc.instantiate(self.artistListItem);
  392. self.artistList.addChild(artistItem);
  393. let artistScript = artistItem.getComponent('LevelHomeArtistItem');
  394. artistScript.initWithArtistData(self.buildingInfo, self.uid, true, artist, showTop, showBottom);
  395. }
  396. let addHuman = function (artist, index) {
  397. DWTool.loadResPrefab("./prefabs/artist_man")
  398. .then((prefab) => {
  399. let human = cc.instantiate(prefab);
  400. human.getComponent('ArtistMan').init(artist);
  401. human.getComponent('ArtistMan').direction = (index > 0) ? 1 : -1;
  402. self.node.addChild(human);
  403. self.humanList.push(human);
  404. });
  405. }
  406. // 当没有自己艺人, 也没有好友艺人入驻时, 这里还得区分主态和客态
  407. if (this.artists.length === 0) {
  408. addAddItemToList(true);
  409. // 没有艺人不显示倍数
  410. this.addition = 0;
  411. } else {
  412. // 有艺人入驻要显示倍数
  413. this.additionNode.active = true;
  414. if (this.artists.length === 1) {
  415. let artist = this.artists[0];
  416. this.addition += artist.stationJobLevel + 1;
  417. // 这里要区分主态和客态, 去别人家园时, 如果有一个是客态自己的艺人, 那么另一个就是可以入驻的按钮
  418. if (artist.role === 2) {
  419. addArtistItemToList(artist, true);
  420. addHuman(artist, (Math.random() - 0.5) * 2);
  421. } else {
  422. addAddItemToList(true, true);
  423. addArtistItemToList(artist);
  424. addHuman(artist, (Math.random() - 0.5) * 2);
  425. }
  426. } else {
  427. for (let i = 0; i < this.artists.length; i++) {
  428. let artist = this.artists[i];
  429. this.addition += artist.stationJobLevel + 1;
  430. if (i === 0) {
  431. addArtistItemToList(artist, true, true);
  432. } else {
  433. addArtistItemToList(artist);
  434. }
  435. addHuman(artist, i);
  436. }
  437. }
  438. }
  439. },
  440. layout(buildingInfo) {
  441. // 判断是否有下一级, 没有的话就是满级
  442. if (buildingInfo.hasNext === 1 && buildingInfo.level <= Global.BuildingManager.getLevelCount(buildingInfo.buildingId)) {
  443. // 判断是否已经解锁
  444. if (buildingInfo.isUnlocked) {
  445. this.artistList.active = true;
  446. this.lockNode.active = false;
  447. this.costLabel.string = DWTool.coinParse(buildingInfo.nextUpScore);
  448. this.rate = buildingInfo.rate;
  449. // 判断是否有足够的金额解锁
  450. if (GameModule.userInfo.grossIncome >= buildingInfo.nextUpScore) {
  451. if(buildingInfo.buildingId === 1 && buildingInfo.level === 1) {
  452. // 第三个引导
  453. GameModule.homeGuide.on('Fire_state3', this.updateBuildingEvent, this)
  454. // 触发引导系统state3状态
  455. GameModule.homeGuide.getComponent('HomeGuide').handleState('state3')
  456. }
  457. this.setState(RoomState.Update);
  458. } else {
  459. this.setState(RoomState.UnLock);
  460. }
  461. } else {
  462. this.artistList.active = false;
  463. this.countDown = 0;
  464. this._currentTime = 0;
  465. this.rateProgressBar.progress = 0;
  466. this.totalRate = 0;
  467. this.lockNode.active = true;
  468. this.costLabel.string = 0;
  469. this.unLockBuildName.string = `<b><color=#ffffff>${buildingInfo.name}</c></b>`;
  470. this.unlockRichText.string = `<img src='alert_coin'/><b><color=#ffffff> ${DWTool.coinParse(buildingInfo.unlockScore)}</c><b/>`;
  471. // 判断是否有足够的金额解锁
  472. if (GameModule.userInfo.grossIncome >= buildingInfo.unlockScore) {
  473. if (this.unlockBuildingRecord[0] === 1 && this.buildingInfo.buildingId === 2) { // 如果第一层已经解锁, 才能执行下一个引导
  474. // 监听完成引导系统state4的事件
  475. GameModule.homeGuide.on('Fire_state4', this.unlockBuildingEvent, this)
  476. // 触发引导系统state4状态
  477. GameModule.homeGuide.getComponent('HomeGuide').handleState('state4')
  478. } else if (this.unlockBuildingRecord[1] === 1 && this.buildingInfo.buildingId === 3) {
  479. // 监听完成引导系统state4的事件
  480. GameModule.homeGuide.on('Fire_state5', this.unlockBuildingEvent, this)
  481. // 触发引导系统state4状态
  482. GameModule.homeGuide.getComponent('HomeGuide').handleState('state5')
  483. }
  484. this.unlockBuildingType.node.active = true;
  485. this.lockBtn.spriteFrame = this.lockBtnFrames[1];
  486. this.lockBtn.node.getComponent(cc.Button).interactable = true;
  487. } else {
  488. this.unlockBuildingType.node.active = false;
  489. this.lockBtn.spriteFrame = this.lockBtnFrames[0];
  490. this.lockBtn.node.getComponent(cc.Button).interactable = false;
  491. }
  492. this.setState(RoomState.Lock);
  493. }
  494. } else {
  495. this.rate = buildingInfo.rate;
  496. this.setState(RoomState.Full);
  497. }
  498. },
  499. setState(state) {
  500. if (this.state === state) { return;}
  501. switch (state) {
  502. case RoomState.Lock:
  503. this.openDoorSkeletion.node.active = true;
  504. this.openDoorSkeletion.clearTracks();
  505. this.openDoorSkeletion.setToSetupPose();
  506. this.lockBtn.node.active = true;
  507. this.lockBottomNode.stopAllActions();
  508. this.lockBottomNode.opacity = 255;
  509. this.lockBottomNode.active = true;
  510. this.lockNode.active = true;
  511. this.updateBtn.node.active = false;
  512. this.updateBtn.spriteFrame = this.updateBtnFrames[0];
  513. this.updateBtn.node.getComponent(cc.Button).interactable = false;
  514. this.maxNode.active = false;
  515. break;
  516. case RoomState.UnLock:
  517. this.openDoorSkeletion.node.active = false;
  518. this.lockBtn.node.active = false;
  519. this.lockBottomNode.active = false;
  520. this.updateBtn.node.active = true;
  521. this.lockNode.active = false;
  522. this.updateBtn.spriteFrame = this.updateBtnFrames[0];
  523. this.updateBtn.node.getComponent(cc.Button).interactable = false;
  524. this.maxNode.active = false;
  525. break;
  526. case RoomState.Update:
  527. this.openDoorSkeletion.node.active = false;
  528. this.lockBtn.node.active = false;
  529. this.lockBottomNode.active = false;
  530. this.updateBtn.node.active = true;
  531. this.lockNode.active = false;
  532. this.updateBtn.spriteFrame = this.updateBtnFrames[1];
  533. this.updateBtn.node.getComponent(cc.Button).interactable = true;
  534. this.maxNode.active = false;
  535. break;
  536. case RoomState.Full:
  537. this.openDoorSkeletion.node.active = false;
  538. this.lockBtn.node.active = false;
  539. this.lockBottomNode.active = false;
  540. this.lockNode.active = false;
  541. this.maxNode.active = true;
  542. this.updateBtn.node.active = false;
  543. default:
  544. break;
  545. }
  546. this.state = state;
  547. },
  548. // 升级建筑
  549. updateBuilding() {
  550. // 从配置文件里获取
  551. let buildModel = Global.BuildingManager.getBuildingInfo(this.cityId, this.buildingInfo.buildingId, this.buildingInfo.level + 1);
  552. // 将已有入驻的艺人赋值给新的model, 保持一个引用
  553. buildModel.artists = this.buildingInfo.artists;
  554. if (this.buildingInfo.isUnlocked) {
  555. GameModule.userInfo.grossIncome -= this.buildingInfo.nextUpScore;
  556. GameModule.userInfo.updateRecordModify(buildModel);
  557. } else {
  558. this.unlockBuildingRecord[this.buildingInfo.buildingId-1] = 1;
  559. GameModule.userInfo.grossIncome -= this.buildingInfo.unlockScore;
  560. GameModule.userInfo.recordUnlockModify.push(buildModel);
  561. }
  562. GameModule.userInfo.stars += 1;
  563. if (this.resetCallback) {
  564. this.resetCallback(buildModel);
  565. }
  566. this.config(buildModel, this.uid);
  567. // 如果满级了, 就发通知告诉userinfo
  568. if (buildModel.hasNext != 1) {
  569. GameEvent.fire(GameNotificationKey.LevelHomeItemBuildingFull);
  570. }
  571. },
  572. // 收取金币
  573. pickupCoin(coin, isShowAnimation = true) {
  574. if (this.currentCoin === coin) {
  575. this.currentCoin = null;
  576. }
  577. if (isShowAnimation) {
  578. let pos = coin.node.convertToWorldSpace(cc.v2(coin.node.width / 2, coin.node.height / 2));
  579. this.showCollectAnim(pos, coin.coinCount)
  580. }
  581. // 点击一次金币, 就将notPickupCount减去coinNode.coinCount
  582. this.notPickupCount -= coin.coinCount;
  583. // console.log(`收入: ${buildingInfo.rate * coin.coinCount}`);
  584. GameModule.userInfo.grossIncome += (this.buildingInfo.rate * coin.coinCount);
  585. this.coinHide(coin);
  586. },
  587. showCollectAnim(pos, colNums) {
  588. let canvasNode = cc.find("Canvas");
  589. let grossCoin = GameModule.userInfo.grossCoin;
  590. let grossCoinPos = grossCoin.node.convertToWorldSpace(cc.v2(grossCoin.node.width / 2, grossCoin.node.height / 2));
  591. // let colNums = 5
  592. let vSize = cc.view.getVisibleSize();
  593. let target = cc.v2(grossCoinPos.x - vSize.width / 2, grossCoinPos.y - vSize.height / 2)
  594. let i = 0;
  595. let runSt = setInterval(() => {
  596. if (i == colNums) {
  597. clearInterval(runSt)
  598. } else {
  599. let ran = (Math.random() - 0.5) * 2 * 3;
  600. let newCoin = cc.instantiate(this.coinPrefab);
  601. let posX = pos.x - vSize.width / 2;
  602. let posY = pos.y - vSize.height / 2;
  603. canvasNode.addChild(newCoin)
  604. newCoin.x = posX + ran * 15;
  605. newCoin.y = posY + 30 + ran * 15;
  606. newCoin.active = true;
  607. newCoin = newCoin.getComponent("LevelHomeCoin")
  608. newCoin.initAnim()
  609. let cbNotiStart = cc.callFunc(() => {
  610. GameEvent.fire(GameNotificationKey.UserCollectCoin, true);
  611. })
  612. let cbDestroy = cc.callFunc(() => {
  613. newCoin.node.destroy();
  614. })
  615. let act = cc.sequence(cc.moveTo(1, target), cbDestroy, cbNotiStart)
  616. newCoin.node.runAction(act.easing(cc.easeIn(2.1)));
  617. i++
  618. }
  619. }, 100);
  620. },
  621. coinHide(coin) {
  622. // 算出金币的x值, 70是金币宽度, -130是第一个金币的x值
  623. let x = -140 + (85 * coin.index);
  624. // 重置金币状态
  625. coin.node.position = cc.v2(x, -108);
  626. coin.node.active = false;
  627. coin.isPlay = false;
  628. coin.isPlaying = false;
  629. coin.coinCount = 0;
  630. coin.totalRate = 0;
  631. },
  632. getFreeCoin() {
  633. for (let i = 0; i < this.coinArrayMax; i++) {
  634. let coinNode = this.coinArray[i];
  635. if (coinNode.coinCount != 10) {
  636. return coinNode;
  637. }
  638. }
  639. return null;
  640. },
  641. generateCoin() {
  642. if (this.currentCoin && this.currentCoin.coinCount < 10) {
  643. this.currentCoin.totalRate += this.rate;
  644. this.currentCoin.coinCount += 1;
  645. this.currentCoin.updateAnimation();
  646. } else {
  647. this.currentCoin = this.getFreeCoin();
  648. if (this.currentCoin) {
  649. this.currentCoin.totalRate += this.rate;
  650. this.currentCoin.coinCount += 1;
  651. if(this.buildingInfo.buildingId === 1 && this.buildingInfo.level === 1 && this.notPickupCount === 1) {
  652. // 监听完成引导系统state2的事件
  653. GameModule.homeGuide.on('Fire_state2', () => {
  654. this.pickupCoin(this.currentCoin);
  655. }, this);
  656. // 触发引导系统state1状态
  657. GameModule.homeGuide.getComponent('HomeGuide').handleState('state2');
  658. }
  659. if (!this.currentCoin.isPlay) {
  660. this.currentCoin.showAnimation();
  661. } else {
  662. this.currentCoin.updateAnimation();
  663. }
  664. }
  665. }
  666. },
  667. update(dt) {
  668. if (this.buildingInfo) {
  669. // 不断刷新界面
  670. this.layout(this.buildingInfo);
  671. // 只有已经解锁进度条才会走
  672. if (this.buildingInfo.isUnlocked) {
  673. // 进度条走完, 开始生产金币
  674. if (Math.floor(this.rateProgressBar.progress) === 1) {
  675. this.rateProgressBar.progress = 1;
  676. if (GameModule.userInfo.levelHomeItemFullCount != 5 && // 是否满级
  677. this.cityId === Global.devCityId) { // 当前访问的是不是这个city
  678. if (this.notPickupCount >= this.coinArrayMax * 10) {
  679. this.rateProgressLabel.node.active = false;
  680. this.coinFullTip.active = true;
  681. } else {
  682. this.coinFullTip.active = false;
  683. this.notPickupCount += 1;
  684. this.generateCoin();
  685. this.rateProgressBar.progress = 0;
  686. this._currentTime = 0;
  687. }
  688. } else { // 满级后的状态
  689. this.rateProgressBar.progress = 0;
  690. this._currentTime = 0;
  691. this.coinFullTip.active = false;
  692. this.rateProgressLabel.node.active = true;
  693. }
  694. } else {
  695. this.rateProgressLabel.node.active = true;
  696. this.coinFullTip.active = false;
  697. this._currentTime += dt;
  698. this.rateProgressBar.progress = this._currentTime / this.countDown;
  699. if (Math.floor(this.rateProgressBar.progress) === 1) {
  700. this.rateProgressBar.progress = 1;
  701. }
  702. let resultCountDown = this.countDown - Math.floor(this._currentTime);
  703. if (this._preCountDown !== resultCountDown) {
  704. this.countdownLabel.string = DWTool.calculateTime(resultCountDown);
  705. this._preCountDown = resultCountDown;
  706. }
  707. }
  708. }
  709. }
  710. },
  711. });