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