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. let _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. let distance = this.getDistance(playerPosition,neighPoint.position);
  686. if(closestNeighorId == null || closestDisNeighor>distance){
  687. closestNeighorId = surroundPointIds[i]
  688. closestDisNeighor = distance;
  689. }
  690. if((this.getOffsetAngle(angle , _angle)<90)&&(minAngleNeighorId == null || this.getOffsetAngle(closestAngleNeighor , _angle)>this.getOffsetAngle(angle , _angle))){
  691. minAngleNeighorId = surroundPointIds[i]
  692. closestAngleNeighor = angle;
  693. }
  694. }
  695. //超出范围了
  696. if (this.getDistance(playerPosition, breakPoint.position) > closestDis) {
  697. console.log('20220627test-joystick-moveCamera')
  698. return await this.moveCamera(
  699. breakPointId,
  700. closestNeighorId,
  701. appId,
  702. userId,
  703. traceId,
  704. actionType,
  705. );
  706. }
  707. else {
  708. let inside = this.isPointInPoly(playerPosition, area);
  709. //超出区域
  710. if(!inside){
  711. if(minAngleNeighorId != null){
  712. console.log('20220627test-joystick-moveDirect')
  713. return await this.moveDirect(
  714. playerPosition,
  715. closestDis,
  716. breakPointId,
  717. minAngleNeighorId,
  718. appId,
  719. userId,
  720. traceId,
  721. actionType,
  722. );
  723. }
  724. else{
  725. return null;
  726. }
  727. }
  728. user.player.position = JSON.parse(JSON.stringify(playerPosition));
  729. this.reply['newUserStates'][0].playerState.player.position = JSON.parse(
  730. JSON.stringify(user.player.position),
  731. );
  732. this.reply.actionResponses[0].actionType = actionType;
  733. const cameraInfo = this.getCameraInfo();
  734. if (cameraInfo != null) {
  735. console.log('20220627test-joystick-自由-合并,'+this.cameraInfos.length)
  736. this.reply['newUserStates'][0].playerState.camera.position =
  737. cameraInfo.camera_position;
  738. this.reply['newUserStates'][0].playerState.camera.angle =
  739. cameraInfo.camera_angle;
  740. if (cameraInfo.mediaSrc) {
  741. this.reply.mediaSrc = cameraInfo.mediaSrc;
  742. this.reply.isIDR = cameraInfo.isIDR;
  743. }
  744. user.camera.position = JSON.parse(
  745. JSON.stringify(cameraInfo.camera_position),
  746. );
  747. // console.log(
  748. // 'handlejoystick-angle->:自由并过渡更新user.angle' +
  749. // cameraInfo.camera_angle.yaw,
  750. // );
  751. user.camera.angle.yaw = cameraInfo.camera_angle.yaw;
  752. this.sendingFrameForJoystick = true;
  753. }
  754. else{
  755. console.log('20220627test-joystick-自由-不合并')
  756. }
  757. return this.reply;
  758. }
  759. } catch (error) {
  760. console.log('MoveService', error);
  761. debugger;
  762. return null;
  763. }
  764. }
  765. async joystick2(actionRequest) {
  766. try {
  767. if (this.sendingFrameForJoystick) {
  768. return null;
  769. }
  770. const userId = actionRequest['user_id'];
  771. const traceId = actionRequest['trace_id'];
  772. const dir_action = actionRequest['dir_action'];
  773. const actionType = actionRequest['action_type'];
  774. const user = this.users[userId];
  775. const breakPointId = user.breakPointId;
  776. const appId = user.appId;
  777. //const step = 0.3;
  778. const step = 0.8;
  779. const closestDis = 80; //小于这个距离就跳到邻居呼吸点
  780. const distance = step * dir_action.speed_level;
  781. let angle = null;
  782. // console.log(
  783. // 'handlejoystick-angle->:dir_action.move_angle' + dir_action.move_angle,
  784. // );
  785. if (user.camera.angle.yaw < 0) {
  786. user.camera.angle.yaw = 360 + user.camera.angle.yaw;
  787. }
  788. let move_angle = dir_action.move_angle + user.camera.angle.yaw;
  789. move_angle = move_angle % 360;
  790. // console.log(
  791. // 'handlejoystick-angle->:叠加后move_angle(也是user.angle)' + move_angle,
  792. // );
  793. //TODO 临时增加断言
  794. const playerPosition: Point = { x: 0, y: 0, z: 0 };
  795. playerPosition.x =
  796. user.player.position.x +
  797. distance * Math.cos((move_angle / 360) * 2 * Math.PI);
  798. playerPosition.y =
  799. user.player.position.y +
  800. distance * Math.sin((move_angle / 360) * 2 * Math.PI);
  801. // const offsetX = playerPosition.x - user.player.position.x;
  802. // const offsetY = playerPosition.y - user.player.position.y;
  803. console.log(
  804. 'handlejoysticktesttest:actionRequest-playerPosition' +
  805. JSON.stringify(playerPosition),
  806. );
  807. //找到邻居点,判断user.player.position与邻居点的距离,如果距离小于closestDis,就要更新camera的position
  808. let chooseBreakPointId = null;
  809. const breakPoint = this.breakPointInfo[breakPointId];
  810. const surroundPointIds = breakPoint.contact;
  811. const traceIds = [];
  812. user.player.angle.yaw = move_angle;
  813. traceIds.push(traceId);
  814. this.reply.traceIds = traceIds;
  815. this.reply['newUserStates'][0].userId = userId;
  816. this.reply['actionResponses'][0].traceId = traceId;
  817. this.reply['newUserStates'][0].playerState.player.angle.yaw = move_angle;
  818. this.reply['newUserStates'][0].playerState.camera.angle = JSON.parse(
  819. JSON.stringify(user.camera.angle),
  820. );
  821. this.reply['newUserStates'][0].playerState.camera.position = JSON.parse(
  822. JSON.stringify(user.camera.position),
  823. );
  824. this.reply['newUserStates'][0].playerState.cameraCenter = JSON.parse(
  825. JSON.stringify(breakPoint.position),
  826. );
  827. this.reply['newUserStates'][0].renderInfo.isMoving = 1;
  828. this.reply['actionResponses'][0].traceId = traceId;
  829. this.reply.mediaSrc = null;
  830. console.log('joystickjoystick:' + this.cameraInfos.length);
  831. if (surroundPointIds.length == 1) {
  832. //console.log('handlejoysticktesttest:actionRequest-笔直'+new Date().getTime());
  833. return await this.moveDirect(
  834. playerPosition,
  835. closestDis,
  836. breakPointId,
  837. surroundPointIds[0],
  838. appId,
  839. userId,
  840. traceId,
  841. actionType,
  842. );
  843. }
  844. let count = 0;
  845. const neighPoints = [];
  846. //人在哪个角度
  847. const _angle = this.getAngle(
  848. breakPoint.position,
  849. { x: breakPoint.position.x + 1, y: breakPoint.position.y },
  850. playerPosition,
  851. );
  852. // const offsetBreakPosition = {
  853. // x: breakPoint.position.x + offsetX,
  854. // y: breakPoint.position.y + offsetY,
  855. // };
  856. let closestNeighorId = null;
  857. let neighDis = null;
  858. let singleInfo = null;
  859. for (let i = 0; i < surroundPointIds.length; ++i) {
  860. const neighPoint = this.breakPointInfo[surroundPointIds[i]];
  861. if (closestNeighorId == null) {
  862. neighDis = this.getDistance(playerPosition, neighPoint.position);
  863. closestNeighorId = surroundPointIds[i];
  864. } else if (
  865. neighDis > this.getDistance(playerPosition, neighPoint.position)
  866. ) {
  867. neighDis = this.getDistance(playerPosition, neighPoint.position);
  868. closestNeighorId = surroundPointIds[i];
  869. }
  870. neighPoint.breakPointId = surroundPointIds[i];
  871. angle = this.getAngle(
  872. breakPoint.position,
  873. { x: breakPoint.position.x + 1, y: breakPoint.position.y },
  874. neighPoint.position,
  875. );
  876. //if(angle<45&&angle!=0){
  877. if (
  878. // Math.abs(angle - move_angle) < 45 &&
  879. // Math.abs(angle - move_angle) != 0
  880. Math.abs(angle - _angle) < 45 &&
  881. Math.abs(angle - _angle) != 0
  882. ) {
  883. neighPoint.angle = angle;
  884. neighPoints.push(neighPoint);
  885. ++count;
  886. } else if (Math.abs(angle - move_angle) == 0) {
  887. neighPoint.angle = angle;
  888. neighPoints.push(neighPoint);
  889. ++count;
  890. break;
  891. }
  892. //if (angle == 0 && Math.abs(360 - move_angle) < 45) {
  893. if (angle == 0 && Math.abs(360 - _angle) < 45) {
  894. neighPoint.angle = angle;
  895. neighPoints.push(neighPoint);
  896. ++count;
  897. }
  898. if (
  899. Math.abs(angle - move_angle) < 45 ||
  900. Math.abs(angle + 360 - move_angle) < 45
  901. ) {
  902. if (singleInfo == null) {
  903. singleInfo = {
  904. angle: angle,
  905. breakPointId: surroundPointIds[i],
  906. };
  907. }
  908. }
  909. }
  910. if (count == 2) {
  911. //人物移动
  912. user.player.position = JSON.parse(JSON.stringify(playerPosition));
  913. this.reply['newUserStates'][0].playerState.player.position = JSON.parse(
  914. JSON.stringify(playerPosition),
  915. );
  916. } else {
  917. if (singleInfo != null) {
  918. // console.log(
  919. // 'joystick校验--->' + breakPointId + '-' + singleInfo.breakPointId,
  920. // );
  921. //console.log('handlejoysticktesttest校验0角度:'+_angle+','+move_angle) //这个没办法,得换方案
  922. //console.log('handlejoysticktesttest:actionRequest-笔直'+new Date().getTime());
  923. return await this.moveDirect(
  924. playerPosition,
  925. closestDis,
  926. breakPointId,
  927. singleInfo.breakPointId,
  928. appId,
  929. userId,
  930. traceId,
  931. actionType,
  932. );
  933. } else if (count == 1) {
  934. //console.log('handlejoysticktesttest:actionRequest-笔直'+new Date().getTime());
  935. return await this.moveDirect(
  936. playerPosition,
  937. closestDis,
  938. breakPointId,
  939. neighPoints[0].breakPointId,
  940. appId,
  941. userId,
  942. traceId,
  943. actionType,
  944. );
  945. } else if (count == 0) {
  946. this.reply['newUserStates'][0].playerState.player.position =
  947. JSON.parse(JSON.stringify(user.player.position));
  948. this.reply.actionResponses[0].actionType = actionType;
  949. //console.log('handlejoysticktesttest:actionRequest-停止'+new Date().getTime());
  950. return this.reply;
  951. }
  952. }
  953. //count == 2
  954. //超出范围了
  955. console.log(
  956. 'handlejoysticktesttest:距离:' +
  957. JSON.stringify(playerPosition) +
  958. '->' +
  959. JSON.stringify(breakPoint.position) +
  960. ':' +
  961. this.getDistance(playerPosition, breakPoint.position),
  962. );
  963. if (this.getDistance(playerPosition, breakPoint.position) > closestDis) {
  964. // let offsetAngle1 = Math.abs(_angle - neighPoints[0].angle);
  965. // if (neighPoints[0].angle == 0) {
  966. // offsetAngle1 = Math.min(offsetAngle1, Math.abs(_angle - 360));
  967. // }
  968. // let offsetAngle2 = Math.abs(_angle - neighPoints[1].angle);
  969. // if (neighPoints[1].angle == 0) {
  970. // offsetAngle2 = Math.min(offsetAngle2, Math.abs(_angle - 360));
  971. // }
  972. // if (offsetAngle1 > offsetAngle2) {
  973. // chooseBreakPointId = neighPoints[1].breakPointId;
  974. // } else {
  975. // chooseBreakPointId = neighPoints[0].breakPointId;
  976. // }
  977. if (closestNeighorId == null) {
  978. debugger;
  979. }
  980. chooseBreakPointId = closestNeighorId;
  981. return await this.moveCamera(
  982. breakPointId,
  983. chooseBreakPointId,
  984. appId,
  985. userId,
  986. traceId,
  987. actionType,
  988. );
  989. } else {
  990. this.reply['newUserStates'][0].playerState.player.position = JSON.parse(
  991. JSON.stringify(user.player.position),
  992. );
  993. this.reply.actionResponses[0].actionType = actionType;
  994. const cameraInfo = this.getCameraInfo();
  995. if (cameraInfo != null) {
  996. console.log('joystick自由--->合并');
  997. this.reply['newUserStates'][0].playerState.camera.position =
  998. cameraInfo.camera_position;
  999. this.reply['newUserStates'][0].playerState.camera.angle =
  1000. cameraInfo.camera_angle;
  1001. if (cameraInfo.mediaSrc) {
  1002. this.reply.mediaSrc = cameraInfo.mediaSrc;
  1003. this.reply.isIDR = cameraInfo.isIDR;
  1004. }
  1005. user.camera.position = JSON.parse(
  1006. JSON.stringify(cameraInfo.camera_position),
  1007. );
  1008. // console.log(
  1009. // 'handlejoystick-angle->:自由并过渡更新user.angle' +
  1010. // cameraInfo.camera_angle.yaw,
  1011. // );
  1012. user.camera.angle.yaw = cameraInfo.camera_angle.yaw;
  1013. this.sendingFrameForJoystick = true;
  1014. } else {
  1015. //console.log('joystick自由--->不合并');
  1016. }
  1017. //console.log('handlejoysticktesttest:actionRequest-自由'+new Date().getTime());
  1018. console.log(
  1019. 'handlejoysticktesttest:自由:' +
  1020. this.reply.mediaSrc +
  1021. '->' +
  1022. JSON.stringify(user.player.position),
  1023. );
  1024. return this.reply;
  1025. }
  1026. } catch (error) {
  1027. console.log('MoveService', error);
  1028. return null;
  1029. }
  1030. }
  1031. //沿着最合适的neighBreakPointId走
  1032. async moveDirect(
  1033. playerPosition,
  1034. closestDis,
  1035. breakPointId,
  1036. neighBreakPointId,
  1037. appId,
  1038. userId,
  1039. traceId,
  1040. actionType,
  1041. ) {
  1042. const breakPoint = this.breakPointInfo[breakPointId];
  1043. const user = this.users[userId];
  1044. const player_Position = this.getTarget(
  1045. playerPosition,
  1046. breakPoint.position,
  1047. this.breakPointInfo[neighBreakPointId].position,
  1048. );
  1049. // console.log('handlejoysticktesttest校验1线段:'+breakPointId+','+neighBreakPointId)
  1050. // console.log('handlejoysticktesttest校验2计算后的:'+JSON.stringify(player_Position))
  1051. // console.log('handlejoysticktesttest校验3人物变化前:'+JSON.stringify(user.player.position))
  1052. // console.log('handlejoysticktesttest校验4人物本应该的坐标:'+JSON.stringify(playerPosition))
  1053. if (player_Position != null) {
  1054. playerPosition.x = player_Position.x;
  1055. playerPosition.y = player_Position.y;
  1056. user.player.position = JSON.parse(JSON.stringify(playerPosition));
  1057. }
  1058. if (this.getDistance(playerPosition, breakPoint.position) > closestDis) {
  1059. //this.reply.moveOver = true;
  1060. return await this.moveCamera(
  1061. breakPointId,
  1062. neighBreakPointId,
  1063. appId,
  1064. userId,
  1065. traceId,
  1066. actionType,
  1067. );
  1068. } else {
  1069. this.reply['newUserStates'][0].playerState.player.position = JSON.parse(
  1070. JSON.stringify(user.player.position),
  1071. );
  1072. this.reply.actionResponses[0].actionType = actionType;
  1073. const cameraInfo = this.getCameraInfo();
  1074. if (cameraInfo != null) {
  1075. this.reply['newUserStates'][0].playerState.camera.position =
  1076. cameraInfo.camera_position;
  1077. this.reply['newUserStates'][0].playerState.camera.angle =
  1078. cameraInfo.camera_angle;
  1079. if (cameraInfo.mediaSrc) {
  1080. this.reply.mediaSrc = cameraInfo.mediaSrc;
  1081. this.reply.isIDR = cameraInfo.isIDR;
  1082. }
  1083. user.camera.position = JSON.parse(
  1084. JSON.stringify(cameraInfo.camera_position),
  1085. );
  1086. console.log(
  1087. 'handlejoystick-angle->:moveDirect更新user.angle' +
  1088. cameraInfo.camera_angle.yaw,
  1089. );
  1090. user.camera.angle.yaw = cameraInfo.camera_angle.yaw;
  1091. this.sendingFrameForJoystick = true;
  1092. }
  1093. return this.reply;
  1094. }
  1095. }
  1096. async moveCamera(
  1097. breakPointId,
  1098. chooseBreakPointId,
  1099. appId,
  1100. userId,
  1101. traceId,
  1102. actionType,
  1103. ) {
  1104. const user = this.users[userId];
  1105. this.reply['newUserStates'][0].playerState.player.position = JSON.parse(
  1106. JSON.stringify(user.player.position),
  1107. );
  1108. if (chooseBreakPointId == null) {
  1109. this.reply.actionResponses[0].actionType = actionType;
  1110. return this.reply;
  1111. }
  1112. //判断人物离该邻接点的距离是否在最小路径内,如果是,跳到这个邻接点里
  1113. //相机纠正加过渡
  1114. else {
  1115. if (chooseBreakPointId == user.breakPointId) {
  1116. return this.reply;
  1117. }
  1118. const angle = user.camera.angle.yaw % 45; //纠正需要
  1119. //通过user.camera.angle矫正相机的angle
  1120. const checkReplys = await this.modeifyCameraAngle(
  1121. angle,
  1122. userId,
  1123. traceId,
  1124. actionType,
  1125. );
  1126. for (let i = 0; i < checkReplys.length; ++i) {
  1127. let isIDR = false;
  1128. if (i == 0 || i == checkReplys.length - 1) {
  1129. isIDR = true;
  1130. }
  1131. this.addCameraInfo(
  1132. checkReplys[i]['newUserStates'][0].playerState.camera.position,
  1133. checkReplys[i]['newUserStates'][0].playerState.camera.angle,
  1134. checkReplys[i].mediaSrc,
  1135. isIDR,
  1136. );
  1137. }
  1138. //replys.push(checkReplys);
  1139. //读redis里的数据,按照frame_index的大小排序
  1140. /*
  1141. const key =
  1142. 'moveframe:app_id:' +
  1143. appId +
  1144. ':start_break_point_id:' +
  1145. breakPointId +
  1146. ':end_break_point_id:' +
  1147. chooseBreakPointId +
  1148. ':angle:' +
  1149. Math.floor(user.camera.angle.yaw / 45);
  1150. const moveFramesRes = await this.cacheService.get(key);
  1151. const moveFrames = JSON.parse(moveFramesRes);
  1152. */
  1153. const index = this.getMoveIndex(user.camera.angle.yaw);
  1154. const moveFrames = await this.getMoveFrames(
  1155. appId,
  1156. breakPointId,
  1157. chooseBreakPointId,
  1158. index,
  1159. );
  1160. this.setCameraInfo(appId, moveFrames, breakPointId, chooseBreakPointId);
  1161. user.breakPointId = chooseBreakPointId;
  1162. const cameraInfo = this.getCameraInfo();
  1163. if (cameraInfo != null) {
  1164. this.reply['newUserStates'][0].playerState.camera.position =
  1165. cameraInfo.camera_position;
  1166. this.reply['newUserStates'][0].playerState.camera.angle =
  1167. cameraInfo.camera_angle;
  1168. user.camera.position = JSON.parse(
  1169. JSON.stringify(cameraInfo.camera_position),
  1170. );
  1171. // console.log(
  1172. // 'handlejoystick-angle->:刚刚过渡更新user.angle' +
  1173. // cameraInfo.camera_angle.yaw,
  1174. // );
  1175. user.camera.angle.yaw = cameraInfo.camera_angle.yaw;
  1176. if (cameraInfo.mediaSrc) {
  1177. this.reply.mediaSrc = cameraInfo.mediaSrc;
  1178. this.reply.isIDR = cameraInfo.isIDR;
  1179. }
  1180. this.sendingFrameForJoystick = true;
  1181. }
  1182. //console.log('handlejoysticktesttest:actionRequest-镜头过渡'+new Date().getTime());
  1183. console.log(
  1184. 'handlejoysticktesttest:actionRequest-镜头过渡:从' +
  1185. breakPointId +
  1186. '到' +
  1187. chooseBreakPointId +
  1188. ',' +
  1189. JSON.stringify(user.player.position),
  1190. );
  1191. return this.reply;
  1192. }
  1193. }
  1194. setCameraInfo(appId, moveFrames, startBreakPointId, endBreakPointId) {
  1195. for (let i = 0; i < moveFrames.length; i += moveInterval) {
  1196. moveFrames[i].endBreakPointId = endBreakPointId;
  1197. moveFrames[i].mediaSrc =
  1198. '/' +
  1199. appId +
  1200. '/' +
  1201. startBreakPointId +
  1202. '/' +
  1203. moveFrames[i].file_name.substring(
  1204. 0,
  1205. moveFrames[i].file_name.indexOf('.'),
  1206. ) +
  1207. '/' +
  1208. moveFrames[i].file_name +
  1209. '?m=' +
  1210. new Date().getTime();
  1211. if (startBreakPointId > endBreakPointId) {
  1212. moveFrames[i].mediaSrc =
  1213. '/' +
  1214. appId +
  1215. '/' +
  1216. endBreakPointId +
  1217. '/' +
  1218. moveFrames[i].file_name.substring(
  1219. 0,
  1220. moveFrames[i].file_name.indexOf('.'),
  1221. ) +
  1222. '/' +
  1223. moveFrames[i].file_name +
  1224. '?m=' +
  1225. new Date().getTime();
  1226. }
  1227. if (i == 0 || i == moveFrames.length - 1) {
  1228. moveFrames[i].isIDR = true;
  1229. } else {
  1230. moveFrames[i].isIDR = false;
  1231. }
  1232. this.cameraInfos.push(moveFrames[i]);
  1233. }
  1234. }
  1235. addCameraInfo(cameraPosition, cameraAngle, mediaSrc, isIDR) {
  1236. this.cameraInfos.push({
  1237. camera_position: cameraPosition,
  1238. camera_angle: cameraAngle,
  1239. mediaSrc: mediaSrc,
  1240. isIDR: isIDR,
  1241. });
  1242. }
  1243. getCameraInfo() {
  1244. if (this.cameraInfos.length > 0) {
  1245. const item = this.cameraInfos.shift();
  1246. return item;
  1247. } else {
  1248. return null;
  1249. }
  1250. }
  1251. complementFrame(userId) {
  1252. if (this.cameraInfos.length > 0) {
  1253. console.log('20220627test-执行complementFrame,'+new Date().getTime())
  1254. const user = this.users[userId];
  1255. const cameraInfo = this.cameraInfos.shift();
  1256. this.reply.traceIds = [];
  1257. this.reply['newUserStates'][0].userId = userId;
  1258. this.reply['actionResponses'][0].traceId = null;
  1259. this.reply['newUserStates'][0].playerState.player.angle.yaw =
  1260. user.player.angle.yaw;
  1261. this.reply['newUserStates'][0].playerState.player.position = JSON.parse(
  1262. JSON.stringify(user.player.position),
  1263. );
  1264. this.reply['newUserStates'][0].playerState.camera.angle = JSON.parse(
  1265. JSON.stringify(cameraInfo.camera_angle),
  1266. );
  1267. this.reply['newUserStates'][0].playerState.camera.position = JSON.parse(
  1268. JSON.stringify(cameraInfo.camera_position),
  1269. );
  1270. this.reply['newUserStates'][0].playerState.cameraCenter = JSON.parse(
  1271. JSON.stringify(user.player.position),
  1272. );
  1273. this.reply['newUserStates'][0].renderInfo.isMoving = 0;
  1274. this.reply['actionResponses'][0].traceId = null;
  1275. if (cameraInfo.mediaSrc) {
  1276. this.reply.mediaSrc = cameraInfo.mediaSrc;
  1277. this.reply.isIDR = cameraInfo.isIDR;
  1278. }
  1279. user.camera.position = JSON.parse(
  1280. JSON.stringify(cameraInfo.camera_position),
  1281. );
  1282. user.camera.angle = JSON.parse(JSON.stringify(cameraInfo.camera_angle));
  1283. this.sendingFrameForJoystick = true;
  1284. return this.reply;
  1285. } else if (this.cameraInfos.length == 0) {
  1286. if (this.reply['newUserStates'][0].renderInfo.isMoving == 1) {
  1287. //console.log('handlejoysticktesttest:complementFrame-2 停止1'+new Date().getTime())
  1288. return this.stopJoystick(userId);
  1289. } else {
  1290. //console.log('handlejoysticktesttest:complementFrame-2 停止2'+new Date().getTime())
  1291. return null;
  1292. }
  1293. }
  1294. }
  1295. stopJoystick(userId) {
  1296. console.log('20220627test:stopJoystick');
  1297. this.reply.traceIds = [];
  1298. const user = this.users[userId];
  1299. this.reply['newUserStates'][0].userId = userId;
  1300. this.reply['actionResponses'][0].traceId = null;
  1301. this.reply['newUserStates'][0].playerState.player.angle.yaw =
  1302. user.player.angle.yaw;
  1303. this.reply['newUserStates'][0].playerState.player.position = JSON.parse(
  1304. JSON.stringify(user.player.position),
  1305. );
  1306. this.reply['newUserStates'][0].playerState.camera.angle = JSON.parse(
  1307. JSON.stringify(user.camera.angle),
  1308. );
  1309. this.reply['newUserStates'][0].playerState.camera.position = JSON.parse(
  1310. JSON.stringify(user.camera.position),
  1311. );
  1312. this.reply['newUserStates'][0].playerState.cameraCenter = JSON.parse(
  1313. JSON.stringify(user.player.position),
  1314. );
  1315. this.reply['newUserStates'][0].renderInfo.isMoving = 0;
  1316. this.reply['actionResponses'][0].traceId = null;
  1317. this.reply.mediaSrc = null;
  1318. return this.reply;
  1319. }
  1320. async modeifyCameraAngle(angle, userId, traceId, actionType) {
  1321. const checkReplys = [];
  1322. if (angle > 22) {
  1323. angle = 45 - angle;
  1324. for (let i = 0; i < angle; ++i) {
  1325. // console.warn('矫正一次:' + i);
  1326. const reply = await this.rotateService.rotateForAngle(userId, 1);
  1327. // console.warn(
  1328. // '矫正:' + reply.newUserStates[0].playerState.camera.angle.yaw,
  1329. // );
  1330. reply.traceIds = [];
  1331. reply.traceIds.push(traceId);
  1332. const actionResponse = this.rotateService.createActionResponse(
  1333. actionType,
  1334. traceId,
  1335. );
  1336. reply.actionResponses = [];
  1337. reply.actionResponses.push(actionResponse);
  1338. checkReplys.push(reply);
  1339. }
  1340. } else if (angle != 0) {
  1341. for (let i = angle; i > -1; --i) {
  1342. // console.warn('矫正一次:' + i);
  1343. const reply = await this.rotateService.rotateForAngle(userId, -1);
  1344. // console.warn(
  1345. // '矫正:' + reply.newUserStates[0].playerState.camera.angle.yaw,
  1346. // );
  1347. reply.traceIds = [];
  1348. reply.traceIds.push(traceId);
  1349. const actionResponse = this.rotateService.createActionResponse(
  1350. actionType,
  1351. traceId,
  1352. );
  1353. reply.actionResponses = [];
  1354. reply.actionResponses.push(actionResponse);
  1355. checkReplys.push(reply);
  1356. }
  1357. }
  1358. return checkReplys;
  1359. }
  1360. getTarget(position, position1, position2) {
  1361. const line = this.createLine1(position1, position2);
  1362. const join = this.getJoinLinePoint(position, line);
  1363. // if (this.isContainForSegment(join, position1, position2)) {
  1364. // return join;
  1365. // } else {
  1366. // return null;
  1367. // }
  1368. return join;
  1369. }
  1370. getDistance(position1, position2) {
  1371. return Math.sqrt(
  1372. (position1.x - position2.x) * (position1.x - position2.x) +
  1373. (position1.y - position2.y) * (position1.y - position2.y),
  1374. );
  1375. }
  1376. isContainForSegment(point, startPoint, endPoint) {
  1377. const minDis = 0.1;
  1378. const dis1 =
  1379. this.getDistance(startPoint, point) + this.getDistance(endPoint, point);
  1380. const dis2 = this.getDistance(startPoint, endPoint);
  1381. if (Math.abs(dis1 - dis2) < minDis) {
  1382. return true;
  1383. } else {
  1384. return false;
  1385. }
  1386. }
  1387. //两条线的交点
  1388. getIntersectionPoint(parameter1, parameter2) {
  1389. if (this.isParallel(parameter1, parameter2)) {
  1390. return null;
  1391. }
  1392. if (
  1393. typeof parameter1.a == 'undefined' &&
  1394. typeof parameter2.a != 'undefined'
  1395. ) {
  1396. if (parameter1.x) {
  1397. return {
  1398. x: parameter1.x,
  1399. y: parameter2.a * parameter1.x + parameter2.b,
  1400. };
  1401. } else if (parameter1.y) {
  1402. return {
  1403. x: (parameter1.y - parameter2.b) / parameter2.a,
  1404. y: parameter1.y,
  1405. };
  1406. }
  1407. } else if (
  1408. typeof parameter2.a == 'undefined' &&
  1409. typeof parameter1.a != 'undefined'
  1410. ) {
  1411. if (parameter2.x) {
  1412. return {
  1413. x: parameter2.x,
  1414. y: parameter1.a * parameter2.x + parameter1.b,
  1415. };
  1416. } else if (parameter2.y) {
  1417. return {
  1418. x: (parameter2.y - parameter1.b) / parameter1.a,
  1419. y: parameter2.y,
  1420. };
  1421. }
  1422. } else if (
  1423. typeof parameter2.a == 'undefined' &&
  1424. typeof parameter1.a == 'undefined'
  1425. ) {
  1426. if (parameter1.hasOwnProperty('x') && parameter2.hasOwnProperty('y')) {
  1427. return { x: parameter1.x, y: parameter2.y };
  1428. } else if (
  1429. parameter1.hasOwnProperty('y') &&
  1430. parameter2.hasOwnProperty('x')
  1431. ) {
  1432. return { x: parameter2.x, y: parameter1.y };
  1433. } else {
  1434. return null;
  1435. }
  1436. }
  1437. if (parameter1.a == parameter2.a) {
  1438. return null;
  1439. }
  1440. const joinpointx =
  1441. (parameter2.b - parameter1.b) / (parameter1.a - parameter2.a);
  1442. const joinpointy =
  1443. (parameter1.a * parameter2.b - parameter2.a * parameter1.b) /
  1444. (parameter1.a - parameter2.a);
  1445. const point = { x: joinpointx, y: joinpointy };
  1446. return point;
  1447. }
  1448. // 垂直线
  1449. getVerticalLine(line, point) {
  1450. if (typeof line.a === 'undefined') {
  1451. if (line.hasOwnProperty('x')) {
  1452. return { y: point.y };
  1453. } else if (line.hasOwnProperty('y')) {
  1454. return { x: point.x };
  1455. } else {
  1456. return null;
  1457. }
  1458. } else if (line.a == 0) {
  1459. return { x: point.x };
  1460. } else {
  1461. const tl = {} as any as VerticalLineType;
  1462. tl.a = -1 / line.a;
  1463. const result = this.createLine2(tl, point);
  1464. return result;
  1465. }
  1466. }
  1467. // 经过point且与line垂直的直线,该直线与line的交点
  1468. getJoinLinePoint(point, line) {
  1469. const verticalLine = this.getVerticalLine(line, point);
  1470. const join = this.getIntersectionPoint(line, verticalLine);
  1471. return join;
  1472. }
  1473. // 与lineA平行并且point在线上
  1474. createLine2(lineA, point) {
  1475. const parameter = {} as any as {
  1476. x: number;
  1477. y: number;
  1478. a: number;
  1479. b: number;
  1480. };
  1481. if (typeof lineA.a === 'undefined') {
  1482. if (typeof lineA.x !== 'undefined') {
  1483. parameter.x = point.x;
  1484. } else if (typeof lineA.y !== 'undefined') {
  1485. parameter.y = point.y;
  1486. }
  1487. } else {
  1488. parameter.a = lineA.a;
  1489. parameter.b = point.y - point.x * lineA.a;
  1490. }
  1491. return parameter;
  1492. }
  1493. createLine1(point1, point2) {
  1494. if (point1.x == point2.x && point1.y == point2.y) {
  1495. return null;
  1496. } else if (this.getFixed(Math.abs(point1.x - point2.x)) == 0) {
  1497. return { x: point1.x };
  1498. } else if (this.getFixed(Math.abs(point1.y - point2.y)) == 0) {
  1499. return { y: point1.y };
  1500. }
  1501. const parametera = (point1.y - point2.y) / (point1.x - point2.x);
  1502. const parameterb =
  1503. (point1.x * point2.y - point2.x * point1.y) / (point1.x - point2.x);
  1504. if (this.getFixed(parametera) == 0) {
  1505. return { y: this.getFixed(parameterb) };
  1506. }
  1507. const parameter = {
  1508. a: this.getFixed(parametera),
  1509. b: this.getFixed(parameterb),
  1510. };
  1511. return parameter;
  1512. }
  1513. //返回true表示平行
  1514. isParallel(line1, line2) {
  1515. if (typeof line1.a == 'undefined' && typeof line2.a == 'undefined') {
  1516. if (line1.hasOwnProperty('x') && line2.hasOwnProperty('x')) {
  1517. return true;
  1518. } else if (line1.hasOwnProperty('y') && line2.hasOwnProperty('y')) {
  1519. return true;
  1520. } else {
  1521. return false;
  1522. }
  1523. } else if (typeof line1.a == 'undefined' || typeof line2.a == 'undefined') {
  1524. return false;
  1525. } else if (this.getFixed(line1.a) == this.getFixed(line2.a)) {
  1526. return true;
  1527. } else {
  1528. return false;
  1529. }
  1530. }
  1531. getFixed(num) {
  1532. const decimal = 2;
  1533. return parseFloat(num.toFixed(decimal));
  1534. }
  1535. isPointInPoly(position, points) {
  1536. const x = position.x;
  1537. const y = position.y;
  1538. let inside = false;
  1539. for (
  1540. let i = 0, j = points.length - 1;
  1541. i < points.length;
  1542. j = i++
  1543. ) {
  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. }