Game.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. const GameModule = require("./utils/GameModule");
  2. const FriendSystemApi = require('./net/FriendSystemApi');
  3. const ShareAction = require('./utils/ShareAction');
  4. const DWTool = require('./utils/DWTool');
  5. const DWAlert = require('./utils/DWAlert');
  6. const HomeApi = require("./net/HomeApi");
  7. const JobApi = require('./net/JobApi');
  8. const { GameNotificationKey, SenceMap, JobPageType } = require('./utils/GameEnum');
  9. const AlertManager = require('./utils/AlertManager');
  10. const tutorial = require('./utils/Bundle').tutorial;
  11. const StateMachine = require('./lib/StateMachine');
  12. const StateMachineHistory = require('./lib/StateMachineHistory');
  13. const WsManager = require('./net/Ws');
  14. cc.Class({
  15. extends: cc.Component,
  16. properties: {
  17. levelHomePrefab: cc.Prefab,
  18. levelFriendHomePrefab: cc.Prefab,
  19. wechatFriendNode: cc.Node,
  20. //用户信息页
  21. userInfoPrefab: cc.Prefab,
  22. myInfoTop: cc.Node,
  23. myTabBottom: cc.Node,
  24. friendInfoTop: cc.Node,
  25. friendTabBottom: cc.Node,
  26. _otherActor: cc.Node,
  27. friendSystemPrefab: cc.Prefab,
  28. friendHeadSprite: cc.Sprite,
  29. talentPagePrefab: cc.Prefab,
  30. noticePoint: cc.Node,
  31. chooseJob: cc.Node,
  32. transition: cc.Node,
  33. mapIcon: cc.Node,
  34. },
  35. onLoad() {
  36. GameModule.game = this.node;
  37. // 创建一个事件监听实例, 用来实现跨节点监听事件
  38. GameEvent.init();
  39. this.senceHistory = [];
  40. this.buyTipNode = null;
  41. //我的家园
  42. let levelHome = cc.instantiate(this.levelHomePrefab);
  43. levelHome = levelHome.getComponent('LevelHome');
  44. levelHome.init(Global.user.uid, Global.cityId);
  45. this.levelHome = levelHome;
  46. //好友系统Tab面板
  47. let friendSystem = cc.instantiate(this.friendSystemPrefab);
  48. this.friendSystemScript = friendSystem.getComponent('FriendSystem');
  49. //好友家园
  50. let friendHome = cc.instantiate(this.levelFriendHomePrefab);
  51. friendHome = friendHome.getComponent('LevelFriendHome')
  52. this.friendHome = friendHome;
  53. //突破界面
  54. let breakOut = "";
  55. this.breakOut = breakOut;
  56. // //转职界面
  57. // this.jobChange = cc.instantiate(this.jobChangePrefab);
  58. // this.jobChange = this.jobChange.getComponent('ChangeJob');
  59. // 星探界面
  60. this.finder = cc.instantiate(this.talentPagePrefab);
  61. this.finder = this.finder.getComponent('TalentPage');
  62. // 引导界面
  63. // this.homeGuide = this.homeGuide.getComponent('HomeGuide')
  64. // this.homeGuide.init()
  65. // 监听事件
  66. this._setEventListener();
  67. // 初始化场景管理状态机
  68. this._initFSM()
  69. // 初始化socket链接
  70. this._initSocketMng();
  71. if (Global.shareUid != -1) {
  72. console.log('shareUid: ' + Global.shareUid);
  73. this.processShareAction();
  74. }
  75. this.friendHeadSprite.node.on(cc.Node.EventType.TOUCH_END, () => {
  76. GameEvent.fire(GameNotificationKey.ShowUserInfomation, this.friendHome.uid);
  77. }, this);
  78. // if (Global.cityId > 1) {
  79. // this.mapIcon.active = true;
  80. // } else {
  81. // this.mapIcon.active = false;
  82. // }
  83. },
  84. _initSocketMng() {
  85. let ja0ckUrl = `wss://172.16.15.170:8108/connect?uid=${Global.user.uid}&token=${Global.user.token}&channel=LuciferChannel&ver=1&os=1`
  86. let testUrl = `wss://test-message-allstar.duowan.com/connect?uid=${Global.user.uid}&token=${Global.user.token}&channel=LuciferChannel&ver=1&os=1`
  87. let saUrl = "ws://172.16.12.245:3000"
  88. let Message = tutorial.Message;
  89. let ws = new WsManager(testUrl, {
  90. binaryType: 'arraybuffer',
  91. autoConnect: true, // 自动连接
  92. reconnection: true, // 断开自动重连
  93. reconnectionDelay: 3000, // 重连间隔时间,单位秒
  94. reconnectionAttempts: 5 // 最大重连尝试次数,默认为Infinity
  95. })
  96. ws.on('open', (res) => {
  97. /**
  98. * targetUid : 目标用户uid
  99. * opt : 操作类型
  100. * {1: 赎身; 2: 抢夺; 3: 签约; 4: 解雇; 5: 入驻;6: 召回;7: 驱赶;8: 举报; 9:通过好友申请;10:成为好友}
  101. * exBossUid : 抢夺成功时,前任老板的uid
  102. */
  103. GameEvent.on(GameNotificationKey.NoticeRoleOpt, this, (targetUid, opt, exBossUid) => {
  104. let payload = {
  105. roleOptMessage: {
  106. targetUid: targetUid,
  107. opt: opt
  108. },
  109. type: "ROLEOPT"
  110. }
  111. //抢夺成功时,需要加上前任老板的uid
  112. if (opt == 2) {
  113. payload.roleOptMessage['exBossUid'] = exBossUid
  114. }
  115. let msg = Message.create(payload);
  116. let buffer = Message.encode(msg).finish()
  117. console.log('[Send msg]: ', msg);
  118. ws.send(buffer);
  119. });
  120. })
  121. ws.on('message', (data) => {
  122. let u8Arr = new Uint8Array(data)
  123. let decodeMsg = Message.decode(u8Arr);
  124. console.log("[Response text msg]: ", decodeMsg);
  125. if (decodeMsg.notificationMessage) {
  126. switch (decodeMsg.notificationMessage.nType) {
  127. case 1:
  128. //有新的通知
  129. this.toggleNoticePoint(true)
  130. break;
  131. case 2:
  132. //刷新好友列表
  133. GameEvent.fire(GameNotificationKey.RefreshFriendList)
  134. // this.levelHome.init(Global.user.uid, Global.cityId);
  135. break;
  136. case 3:
  137. //刷新我的艺人列表
  138. GameEvent.fire(GameNotificationKey.RefreshArtistManagerList)
  139. break;
  140. default:
  141. break;
  142. }
  143. }
  144. })
  145. ws.on('close', () => {
  146. GameEvent.off(GameNotificationKey.NoticeRoleOpt, this);
  147. })
  148. ws.on('error', () => {
  149. GameEvent.off(GameNotificationKey.NoticeRoleOpt, this);
  150. })
  151. },
  152. _initFSM() {
  153. let self = this
  154. let gameFSM = StateMachine.factory({
  155. init: 'levelhome',
  156. transitions: [
  157. { name: 'visitfriend', from: ['levelhome', 'userinfo', 'friendhome'], to: 'friendhome' },
  158. { name: 'showuserinfo', from: ['levelhome', 'friendhome'], to: 'userinfo' },
  159. { name: 'visitcitymap', from: 'levelhome', to: 'citymap' },
  160. { name: 'visitcity', from: 'citymap', to: 'levelhome' },
  161. { name: 'changejob', from: '', to: '' }
  162. ],
  163. methods: {
  164. onBeforeVisitcity(lifecycle, cid) {
  165. //新城市初始化所有状态
  166. self.levelHome.init(Global.user.uid, cid);
  167. },
  168. onVisitfriend(lifecycle, uid) {
  169. //如果是friendhome状态之间跳转,则不切换右下方操作区域UI
  170. let toggleTop = true, toggleBottom = cc.Node;
  171. if (lifecycle.from == "friendhome") {
  172. self._onLeaveFriendhome(toggleTop, toggleBottom)
  173. self._onEnterFriendhome(uid, toggleTop, toggleBottom)
  174. }
  175. self._closeFriendSystem();
  176. self.mapIcon.active = false;
  177. },
  178. onShowuserinfo() {
  179. },
  180. onVisitcitymap() {
  181. },
  182. onEnterFriendhome(lifecycle, uid) {
  183. self._onEnterFriendhome(uid)
  184. },
  185. onLeaveFriendhome() {
  186. self._onLeaveFriendhome()
  187. },
  188. onEnterLevelhome() {
  189. self.mapIcon.active = true;
  190. self._onEnterLevelhome()
  191. },
  192. onLeaveLevelhome() {
  193. self._onLeaveLevelhome()
  194. },
  195. onEnterUserinfo(lifecycle, uid) {
  196. self._enterUserInfo(uid)
  197. self._hideTabbar();
  198. },
  199. onLeaveUserinfo() {
  200. self._showTabbar();
  201. },
  202. onEnterCitymap(lifecycle, showAnim) {
  203. self._onEnterCitymap(showAnim);
  204. },
  205. onLeaveCitymap() {
  206. self._onLeaveCitymap()
  207. }
  208. },
  209. plugins: [
  210. new StateMachineHistory()
  211. ]
  212. })
  213. this.gameFSM = new gameFSM('levelhome')
  214. },
  215. /**
  216. * 进入好友家园
  217. * @param {Number} uid 用户id
  218. * @param {Boolean} showTop 是否显示顶部信息
  219. * @param {Boolean} showBottom 是否显示底部操作栏
  220. */
  221. _onEnterFriendhome(uid, showTop = true, showBottom = true) {
  222. if (uid) {
  223. this.friendHome.initFriend(uid, res => {
  224. this._showFriendUI(showTop, showBottom)
  225. this.friendHome.node.active = true;
  226. })
  227. } else {
  228. this.friendHome.reInitFriend(() => {
  229. this._showFriendUI(showTop, showBottom)
  230. this.friendHome.node.active = true;
  231. });
  232. }
  233. },
  234. /**
  235. * 离开好友家园
  236. * @param {Boolean} hideTop 是否隐藏顶部信息
  237. * @param {Boolean} hideBottom 是否隐藏底部操作栏
  238. */
  239. _onLeaveFriendhome(hideTop = true, hideBottom = true) {
  240. this._hideFriendUI(hideTop, hideBottom)
  241. this.friendHome.node.active = false;
  242. },
  243. /**
  244. * 进入我的家园
  245. */
  246. _onEnterLevelhome() {
  247. this._showHomeUI()
  248. if (!this.isFirstLoad) {
  249. this.isFirstLoad = true;
  250. let actionBottom = cc.moveBy(0.4, cc.v2(0, this.myTabBottom.height + 27));
  251. this.myTabBottom.runAction(actionBottom);
  252. this.transition.active = true;
  253. let actionFade = cc.sequence(cc.fadeOut(2), cc.callFunc(() => {
  254. this.transition.active = false;
  255. }))
  256. this.transition.runAction(actionFade.easing(cc.easeIn(1.5)));
  257. } else {
  258. this.transition.active = true;
  259. this.transition.opacity = 200;
  260. let actionFade = cc.sequence(cc.fadeOut(1), cc.callFunc(() => {
  261. this.transition.active = false;
  262. }))
  263. this.transition.runAction(actionFade.easing(cc.easeIn(1.5)));
  264. }
  265. this.levelHome.node.active = true;
  266. this.refreshActorNoJob();
  267. },
  268. /**
  269. * 离开我的家园
  270. */
  271. _onLeaveLevelhome() {
  272. this._hideHomeUI()
  273. },
  274. /**
  275. * 显示我的家园界面所需UI
  276. * 恢复LevelHome的位置到Canvas场景内
  277. */
  278. _showHomeUI() {
  279. let actionTop = cc.moveBy(0.4, cc.v2(0, -this.myInfoTop.height));
  280. this.myInfoTop.runAction(actionTop);
  281. this.levelHome.node.x = 0;
  282. },
  283. /**
  284. * 隐藏我的家园界面所需UI
  285. * 把LevelHome移出Canvas场景外
  286. */
  287. _hideHomeUI() {
  288. let actionTop = cc.moveBy(0.4, cc.v2(0, this.myInfoTop.height));
  289. this.myInfoTop.runAction(actionTop);
  290. setTimeout(() => {
  291. this.levelHome.node.x = -800;
  292. }, 500)
  293. },
  294. /**
  295. * 显示好友家园界面所需UI
  296. * @param {Boolean} showTop 是否显示顶部信息
  297. * @param {Boolean} showBottom 是否显示底部操作栏
  298. */
  299. _showFriendUI(showTop = true, showBottom = true) {
  300. if (showTop) {
  301. let actionTop = cc.moveBy(0.4, cc.v2(0, -this.friendInfoTop.height));
  302. this.friendInfoTop.runAction(actionTop);
  303. }
  304. if (showBottom) {
  305. let actionRight = cc.moveBy(0.4, cc.v2(-270, 0));
  306. this.friendTabBottom.runAction(actionRight);
  307. }
  308. },
  309. /**
  310. * 隐藏好友家园界面所需UI
  311. * @param {Boolean} hideTop 是否隐藏顶部信息
  312. * @param {Boolean} hideBottom 是否隐藏底部操作栏
  313. */
  314. _hideFriendUI(hideTop = true, hideBottom = true) {
  315. if (hideTop) {
  316. let actionTop = cc.moveBy(0.3, cc.v2(0, this.friendInfoTop.height));
  317. this.friendInfoTop.runAction(actionTop);
  318. }
  319. if (hideBottom) {
  320. let actionRight = cc.moveBy(0.3, cc.v2(270, 0));
  321. this.friendTabBottom.runAction(actionRight);
  322. }
  323. },
  324. /**
  325. * 恢复Tab的位置到Canvas场景内
  326. */
  327. _showTabbar() {
  328. this.myTabBottom.x = 0;
  329. },
  330. /**
  331. * 把Tab移出Canvas场景外
  332. */
  333. _hideTabbar() {
  334. setTimeout(() => {
  335. this.myTabBottom.x = -800;
  336. }, 200)
  337. },
  338. /**
  339. * 进入个人信息页
  340. */
  341. _enterUserInfo(uid) {
  342. if (this.userInfo) {
  343. this.userInfo.init(this, uid);
  344. } else {
  345. DWTool.loadResPrefab('./prefabs/user_information')
  346. .then((result) => {
  347. this.userInfo = cc.instantiate(result);
  348. this.userInfo = this.userInfo.getComponent('UserInformation');
  349. this.userInfo.init(this, uid);
  350. }).catch((err) => {
  351. cc.error(err);
  352. });
  353. }
  354. },
  355. _showFriendSystem() {
  356. this.friendSystemScript.show(this.wechatFriendNode);
  357. },
  358. _closeFriendSystem() {
  359. this.friendSystemScript.close();
  360. },
  361. /**
  362. * 进入城市地图界面
  363. * @param {Boolean} showAnim 是否播放迁移到新城市的动画
  364. */
  365. _onEnterCitymap(showAnim) {
  366. let self = this;
  367. if (this.cityMap) {
  368. this.cityMap.show(showAnim);
  369. } else {
  370. cc.loader.loadRes('/prefabs/map', cc.Prefab, (error, prefab) => {
  371. if (error === null) {
  372. this.cityMap = cc.instantiate(prefab);
  373. cc.find('Canvas').addChild(this.cityMap);
  374. this.cityMap = this.cityMap.getComponent('CityMapCtrl');
  375. this.cityMap.init(this);
  376. this.cityMap.show(showAnim);
  377. } else {
  378. console.log(JSON.stringify(error));
  379. }
  380. });
  381. }
  382. // this._hideLevelHome();
  383. // this.hideLevelHome();
  384. },
  385. /**
  386. * 离开城市地图界面
  387. */
  388. _onLeaveCitymap() {
  389. if (this.cityMap) {
  390. this.cityMap.node.active = false;
  391. }
  392. },
  393. /**
  394. * 查看他人艺人列表
  395. */
  396. showOtherActors() {
  397. DWTool.loadResPrefab('./prefabs/other_actor')
  398. .then((result) => {
  399. this._otherActor = cc.instantiate(result);
  400. this._otherActor.getComponent("OtherActor").uid = this.friendHome.uid;
  401. this._otherActor.parent = cc.find("Canvas");
  402. }).catch((err) => {
  403. cc.error(err);
  404. });
  405. },
  406. /**
  407. * 关闭他人艺人列表
  408. */
  409. closeOtherActors() {
  410. this._otherActor.destroy();
  411. },
  412. showCompanyMax() {
  413. if (Global.cityId === 1) {
  414. }
  415. ///由于阿德的上报等级不够及时,所以满级等5秒再显示去下个城市的按钮
  416. this.scheduleOnce(() => {
  417. cc.loader.loadRes('/prefabs/current_company_max', cc.Prefab, (error, prefab) => {
  418. if (error === null) {
  419. let companyMaxAlert = cc.instantiate(prefab);
  420. cc.find('Canvas').addChild(companyMaxAlert);
  421. } else {
  422. console.log(JSON.stringify(error));
  423. }
  424. });
  425. }, 5);
  426. },
  427. //转职
  428. //选择职业
  429. //职业突破页面
  430. showJobPage(type, userInfo, zIndex) {
  431. let self = this;
  432. cc.loader.loadRes('/prefabs/change_job', cc.Prefab, (error, prefab) => {
  433. if (error === null) {
  434. let jobChange = cc.instantiate(prefab);
  435. self.jobChange = jobChange.getComponent('ChangeJob');
  436. self.jobChange.show(type, userInfo, zIndex);
  437. } else {
  438. console.log(JSON.stringify(error));
  439. }
  440. });
  441. },
  442. showChooseJob() {
  443. let self = this;
  444. cc.loader.loadRes('/prefabs/change_job', cc.Prefab, (error, prefab) => {
  445. if (error === null) {
  446. let jobChange = cc.instantiate(prefab);
  447. this.jobChange = jobChange.getComponent('ChangeJob');
  448. this.jobChange.showFromTalent(JobPageType.ChooseJob, self.noJobActors);
  449. } else {
  450. console.log(JSON.stringify(error));
  451. }
  452. });
  453. },
  454. //关闭转职,选择职业,突破页面
  455. closeJobPage() {
  456. this.jobChange.close();
  457. },
  458. showTalentPage() {
  459. this.finder.show();
  460. },
  461. /**
  462. * 是否显示按钮提示
  463. * @param {Boolean} flag
  464. */
  465. toggleNoticePoint(flag) {
  466. if (flag) {
  467. this.noticePoint.active = true
  468. } else {
  469. this.noticePoint.active = false
  470. }
  471. },
  472. showCatFlyAnimation() {
  473. GameEvent.fire(GameNotificationKey.showCatFlyAnimation);
  474. },
  475. handleCityMap: _.debounce(() => {
  476. GameEvent.fire(GameNotificationKey.ShowCityMap, false);
  477. // GameEvent.fire(GameNotificationKey.showCatFlyAnimation);
  478. }, 1000, true),
  479. handleNoticePopup: _.debounce((event) => {
  480. AlertManager.showNoticePopup();
  481. event.target.getChildByName("notice_point").active = false
  482. }, 1000, true),
  483. handleBack: _.debounce(() => {
  484. GameEvent.fire(GameNotificationKey.HistoryBack)
  485. }, 1000, true),
  486. // 监听事件
  487. _setEventListener() {
  488. GameEvent.on(GameNotificationKey.ShowCityMap, this, (showAnim) => {
  489. this.gameFSM.visitcitymap(showAnim);
  490. });
  491. GameEvent.on(GameNotificationKey.ShowFriendSystem, this, () => {
  492. this._showFriendSystem();
  493. });
  494. GameEvent.on(GameNotificationKey.VisitFriendHome, this, (uid) => {
  495. this.gameFSM.visitfriend(uid)
  496. });
  497. GameEvent.on(GameNotificationKey.HistoryBack, this, () => {
  498. this.gameFSM.historyBack()
  499. })
  500. GameEvent.on(GameNotificationKey.ShowUserInfomation, this, (uid) => {
  501. this.gameFSM.showuserinfo(uid)
  502. });
  503. GameEvent.on(GameNotificationKey.ProcessShareAction, this, this.processShareAction);
  504. GameEvent.on(GameNotificationKey.ShowJobPage, this, this.showJobPage);
  505. GameEvent.on(GameNotificationKey.ShowJobPageFromTalent, this, this.showChooseJob);
  506. GameEvent.on(GameNotificationKey.RefreshFriendList, this, this.refreshActorNoJob);
  507. GameEvent.on(GameNotificationKey.CurrentCompanyMax, this, this.showCompanyMax);
  508. },
  509. onDestroy() {
  510. GameEvent.off(GameNotificationKey.ShowFriendSystem, this);
  511. GameEvent.off(GameNotificationKey.VisitFriendHome, this);
  512. GameEvent.off(GameNotificationKey.HistoryBack, this);
  513. GameEvent.off(GameNotificationKey.ProcessShareAction, this);
  514. GameEvent.off(GameNotificationKey.ShowUserInfomation, this);
  515. GameEvent.off(GameNotificationKey.ShowJobChoose, this);
  516. GameEvent.off(GameNotificationKey.ShowJobPage, this);
  517. GameEvent.off(GameNotificationKey.RefreshFriendList, this);
  518. GameEvent.off(GameNotificationKey.ShowJobPageFromTalent, this);
  519. GameEvent.off(GameNotificationKey.ShowCityMap, this);
  520. GameEvent.off(GameNotificationKey.CurrentCompanyMax, this);
  521. },
  522. processShareAction() {
  523. console.log('processShareAction');
  524. if (Global.user != null) {
  525. if (Global.shareType == ShareAction.ADD_FRIEND) {
  526. let shareUid = Global.shareUid
  527. FriendSystemApi.addFriend(shareUid, () => {
  528. GameEvent.fire('refresh_friend_list');
  529. setTimeout(() => {
  530. GameEvent.fire(GameNotificationKey.NoticeRoleOpt, shareUid, 10)
  531. }, 2500)
  532. });
  533. }
  534. Global.shareUid = -1;
  535. Global.shareType = ShareAction.None;
  536. }
  537. },
  538. refreshActorNoJob() {
  539. JobApi.actorsNoJob(
  540. (response) => {
  541. if (response.list && !_.isEmpty(response.list) && response.list.length > 0) {
  542. this.chooseJob.active = true;
  543. this.noJobActors = response.list;
  544. } else {
  545. this.chooseJob.active = false;
  546. }
  547. },
  548. (code, msg) => {
  549. // this.chooseJob.active = false;
  550. }
  551. );
  552. }
  553. });