LevelHomeItem.js 36 KB

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