1
0

LevelHomeItem.js 28 KB

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