LevelFriendHomeItem.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. const DWTool = require('../utils/DWTool');
  2. const { GameNotificationKey } = require("../utils/GameEnum");
  3. const ThemeManager = require("../utils/ThemeManger");
  4. const HomeApi = require("../net/HomeApi");
  5. cc.Class({
  6. extends: cc.Component,
  7. properties: {
  8. // Public Properties
  9. bgNode: cc.Node,
  10. /** 当前显示的图片 */
  11. buildSprite: cc.Sprite,
  12. /** 柱子 */
  13. pillarTop: cc.Sprite,
  14. pillarBottom: cc.Sprite,
  15. pillarRight: cc.Sprite,
  16. pillarLeft: cc.Sprite,
  17. bottomBg: cc.Sprite,
  18. /** 未解锁状态的节点 */
  19. lockNode: cc.Node,
  20. /** 建筑昵称 */
  21. buildNameLabel: cc.Label,
  22. lockBottomNode: cc.Node,
  23. /** 等级节点 */
  24. levelProgressNode: cc.Node,
  25. /** 等级进度条 */
  26. levelProgressBar: cc.ProgressBar,
  27. /** 等级 */
  28. levelProgressLabel: cc.Label,
  29. /** 生产金币节点 */
  30. rateProgressNode: cc.Node,
  31. /** 生产金币进度条 */
  32. rateProgressBar: cc.ProgressBar,
  33. /** 生产了多少金币 */
  34. rateProgressLabel: cc.Label,
  35. /** 倒计时 */
  36. countdownLabel: cc.Label,
  37. artistList: cc.Node,
  38. artistListItem: cc.Prefab,
  39. openDoorSprite: cc.Sprite,
  40. // 显示加成的节点
  41. additionNode: cc.Node,
  42. // 显示加成倍数
  43. additionLabel: cc.Label,
  44. // 加成的骨骼
  45. additionSkeleton: sp.Skeleton,
  46. // 满级提示
  47. maxNode: cc.Node,
  48. addition: {
  49. get: function () {
  50. if (!this._addition) {
  51. this._addition = 0;
  52. }
  53. return this._addition;
  54. },
  55. set: function (value) {
  56. this._addition = value;
  57. if (this._addition === 0) {
  58. this.additionNode.active = false;
  59. this.additionSkeleton.setAnimation(0, 'jiasutiao_2', true);
  60. this.rateProgressBar.barSprite.node.active = true;
  61. } else {
  62. this.additionNode.active = true;
  63. this.additionLabel.string = `X${this._addition}`;
  64. this.additionSkeleton.setAnimation(0, 'jiasutiao_1', true);
  65. this.rateProgressBar.barSprite.node.active = false;
  66. }
  67. }
  68. },
  69. countDown: {
  70. get: function () {
  71. if (!this._countDown) {
  72. this._countDown = 0;
  73. }
  74. return this._countDown;
  75. },
  76. set: function (value) {
  77. this._countDown = value;
  78. this.countdownLabel.string = DWTool.calculateTime(this._countDown);
  79. this._preCountDown = this._countDown;
  80. }
  81. },
  82. rate: {
  83. get: function () {
  84. if (!this._rate) {
  85. this._rate = 0;
  86. }
  87. return this._rate;
  88. },
  89. set: function (value) {
  90. this._rate = value;
  91. if (this.addition > 0) {
  92. this._rate = this._rate * this.addition;
  93. }
  94. this.rateProgressLabel.string = DWTool.coinParse(this._rate);
  95. }
  96. },
  97. },
  98. // LIFE-CYCLE CALLBACKS:
  99. onLoad() {
  100. this._currentTime = 0;
  101. this.humanList = [];
  102. this.isFirstLoad = true;
  103. // 监听自定义事件
  104. this.setEventListener();
  105. },
  106. // 当该节点active为false时, 重置数据
  107. onDisable() {
  108. this.addition = 0;
  109. this.isFirstLoad = true;
  110. this.buildingInfo = null;
  111. for (let child of this.artistList.children) { child.destroy(); }
  112. for (let child of this.humanList) { child.destroy(); }
  113. this.countDown = 0;
  114. this.rate = 0;
  115. this._currentTime = 0;
  116. this.levelProgressBar.progress = 0;
  117. this.rateProgressBar.progress = 0;
  118. this.levelProgressLabel.string = "";
  119. },
  120. onDestroy() {
  121. GameEvent.off(GameNotificationKey.ResidentArtist, this);
  122. GameEvent.off(GameNotificationKey.RefreshLevelHomeArtistList, this);
  123. },
  124. setEventListener() {
  125. // 这个是入驻艺人成功时调用的
  126. GameEvent.on(GameNotificationKey.ResidentArtist, this, (uid, buildingId) => {
  127. if (this.node.active && this.uid === uid && this.buildingInfo.buildingId === buildingId) {
  128. HomeApi.friendGetArtistsInBuilding(this.uid, this.buildingInfo.buildingId, (data) => {
  129. this.artists = data.list || [];
  130. this.artistListLayout();
  131. }, (code, msg) => {
  132. console.log(msg);
  133. })
  134. }
  135. });
  136. // 这个是召回驱赶艺人时调用的
  137. GameEvent.on(GameNotificationKey.RefreshLevelHomeArtistList, this, (uid, buildingId) => {
  138. if (this.node.active && this.uid === uid && this.buildingInfo.buildingId === buildingId) {
  139. HomeApi.friendGetArtistsInBuilding(this.uid, this.buildingInfo.buildingId, (data) => {
  140. this.artists = data.list || [];
  141. this.artistListLayout();
  142. }, (code, msg) => {
  143. console.log(msg);
  144. })
  145. }
  146. });
  147. },
  148. /**
  149. * Public Method, 用来设置建筑背景图
  150. * @param {*} index
  151. */
  152. init(cityId, index) {
  153. if (arguments.length < 2) {
  154. throw new Error("init Missing parameter...");
  155. }
  156. this.cityId = cityId;
  157. this.index = index;
  158. ThemeManager.setBuildItemColor(this.cityId, this.bgNode);
  159. ThemeManager.setItemBuildSpriteFrame(this.cityId, this.buildSprite, index);
  160. ThemeManager.setItemPillarTopSpriteFrame(this.cityId, this.pillarTop);
  161. ThemeManager.setItemPillarBottomSpriteFrame(this.cityId, this.pillarBottom);
  162. ThemeManager.setItemPillarRightSpriteFrame(this.cityId, this.pillarRight);
  163. ThemeManager.setItemPillarLeftSpriteFrame(this.cityId, this.pillarLeft);
  164. ThemeManager.setItemDownSpriteFrame(this.cityId, this.bottomBg);
  165. let lockBottomSprite = this.lockBottomNode.getComponent(cc.Sprite);
  166. ThemeManager.setItemLockDownSpriteFrame(this.cityId, lockBottomSprite);
  167. },
  168. /**
  169. * Public Method, 配置建筑的内部样式
  170. * @param {*} buildingInfo 建筑信息
  171. * @param {*} uid 当前用户的uid
  172. */
  173. config(buildingInfo, uid) {
  174. if (arguments.length < 2) {
  175. throw new Error("Config Missing parameter...");
  176. }
  177. this.buildingInfo = buildingInfo;
  178. this.uid = uid;
  179. this.artists = this.buildingInfo.artists;
  180. // 这里设置一些只需要设置一次的数据
  181. this.countDown = buildingInfo.rateUnit;
  182. this.buildNameLabel.string = buildingInfo.name;
  183. this._notPickupCount = buildingInfo.coinCount;
  184. if (buildingInfo.coinCount === this.coinArrayMax * 10) {
  185. this.rateProgressBar.progress = 1;
  186. this.countdownLabel.string = DWTool.calculateTime(0);
  187. }
  188. this.levelProgressBar.progress = buildingInfo.level / Global.BuildingManager.getLevelCount(buildingInfo.buildingId);
  189. this.levelProgressLabel.string = `LV.${buildingInfo.level}`;
  190. if (this.isFirstLoad) {
  191. this.isFirstLoad = false;
  192. this.artistListLayout();
  193. }
  194. this.layout(buildingInfo);
  195. },
  196. artistListLayout() {
  197. for (let child of this.artistList.children) { child.destroy(); }
  198. for (let child of this.humanList) { child.destroy(); }
  199. let self = this;
  200. let addAddItemToList = function (showTop = false, showBottom = false) {
  201. let addArtistItem = cc.instantiate(self.artistListItem);
  202. self.artistList.addChild(addArtistItem);
  203. let addArtistScript = addArtistItem.getComponent('LevelHomeArtistItem');
  204. addArtistScript.initWithBuildingInfo(self.buildingInfo, self.uid, false, showTop, showBottom);
  205. }
  206. let addArtistItemToList = function (artist, showTop = false, showBottom = false) {
  207. let artistItem = cc.instantiate(self.artistListItem);
  208. self.artistList.addChild(artistItem);
  209. let artistScript = artistItem.getComponent('LevelHomeArtistItem');
  210. artistScript.initWithArtistData(self.buildingInfo, self.uid, false, artist, showTop, showBottom);
  211. }
  212. let addHuman = function (artist, index) {
  213. DWTool.loadResPrefab("./prefabs/artist_man")
  214. .then((prefab) => {
  215. let human = cc.instantiate(prefab);
  216. human.getComponent('ArtistMan').init(artist);
  217. human.getComponent('ArtistMan').direction = (index > 0) ? 1 : -1;
  218. self.node.addChild(human);
  219. self.humanList.push(human);
  220. });
  221. }
  222. // 当没有自己艺人, 也没有好友艺人入驻时, 这里还得区分主态和客态
  223. if (this.artists.length === 0) {
  224. addAddItemToList(true);
  225. // 没有艺人不显示倍数
  226. this.addition = 0;
  227. } else {
  228. // 有艺人入驻要显示倍数
  229. this.additionNode.active = true;
  230. if (this.artists.length === 1) {
  231. let artist = this.artists[0];
  232. this.addition += artist.stationJobLevel + 1;
  233. // 这里要区分主态和客态, 去别人家园时, 如果有一个是客态自己的艺人, 那么另一个就是可以入驻的按钮
  234. if (artist.role === 2) {
  235. addArtistItemToList(artist, true, true);
  236. addHuman(artist, (Math.random() - 0.5) * 2);
  237. addAddItemToList();
  238. } else {
  239. addArtistItemToList(artist, true);
  240. addHuman(artist, (Math.random() - 0.5) * 2);
  241. }
  242. } else {
  243. for (let i = 0; i < this.artists.length; i++) {
  244. let artist = this.artists[i];
  245. this.addition += artist.stationJobLevel + 1;
  246. if (i === 0) {
  247. addArtistItemToList(artist, true, true);
  248. } else {
  249. addArtistItemToList(artist);
  250. }
  251. addHuman(artist, i);
  252. }
  253. }
  254. }
  255. },
  256. layout(buildingInfo) {
  257. this.rateProgressBar.progress = 1;
  258. if (buildingInfo.isUnlocked) {
  259. this.artistList.active = true;
  260. this.lockNode.active = false;
  261. this.rate = buildingInfo.rate;
  262. this.maxNode.active = (buildingInfo.hasNext != 1) ? true : false;
  263. } else {
  264. this.artistList.active = false;
  265. this.lockNode.active = true;
  266. this.countDown = 0;
  267. // this.rateProgressBar.progress = 0;
  268. this.totalRate = 0;
  269. }
  270. },
  271. // update(dt) {
  272. // return;
  273. // if (this.buildingInfo) {
  274. // // 不断刷新界面
  275. // this.layout(this.buildingInfo);
  276. // // 只有已经解锁进度条才会走
  277. // if (this.buildingInfo.isUnlocked) {
  278. // // 进度条走完, 开始生产金币
  279. // if (Math.floor(this.rateProgressBar.progress) === 1) {
  280. // if (Math.floor(this.rateProgressBar.progress) === 1) {
  281. // this.rateProgressBar.progress = 1;
  282. // }
  283. // this.rateProgressBar.progress = 0;
  284. // this._currentTime = 0;
  285. // } else {
  286. // this._currentTime += dt;
  287. // this.rateProgressBar.progress = this._currentTime / this.countDown;
  288. // if (Math.floor(this.rateProgressBar.progress) === 1) {
  289. // this.rateProgressBar.progress = 1;
  290. // }
  291. // let resultCountDown = this.countDown - Math.floor(this._currentTime);
  292. // if (this._preCountDown !== resultCountDown) {
  293. // this.countdownLabel.string = DWTool.calculateTime(resultCountDown);
  294. // this._preCountDown = resultCountDown;
  295. // }
  296. // }
  297. // }
  298. // }
  299. // },
  300. });