LevelHomeItem.js 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  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. ThemeManager.setItemDownSpriteFrame(this.cityId, this.bottomBg);
  296. let lockBottomSprite = this.lockBottomNode.getComponent(cc.Sprite);
  297. ThemeManager.setItemLockDownSpriteFrame(this.cityId, lockBottomSprite);
  298. },
  299. /**
  300. * Public Method, 配置建筑的内部样式
  301. * @param {*} buildingInfo 建筑信息
  302. * @param {*} uid 当前用户的uid
  303. */
  304. config(buildingInfo, uid, unlockBuildingRecord, resetCallback) {
  305. this.unlockBuildingRecord = unlockBuildingRecord;
  306. if (resetCallback) {
  307. this.resetCallback = resetCallback;
  308. }
  309. if(buildingInfo.buildingId == 1) {
  310. if(buildingInfo.level == 0) {
  311. // 监听完成引导系统state1的事件
  312. GameModule.homeGuide.on('Fire_state1', this.unlockBuildingEvent, this)
  313. // 触发引导系统state1状态
  314. GameModule.homeGuide.getComponent('HomeGuide').handleState('state1')
  315. }
  316. }
  317. this.buildingInfo = buildingInfo;
  318. this.uid = uid;
  319. this.artists = this.buildingInfo.artists;
  320. // 这里设置一些只需要设置一次的数据
  321. this.countDown = buildingInfo.rateUnit;
  322. this.buildNameLabel.string = buildingInfo.name;
  323. this._notPickupCount = buildingInfo.coinCount;
  324. if (buildingInfo.coinCount === this.coinArrayMax * 10) {
  325. this.rateProgressBar.progress = 1;
  326. this.countdownLabel.string = DWTool.calculateTime(0);
  327. }
  328. this.levelProgressBar.progress = buildingInfo.level / Global.BuildingManager.getLevelCount(buildingInfo.buildingId);
  329. this.levelProgressLabel.string = `LV.${buildingInfo.level}`;
  330. if (this.isFirstLoad) {
  331. this.isFirstLoad = false;
  332. this.artistListLayout();
  333. }
  334. //城市开发完毕的时候不显示艺人入驻按钮
  335. if (GameModule.userInfo.levelHomeItemFullCount != 5 && this.cityId === Global.devCityId) {
  336. this.artistList.active = true;
  337. } else {
  338. this.artistList.active = false;
  339. }
  340. this.initializeCoinPosition();
  341. this.layout(buildingInfo);
  342. },
  343. /** 初始化金币的位置 */
  344. initializeCoinPosition() {
  345. // 2018年08月18日15:25, 子奇要求我去好友家园时, 不显示未收取金币
  346. // 城市已经开发完毕不显示金币
  347. if (this.cityId === Global.devCityId && GameModule.userInfo.levelHomeItemFullCount != 5) {
  348. this.coinWrap.active = true;
  349. } else {
  350. this.coinWrap.active = false;
  351. return;
  352. }
  353. if (!this.buildingInfo.coinCount) {
  354. for (let i = 0; i < this.coinArrayMax; i++) {
  355. let coin = this.coinArray[i];
  356. this.coinHide(coin);
  357. }
  358. return;
  359. }
  360. for (let i = 0; i < this.coinArrayMax; i++) {
  361. let coin = this.coinArray[i];
  362. while (this.buildingInfo.coinCount != 0 && coin.coinCount != 10) {
  363. coin.coinCount += 1;
  364. this.buildingInfo.coinCount -= 1;
  365. }
  366. if (coin.coinCount != 0) {
  367. let x = -140 + (85 * i);
  368. coin.isPlay = true;
  369. coin.node.position = cc.v2(x, -28);
  370. coin.node.active = true;
  371. if (this.addition > 0) {
  372. let rate = this.buildingInfo.rate * this.addition;
  373. coin.totalRate = rate * coin.coinCount;
  374. } else {
  375. coin.totalRate = this.buildingInfo.rate * coin.coinCount;
  376. }
  377. coin.initStatic(i);
  378. }
  379. }
  380. },
  381. artistListLayout() {
  382. for (let child of this.artistList.children) { child.destroy(); }
  383. for (let child of this.humanList) { child.destroy(); }
  384. let self = this;
  385. let addAddItemToList = function (showTop = false, showBottom = false) {
  386. let addArtistItem = cc.instantiate(self.artistListItem);
  387. self.artistList.addChild(addArtistItem);
  388. let addArtistScript = addArtistItem.getComponent('LevelHomeArtistItem');
  389. addArtistScript.initWithBuildingInfo(self.buildingInfo, self.uid, true, showTop, showBottom);
  390. }
  391. let addArtistItemToList = function (artist, showTop = false, showBottom = false) {
  392. let artistItem = cc.instantiate(self.artistListItem);
  393. self.artistList.addChild(artistItem);
  394. let artistScript = artistItem.getComponent('LevelHomeArtistItem');
  395. artistScript.initWithArtistData(self.buildingInfo, self.uid, true, artist, showTop, showBottom);
  396. }
  397. let addHuman = function (artist, index) {
  398. DWTool.loadResPrefab("./prefabs/artist_man")
  399. .then((prefab) => {
  400. let human = cc.instantiate(prefab);
  401. human.getComponent('ArtistMan').init(artist);
  402. human.getComponent('ArtistMan').direction = (index > 0) ? 1 : -1;
  403. self.node.addChild(human);
  404. self.humanList.push(human);
  405. });
  406. }
  407. // 当没有自己艺人, 也没有好友艺人入驻时, 这里还得区分主态和客态
  408. if (this.artists.length === 0) {
  409. addAddItemToList(true);
  410. // 没有艺人不显示倍数
  411. this.addition = 0;
  412. } else {
  413. // 有艺人入驻要显示倍数
  414. this.additionNode.active = true;
  415. if (this.artists.length === 1) {
  416. let artist = this.artists[0];
  417. this.addition += artist.stationJobLevel + 1;
  418. // 这里要区分主态和客态, 去别人家园时, 如果有一个是客态自己的艺人, 那么另一个就是可以入驻的按钮
  419. if (artist.role === 2) {
  420. addArtistItemToList(artist, true);
  421. addHuman(artist, (Math.random() - 0.5) * 2);
  422. } else {
  423. addAddItemToList(true, true);
  424. addArtistItemToList(artist);
  425. addHuman(artist, (Math.random() - 0.5) * 2);
  426. }
  427. } else {
  428. for (let i = 0; i < this.artists.length; i++) {
  429. let artist = this.artists[i];
  430. this.addition += artist.stationJobLevel + 1;
  431. if (i === 0) {
  432. addArtistItemToList(artist, true, true);
  433. } else {
  434. addArtistItemToList(artist);
  435. }
  436. addHuman(artist, i);
  437. }
  438. }
  439. }
  440. },
  441. layout(buildingInfo) {
  442. // 判断是否有下一级, 没有的话就是满级
  443. if (buildingInfo.hasNext === 1 && buildingInfo.level <= Global.BuildingManager.getLevelCount(buildingInfo.buildingId)) {
  444. // 判断是否已经解锁
  445. if (buildingInfo.isUnlocked) {
  446. this.artistList.active = true;
  447. this.lockNode.active = false;
  448. this.costLabel.string = DWTool.coinParse(buildingInfo.nextUpScore);
  449. this.rate = buildingInfo.rate;
  450. // 判断是否有足够的金额解锁
  451. if (GameModule.userInfo.grossIncome >= buildingInfo.nextUpScore) {
  452. if(buildingInfo.buildingId === 1 && buildingInfo.level === 1) {
  453. // 第三个引导
  454. GameModule.homeGuide.on('Fire_state3', this.updateBuildingEvent, this)
  455. // 触发引导系统state3状态
  456. GameModule.homeGuide.getComponent('HomeGuide').handleState('state3')
  457. }
  458. this.setState(RoomState.Update);
  459. } else {
  460. this.setState(RoomState.UnLock);
  461. }
  462. } else {
  463. this.artistList.active = false;
  464. this.countDown = 0;
  465. this._currentTime = 0;
  466. this.rateProgressBar.progress = 0;
  467. this.totalRate = 0;
  468. this.lockNode.active = true;
  469. this.costLabel.string = 0;
  470. this.unLockBuildName.string = `<b><color=#ffffff>${buildingInfo.name}</c></b>`;
  471. this.unlockRichText.string = `<img src='alert_coin'/><b><color=#ffffff> ${DWTool.coinParse(buildingInfo.unlockScore)}</c><b/>`;
  472. // 判断是否有足够的金额解锁
  473. if (GameModule.userInfo.grossIncome >= buildingInfo.unlockScore) {
  474. if (this.unlockBuildingRecord[0] === 1 && this.buildingInfo.buildingId === 2) { // 如果第一层已经解锁, 才能执行下一个引导
  475. // 监听完成引导系统state4的事件
  476. GameModule.homeGuide.on('Fire_state4', this.unlockBuildingEvent, this)
  477. // 触发引导系统state4状态
  478. GameModule.homeGuide.getComponent('HomeGuide').handleState('state4')
  479. } else if (this.unlockBuildingRecord[1] === 1 && this.buildingInfo.buildingId === 3) {
  480. // 监听完成引导系统state4的事件
  481. GameModule.homeGuide.on('Fire_state5', this.unlockBuildingEvent, this)
  482. // 触发引导系统state4状态
  483. GameModule.homeGuide.getComponent('HomeGuide').handleState('state5')
  484. }
  485. this.unlockBuildingType.node.active = true;
  486. this.lockBtn.spriteFrame = this.lockBtnFrames[1];
  487. this.lockBtn.node.getComponent(cc.Button).interactable = true;
  488. } else {
  489. this.unlockBuildingType.node.active = false;
  490. this.lockBtn.spriteFrame = this.lockBtnFrames[0];
  491. this.lockBtn.node.getComponent(cc.Button).interactable = false;
  492. }
  493. this.setState(RoomState.Lock);
  494. }
  495. } else {
  496. this.rate = buildingInfo.rate;
  497. this.setState(RoomState.Full);
  498. }
  499. },
  500. setState(state) {
  501. if (this.state === state) { return;}
  502. switch (state) {
  503. case RoomState.Lock:
  504. this.openDoorSkeletion.node.active = true;
  505. this.openDoorSkeletion.clearTracks();
  506. this.openDoorSkeletion.setToSetupPose();
  507. this.lockBtn.node.active = true;
  508. this.lockBottomNode.stopAllActions();
  509. this.lockBottomNode.opacity = 255;
  510. this.lockBottomNode.active = true;
  511. this.lockNode.active = true;
  512. this.updateBtn.node.active = false;
  513. this.updateBtn.spriteFrame = this.updateBtnFrames[0];
  514. this.updateBtn.node.getComponent(cc.Button).interactable = false;
  515. this.maxNode.active = false;
  516. break;
  517. case RoomState.UnLock:
  518. this.openDoorSkeletion.node.active = false;
  519. this.lockBtn.node.active = false;
  520. this.lockBottomNode.active = false;
  521. this.updateBtn.node.active = true;
  522. this.lockNode.active = false;
  523. this.updateBtn.spriteFrame = this.updateBtnFrames[0];
  524. this.updateBtn.node.getComponent(cc.Button).interactable = false;
  525. this.maxNode.active = false;
  526. break;
  527. case RoomState.Update:
  528. this.openDoorSkeletion.node.active = false;
  529. this.lockBtn.node.active = false;
  530. this.lockBottomNode.active = false;
  531. this.updateBtn.node.active = true;
  532. this.lockNode.active = false;
  533. this.updateBtn.spriteFrame = this.updateBtnFrames[1];
  534. this.updateBtn.node.getComponent(cc.Button).interactable = true;
  535. this.maxNode.active = false;
  536. break;
  537. case RoomState.Full:
  538. this.openDoorSkeletion.node.active = false;
  539. this.lockBtn.node.active = false;
  540. this.lockBottomNode.active = false;
  541. this.lockNode.active = false;
  542. this.maxNode.active = true;
  543. this.updateBtn.node.active = false;
  544. default:
  545. break;
  546. }
  547. this.state = state;
  548. },
  549. // 升级建筑
  550. updateBuilding() {
  551. // 从配置文件里获取
  552. let buildModel = Global.BuildingManager.getBuildingInfo(this.cityId, this.buildingInfo.buildingId, this.buildingInfo.level + 1);
  553. // 将已有入驻的艺人赋值给新的model, 保持一个引用
  554. buildModel.artists = this.buildingInfo.artists;
  555. if (this.buildingInfo.isUnlocked) {
  556. GameModule.userInfo.grossIncome -= this.buildingInfo.nextUpScore;
  557. GameModule.userInfo.updateRecordModify(buildModel);
  558. } else {
  559. this.unlockBuildingRecord[this.buildingInfo.buildingId-1] = 1;
  560. GameModule.userInfo.grossIncome -= this.buildingInfo.unlockScore;
  561. GameModule.userInfo.recordUnlockModify.push(buildModel);
  562. }
  563. GameModule.userInfo.stars += 1;
  564. if (this.resetCallback) {
  565. this.resetCallback(buildModel);
  566. }
  567. this.config(buildModel, this.uid);
  568. // 如果满级了, 就发通知告诉userinfo
  569. if (buildModel.hasNext != 1) {
  570. GameEvent.fire(GameNotificationKey.LevelHomeItemBuildingFull);
  571. }
  572. },
  573. // 收取金币
  574. pickupCoin(coin, isShowAnimation = true) {
  575. if (this.currentCoin === coin) {
  576. this.currentCoin = null;
  577. }
  578. if (isShowAnimation) {
  579. let pos = coin.node.convertToWorldSpace(cc.v2(coin.node.width / 2, coin.node.height / 2));
  580. this.showCollectAnim(pos, coin.coinCount)
  581. }
  582. // 点击一次金币, 就将notPickupCount减去coinNode.coinCount
  583. this.notPickupCount -= coin.coinCount;
  584. // console.log(`收入: ${buildingInfo.rate * coin.coinCount}`);
  585. GameModule.userInfo.grossIncome += (this.buildingInfo.rate * coin.coinCount);
  586. this.coinHide(coin);
  587. },
  588. showCollectAnim(pos, colNums) {
  589. let canvasNode = cc.find("Canvas");
  590. let grossCoin = GameModule.userInfo.grossCoin;
  591. let grossCoinPos = grossCoin.node.convertToWorldSpace(cc.v2(grossCoin.node.width / 2, grossCoin.node.height / 2));
  592. // let colNums = 5
  593. let vSize = cc.view.getVisibleSize();
  594. let target = cc.v2(grossCoinPos.x - vSize.width / 2, grossCoinPos.y - vSize.height / 2)
  595. let i = 0;
  596. let runSt = setInterval(() => {
  597. if (i == colNums) {
  598. clearInterval(runSt)
  599. } else {
  600. let ran = (Math.random() - 0.5) * 2 * 3;
  601. let newCoin = cc.instantiate(this.coinPrefab);
  602. let posX = pos.x - vSize.width / 2;
  603. let posY = pos.y - vSize.height / 2;
  604. canvasNode.addChild(newCoin)
  605. newCoin.x = posX + ran * 15;
  606. newCoin.y = posY + 30 + ran * 15;
  607. newCoin.active = true;
  608. newCoin = newCoin.getComponent("LevelHomeCoin")
  609. newCoin.initAnim()
  610. let cbNotiStart = cc.callFunc(() => {
  611. GameEvent.fire(GameNotificationKey.UserCollectCoin, true);
  612. })
  613. let cbDestroy = cc.callFunc(() => {
  614. newCoin.node.destroy();
  615. })
  616. let act = cc.sequence(cc.moveTo(1, target), cbDestroy, cbNotiStart)
  617. newCoin.node.runAction(act.easing(cc.easeIn(2.1)));
  618. i++
  619. }
  620. }, 100);
  621. },
  622. coinHide(coin) {
  623. // 算出金币的x值, 70是金币宽度, -130是第一个金币的x值
  624. let x = -140 + (85 * coin.index);
  625. // 重置金币状态
  626. coin.node.position = cc.v2(x, -108);
  627. coin.node.active = false;
  628. coin.isPlay = false;
  629. coin.isPlaying = false;
  630. coin.coinCount = 0;
  631. coin.totalRate = 0;
  632. },
  633. getFreeCoin() {
  634. for (let i = 0; i < this.coinArrayMax; i++) {
  635. let coinNode = this.coinArray[i];
  636. if (coinNode.coinCount != 10) {
  637. return coinNode;
  638. }
  639. }
  640. return null;
  641. },
  642. generateCoin() {
  643. if (this.currentCoin && this.currentCoin.coinCount < 10) {
  644. this.currentCoin.totalRate += this.rate;
  645. this.currentCoin.coinCount += 1;
  646. this.currentCoin.updateAnimation();
  647. } else {
  648. this.currentCoin = this.getFreeCoin();
  649. if (this.currentCoin) {
  650. this.currentCoin.totalRate += this.rate;
  651. this.currentCoin.coinCount += 1;
  652. if(this.buildingInfo.buildingId === 1 && this.buildingInfo.level === 1 && this.notPickupCount === 1) {
  653. // 监听完成引导系统state2的事件
  654. GameModule.homeGuide.on('Fire_state2', () => {
  655. this.pickupCoin(this.currentCoin);
  656. }, this);
  657. // 触发引导系统state1状态
  658. GameModule.homeGuide.getComponent('HomeGuide').handleState('state2');
  659. }
  660. if (!this.currentCoin.isPlay) {
  661. this.currentCoin.showAnimation();
  662. } else {
  663. this.currentCoin.updateAnimation();
  664. }
  665. }
  666. }
  667. },
  668. update(dt) {
  669. if (this.buildingInfo) {
  670. // 不断刷新界面
  671. this.layout(this.buildingInfo);
  672. // 只有已经解锁进度条才会走
  673. if (this.buildingInfo.isUnlocked) {
  674. // 进度条走完, 开始生产金币
  675. if (Math.floor(this.rateProgressBar.progress) === 1) {
  676. this.rateProgressBar.progress = 1;
  677. if (GameModule.userInfo.levelHomeItemFullCount != 5 && // 是否满级
  678. this.cityId === Global.devCityId) { // 当前访问的是不是这个city
  679. if (this.notPickupCount >= this.coinArrayMax * 10) {
  680. this.rateProgressLabel.node.active = false;
  681. this.coinFullTip.active = true;
  682. } else {
  683. this.coinFullTip.active = false;
  684. this.notPickupCount += 1;
  685. this.generateCoin();
  686. this.rateProgressBar.progress = 0;
  687. this._currentTime = 0;
  688. }
  689. } else { // 满级后的状态
  690. this.rateProgressBar.progress = 0;
  691. this._currentTime = 0;
  692. this.coinFullTip.active = false;
  693. this.rateProgressLabel.node.active = true;
  694. }
  695. } else {
  696. this.rateProgressLabel.node.active = true;
  697. this.coinFullTip.active = false;
  698. this._currentTime += dt;
  699. this.rateProgressBar.progress = this._currentTime / this.countDown;
  700. if (Math.floor(this.rateProgressBar.progress) === 1) {
  701. this.rateProgressBar.progress = 1;
  702. }
  703. let resultCountDown = this.countDown - Math.floor(this._currentTime);
  704. if (this._preCountDown !== resultCountDown) {
  705. this.countdownLabel.string = DWTool.calculateTime(resultCountDown);
  706. this._preCountDown = resultCountDown;
  707. }
  708. }
  709. }
  710. }
  711. },
  712. });