LevelFriendHomeItem.js 11 KB

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