Game.js 20 KB

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