LevelHomeItem.js 35 KB

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