ArtistResident.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. const DWTool = require("../utils/DWTool");
  2. const HomeApi = require("../net/HomeApi");
  3. const { GameNotificationKey } = require("../utils/GameEnum");
  4. const Base64 = require("../lib/Base64").Base64;
  5. const AlertManager = require("../utils/AlertManager");
  6. cc.Class({
  7. extends: cc.Component,
  8. properties: {
  9. content: cc.Node,
  10. scrollView: cc.ScrollView,
  11. talentBtn: cc.Button,
  12. confirmBtn: cc.Button,
  13. },
  14. init(buildingInfo, uid, isSelf) {
  15. this.buildingInfo = buildingInfo;
  16. this.uid = uid;
  17. this.isSelf = isSelf;
  18. },
  19. // LIFE-CYCLE CALLBACKS:
  20. onLoad() {
  21. this.node.height = cc.view.getVisibleSize().height;
  22. this.contentY = this.content.y = (this.content.height - this.node.height) / 2;
  23. this._residentList = [];
  24. this._itemScripts = [];
  25. this._currentResidentArtist = null;
  26. this.getNetworkData();
  27. GameEvent.on(GameNotificationKey.RefreshResidentArtistList, this, () => {
  28. for (let child of this.scrollView.content.children) {
  29. child.destroy();
  30. }
  31. this.getNetworkData();
  32. });
  33. this.confirmEvent = _.debounce(() => {
  34. if (this._currentResidentArtist && !this._currentResidentArtist.isStationed) {
  35. let reportFormInfo = {};
  36. reportFormInfo["artistUid"] = this._currentResidentArtist.uid;
  37. reportFormInfo["buildingId"] = this.buildingInfo.buildingId;
  38. reportFormInfo["buildingLevel"] = this.buildingInfo.level;
  39. reportFormInfo["targetUid"] = this.uid;
  40. HomeApi.friendStation(JSON.stringify(reportFormInfo), (responseData) => {
  41. this.close();
  42. // 入驻成功, 要将现有的金币收取, 并且刷新入驻艺人列表
  43. GameEvent.fire(GameNotificationKey.ResidentArtist, this.uid, this.buildingInfo.buildingId);
  44. // 触发入驻通知,opt = 5
  45. GameEvent.fire(GameNotificationKey.NoticeRoleOpt, this.uid, 5)
  46. }, (code, msg) => {
  47. // 亲密度不够
  48. if (code === 814) {
  49. AlertManager.showIntimacyBotEnough();
  50. }
  51. console.log(msg);
  52. });
  53. } else {
  54. this.node.destroy();
  55. }
  56. }, 1000, true);
  57. this.showTalentEvent = _.debounce(() => {
  58. this.close();
  59. AlertManager.showTalentAlert();
  60. }, 1000, true);
  61. },
  62. onDestroy() {
  63. GameEvent.off(GameNotificationKey.RefreshResidentArtistList, this);
  64. },
  65. start() {
  66. this.content.y = -cc.view.getVisibleSize().height;
  67. let s = cc.sequence(cc.moveTo(0.2, 0, this.contentY + 20).easing(cc.easeCubicActionOut()), cc.moveBy(0.05, 0, -20));
  68. this.content.runAction(s);
  69. },
  70. getNetworkData() {
  71. HomeApi.friendGetArtistsByBuildingId(this.uid, this.buildingInfo.buildingId, (responseData) => {
  72. this._residentList = responseData.list || [];
  73. this.canBeStationedList = this._residentList.filter(item => item.canBeStationed == 1);
  74. this.cannotBeStationedList = this._residentList.filter(item => item.canBeStationed == 0);
  75. if (this.canBeStationedList.length === 0) {
  76. this.talentBtn.node.active = true;
  77. this.confirmBtn.node.active = false;
  78. } else {
  79. this.talentBtn.node.active = false;
  80. this.confirmBtn.node.active = true;
  81. }
  82. this.layout();
  83. }, (err) => {
  84. console.log(err);
  85. });
  86. },
  87. layout() {
  88. if (this._residentList.length === 0) {
  89. this.loadTaletPrefab()
  90. .then((result) => {
  91. let item = cc.instantiate(result);
  92. this.scrollView.content.addChild(item);
  93. });
  94. } else {
  95. if (this.canBeStationedList.length === 0) {
  96. this.loadTaletPrefab()
  97. .then((talent) => {
  98. let item = cc.instantiate(talent);
  99. this.scrollView.content.addChild(item);
  100. return this.loadDashPrefab();
  101. })
  102. .then((dash) => {
  103. let item = cc.instantiate(dash);
  104. this.scrollView.content.addChild(item);
  105. return this.loadPlaceholderItemPrefab();
  106. })
  107. .then((cannotBeStationedItem) => {
  108. for (let i = 0; i < this.cannotBeStationedList.length; i++) {
  109. let item = cc.instantiate(cannotBeStationedItem);
  110. let itemScript = item.getComponent(`ArtistResidentItemPlaceholder`);
  111. itemScript.init(this.cannotBeStationedList[i]);
  112. this.scrollView.content.addChild(item);
  113. }
  114. }).catch((err) => {
  115. console.log(err);
  116. });
  117. } else {
  118. this.loadItemPrefab()
  119. .then((canBeStationedItem) => {
  120. for (let i = 0; i < this.canBeStationedList.length; i++) {
  121. let item = cc.instantiate(canBeStationedItem);
  122. let itemScript = item.getComponent(`ArtistResidentItem`);
  123. this._itemScripts.push(itemScript)
  124. itemScript.init(this.uid, this.buildingInfo, this.isSelf, this.canBeStationedList[i], (artistData) => {
  125. this._currentResidentArtist = artistData;
  126. this._itemScripts.forEach(itemScript => {
  127. itemScript.setStyle(this._currentResidentArtist.uid);
  128. });
  129. });
  130. this.scrollView.content.addChild(item);
  131. }
  132. if (this.cannotBeStationedList.length > 0) {
  133. return this.loadDashPrefab();
  134. }
  135. return;
  136. })
  137. .then((dash) => {
  138. if (!dash) { return; }
  139. let item = cc.instantiate(dash);
  140. this.scrollView.content.addChild(item);
  141. return this.loadPlaceholderItemPrefab();
  142. })
  143. .then((cannotBeStationedItem) => {
  144. if (!cannotBeStationedItem) { return; }
  145. for (let i = 0; i < this.cannotBeStationedList.length; i++) {
  146. let item = cc.instantiate(cannotBeStationedItem);
  147. let itemScript = item.getComponent(`ArtistResidentItemPlaceholder`);
  148. itemScript.init(this.cannotBeStationedList[i]);
  149. this.scrollView.content.addChild(item);
  150. }
  151. }).catch((err) => {
  152. console.log(err);
  153. });
  154. }
  155. }
  156. },
  157. guide() {
  158. if (this._itemScripts.length > 0) {
  159. let itemScript = this._itemScripts[0];
  160. itemScript.selectItem();
  161. }
  162. },
  163. /**
  164. * 星探的预制件
  165. */
  166. loadTaletPrefab() {
  167. return new Promise((resolve, reject) => {
  168. cc.loader.loadRes('./prefabs/artist_resident_talent', cc.Prefab, (error, prefab) => {
  169. if (error) {
  170. reject(error);
  171. } else {
  172. resolve(prefab)
  173. }
  174. });
  175. });
  176. },
  177. /**
  178. * 虚线的预制件
  179. */
  180. loadDashPrefab() {
  181. return new Promise((resolve, reject) => {
  182. cc.loader.loadRes('./prefabs/artist_resident_dash', cc.Prefab, (error, prefab) => {
  183. if (error) {
  184. reject(error);
  185. } else {
  186. resolve(prefab)
  187. }
  188. });
  189. });
  190. },
  191. loadItemPrefab() {
  192. return new Promise((resolve, reject) => {
  193. cc.loader.loadRes('./prefabs/artist_resident_item', cc.Prefab, (error, prefab) => {
  194. if (error) {
  195. reject(error);
  196. } else {
  197. resolve(prefab)
  198. }
  199. });
  200. });
  201. },
  202. loadPlaceholderItemPrefab() {
  203. return new Promise((resolve, reject) => {
  204. cc.loader.loadRes('./prefabs/artist_resident_item_placeholder', cc.Prefab, (error, prefab) => {
  205. if (error) {
  206. reject(error);
  207. } else {
  208. resolve(prefab)
  209. }
  210. });
  211. });
  212. },
  213. confirm() {
  214. this.confirmEvent();
  215. },
  216. showTalent() {
  217. this.showTalentEvent();
  218. },
  219. close() {
  220. let finish = cc.callFunc(() => {
  221. this.node.destroy();
  222. }, this)
  223. this.content.runAction(cc.sequence(cc.moveTo(0.2, 0, -cc.view.getVisibleSize().height).easing(cc.easeCubicActionIn()), finish));
  224. // this.node.destroy();
  225. },
  226. // update (dt) {},
  227. });