move.service.ts 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717
  1. import { Injectable, Logger, OnModuleInit } from '@nestjs/common';
  2. import { ConfigService } from '@nestjs/config';
  3. import { readFileSync } from 'fs';
  4. import { join } from 'path';
  5. import { CacheService } from 'src/cache/cache.service';
  6. import { RotateService } from 'src/rotate/rotate.service';
  7. import configuration from 'src/config/configuration';
  8. // import * as BreakPointIds from '../../ws/points-BreakPointId.json';
  9. // import { SceneService } from 'src/scene/scene.service';
  10. const seqExeAsyncFn = (asyncFn) => {
  11. let runPromise = null;
  12. return function seq(...args) {
  13. if (!runPromise) {
  14. //debugger;
  15. runPromise = asyncFn.apply(this, args);
  16. runPromise.then((data) => {
  17. //debugger;
  18. // console.log('seq result', data);
  19. });
  20. runPromise.then(() => (runPromise = null));
  21. return runPromise;
  22. } else {
  23. return runPromise.then(() => seq.apply(this, args));
  24. }
  25. };
  26. };
  27. let moveInterval = 3;
  28. @Injectable()
  29. export class MoveService implements OnModuleInit {
  30. constructor(
  31. private cacheService: CacheService,
  32. private rotateService: RotateService,
  33. private configService: ConfigService,
  34. ) {}
  35. private logger: Logger = new Logger('MoveService');
  36. private Actions = {
  37. Clicking: 1,
  38. Rotation: 1014,
  39. Joystick: 15,
  40. };
  41. public users = this.rotateService.users;
  42. private reply = {
  43. traceIds: [],
  44. vehicle: null,
  45. mediaSrc: null,
  46. isIDR: false,
  47. newUserStates: [
  48. {
  49. userId: 'dcff36ae4fc1d',
  50. playerState: {
  51. roomTypeId: '',
  52. person: 0,
  53. avatarId: '',
  54. skinId: '',
  55. roomId: '',
  56. isHost: false,
  57. isFollowHost: false,
  58. skinDataVersion: '',
  59. avatarComponents: '',
  60. nickName: '',
  61. movingMode: 0,
  62. attitude: '',
  63. areaName: '',
  64. pathName: '',
  65. pathId: '',
  66. avatarSize: 1,
  67. extra: '',
  68. prioritySync: false,
  69. player: {
  70. position: { x: -700, y: 0, z: 0 },
  71. angle: {
  72. pitch: 0,
  73. yaw: 0,
  74. roll: 0,
  75. },
  76. },
  77. camera: {
  78. position: { x: -1145, y: 0, z: 160 },
  79. angle: {
  80. pitch: 0,
  81. yaw: 0,
  82. roll: 0,
  83. },
  84. },
  85. cameraCenter: { x: -700, y: 0, z: 0 },
  86. },
  87. renderInfo: {
  88. renderType: 0,
  89. videoFrame: null,
  90. cameraStateType: 3,
  91. isMoving: 1,
  92. needIfr: 0,
  93. isVideo: 0,
  94. stillFrame: 0,
  95. isRotating: 0,
  96. isFollowing: 0,
  97. clientPanoTitlesBitmap: [],
  98. clientPanoTreceId: '',
  99. prefetchVideoId: '',
  100. noMedia: false,
  101. },
  102. event: null,
  103. relation: 1,
  104. },
  105. ],
  106. actionResponses: [
  107. {
  108. actionType: 1,
  109. pointType: 100,
  110. extra: '',
  111. traceId: '',
  112. packetId: '',
  113. nps: [],
  114. peopleNum: 0,
  115. zoneId: '',
  116. echoMsg: '',
  117. reserveDetail: null,
  118. userWithAvatarList: [],
  119. newUserStates: [],
  120. code: 0,
  121. msg: '',
  122. },
  123. ],
  124. getStateType: 0,
  125. code: 0,
  126. msg: 'OK',
  127. };
  128. private breakPointInfo: any;
  129. public cameraInfos = [];
  130. public sendingFrameForJoystick = false;
  131. // eslint-disable-next-line @typescript-eslint/no-empty-function
  132. async onModuleInit() {
  133. //const app_id = '0000000003';
  134. // const app_id = '0000000007';
  135. // const prefix = '/mnt/metaverse/scene';
  136. const app_id = configuration().app.appId;
  137. const prefix = configuration().app.prefix;
  138. console.log('app_id', app_id, configuration().app.appId);
  139. console.log('prefix', prefix, configuration().app.appId);
  140. let path;
  141. // let path: string;
  142. if (process.env.NODE_ENV === 'development') {
  143. path = join(__dirname, `../ws/${app_id}/points-${app_id}.json`);
  144. console.log('测试服JSON-move', path);
  145. }
  146. if (process.env.NODE_ENV === 'production') {
  147. path = join(`${prefix}/${app_id}/points-${app_id}.json`);
  148. console.log('正式服JSON-move', path);
  149. }
  150. this.loadJSON(path);
  151. }
  152. async loadJSON(path) {
  153. try {
  154. const data = await readFileSync(path);
  155. const BreakPointInfo = JSON.parse(Buffer.from(data).toString('utf-8'));
  156. this.breakPointInfo = BreakPointInfo;
  157. // console.log('BreakPointInfo', BreakPointInfo);
  158. } catch (error) {
  159. this.logger.error('load-json-error', error);
  160. }
  161. }
  162. init(app_id, userId) {
  163. const user = {
  164. appId: null,
  165. userId: null,
  166. breakPointId: null,
  167. roomId: null,
  168. player: {
  169. position: { x: -700, y: 0, z: 0 },
  170. angle: {
  171. pitch: 0,
  172. yaw: 0,
  173. roll: 0,
  174. },
  175. },
  176. camera: {
  177. position: { x: -1145, y: 0, z: 160 },
  178. angle: {
  179. pitch: 0,
  180. yaw: 0,
  181. roll: 0,
  182. },
  183. },
  184. rotateInfo: {
  185. frameIndex: 0,
  186. horizontal_move: 0,
  187. },
  188. moveInfo: {},
  189. // traceIds: [],
  190. // actionResponses:[]
  191. };
  192. user.appId = app_id;
  193. user.userId = userId;
  194. user.breakPointId = Number(this.configService.get('app.startPoint')) || 0;
  195. console.log('user-init', user);
  196. this.users[userId] = user;
  197. }
  198. async getMoveFrames(
  199. appId,
  200. start_break_point_id,
  201. end_break_point_id,
  202. angleIndex,
  203. ) {
  204. console.log(
  205. 'handlejoystick-angle->相机过渡angleIndex-------------------------:' +
  206. angleIndex,
  207. );
  208. let moveFramesRes, moveFrames;
  209. let key =
  210. 'moveframe:app_id:' +
  211. appId +
  212. ':start_break_point_id:' +
  213. start_break_point_id +
  214. ':end_break_point_id:' +
  215. end_break_point_id +
  216. ':angle:' +
  217. angleIndex;
  218. //倒序
  219. if (start_break_point_id > end_break_point_id) {
  220. key =
  221. 'moveframe:app_id:' +
  222. appId +
  223. ':start_break_point_id:' +
  224. end_break_point_id +
  225. ':end_break_point_id:' +
  226. start_break_point_id +
  227. ':angle:' +
  228. angleIndex;
  229. moveFramesRes = await this.cacheService.get(key);
  230. moveFrames = JSON.parse(moveFramesRes);
  231. moveFrames = moveFrames.reverse();
  232. } else {
  233. moveFramesRes = await this.cacheService.get(key);
  234. moveFrames = JSON.parse(moveFramesRes);
  235. }
  236. return moveFrames;
  237. }
  238. async move(pathArray, actionRequest) {
  239. try {
  240. const userId = actionRequest['user_id'];
  241. const traceId = actionRequest['trace_id'];
  242. const actionType = actionRequest['action_type'];
  243. const user = this.users[userId];
  244. const appId = user.appId;
  245. const path = pathArray || [100, 101, 102]; //需要计算路径
  246. const angle = user.camera.angle.yaw % 45; //纠正需要
  247. const replys = [];
  248. const traceIds = [];
  249. traceIds.push(traceId);
  250. const checkReplys = await this.modeifyCameraAngle(
  251. angle,
  252. userId,
  253. traceId,
  254. actionType,
  255. );
  256. for (let i = 0; i < checkReplys.length; ++i) {
  257. checkReplys[i].actionResponses[0].actionType = actionType;
  258. if (i == 0 || i == checkReplys.length - 1) {
  259. checkReplys[i].isIDR = true;
  260. } else {
  261. checkReplys[i].isIDR = false;
  262. }
  263. }
  264. //replys['P' + user.breakPointId + 'T' + user.breakPointId] = checkReplys;
  265. replys.push(checkReplys);
  266. console.log('路径:' + path);
  267. //过渡传到缓存里
  268. this.reply.traceIds = traceIds;
  269. this.reply['newUserStates'][0].userId = userId;
  270. this.reply['actionResponses'][0].traceId = traceId;
  271. //const index = Math.floor((user.camera.angle.yaw + 1) / 45) % 8; //过渡需要
  272. const index = this.getMoveIndex(user.camera.angle.yaw);
  273. for (let i = 0; i < path.length - 1; ++i) {
  274. const start_break_point_id = path[i];
  275. const end_break_point_id = path[i + 1];
  276. const moveFrames = await this.getMoveFrames(
  277. appId,
  278. start_break_point_id,
  279. end_break_point_id,
  280. index,
  281. );
  282. if (!moveFrames) {
  283. return replys;
  284. }
  285. const pathReplys = this.createCacheReplys(
  286. appId,
  287. moveFrames,
  288. traceId,
  289. userId,
  290. start_break_point_id,
  291. end_break_point_id,
  292. false,
  293. );
  294. //第一段
  295. if (i == 0) {
  296. //第一帧
  297. pathReplys[0].isIDR = true;
  298. }
  299. //最后一段
  300. if (i == path.length - 2) {
  301. //最后一帧
  302. pathReplys[pathReplys.length - 1][
  303. 'newUserStates'
  304. ][0].renderInfo.isMoving = 0;
  305. }
  306. for (let j = 0; j < pathReplys.length; ++j) {
  307. pathReplys[j].actionResponses[0].actionType = actionType;
  308. }
  309. replys.push(pathReplys);
  310. //replys['P' + start_break_point_id + 'T' + end_break_point_id] =pathReplys;
  311. }
  312. return replys;
  313. } catch (error) {
  314. console.log('MoveService', error);
  315. }
  316. }
  317. createCacheReplys(
  318. appId,
  319. moveFrames,
  320. traceId,
  321. userId,
  322. startBreakPointId,
  323. endBreakPointId,
  324. isFromUser,
  325. ) {
  326. const replys = [];
  327. const startPosition = this.breakPointInfo[startBreakPointId].position;
  328. const endPosition = this.breakPointInfo[endBreakPointId].position;
  329. const angle = this.getAngle(
  330. startPosition,
  331. {
  332. x: startPosition.x + 1,
  333. y: startPosition.y,
  334. },
  335. endPosition,
  336. );
  337. const user = this.users[userId];
  338. let i;
  339. for (i = 1; i < moveFrames.length; i += moveInterval) {
  340. const moveFrame = moveFrames[i];
  341. const reply = JSON.parse(JSON.stringify(this.reply));
  342. if (reply.traceIds.indexOf(traceId) == -1) {
  343. reply.traceIds.push(traceId);
  344. }
  345. reply['newUserStates'][0].userId = userId;
  346. if (!isFromUser) {
  347. reply['newUserStates'][0].playerState.player.position = {
  348. x:
  349. startPosition.x +
  350. ((endPosition.x - startPosition.x) / moveFrames.length) * i,
  351. y:
  352. startPosition.y +
  353. ((endPosition.y - startPosition.y) / moveFrames.length) * i,
  354. z:
  355. startPosition.z +
  356. ((endPosition.z - startPosition.z) / moveFrames.length) * i,
  357. };
  358. reply['newUserStates'][0].playerState.player.angle.yaw = angle;
  359. reply['newUserStates'][0].playerState.cameraCenter =
  360. reply['newUserStates'][0].playerState.player.position;
  361. } else {
  362. reply['newUserStates'][0].playerState.player.position = JSON.parse(
  363. JSON.stringify(user.player.position),
  364. );
  365. reply['newUserStates'][0].playerState.player.angle.yaw =
  366. user.player.angle.yaw;
  367. reply['newUserStates'][0].playerState.cameraCenter = JSON.parse(
  368. JSON.stringify(user.player.position),
  369. );
  370. }
  371. reply['newUserStates'][0].playerState.camera.position = JSON.parse(
  372. JSON.stringify(moveFrame.camera_position),
  373. );
  374. if (i == 1) {
  375. console.log('move-2' + moveFrame.camera_angle.yaw);
  376. }
  377. if (moveFrame.camera_angle.yaw < 0) {
  378. moveFrame.camera_angle.yaw += 360;
  379. } else if (moveFrame.camera_angle.yaw > 359) {
  380. moveFrame.camera_angle.yaw -= 360;
  381. }
  382. reply['newUserStates'][0].playerState.camera.angle =
  383. moveFrame.camera_angle;
  384. reply['newUserStates'][0].renderInfo.isMoving = 1;
  385. reply['actionResponses'][0].traceId = traceId;
  386. reply.mediaSrc =
  387. '/' +
  388. appId +
  389. '/' +
  390. startBreakPointId +
  391. '/' +
  392. moveFrame.file_name.substring(0, moveFrame.file_name.indexOf('.')) +
  393. '/' +
  394. moveFrame.file_name +
  395. '?m=' +
  396. new Date().getTime();
  397. if (startBreakPointId > endBreakPointId) {
  398. reply.mediaSrc =
  399. '/' +
  400. appId +
  401. '/' +
  402. endBreakPointId +
  403. '/' +
  404. moveFrame.file_name.substring(0, moveFrame.file_name.indexOf('.')) +
  405. '/' +
  406. moveFrame.file_name +
  407. '?m=' +
  408. new Date().getTime();
  409. }
  410. reply.startBreakPointId = startBreakPointId;
  411. reply.endBreakPointId = endBreakPointId;
  412. // if (i == moveFrames.length - 1) {
  413. // reply.isIDR = true;
  414. // } else {
  415. // reply.isIDR = false;
  416. // }
  417. reply.isIDR = false;
  418. replys.push(reply);
  419. }
  420. if (i != moveFrames.length - 1) {
  421. i = moveFrames.length - 1;
  422. const moveFrame = moveFrames[i];
  423. const reply = JSON.parse(JSON.stringify(this.reply));
  424. reply.traceIds.push(traceId);
  425. reply['newUserStates'][0].userId = userId;
  426. reply['newUserStates'][0].renderInfo.isMoving = 1;
  427. if (!isFromUser) {
  428. reply['newUserStates'][0].playerState.player.position = {
  429. x:
  430. startPosition.x +
  431. ((endPosition.x - startPosition.x) / moveFrames.length) * i,
  432. y:
  433. startPosition.y +
  434. ((endPosition.y - startPosition.y) / moveFrames.length) * i,
  435. z:
  436. startPosition.z +
  437. ((endPosition.z - startPosition.z) / moveFrames.length) * i,
  438. };
  439. reply['newUserStates'][0].playerState.player.angle.yaw = angle;
  440. reply['newUserStates'][0].playerState.cameraCenter =
  441. reply['newUserStates'][0].playerState.player.position;
  442. } else {
  443. reply['newUserStates'][0].playerState.player.position = JSON.parse(
  444. JSON.stringify(user.player.position),
  445. );
  446. reply['newUserStates'][0].playerState.player.angle.yaw =
  447. user.player.angle.yaw;
  448. reply['newUserStates'][0].playerState.cameraCenter = JSON.parse(
  449. JSON.stringify(user.player.position),
  450. );
  451. }
  452. reply['newUserStates'][0].playerState.camera.position =
  453. moveFrame.camera_position;
  454. if (moveFrame.camera_angle.yaw < 0) {
  455. moveFrame.camera_angle.yaw += 360;
  456. } else if (moveFrame.camera_angle.yaw > 359) {
  457. moveFrame.camera_angle.yaw -= 360;
  458. }
  459. reply['newUserStates'][0].playerState.camera.angle =
  460. moveFrame.camera_angle;
  461. reply['actionResponses'][0].traceId = traceId;
  462. reply.mediaSrc =
  463. '/' +
  464. appId +
  465. '/' +
  466. startBreakPointId +
  467. '/' +
  468. moveFrame.file_name.substring(0, moveFrame.file_name.indexOf('.')) +
  469. '/' +
  470. moveFrame.file_name +
  471. '?m=' +
  472. new Date().getTime();
  473. if (startBreakPointId > endBreakPointId) {
  474. reply.mediaSrc =
  475. '/' +
  476. appId +
  477. '/' +
  478. endBreakPointId +
  479. '/' +
  480. moveFrame.file_name.substring(0, moveFrame.file_name.indexOf('.')) +
  481. '/' +
  482. moveFrame.file_name +
  483. '?m=' +
  484. new Date().getTime();
  485. }
  486. reply.startBreakPointId = startBreakPointId;
  487. reply.endBreakPointId = endBreakPointId;
  488. replys.push(reply);
  489. }
  490. replys[replys.length - 1].isIDR = true;
  491. return replys;
  492. }
  493. //需要通知user,人物和相机走到哪一个呼吸点位了
  494. updateUser(userId, breakPointId, lastReply) {
  495. const user = this.users[userId];
  496. user.breakPointId = breakPointId;
  497. if (lastReply.actionResponses[0].actionType != 15) {
  498. user.player.position =
  499. lastReply['newUserStates'][0].playerState.player.position;
  500. }
  501. user.player.angle = lastReply['newUserStates'][0].playerState.player.angle;
  502. user.camera.position =
  503. lastReply['newUserStates'][0].playerState.camera.position;
  504. user.camera.angle = lastReply['newUserStates'][0].playerState.camera.angle;
  505. }
  506. getBreakPoints(actionRequest) {
  507. const userId = actionRequest['user_id'];
  508. const traceId = actionRequest['trace_id'];
  509. const actionType = actionRequest['action_type'];
  510. const user = this.users[userId];
  511. const appId = user.appId;
  512. const breakPointId = user.breakPointId;
  513. const reply = {
  514. actionType: actionType,
  515. pointType: 100,
  516. extra: '',
  517. traceId: traceId,
  518. packetId: '',
  519. nps: [],
  520. peopleNum: 0,
  521. zoneId: '',
  522. echoMsg: '',
  523. reserveDetail: null,
  524. userWithAvatarList: [],
  525. newUserStates: [],
  526. code: 0,
  527. msg: '',
  528. };
  529. //const breakPoints = await this.cacheService.get('breakpoints:app_id:'+appId+':break_point_id:'+breakPointId);
  530. //获取redis表全部元素,'breakpoints:app_id:'+appId+':break_point_id:'开头的
  531. //const keys = await this.cacheService.keys(`breakpoints:app_id:${appId}*`);
  532. for (const key in this.breakPointInfo) {
  533. const breakPoint = this.breakPointInfo[key];
  534. //const breakPoint = JSON.parse(breakPointRes);
  535. //const position = breakPoint.position;
  536. reply['nps'].push({
  537. position: breakPoint.position,
  538. breakPointId: Number(key),
  539. });
  540. }
  541. // for (let i = 0; i < keys.length; ++i) {
  542. // const breakPointRes = await this.cacheService.get(keys[i]);
  543. // const breakPoint = JSON.parse(breakPointRes);
  544. // const position = breakPoint.position;
  545. // reply['nps'].push({
  546. // position: position,
  547. // breakPointId: breakPoint.breakPointId,
  548. // });
  549. // }
  550. return reply;
  551. }
  552. getAngle(point, point1, point2) {
  553. const x1 = point1.x - point.x;
  554. const y1 = point1.y - point.y;
  555. const x2 = point2.x - point.x;
  556. const y2 = point2.y - point.y;
  557. const dot = x1 * x2 + y1 * y2;
  558. const det = x1 * y2 - y1 * x2;
  559. const angle = (Math.atan2(det, dot) / Math.PI) * 180;
  560. return (angle + 360) % 360;
  561. }
  562. async stop(traceId, userId, breakPointId, cameraAngle, playerAngle) {
  563. //const breakPointId = movePointIds.substring(movePointIds.indexOf('-') + 1);
  564. const user = this.users[userId];
  565. const startBreakPointId = user.breakPointId;
  566. user.breakPointId = breakPointId;
  567. const appId = user.appId;
  568. const breakPoint = this.breakPointInfo[breakPointId];
  569. user.player.position = breakPoint.position;
  570. user.player.angle = playerAngle;
  571. const rotateKey =
  572. 'rotateframe:app_id:' +
  573. appId +
  574. ':frame_index:' +
  575. cameraAngle.yaw +
  576. ':break_point_id:' +
  577. breakPointId;
  578. const rotateDataRes = await this.cacheService.get(rotateKey);
  579. const rotateData = JSON.parse(rotateDataRes);
  580. user.camera.position = rotateData.cameraPosition;
  581. user.camera.angle = rotateData.cameraAngle;
  582. const reply = JSON.parse(JSON.stringify(this.reply));
  583. reply.traceIds.push(traceId);
  584. reply['newUserStates'][0].userId = userId;
  585. reply['newUserStates'][0].playerState.player.position = breakPoint.position;
  586. reply['newUserStates'][0].playerState.player.angle = playerAngle;
  587. reply['newUserStates'][0].playerState.camera.position =
  588. rotateData.cameraPosition;
  589. reply['newUserStates'][0].playerState.camera.angle = rotateData.cameraAngle;
  590. reply['newUserStates'][0].playerState.cameraCenter = breakPoint.position;
  591. reply['actionResponses'][0].traceId = traceId;
  592. reply.mediaSrc =
  593. '/' +
  594. appId +
  595. '/' +
  596. breakPointId +
  597. '/' +
  598. rotateData.directory +
  599. '/' +
  600. rotateData.fileName +
  601. '?m=' +
  602. new Date().getTime();
  603. reply.startBreakPointId = startBreakPointId;
  604. reply.endBreakPointId = breakPointId;
  605. reply['newUserStates'][0].renderInfo.isMoving = 0;
  606. return reply;
  607. }
  608. // 顺序旋转请求
  609. seqExeJoystick = seqExeAsyncFn(this.joystick);
  610. async joystick(actionRequest) {
  611. try {
  612. const userId = actionRequest['user_id'];
  613. const traceId = actionRequest['trace_id'];
  614. const dir_action = actionRequest['dir_action'];
  615. const actionType = actionRequest['action_type'];
  616. const user = this.users[userId];
  617. const breakPointId = user.breakPointId;
  618. const appId = user.appId;
  619. moveInterval = 1;
  620. const replys = [];
  621. const step = 0.5;
  622. const closestDis = 50; //小于这个距离就跳到邻居呼吸点
  623. const distance = step * dir_action.speed_level;
  624. let angle = null;
  625. let move_angle = dir_action.move_angle + user.camera.angle.yaw;
  626. move_angle = move_angle % 360;
  627. //TODO 临时增加断言
  628. const playerPosition: Point = { x: 0, y: 0, z: 0 };
  629. playerPosition.x =
  630. user.player.position.x +
  631. distance * Math.cos((move_angle / 360) * 2 * Math.PI);
  632. playerPosition.y =
  633. user.player.position.y +
  634. distance * Math.sin((move_angle / 360) * 2 * Math.PI);
  635. //可行走的区域
  636. const area = [
  637. { x: -600, y: -300 },
  638. { x: -600, y: 250 },
  639. { x: 550, y: 250 },
  640. { x: 550, y: -300 },
  641. ];
  642. //找到邻居点,判断user.player.position与邻居点的距离,如果距离小于closestDis,就要更新camera的position
  643. const breakPoint = this.breakPointInfo[breakPointId];
  644. const surroundPointIds = breakPoint.contact;
  645. //const neighAngles = [];
  646. const traceIds = [];
  647. user.player.angle.yaw = move_angle;
  648. traceIds.push(traceId);
  649. this.reply.traceIds = traceIds;
  650. this.reply['newUserStates'][0].userId = userId;
  651. this.reply['actionResponses'][0].traceId = traceId;
  652. this.reply['newUserStates'][0].playerState.player.angle.yaw = move_angle;
  653. this.reply['newUserStates'][0].playerState.camera.angle = JSON.parse(
  654. JSON.stringify(user.camera.angle),
  655. );
  656. this.reply['newUserStates'][0].playerState.camera.position = JSON.parse(
  657. JSON.stringify(user.camera.position),
  658. );
  659. this.reply['newUserStates'][0].playerState.cameraCenter = JSON.parse(
  660. JSON.stringify(user.player.position),
  661. );
  662. this.reply['newUserStates'][0].renderInfo.isMoving = 1;
  663. this.reply['actionResponses'][0].traceId = traceId;
  664. this.reply.mediaSrc = null;
  665. //人在哪个角度
  666. const _angle = this.getAngle(
  667. breakPoint.position,
  668. { x: breakPoint.position.x + 1, y: breakPoint.position.y },
  669. playerPosition,
  670. );
  671. //需要找到最近的邻居点和最接近角度的邻居点
  672. let closestNeighorId = null;
  673. let closestDisNeighor = null;
  674. //超出行走区域的时候用得上
  675. let minAngleNeighorId = null;
  676. let closestAngleNeighor = null;
  677. for (let i = 0; i < surroundPointIds.length; ++i) {
  678. const neighPoint = this.breakPointInfo[surroundPointIds[i]];
  679. neighPoint.breakPointId = surroundPointIds[i];
  680. angle = this.getAngle(
  681. breakPoint.position,
  682. { x: breakPoint.position.x + 1, y: breakPoint.position.y },
  683. neighPoint.position,
  684. );
  685. const distance = this.getDistance(playerPosition, neighPoint.position);
  686. if (closestNeighorId == null || closestDisNeighor > distance) {
  687. closestNeighorId = surroundPointIds[i];
  688. closestDisNeighor = distance;
  689. }
  690. if (
  691. this.getOffsetAngle(angle, _angle) < 90 &&
  692. (minAngleNeighorId == null ||
  693. this.getOffsetAngle(closestAngleNeighor, _angle) >
  694. this.getOffsetAngle(angle, _angle))
  695. ) {
  696. minAngleNeighorId = surroundPointIds[i];
  697. closestAngleNeighor = angle;
  698. }
  699. }
  700. //超出范围了
  701. if (this.getDistance(playerPosition, breakPoint.position) > closestDis) {
  702. console.log('20220627test-joystick-moveCamera');
  703. return await this.moveCamera(
  704. breakPointId,
  705. closestNeighorId,
  706. appId,
  707. userId,
  708. traceId,
  709. actionType,
  710. );
  711. } else {
  712. const inside = this.isPointInPoly(playerPosition, area);
  713. //超出区域
  714. if (!inside) {
  715. if (minAngleNeighorId != null) {
  716. console.log('20220627test-joystick-moveDirect');
  717. return await this.moveDirect(
  718. playerPosition,
  719. closestDis,
  720. breakPointId,
  721. minAngleNeighorId,
  722. appId,
  723. userId,
  724. traceId,
  725. actionType,
  726. );
  727. } else {
  728. return null;
  729. }
  730. }
  731. user.player.position = JSON.parse(JSON.stringify(playerPosition));
  732. this.reply['newUserStates'][0].playerState.player.position = JSON.parse(
  733. JSON.stringify(user.player.position),
  734. );
  735. this.reply.actionResponses[0].actionType = actionType;
  736. const cameraInfo = this.getCameraInfo();
  737. if (cameraInfo != null) {
  738. console.log(
  739. '20220627test-joystick-自由-合并,' + this.cameraInfos.length,
  740. );
  741. this.reply['newUserStates'][0].playerState.camera.position =
  742. cameraInfo.camera_position;
  743. this.reply['newUserStates'][0].playerState.camera.angle =
  744. cameraInfo.camera_angle;
  745. if (cameraInfo.mediaSrc) {
  746. this.reply.mediaSrc = cameraInfo.mediaSrc;
  747. this.reply.isIDR = cameraInfo.isIDR;
  748. }
  749. user.camera.position = JSON.parse(
  750. JSON.stringify(cameraInfo.camera_position),
  751. );
  752. // console.log(
  753. // 'handlejoystick-angle->:自由并过渡更新user.angle' +
  754. // cameraInfo.camera_angle.yaw,
  755. // );
  756. user.camera.angle.yaw = cameraInfo.camera_angle.yaw;
  757. this.sendingFrameForJoystick = true;
  758. } else {
  759. console.log('20220627test-joystick-自由-不合并');
  760. }
  761. return this.reply;
  762. }
  763. } catch (error) {
  764. console.log('MoveService', error);
  765. debugger;
  766. return null;
  767. }
  768. }
  769. async joystick2(actionRequest) {
  770. try {
  771. if (this.sendingFrameForJoystick) {
  772. return null;
  773. }
  774. const userId = actionRequest['user_id'];
  775. const traceId = actionRequest['trace_id'];
  776. const dir_action = actionRequest['dir_action'];
  777. const actionType = actionRequest['action_type'];
  778. const user = this.users[userId];
  779. const breakPointId = user.breakPointId;
  780. const appId = user.appId;
  781. //const step = 0.3;
  782. const step = 0.8;
  783. const closestDis = 80; //小于这个距离就跳到邻居呼吸点
  784. const distance = step * dir_action.speed_level;
  785. let angle = null;
  786. // console.log(
  787. // 'handlejoystick-angle->:dir_action.move_angle' + dir_action.move_angle,
  788. // );
  789. if (user.camera.angle.yaw < 0) {
  790. user.camera.angle.yaw = 360 + user.camera.angle.yaw;
  791. }
  792. let move_angle = dir_action.move_angle + user.camera.angle.yaw;
  793. move_angle = move_angle % 360;
  794. // console.log(
  795. // 'handlejoystick-angle->:叠加后move_angle(也是user.angle)' + move_angle,
  796. // );
  797. //TODO 临时增加断言
  798. const playerPosition: Point = { x: 0, y: 0, z: 0 };
  799. playerPosition.x =
  800. user.player.position.x +
  801. distance * Math.cos((move_angle / 360) * 2 * Math.PI);
  802. playerPosition.y =
  803. user.player.position.y +
  804. distance * Math.sin((move_angle / 360) * 2 * Math.PI);
  805. // const offsetX = playerPosition.x - user.player.position.x;
  806. // const offsetY = playerPosition.y - user.player.position.y;
  807. console.log(
  808. 'handlejoysticktesttest:actionRequest-playerPosition' +
  809. JSON.stringify(playerPosition),
  810. );
  811. //找到邻居点,判断user.player.position与邻居点的距离,如果距离小于closestDis,就要更新camera的position
  812. let chooseBreakPointId = null;
  813. const breakPoint = this.breakPointInfo[breakPointId];
  814. const surroundPointIds = breakPoint.contact;
  815. const traceIds = [];
  816. user.player.angle.yaw = move_angle;
  817. traceIds.push(traceId);
  818. this.reply.traceIds = traceIds;
  819. this.reply['newUserStates'][0].userId = userId;
  820. this.reply['actionResponses'][0].traceId = traceId;
  821. this.reply['newUserStates'][0].playerState.player.angle.yaw = move_angle;
  822. this.reply['newUserStates'][0].playerState.camera.angle = JSON.parse(
  823. JSON.stringify(user.camera.angle),
  824. );
  825. this.reply['newUserStates'][0].playerState.camera.position = JSON.parse(
  826. JSON.stringify(user.camera.position),
  827. );
  828. this.reply['newUserStates'][0].playerState.cameraCenter = JSON.parse(
  829. JSON.stringify(breakPoint.position),
  830. );
  831. this.reply['newUserStates'][0].renderInfo.isMoving = 1;
  832. this.reply['actionResponses'][0].traceId = traceId;
  833. this.reply.mediaSrc = null;
  834. console.log('joystickjoystick:' + this.cameraInfos.length);
  835. if (surroundPointIds.length == 1) {
  836. //console.log('handlejoysticktesttest:actionRequest-笔直'+new Date().getTime());
  837. return await this.moveDirect(
  838. playerPosition,
  839. closestDis,
  840. breakPointId,
  841. surroundPointIds[0],
  842. appId,
  843. userId,
  844. traceId,
  845. actionType,
  846. );
  847. }
  848. let count = 0;
  849. const neighPoints = [];
  850. //人在哪个角度
  851. const _angle = this.getAngle(
  852. breakPoint.position,
  853. { x: breakPoint.position.x + 1, y: breakPoint.position.y },
  854. playerPosition,
  855. );
  856. // const offsetBreakPosition = {
  857. // x: breakPoint.position.x + offsetX,
  858. // y: breakPoint.position.y + offsetY,
  859. // };
  860. let closestNeighorId = null;
  861. let neighDis = null;
  862. let singleInfo = null;
  863. for (let i = 0; i < surroundPointIds.length; ++i) {
  864. const neighPoint = this.breakPointInfo[surroundPointIds[i]];
  865. if (closestNeighorId == null) {
  866. neighDis = this.getDistance(playerPosition, neighPoint.position);
  867. closestNeighorId = surroundPointIds[i];
  868. } else if (
  869. neighDis > this.getDistance(playerPosition, neighPoint.position)
  870. ) {
  871. neighDis = this.getDistance(playerPosition, neighPoint.position);
  872. closestNeighorId = surroundPointIds[i];
  873. }
  874. neighPoint.breakPointId = surroundPointIds[i];
  875. angle = this.getAngle(
  876. breakPoint.position,
  877. { x: breakPoint.position.x + 1, y: breakPoint.position.y },
  878. neighPoint.position,
  879. );
  880. //if(angle<45&&angle!=0){
  881. if (
  882. // Math.abs(angle - move_angle) < 45 &&
  883. // Math.abs(angle - move_angle) != 0
  884. Math.abs(angle - _angle) < 45 &&
  885. Math.abs(angle - _angle) != 0
  886. ) {
  887. neighPoint.angle = angle;
  888. neighPoints.push(neighPoint);
  889. ++count;
  890. } else if (Math.abs(angle - move_angle) == 0) {
  891. neighPoint.angle = angle;
  892. neighPoints.push(neighPoint);
  893. ++count;
  894. break;
  895. }
  896. //if (angle == 0 && Math.abs(360 - move_angle) < 45) {
  897. if (angle == 0 && Math.abs(360 - _angle) < 45) {
  898. neighPoint.angle = angle;
  899. neighPoints.push(neighPoint);
  900. ++count;
  901. }
  902. if (
  903. Math.abs(angle - move_angle) < 45 ||
  904. Math.abs(angle + 360 - move_angle) < 45
  905. ) {
  906. if (singleInfo == null) {
  907. singleInfo = {
  908. angle: angle,
  909. breakPointId: surroundPointIds[i],
  910. };
  911. }
  912. }
  913. }
  914. if (count == 2) {
  915. //人物移动
  916. user.player.position = JSON.parse(JSON.stringify(playerPosition));
  917. this.reply['newUserStates'][0].playerState.player.position = JSON.parse(
  918. JSON.stringify(playerPosition),
  919. );
  920. } else {
  921. if (singleInfo != null) {
  922. // console.log(
  923. // 'joystick校验--->' + breakPointId + '-' + singleInfo.breakPointId,
  924. // );
  925. //console.log('handlejoysticktesttest校验0角度:'+_angle+','+move_angle) //这个没办法,得换方案
  926. //console.log('handlejoysticktesttest:actionRequest-笔直'+new Date().getTime());
  927. return await this.moveDirect(
  928. playerPosition,
  929. closestDis,
  930. breakPointId,
  931. singleInfo.breakPointId,
  932. appId,
  933. userId,
  934. traceId,
  935. actionType,
  936. );
  937. } else if (count == 1) {
  938. //console.log('handlejoysticktesttest:actionRequest-笔直'+new Date().getTime());
  939. return await this.moveDirect(
  940. playerPosition,
  941. closestDis,
  942. breakPointId,
  943. neighPoints[0].breakPointId,
  944. appId,
  945. userId,
  946. traceId,
  947. actionType,
  948. );
  949. } else if (count == 0) {
  950. this.reply['newUserStates'][0].playerState.player.position =
  951. JSON.parse(JSON.stringify(user.player.position));
  952. this.reply.actionResponses[0].actionType = actionType;
  953. //console.log('handlejoysticktesttest:actionRequest-停止'+new Date().getTime());
  954. return this.reply;
  955. }
  956. }
  957. //count == 2
  958. //超出范围了
  959. console.log(
  960. 'handlejoysticktesttest:距离:' +
  961. JSON.stringify(playerPosition) +
  962. '->' +
  963. JSON.stringify(breakPoint.position) +
  964. ':' +
  965. this.getDistance(playerPosition, breakPoint.position),
  966. );
  967. if (this.getDistance(playerPosition, breakPoint.position) > closestDis) {
  968. // let offsetAngle1 = Math.abs(_angle - neighPoints[0].angle);
  969. // if (neighPoints[0].angle == 0) {
  970. // offsetAngle1 = Math.min(offsetAngle1, Math.abs(_angle - 360));
  971. // }
  972. // let offsetAngle2 = Math.abs(_angle - neighPoints[1].angle);
  973. // if (neighPoints[1].angle == 0) {
  974. // offsetAngle2 = Math.min(offsetAngle2, Math.abs(_angle - 360));
  975. // }
  976. // if (offsetAngle1 > offsetAngle2) {
  977. // chooseBreakPointId = neighPoints[1].breakPointId;
  978. // } else {
  979. // chooseBreakPointId = neighPoints[0].breakPointId;
  980. // }
  981. if (closestNeighorId == null) {
  982. debugger;
  983. }
  984. chooseBreakPointId = closestNeighorId;
  985. return await this.moveCamera(
  986. breakPointId,
  987. chooseBreakPointId,
  988. appId,
  989. userId,
  990. traceId,
  991. actionType,
  992. );
  993. } else {
  994. this.reply['newUserStates'][0].playerState.player.position = JSON.parse(
  995. JSON.stringify(user.player.position),
  996. );
  997. this.reply.actionResponses[0].actionType = actionType;
  998. const cameraInfo = this.getCameraInfo();
  999. if (cameraInfo != null) {
  1000. console.log('joystick自由--->合并');
  1001. this.reply['newUserStates'][0].playerState.camera.position =
  1002. cameraInfo.camera_position;
  1003. this.reply['newUserStates'][0].playerState.camera.angle =
  1004. cameraInfo.camera_angle;
  1005. if (cameraInfo.mediaSrc) {
  1006. this.reply.mediaSrc = cameraInfo.mediaSrc;
  1007. this.reply.isIDR = cameraInfo.isIDR;
  1008. }
  1009. user.camera.position = JSON.parse(
  1010. JSON.stringify(cameraInfo.camera_position),
  1011. );
  1012. // console.log(
  1013. // 'handlejoystick-angle->:自由并过渡更新user.angle' +
  1014. // cameraInfo.camera_angle.yaw,
  1015. // );
  1016. user.camera.angle.yaw = cameraInfo.camera_angle.yaw;
  1017. this.sendingFrameForJoystick = true;
  1018. } else {
  1019. //console.log('joystick自由--->不合并');
  1020. }
  1021. //console.log('handlejoysticktesttest:actionRequest-自由'+new Date().getTime());
  1022. console.log(
  1023. 'handlejoysticktesttest:自由:' +
  1024. this.reply.mediaSrc +
  1025. '->' +
  1026. JSON.stringify(user.player.position),
  1027. );
  1028. return this.reply;
  1029. }
  1030. } catch (error) {
  1031. console.log('MoveService', error);
  1032. return null;
  1033. }
  1034. }
  1035. //沿着最合适的neighBreakPointId走
  1036. async moveDirect(
  1037. playerPosition,
  1038. closestDis,
  1039. breakPointId,
  1040. neighBreakPointId,
  1041. appId,
  1042. userId,
  1043. traceId,
  1044. actionType,
  1045. ) {
  1046. const breakPoint = this.breakPointInfo[breakPointId];
  1047. const user = this.users[userId];
  1048. const player_Position = this.getTarget(
  1049. playerPosition,
  1050. breakPoint.position,
  1051. this.breakPointInfo[neighBreakPointId].position,
  1052. );
  1053. // console.log('handlejoysticktesttest校验1线段:'+breakPointId+','+neighBreakPointId)
  1054. // console.log('handlejoysticktesttest校验2计算后的:'+JSON.stringify(player_Position))
  1055. // console.log('handlejoysticktesttest校验3人物变化前:'+JSON.stringify(user.player.position))
  1056. // console.log('handlejoysticktesttest校验4人物本应该的坐标:'+JSON.stringify(playerPosition))
  1057. if (player_Position != null) {
  1058. playerPosition.x = player_Position.x;
  1059. playerPosition.y = player_Position.y;
  1060. user.player.position = JSON.parse(JSON.stringify(playerPosition));
  1061. }
  1062. if (this.getDistance(playerPosition, breakPoint.position) > closestDis) {
  1063. //this.reply.moveOver = true;
  1064. return await this.moveCamera(
  1065. breakPointId,
  1066. neighBreakPointId,
  1067. appId,
  1068. userId,
  1069. traceId,
  1070. actionType,
  1071. );
  1072. } else {
  1073. this.reply['newUserStates'][0].playerState.player.position = JSON.parse(
  1074. JSON.stringify(user.player.position),
  1075. );
  1076. this.reply.actionResponses[0].actionType = actionType;
  1077. const cameraInfo = this.getCameraInfo();
  1078. if (cameraInfo != null) {
  1079. this.reply['newUserStates'][0].playerState.camera.position =
  1080. cameraInfo.camera_position;
  1081. this.reply['newUserStates'][0].playerState.camera.angle =
  1082. cameraInfo.camera_angle;
  1083. if (cameraInfo.mediaSrc) {
  1084. this.reply.mediaSrc = cameraInfo.mediaSrc;
  1085. this.reply.isIDR = cameraInfo.isIDR;
  1086. }
  1087. user.camera.position = JSON.parse(
  1088. JSON.stringify(cameraInfo.camera_position),
  1089. );
  1090. console.log(
  1091. 'handlejoystick-angle->:moveDirect更新user.angle' +
  1092. cameraInfo.camera_angle.yaw,
  1093. );
  1094. user.camera.angle.yaw = cameraInfo.camera_angle.yaw;
  1095. this.sendingFrameForJoystick = true;
  1096. }
  1097. return this.reply;
  1098. }
  1099. }
  1100. async moveCamera(
  1101. breakPointId,
  1102. chooseBreakPointId,
  1103. appId,
  1104. userId,
  1105. traceId,
  1106. actionType,
  1107. ) {
  1108. const user = this.users[userId];
  1109. this.reply['newUserStates'][0].playerState.player.position = JSON.parse(
  1110. JSON.stringify(user.player.position),
  1111. );
  1112. if (chooseBreakPointId == null) {
  1113. this.reply.actionResponses[0].actionType = actionType;
  1114. return this.reply;
  1115. }
  1116. //判断人物离该邻接点的距离是否在最小路径内,如果是,跳到这个邻接点里
  1117. //相机纠正加过渡
  1118. else {
  1119. if (chooseBreakPointId == user.breakPointId) {
  1120. return this.reply;
  1121. }
  1122. const angle = user.camera.angle.yaw % 45; //纠正需要
  1123. //通过user.camera.angle矫正相机的angle
  1124. const checkReplys = await this.modeifyCameraAngle(
  1125. angle,
  1126. userId,
  1127. traceId,
  1128. actionType,
  1129. );
  1130. for (let i = 0; i < checkReplys.length; ++i) {
  1131. let isIDR = false;
  1132. if (i == 0 || i == checkReplys.length - 1) {
  1133. isIDR = true;
  1134. }
  1135. this.addCameraInfo(
  1136. checkReplys[i]['newUserStates'][0].playerState.camera.position,
  1137. checkReplys[i]['newUserStates'][0].playerState.camera.angle,
  1138. checkReplys[i].mediaSrc,
  1139. isIDR,
  1140. );
  1141. }
  1142. //replys.push(checkReplys);
  1143. //读redis里的数据,按照frame_index的大小排序
  1144. /*
  1145. const key =
  1146. 'moveframe:app_id:' +
  1147. appId +
  1148. ':start_break_point_id:' +
  1149. breakPointId +
  1150. ':end_break_point_id:' +
  1151. chooseBreakPointId +
  1152. ':angle:' +
  1153. Math.floor(user.camera.angle.yaw / 45);
  1154. const moveFramesRes = await this.cacheService.get(key);
  1155. const moveFrames = JSON.parse(moveFramesRes);
  1156. */
  1157. const index = this.getMoveIndex(user.camera.angle.yaw);
  1158. const moveFrames = await this.getMoveFrames(
  1159. appId,
  1160. breakPointId,
  1161. chooseBreakPointId,
  1162. index,
  1163. );
  1164. this.setCameraInfo(appId, moveFrames, breakPointId, chooseBreakPointId);
  1165. user.breakPointId = chooseBreakPointId;
  1166. const cameraInfo = this.getCameraInfo();
  1167. if (cameraInfo != null) {
  1168. this.reply['newUserStates'][0].playerState.camera.position =
  1169. cameraInfo.camera_position;
  1170. this.reply['newUserStates'][0].playerState.camera.angle =
  1171. cameraInfo.camera_angle;
  1172. user.camera.position = JSON.parse(
  1173. JSON.stringify(cameraInfo.camera_position),
  1174. );
  1175. // console.log(
  1176. // 'handlejoystick-angle->:刚刚过渡更新user.angle' +
  1177. // cameraInfo.camera_angle.yaw,
  1178. // );
  1179. user.camera.angle.yaw = cameraInfo.camera_angle.yaw;
  1180. if (cameraInfo.mediaSrc) {
  1181. this.reply.mediaSrc = cameraInfo.mediaSrc;
  1182. this.reply.isIDR = cameraInfo.isIDR;
  1183. }
  1184. this.sendingFrameForJoystick = true;
  1185. }
  1186. //console.log('handlejoysticktesttest:actionRequest-镜头过渡'+new Date().getTime());
  1187. console.log(
  1188. 'handlejoysticktesttest:actionRequest-镜头过渡:从' +
  1189. breakPointId +
  1190. '到' +
  1191. chooseBreakPointId +
  1192. ',' +
  1193. JSON.stringify(user.player.position),
  1194. );
  1195. return this.reply;
  1196. }
  1197. }
  1198. setCameraInfo(appId, moveFrames, startBreakPointId, endBreakPointId) {
  1199. for (let i = 0; i < moveFrames.length; i += moveInterval) {
  1200. moveFrames[i].endBreakPointId = endBreakPointId;
  1201. moveFrames[i].mediaSrc =
  1202. '/' +
  1203. appId +
  1204. '/' +
  1205. startBreakPointId +
  1206. '/' +
  1207. moveFrames[i].file_name.substring(
  1208. 0,
  1209. moveFrames[i].file_name.indexOf('.'),
  1210. ) +
  1211. '/' +
  1212. moveFrames[i].file_name +
  1213. '?m=' +
  1214. new Date().getTime();
  1215. if (startBreakPointId > endBreakPointId) {
  1216. moveFrames[i].mediaSrc =
  1217. '/' +
  1218. appId +
  1219. '/' +
  1220. endBreakPointId +
  1221. '/' +
  1222. moveFrames[i].file_name.substring(
  1223. 0,
  1224. moveFrames[i].file_name.indexOf('.'),
  1225. ) +
  1226. '/' +
  1227. moveFrames[i].file_name +
  1228. '?m=' +
  1229. new Date().getTime();
  1230. }
  1231. if (i == 0 || i == moveFrames.length - 1) {
  1232. moveFrames[i].isIDR = true;
  1233. } else {
  1234. moveFrames[i].isIDR = false;
  1235. }
  1236. this.cameraInfos.push(moveFrames[i]);
  1237. }
  1238. }
  1239. addCameraInfo(cameraPosition, cameraAngle, mediaSrc, isIDR) {
  1240. this.cameraInfos.push({
  1241. camera_position: cameraPosition,
  1242. camera_angle: cameraAngle,
  1243. mediaSrc: mediaSrc,
  1244. isIDR: isIDR,
  1245. });
  1246. }
  1247. getCameraInfo() {
  1248. if (this.cameraInfos.length > 0) {
  1249. const item = this.cameraInfos.shift();
  1250. return item;
  1251. } else {
  1252. return null;
  1253. }
  1254. }
  1255. complementFrame(userId) {
  1256. if (this.cameraInfos.length > 0) {
  1257. console.log('20220627test-执行complementFrame,' + new Date().getTime());
  1258. const user = this.users[userId];
  1259. const cameraInfo = this.cameraInfos.shift();
  1260. this.reply.traceIds = [];
  1261. this.reply['newUserStates'][0].userId = userId;
  1262. this.reply['actionResponses'][0].traceId = null;
  1263. this.reply['newUserStates'][0].playerState.player.angle.yaw =
  1264. user.player.angle.yaw;
  1265. this.reply['newUserStates'][0].playerState.player.position = JSON.parse(
  1266. JSON.stringify(user.player.position),
  1267. );
  1268. this.reply['newUserStates'][0].playerState.camera.angle = JSON.parse(
  1269. JSON.stringify(cameraInfo.camera_angle),
  1270. );
  1271. this.reply['newUserStates'][0].playerState.camera.position = JSON.parse(
  1272. JSON.stringify(cameraInfo.camera_position),
  1273. );
  1274. this.reply['newUserStates'][0].playerState.cameraCenter = JSON.parse(
  1275. JSON.stringify(user.player.position),
  1276. );
  1277. this.reply['newUserStates'][0].renderInfo.isMoving = 0;
  1278. this.reply['actionResponses'][0].traceId = null;
  1279. if (cameraInfo.mediaSrc) {
  1280. this.reply.mediaSrc = cameraInfo.mediaSrc;
  1281. this.reply.isIDR = cameraInfo.isIDR;
  1282. }
  1283. user.camera.position = JSON.parse(
  1284. JSON.stringify(cameraInfo.camera_position),
  1285. );
  1286. user.camera.angle = JSON.parse(JSON.stringify(cameraInfo.camera_angle));
  1287. this.sendingFrameForJoystick = true;
  1288. return this.reply;
  1289. } else if (this.cameraInfos.length == 0) {
  1290. if (this.reply['newUserStates'][0].renderInfo.isMoving == 1) {
  1291. //console.log('handlejoysticktesttest:complementFrame-2 停止1'+new Date().getTime())
  1292. return this.stopJoystick(userId);
  1293. } else {
  1294. //console.log('handlejoysticktesttest:complementFrame-2 停止2'+new Date().getTime())
  1295. return null;
  1296. }
  1297. }
  1298. }
  1299. stopJoystick(userId) {
  1300. console.log('20220627test:stopJoystick');
  1301. this.reply.traceIds = [];
  1302. const user = this.users[userId];
  1303. this.reply['newUserStates'][0].userId = userId;
  1304. this.reply['actionResponses'][0].traceId = null;
  1305. this.reply['newUserStates'][0].playerState.player.angle.yaw =
  1306. user.player.angle.yaw;
  1307. this.reply['newUserStates'][0].playerState.player.position = JSON.parse(
  1308. JSON.stringify(user.player.position),
  1309. );
  1310. this.reply['newUserStates'][0].playerState.camera.angle = JSON.parse(
  1311. JSON.stringify(user.camera.angle),
  1312. );
  1313. this.reply['newUserStates'][0].playerState.camera.position = JSON.parse(
  1314. JSON.stringify(user.camera.position),
  1315. );
  1316. this.reply['newUserStates'][0].playerState.cameraCenter = JSON.parse(
  1317. JSON.stringify(user.player.position),
  1318. );
  1319. this.reply['newUserStates'][0].renderInfo.isMoving = 0;
  1320. this.reply['actionResponses'][0].traceId = null;
  1321. this.reply.mediaSrc = null;
  1322. return this.reply;
  1323. }
  1324. async modeifyCameraAngle(angle, userId, traceId, actionType) {
  1325. const checkReplys = [];
  1326. if (angle > 22) {
  1327. angle = 45 - angle;
  1328. for (let i = 0; i < angle; ++i) {
  1329. // console.warn('矫正一次:' + i);
  1330. const reply = await this.rotateService.rotateForAngle(userId, 1);
  1331. // console.warn(
  1332. // '矫正:' + reply.newUserStates[0].playerState.camera.angle.yaw,
  1333. // );
  1334. reply.traceIds = [];
  1335. reply.traceIds.push(traceId);
  1336. const actionResponse = this.rotateService.createActionResponse(
  1337. actionType,
  1338. traceId,
  1339. );
  1340. reply.actionResponses = [];
  1341. reply.actionResponses.push(actionResponse);
  1342. checkReplys.push(reply);
  1343. }
  1344. } else if (angle != 0) {
  1345. for (let i = angle; i > -1; --i) {
  1346. // console.warn('矫正一次:' + i);
  1347. const reply = await this.rotateService.rotateForAngle(userId, -1);
  1348. // console.warn(
  1349. // '矫正:' + reply.newUserStates[0].playerState.camera.angle.yaw,
  1350. // );
  1351. reply.traceIds = [];
  1352. reply.traceIds.push(traceId);
  1353. const actionResponse = this.rotateService.createActionResponse(
  1354. actionType,
  1355. traceId,
  1356. );
  1357. reply.actionResponses = [];
  1358. reply.actionResponses.push(actionResponse);
  1359. checkReplys.push(reply);
  1360. }
  1361. }
  1362. return checkReplys;
  1363. }
  1364. getTarget(position, position1, position2) {
  1365. const line = this.createLine1(position1, position2);
  1366. const join = this.getJoinLinePoint(position, line);
  1367. // if (this.isContainForSegment(join, position1, position2)) {
  1368. // return join;
  1369. // } else {
  1370. // return null;
  1371. // }
  1372. return join;
  1373. }
  1374. getDistance(position1, position2) {
  1375. return Math.sqrt(
  1376. (position1.x - position2.x) * (position1.x - position2.x) +
  1377. (position1.y - position2.y) * (position1.y - position2.y),
  1378. );
  1379. }
  1380. isContainForSegment(point, startPoint, endPoint) {
  1381. const minDis = 0.1;
  1382. const dis1 =
  1383. this.getDistance(startPoint, point) + this.getDistance(endPoint, point);
  1384. const dis2 = this.getDistance(startPoint, endPoint);
  1385. if (Math.abs(dis1 - dis2) < minDis) {
  1386. return true;
  1387. } else {
  1388. return false;
  1389. }
  1390. }
  1391. //两条线的交点
  1392. getIntersectionPoint(parameter1, parameter2) {
  1393. if (this.isParallel(parameter1, parameter2)) {
  1394. return null;
  1395. }
  1396. if (
  1397. typeof parameter1.a == 'undefined' &&
  1398. typeof parameter2.a != 'undefined'
  1399. ) {
  1400. if (parameter1.x) {
  1401. return {
  1402. x: parameter1.x,
  1403. y: parameter2.a * parameter1.x + parameter2.b,
  1404. };
  1405. } else if (parameter1.y) {
  1406. return {
  1407. x: (parameter1.y - parameter2.b) / parameter2.a,
  1408. y: parameter1.y,
  1409. };
  1410. }
  1411. } else if (
  1412. typeof parameter2.a == 'undefined' &&
  1413. typeof parameter1.a != 'undefined'
  1414. ) {
  1415. if (parameter2.x) {
  1416. return {
  1417. x: parameter2.x,
  1418. y: parameter1.a * parameter2.x + parameter1.b,
  1419. };
  1420. } else if (parameter2.y) {
  1421. return {
  1422. x: (parameter2.y - parameter1.b) / parameter1.a,
  1423. y: parameter2.y,
  1424. };
  1425. }
  1426. } else if (
  1427. typeof parameter2.a == 'undefined' &&
  1428. typeof parameter1.a == 'undefined'
  1429. ) {
  1430. if (parameter1.hasOwnProperty('x') && parameter2.hasOwnProperty('y')) {
  1431. return { x: parameter1.x, y: parameter2.y };
  1432. } else if (
  1433. parameter1.hasOwnProperty('y') &&
  1434. parameter2.hasOwnProperty('x')
  1435. ) {
  1436. return { x: parameter2.x, y: parameter1.y };
  1437. } else {
  1438. return null;
  1439. }
  1440. }
  1441. if (parameter1.a == parameter2.a) {
  1442. return null;
  1443. }
  1444. const joinpointx =
  1445. (parameter2.b - parameter1.b) / (parameter1.a - parameter2.a);
  1446. const joinpointy =
  1447. (parameter1.a * parameter2.b - parameter2.a * parameter1.b) /
  1448. (parameter1.a - parameter2.a);
  1449. const point = { x: joinpointx, y: joinpointy };
  1450. return point;
  1451. }
  1452. // 垂直线
  1453. getVerticalLine(line, point) {
  1454. if (typeof line.a === 'undefined') {
  1455. if (line.hasOwnProperty('x')) {
  1456. return { y: point.y };
  1457. } else if (line.hasOwnProperty('y')) {
  1458. return { x: point.x };
  1459. } else {
  1460. return null;
  1461. }
  1462. } else if (line.a == 0) {
  1463. return { x: point.x };
  1464. } else {
  1465. const tl = {} as any as VerticalLineType;
  1466. tl.a = -1 / line.a;
  1467. const result = this.createLine2(tl, point);
  1468. return result;
  1469. }
  1470. }
  1471. // 经过point且与line垂直的直线,该直线与line的交点
  1472. getJoinLinePoint(point, line) {
  1473. const verticalLine = this.getVerticalLine(line, point);
  1474. const join = this.getIntersectionPoint(line, verticalLine);
  1475. return join;
  1476. }
  1477. // 与lineA平行并且point在线上
  1478. createLine2(lineA, point) {
  1479. const parameter = {} as any as {
  1480. x: number;
  1481. y: number;
  1482. a: number;
  1483. b: number;
  1484. };
  1485. if (typeof lineA.a === 'undefined') {
  1486. if (typeof lineA.x !== 'undefined') {
  1487. parameter.x = point.x;
  1488. } else if (typeof lineA.y !== 'undefined') {
  1489. parameter.y = point.y;
  1490. }
  1491. } else {
  1492. parameter.a = lineA.a;
  1493. parameter.b = point.y - point.x * lineA.a;
  1494. }
  1495. return parameter;
  1496. }
  1497. createLine1(point1, point2) {
  1498. if (point1.x == point2.x && point1.y == point2.y) {
  1499. return null;
  1500. } else if (this.getFixed(Math.abs(point1.x - point2.x)) == 0) {
  1501. return { x: point1.x };
  1502. } else if (this.getFixed(Math.abs(point1.y - point2.y)) == 0) {
  1503. return { y: point1.y };
  1504. }
  1505. const parametera = (point1.y - point2.y) / (point1.x - point2.x);
  1506. const parameterb =
  1507. (point1.x * point2.y - point2.x * point1.y) / (point1.x - point2.x);
  1508. if (this.getFixed(parametera) == 0) {
  1509. return { y: this.getFixed(parameterb) };
  1510. }
  1511. const parameter = {
  1512. a: this.getFixed(parametera),
  1513. b: this.getFixed(parameterb),
  1514. };
  1515. return parameter;
  1516. }
  1517. //返回true表示平行
  1518. isParallel(line1, line2) {
  1519. if (typeof line1.a == 'undefined' && typeof line2.a == 'undefined') {
  1520. if (line1.hasOwnProperty('x') && line2.hasOwnProperty('x')) {
  1521. return true;
  1522. } else if (line1.hasOwnProperty('y') && line2.hasOwnProperty('y')) {
  1523. return true;
  1524. } else {
  1525. return false;
  1526. }
  1527. } else if (typeof line1.a == 'undefined' || typeof line2.a == 'undefined') {
  1528. return false;
  1529. } else if (this.getFixed(line1.a) == this.getFixed(line2.a)) {
  1530. return true;
  1531. } else {
  1532. return false;
  1533. }
  1534. }
  1535. getFixed(num) {
  1536. const decimal = 2;
  1537. return parseFloat(num.toFixed(decimal));
  1538. }
  1539. isPointInPoly(position, points) {
  1540. const x = position.x;
  1541. const y = position.y;
  1542. let inside = false;
  1543. for (let i = 0, j = points.length - 1; i < points.length; j = i++) {
  1544. const pt1 = points[i];
  1545. const pt2 = points[j];
  1546. const xi = pt1.x;
  1547. const yi = pt1.y;
  1548. const xj = pt2.x;
  1549. const yj = pt2.y;
  1550. const intersect =
  1551. yi > y != yj > y && x < ((xj - xi) * (y - yi)) / (yj - yi) + xi;
  1552. if (intersect) inside = !inside;
  1553. }
  1554. return inside;
  1555. }
  1556. getMoveIndex(angle) {
  1557. if (angle < 0) {
  1558. angle = 360 + angle;
  1559. }
  1560. for (let i = 0; i < 8; ++i) {
  1561. if (angle < 45 * i + 45 / 2) {
  1562. return i;
  1563. }
  1564. }
  1565. //超过了337
  1566. return 0;
  1567. }
  1568. getOffsetAngle(angle1, angle2) {
  1569. if (angle1 < 0) {
  1570. angle1 = 360 + angle1;
  1571. //return Math.min(Math.abs(angle1-angle2),Math.abs());
  1572. }
  1573. if (angle2 < 0) {
  1574. angle2 = 360 + angle2;
  1575. }
  1576. return Math.min(Math.abs(angle1 - angle2), 360 - Math.abs(angle1 - angle2));
  1577. }
  1578. }