| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717 |
- import { Injectable, Logger, OnModuleInit } from '@nestjs/common';
- import { ConfigService } from '@nestjs/config';
- import { readFileSync } from 'fs';
- import { join } from 'path';
- import { CacheService } from 'src/cache/cache.service';
- import { RotateService } from 'src/rotate/rotate.service';
- import configuration from 'src/config/configuration';
- // import * as BreakPointIds from '../../ws/points-BreakPointId.json';
- // import { SceneService } from 'src/scene/scene.service';
- const seqExeAsyncFn = (asyncFn) => {
- let runPromise = null;
- return function seq(...args) {
- if (!runPromise) {
- //debugger;
- runPromise = asyncFn.apply(this, args);
- runPromise.then((data) => {
- //debugger;
- // console.log('seq result', data);
- });
- runPromise.then(() => (runPromise = null));
- return runPromise;
- } else {
- return runPromise.then(() => seq.apply(this, args));
- }
- };
- };
- let moveInterval = 3;
- @Injectable()
- export class MoveService implements OnModuleInit {
- constructor(
- private cacheService: CacheService,
- private rotateService: RotateService,
- private configService: ConfigService,
- ) {}
- private logger: Logger = new Logger('MoveService');
- private Actions = {
- Clicking: 1,
- Rotation: 1014,
- Joystick: 15,
- };
- public users = this.rotateService.users;
- private reply = {
- traceIds: [],
- vehicle: null,
- mediaSrc: null,
- isIDR: false,
- newUserStates: [
- {
- userId: 'dcff36ae4fc1d',
- playerState: {
- roomTypeId: '',
- person: 0,
- avatarId: '',
- skinId: '',
- roomId: '',
- isHost: false,
- isFollowHost: false,
- skinDataVersion: '',
- avatarComponents: '',
- nickName: '',
- movingMode: 0,
- attitude: '',
- areaName: '',
- pathName: '',
- pathId: '',
- avatarSize: 1,
- extra: '',
- prioritySync: false,
- player: {
- position: { x: -700, y: 0, z: 0 },
- angle: {
- pitch: 0,
- yaw: 0,
- roll: 0,
- },
- },
- camera: {
- position: { x: -1145, y: 0, z: 160 },
- angle: {
- pitch: 0,
- yaw: 0,
- roll: 0,
- },
- },
- cameraCenter: { x: -700, y: 0, z: 0 },
- },
- renderInfo: {
- renderType: 0,
- videoFrame: null,
- cameraStateType: 3,
- isMoving: 1,
- needIfr: 0,
- isVideo: 0,
- stillFrame: 0,
- isRotating: 0,
- isFollowing: 0,
- clientPanoTitlesBitmap: [],
- clientPanoTreceId: '',
- prefetchVideoId: '',
- noMedia: false,
- },
- event: null,
- relation: 1,
- },
- ],
- actionResponses: [
- {
- actionType: 1,
- pointType: 100,
- extra: '',
- traceId: '',
- packetId: '',
- nps: [],
- peopleNum: 0,
- zoneId: '',
- echoMsg: '',
- reserveDetail: null,
- userWithAvatarList: [],
- newUserStates: [],
- code: 0,
- msg: '',
- },
- ],
- getStateType: 0,
- code: 0,
- msg: 'OK',
- };
- private breakPointInfo: any;
- public cameraInfos = [];
- public sendingFrameForJoystick = false;
- // eslint-disable-next-line @typescript-eslint/no-empty-function
- async onModuleInit() {
- //const app_id = '0000000003';
- // const app_id = '0000000007';
- // const prefix = '/mnt/metaverse/scene';
- const app_id = configuration().app.appId;
- const prefix = configuration().app.prefix;
- console.log('app_id', app_id, configuration().app.appId);
- console.log('prefix', prefix, configuration().app.appId);
- let path;
- // let path: string;
- if (process.env.NODE_ENV === 'development') {
- path = join(__dirname, `../ws/${app_id}/points-${app_id}.json`);
- console.log('测试服JSON-move', path);
- }
- if (process.env.NODE_ENV === 'production') {
- path = join(`${prefix}/${app_id}/points-${app_id}.json`);
- console.log('正式服JSON-move', path);
- }
- this.loadJSON(path);
- }
- async loadJSON(path) {
- try {
- const data = await readFileSync(path);
- const BreakPointInfo = JSON.parse(Buffer.from(data).toString('utf-8'));
- this.breakPointInfo = BreakPointInfo;
- // console.log('BreakPointInfo', BreakPointInfo);
- } catch (error) {
- this.logger.error('load-json-error', error);
- }
- }
- init(app_id, userId) {
- const user = {
- appId: null,
- userId: null,
- breakPointId: null,
- roomId: null,
- player: {
- position: { x: -700, y: 0, z: 0 },
- angle: {
- pitch: 0,
- yaw: 0,
- roll: 0,
- },
- },
- camera: {
- position: { x: -1145, y: 0, z: 160 },
- angle: {
- pitch: 0,
- yaw: 0,
- roll: 0,
- },
- },
- rotateInfo: {
- frameIndex: 0,
- horizontal_move: 0,
- },
- moveInfo: {},
- // traceIds: [],
- // actionResponses:[]
- };
- user.appId = app_id;
- user.userId = userId;
- user.breakPointId = Number(this.configService.get('app.startPoint')) || 0;
- console.log('user-init', user);
- this.users[userId] = user;
- }
- async getMoveFrames(
- appId,
- start_break_point_id,
- end_break_point_id,
- angleIndex,
- ) {
- console.log(
- 'handlejoystick-angle->相机过渡angleIndex-------------------------:' +
- angleIndex,
- );
- let moveFramesRes, moveFrames;
- let key =
- 'moveframe:app_id:' +
- appId +
- ':start_break_point_id:' +
- start_break_point_id +
- ':end_break_point_id:' +
- end_break_point_id +
- ':angle:' +
- angleIndex;
- //倒序
- if (start_break_point_id > end_break_point_id) {
- key =
- 'moveframe:app_id:' +
- appId +
- ':start_break_point_id:' +
- end_break_point_id +
- ':end_break_point_id:' +
- start_break_point_id +
- ':angle:' +
- angleIndex;
- moveFramesRes = await this.cacheService.get(key);
- moveFrames = JSON.parse(moveFramesRes);
- moveFrames = moveFrames.reverse();
- } else {
- moveFramesRes = await this.cacheService.get(key);
- moveFrames = JSON.parse(moveFramesRes);
- }
- return moveFrames;
- }
- async move(pathArray, actionRequest) {
- try {
- const userId = actionRequest['user_id'];
- const traceId = actionRequest['trace_id'];
- const actionType = actionRequest['action_type'];
- const user = this.users[userId];
- const appId = user.appId;
- const path = pathArray || [100, 101, 102]; //需要计算路径
- const angle = user.camera.angle.yaw % 45; //纠正需要
- const replys = [];
- const traceIds = [];
- traceIds.push(traceId);
- const checkReplys = await this.modeifyCameraAngle(
- angle,
- userId,
- traceId,
- actionType,
- );
- for (let i = 0; i < checkReplys.length; ++i) {
- checkReplys[i].actionResponses[0].actionType = actionType;
- if (i == 0 || i == checkReplys.length - 1) {
- checkReplys[i].isIDR = true;
- } else {
- checkReplys[i].isIDR = false;
- }
- }
- //replys['P' + user.breakPointId + 'T' + user.breakPointId] = checkReplys;
- replys.push(checkReplys);
- console.log('路径:' + path);
- //过渡传到缓存里
- this.reply.traceIds = traceIds;
- this.reply['newUserStates'][0].userId = userId;
- this.reply['actionResponses'][0].traceId = traceId;
- //const index = Math.floor((user.camera.angle.yaw + 1) / 45) % 8; //过渡需要
- const index = this.getMoveIndex(user.camera.angle.yaw);
- for (let i = 0; i < path.length - 1; ++i) {
- const start_break_point_id = path[i];
- const end_break_point_id = path[i + 1];
- const moveFrames = await this.getMoveFrames(
- appId,
- start_break_point_id,
- end_break_point_id,
- index,
- );
- if (!moveFrames) {
- return replys;
- }
- const pathReplys = this.createCacheReplys(
- appId,
- moveFrames,
- traceId,
- userId,
- start_break_point_id,
- end_break_point_id,
- false,
- );
- //第一段
- if (i == 0) {
- //第一帧
- pathReplys[0].isIDR = true;
- }
- //最后一段
- if (i == path.length - 2) {
- //最后一帧
- pathReplys[pathReplys.length - 1][
- 'newUserStates'
- ][0].renderInfo.isMoving = 0;
- }
- for (let j = 0; j < pathReplys.length; ++j) {
- pathReplys[j].actionResponses[0].actionType = actionType;
- }
- replys.push(pathReplys);
- //replys['P' + start_break_point_id + 'T' + end_break_point_id] =pathReplys;
- }
- return replys;
- } catch (error) {
- console.log('MoveService', error);
- }
- }
- createCacheReplys(
- appId,
- moveFrames,
- traceId,
- userId,
- startBreakPointId,
- endBreakPointId,
- isFromUser,
- ) {
- const replys = [];
- const startPosition = this.breakPointInfo[startBreakPointId].position;
- const endPosition = this.breakPointInfo[endBreakPointId].position;
- const angle = this.getAngle(
- startPosition,
- {
- x: startPosition.x + 1,
- y: startPosition.y,
- },
- endPosition,
- );
- const user = this.users[userId];
- let i;
- for (i = 1; i < moveFrames.length; i += moveInterval) {
- const moveFrame = moveFrames[i];
- const reply = JSON.parse(JSON.stringify(this.reply));
- if (reply.traceIds.indexOf(traceId) == -1) {
- reply.traceIds.push(traceId);
- }
- reply['newUserStates'][0].userId = userId;
- if (!isFromUser) {
- reply['newUserStates'][0].playerState.player.position = {
- x:
- startPosition.x +
- ((endPosition.x - startPosition.x) / moveFrames.length) * i,
- y:
- startPosition.y +
- ((endPosition.y - startPosition.y) / moveFrames.length) * i,
- z:
- startPosition.z +
- ((endPosition.z - startPosition.z) / moveFrames.length) * i,
- };
- reply['newUserStates'][0].playerState.player.angle.yaw = angle;
- reply['newUserStates'][0].playerState.cameraCenter =
- reply['newUserStates'][0].playerState.player.position;
- } else {
- reply['newUserStates'][0].playerState.player.position = JSON.parse(
- JSON.stringify(user.player.position),
- );
- reply['newUserStates'][0].playerState.player.angle.yaw =
- user.player.angle.yaw;
- reply['newUserStates'][0].playerState.cameraCenter = JSON.parse(
- JSON.stringify(user.player.position),
- );
- }
- reply['newUserStates'][0].playerState.camera.position = JSON.parse(
- JSON.stringify(moveFrame.camera_position),
- );
- if (i == 1) {
- console.log('move-2' + moveFrame.camera_angle.yaw);
- }
- if (moveFrame.camera_angle.yaw < 0) {
- moveFrame.camera_angle.yaw += 360;
- } else if (moveFrame.camera_angle.yaw > 359) {
- moveFrame.camera_angle.yaw -= 360;
- }
- reply['newUserStates'][0].playerState.camera.angle =
- moveFrame.camera_angle;
- reply['newUserStates'][0].renderInfo.isMoving = 1;
- reply['actionResponses'][0].traceId = traceId;
- reply.mediaSrc =
- '/' +
- appId +
- '/' +
- startBreakPointId +
- '/' +
- moveFrame.file_name.substring(0, moveFrame.file_name.indexOf('.')) +
- '/' +
- moveFrame.file_name +
- '?m=' +
- new Date().getTime();
- if (startBreakPointId > endBreakPointId) {
- reply.mediaSrc =
- '/' +
- appId +
- '/' +
- endBreakPointId +
- '/' +
- moveFrame.file_name.substring(0, moveFrame.file_name.indexOf('.')) +
- '/' +
- moveFrame.file_name +
- '?m=' +
- new Date().getTime();
- }
- reply.startBreakPointId = startBreakPointId;
- reply.endBreakPointId = endBreakPointId;
- // if (i == moveFrames.length - 1) {
- // reply.isIDR = true;
- // } else {
- // reply.isIDR = false;
- // }
- reply.isIDR = false;
- replys.push(reply);
- }
- if (i != moveFrames.length - 1) {
- i = moveFrames.length - 1;
- const moveFrame = moveFrames[i];
- const reply = JSON.parse(JSON.stringify(this.reply));
- reply.traceIds.push(traceId);
- reply['newUserStates'][0].userId = userId;
- reply['newUserStates'][0].renderInfo.isMoving = 1;
- if (!isFromUser) {
- reply['newUserStates'][0].playerState.player.position = {
- x:
- startPosition.x +
- ((endPosition.x - startPosition.x) / moveFrames.length) * i,
- y:
- startPosition.y +
- ((endPosition.y - startPosition.y) / moveFrames.length) * i,
- z:
- startPosition.z +
- ((endPosition.z - startPosition.z) / moveFrames.length) * i,
- };
- reply['newUserStates'][0].playerState.player.angle.yaw = angle;
- reply['newUserStates'][0].playerState.cameraCenter =
- reply['newUserStates'][0].playerState.player.position;
- } else {
- reply['newUserStates'][0].playerState.player.position = JSON.parse(
- JSON.stringify(user.player.position),
- );
- reply['newUserStates'][0].playerState.player.angle.yaw =
- user.player.angle.yaw;
- reply['newUserStates'][0].playerState.cameraCenter = JSON.parse(
- JSON.stringify(user.player.position),
- );
- }
- reply['newUserStates'][0].playerState.camera.position =
- moveFrame.camera_position;
- if (moveFrame.camera_angle.yaw < 0) {
- moveFrame.camera_angle.yaw += 360;
- } else if (moveFrame.camera_angle.yaw > 359) {
- moveFrame.camera_angle.yaw -= 360;
- }
- reply['newUserStates'][0].playerState.camera.angle =
- moveFrame.camera_angle;
- reply['actionResponses'][0].traceId = traceId;
- reply.mediaSrc =
- '/' +
- appId +
- '/' +
- startBreakPointId +
- '/' +
- moveFrame.file_name.substring(0, moveFrame.file_name.indexOf('.')) +
- '/' +
- moveFrame.file_name +
- '?m=' +
- new Date().getTime();
- if (startBreakPointId > endBreakPointId) {
- reply.mediaSrc =
- '/' +
- appId +
- '/' +
- endBreakPointId +
- '/' +
- moveFrame.file_name.substring(0, moveFrame.file_name.indexOf('.')) +
- '/' +
- moveFrame.file_name +
- '?m=' +
- new Date().getTime();
- }
- reply.startBreakPointId = startBreakPointId;
- reply.endBreakPointId = endBreakPointId;
- replys.push(reply);
- }
- replys[replys.length - 1].isIDR = true;
- return replys;
- }
- //需要通知user,人物和相机走到哪一个呼吸点位了
- updateUser(userId, breakPointId, lastReply) {
- const user = this.users[userId];
- user.breakPointId = breakPointId;
- if (lastReply.actionResponses[0].actionType != 15) {
- user.player.position =
- lastReply['newUserStates'][0].playerState.player.position;
- }
- user.player.angle = lastReply['newUserStates'][0].playerState.player.angle;
- user.camera.position =
- lastReply['newUserStates'][0].playerState.camera.position;
- user.camera.angle = lastReply['newUserStates'][0].playerState.camera.angle;
- }
- getBreakPoints(actionRequest) {
- const userId = actionRequest['user_id'];
- const traceId = actionRequest['trace_id'];
- const actionType = actionRequest['action_type'];
- const user = this.users[userId];
- const appId = user.appId;
- const breakPointId = user.breakPointId;
- const reply = {
- actionType: actionType,
- pointType: 100,
- extra: '',
- traceId: traceId,
- packetId: '',
- nps: [],
- peopleNum: 0,
- zoneId: '',
- echoMsg: '',
- reserveDetail: null,
- userWithAvatarList: [],
- newUserStates: [],
- code: 0,
- msg: '',
- };
- //const breakPoints = await this.cacheService.get('breakpoints:app_id:'+appId+':break_point_id:'+breakPointId);
- //获取redis表全部元素,'breakpoints:app_id:'+appId+':break_point_id:'开头的
- //const keys = await this.cacheService.keys(`breakpoints:app_id:${appId}*`);
- for (const key in this.breakPointInfo) {
- const breakPoint = this.breakPointInfo[key];
- //const breakPoint = JSON.parse(breakPointRes);
- //const position = breakPoint.position;
- reply['nps'].push({
- position: breakPoint.position,
- breakPointId: Number(key),
- });
- }
- // for (let i = 0; i < keys.length; ++i) {
- // const breakPointRes = await this.cacheService.get(keys[i]);
- // const breakPoint = JSON.parse(breakPointRes);
- // const position = breakPoint.position;
- // reply['nps'].push({
- // position: position,
- // breakPointId: breakPoint.breakPointId,
- // });
- // }
- return reply;
- }
- getAngle(point, point1, point2) {
- const x1 = point1.x - point.x;
- const y1 = point1.y - point.y;
- const x2 = point2.x - point.x;
- const y2 = point2.y - point.y;
- const dot = x1 * x2 + y1 * y2;
- const det = x1 * y2 - y1 * x2;
- const angle = (Math.atan2(det, dot) / Math.PI) * 180;
- return (angle + 360) % 360;
- }
- async stop(traceId, userId, breakPointId, cameraAngle, playerAngle) {
- //const breakPointId = movePointIds.substring(movePointIds.indexOf('-') + 1);
- const user = this.users[userId];
- const startBreakPointId = user.breakPointId;
- user.breakPointId = breakPointId;
- const appId = user.appId;
- const breakPoint = this.breakPointInfo[breakPointId];
- user.player.position = breakPoint.position;
- user.player.angle = playerAngle;
- const rotateKey =
- 'rotateframe:app_id:' +
- appId +
- ':frame_index:' +
- cameraAngle.yaw +
- ':break_point_id:' +
- breakPointId;
- const rotateDataRes = await this.cacheService.get(rotateKey);
- const rotateData = JSON.parse(rotateDataRes);
- user.camera.position = rotateData.cameraPosition;
- user.camera.angle = rotateData.cameraAngle;
- const reply = JSON.parse(JSON.stringify(this.reply));
- reply.traceIds.push(traceId);
- reply['newUserStates'][0].userId = userId;
- reply['newUserStates'][0].playerState.player.position = breakPoint.position;
- reply['newUserStates'][0].playerState.player.angle = playerAngle;
- reply['newUserStates'][0].playerState.camera.position =
- rotateData.cameraPosition;
- reply['newUserStates'][0].playerState.camera.angle = rotateData.cameraAngle;
- reply['newUserStates'][0].playerState.cameraCenter = breakPoint.position;
- reply['actionResponses'][0].traceId = traceId;
- reply.mediaSrc =
- '/' +
- appId +
- '/' +
- breakPointId +
- '/' +
- rotateData.directory +
- '/' +
- rotateData.fileName +
- '?m=' +
- new Date().getTime();
- reply.startBreakPointId = startBreakPointId;
- reply.endBreakPointId = breakPointId;
- reply['newUserStates'][0].renderInfo.isMoving = 0;
- return reply;
- }
- // 顺序旋转请求
- seqExeJoystick = seqExeAsyncFn(this.joystick);
- async joystick(actionRequest) {
- try {
- const userId = actionRequest['user_id'];
- const traceId = actionRequest['trace_id'];
- const dir_action = actionRequest['dir_action'];
- const actionType = actionRequest['action_type'];
- const user = this.users[userId];
- const breakPointId = user.breakPointId;
- const appId = user.appId;
- moveInterval = 1;
- const replys = [];
- const step = 0.5;
- const closestDis = 50; //小于这个距离就跳到邻居呼吸点
- const distance = step * dir_action.speed_level;
- let angle = null;
- let move_angle = dir_action.move_angle + user.camera.angle.yaw;
- move_angle = move_angle % 360;
- //TODO 临时增加断言
- const playerPosition: Point = { x: 0, y: 0, z: 0 };
- playerPosition.x =
- user.player.position.x +
- distance * Math.cos((move_angle / 360) * 2 * Math.PI);
- playerPosition.y =
- user.player.position.y +
- distance * Math.sin((move_angle / 360) * 2 * Math.PI);
- //可行走的区域
- const area = [
- { x: -600, y: -300 },
- { x: -600, y: 250 },
- { x: 550, y: 250 },
- { x: 550, y: -300 },
- ];
- //找到邻居点,判断user.player.position与邻居点的距离,如果距离小于closestDis,就要更新camera的position
- const breakPoint = this.breakPointInfo[breakPointId];
- const surroundPointIds = breakPoint.contact;
- //const neighAngles = [];
- const traceIds = [];
- user.player.angle.yaw = move_angle;
- traceIds.push(traceId);
- this.reply.traceIds = traceIds;
- this.reply['newUserStates'][0].userId = userId;
- this.reply['actionResponses'][0].traceId = traceId;
- this.reply['newUserStates'][0].playerState.player.angle.yaw = move_angle;
- this.reply['newUserStates'][0].playerState.camera.angle = JSON.parse(
- JSON.stringify(user.camera.angle),
- );
- this.reply['newUserStates'][0].playerState.camera.position = JSON.parse(
- JSON.stringify(user.camera.position),
- );
- this.reply['newUserStates'][0].playerState.cameraCenter = JSON.parse(
- JSON.stringify(user.player.position),
- );
- this.reply['newUserStates'][0].renderInfo.isMoving = 1;
- this.reply['actionResponses'][0].traceId = traceId;
- this.reply.mediaSrc = null;
- //人在哪个角度
- const _angle = this.getAngle(
- breakPoint.position,
- { x: breakPoint.position.x + 1, y: breakPoint.position.y },
- playerPosition,
- );
- //需要找到最近的邻居点和最接近角度的邻居点
- let closestNeighorId = null;
- let closestDisNeighor = null;
- //超出行走区域的时候用得上
- let minAngleNeighorId = null;
- let closestAngleNeighor = null;
- for (let i = 0; i < surroundPointIds.length; ++i) {
- const neighPoint = this.breakPointInfo[surroundPointIds[i]];
- neighPoint.breakPointId = surroundPointIds[i];
- angle = this.getAngle(
- breakPoint.position,
- { x: breakPoint.position.x + 1, y: breakPoint.position.y },
- neighPoint.position,
- );
- const distance = this.getDistance(playerPosition, neighPoint.position);
- if (closestNeighorId == null || closestDisNeighor > distance) {
- closestNeighorId = surroundPointIds[i];
- closestDisNeighor = distance;
- }
- if (
- this.getOffsetAngle(angle, _angle) < 90 &&
- (minAngleNeighorId == null ||
- this.getOffsetAngle(closestAngleNeighor, _angle) >
- this.getOffsetAngle(angle, _angle))
- ) {
- minAngleNeighorId = surroundPointIds[i];
- closestAngleNeighor = angle;
- }
- }
- //超出范围了
- if (this.getDistance(playerPosition, breakPoint.position) > closestDis) {
- console.log('20220627test-joystick-moveCamera');
- return await this.moveCamera(
- breakPointId,
- closestNeighorId,
- appId,
- userId,
- traceId,
- actionType,
- );
- } else {
- const inside = this.isPointInPoly(playerPosition, area);
- //超出区域
- if (!inside) {
- if (minAngleNeighorId != null) {
- console.log('20220627test-joystick-moveDirect');
- return await this.moveDirect(
- playerPosition,
- closestDis,
- breakPointId,
- minAngleNeighorId,
- appId,
- userId,
- traceId,
- actionType,
- );
- } else {
- return null;
- }
- }
- user.player.position = JSON.parse(JSON.stringify(playerPosition));
- this.reply['newUserStates'][0].playerState.player.position = JSON.parse(
- JSON.stringify(user.player.position),
- );
- this.reply.actionResponses[0].actionType = actionType;
- const cameraInfo = this.getCameraInfo();
- if (cameraInfo != null) {
- console.log(
- '20220627test-joystick-自由-合并,' + this.cameraInfos.length,
- );
- this.reply['newUserStates'][0].playerState.camera.position =
- cameraInfo.camera_position;
- this.reply['newUserStates'][0].playerState.camera.angle =
- cameraInfo.camera_angle;
- if (cameraInfo.mediaSrc) {
- this.reply.mediaSrc = cameraInfo.mediaSrc;
- this.reply.isIDR = cameraInfo.isIDR;
- }
- user.camera.position = JSON.parse(
- JSON.stringify(cameraInfo.camera_position),
- );
- // console.log(
- // 'handlejoystick-angle->:自由并过渡更新user.angle' +
- // cameraInfo.camera_angle.yaw,
- // );
- user.camera.angle.yaw = cameraInfo.camera_angle.yaw;
- this.sendingFrameForJoystick = true;
- } else {
- console.log('20220627test-joystick-自由-不合并');
- }
- return this.reply;
- }
- } catch (error) {
- console.log('MoveService', error);
- debugger;
- return null;
- }
- }
- async joystick2(actionRequest) {
- try {
- if (this.sendingFrameForJoystick) {
- return null;
- }
- const userId = actionRequest['user_id'];
- const traceId = actionRequest['trace_id'];
- const dir_action = actionRequest['dir_action'];
- const actionType = actionRequest['action_type'];
- const user = this.users[userId];
- const breakPointId = user.breakPointId;
- const appId = user.appId;
- //const step = 0.3;
- const step = 0.8;
- const closestDis = 80; //小于这个距离就跳到邻居呼吸点
- const distance = step * dir_action.speed_level;
- let angle = null;
- // console.log(
- // 'handlejoystick-angle->:dir_action.move_angle' + dir_action.move_angle,
- // );
- if (user.camera.angle.yaw < 0) {
- user.camera.angle.yaw = 360 + user.camera.angle.yaw;
- }
- let move_angle = dir_action.move_angle + user.camera.angle.yaw;
- move_angle = move_angle % 360;
- // console.log(
- // 'handlejoystick-angle->:叠加后move_angle(也是user.angle)' + move_angle,
- // );
- //TODO 临时增加断言
- const playerPosition: Point = { x: 0, y: 0, z: 0 };
- playerPosition.x =
- user.player.position.x +
- distance * Math.cos((move_angle / 360) * 2 * Math.PI);
- playerPosition.y =
- user.player.position.y +
- distance * Math.sin((move_angle / 360) * 2 * Math.PI);
- // const offsetX = playerPosition.x - user.player.position.x;
- // const offsetY = playerPosition.y - user.player.position.y;
- console.log(
- 'handlejoysticktesttest:actionRequest-playerPosition' +
- JSON.stringify(playerPosition),
- );
- //找到邻居点,判断user.player.position与邻居点的距离,如果距离小于closestDis,就要更新camera的position
- let chooseBreakPointId = null;
- const breakPoint = this.breakPointInfo[breakPointId];
- const surroundPointIds = breakPoint.contact;
- const traceIds = [];
- user.player.angle.yaw = move_angle;
- traceIds.push(traceId);
- this.reply.traceIds = traceIds;
- this.reply['newUserStates'][0].userId = userId;
- this.reply['actionResponses'][0].traceId = traceId;
- this.reply['newUserStates'][0].playerState.player.angle.yaw = move_angle;
- this.reply['newUserStates'][0].playerState.camera.angle = JSON.parse(
- JSON.stringify(user.camera.angle),
- );
- this.reply['newUserStates'][0].playerState.camera.position = JSON.parse(
- JSON.stringify(user.camera.position),
- );
- this.reply['newUserStates'][0].playerState.cameraCenter = JSON.parse(
- JSON.stringify(breakPoint.position),
- );
- this.reply['newUserStates'][0].renderInfo.isMoving = 1;
- this.reply['actionResponses'][0].traceId = traceId;
- this.reply.mediaSrc = null;
- console.log('joystickjoystick:' + this.cameraInfos.length);
- if (surroundPointIds.length == 1) {
- //console.log('handlejoysticktesttest:actionRequest-笔直'+new Date().getTime());
- return await this.moveDirect(
- playerPosition,
- closestDis,
- breakPointId,
- surroundPointIds[0],
- appId,
- userId,
- traceId,
- actionType,
- );
- }
- let count = 0;
- const neighPoints = [];
- //人在哪个角度
- const _angle = this.getAngle(
- breakPoint.position,
- { x: breakPoint.position.x + 1, y: breakPoint.position.y },
- playerPosition,
- );
- // const offsetBreakPosition = {
- // x: breakPoint.position.x + offsetX,
- // y: breakPoint.position.y + offsetY,
- // };
- let closestNeighorId = null;
- let neighDis = null;
- let singleInfo = null;
- for (let i = 0; i < surroundPointIds.length; ++i) {
- const neighPoint = this.breakPointInfo[surroundPointIds[i]];
- if (closestNeighorId == null) {
- neighDis = this.getDistance(playerPosition, neighPoint.position);
- closestNeighorId = surroundPointIds[i];
- } else if (
- neighDis > this.getDistance(playerPosition, neighPoint.position)
- ) {
- neighDis = this.getDistance(playerPosition, neighPoint.position);
- closestNeighorId = surroundPointIds[i];
- }
- neighPoint.breakPointId = surroundPointIds[i];
- angle = this.getAngle(
- breakPoint.position,
- { x: breakPoint.position.x + 1, y: breakPoint.position.y },
- neighPoint.position,
- );
- //if(angle<45&&angle!=0){
- if (
- // Math.abs(angle - move_angle) < 45 &&
- // Math.abs(angle - move_angle) != 0
- Math.abs(angle - _angle) < 45 &&
- Math.abs(angle - _angle) != 0
- ) {
- neighPoint.angle = angle;
- neighPoints.push(neighPoint);
- ++count;
- } else if (Math.abs(angle - move_angle) == 0) {
- neighPoint.angle = angle;
- neighPoints.push(neighPoint);
- ++count;
- break;
- }
- //if (angle == 0 && Math.abs(360 - move_angle) < 45) {
- if (angle == 0 && Math.abs(360 - _angle) < 45) {
- neighPoint.angle = angle;
- neighPoints.push(neighPoint);
- ++count;
- }
- if (
- Math.abs(angle - move_angle) < 45 ||
- Math.abs(angle + 360 - move_angle) < 45
- ) {
- if (singleInfo == null) {
- singleInfo = {
- angle: angle,
- breakPointId: surroundPointIds[i],
- };
- }
- }
- }
- if (count == 2) {
- //人物移动
- user.player.position = JSON.parse(JSON.stringify(playerPosition));
- this.reply['newUserStates'][0].playerState.player.position = JSON.parse(
- JSON.stringify(playerPosition),
- );
- } else {
- if (singleInfo != null) {
- // console.log(
- // 'joystick校验--->' + breakPointId + '-' + singleInfo.breakPointId,
- // );
- //console.log('handlejoysticktesttest校验0角度:'+_angle+','+move_angle) //这个没办法,得换方案
- //console.log('handlejoysticktesttest:actionRequest-笔直'+new Date().getTime());
- return await this.moveDirect(
- playerPosition,
- closestDis,
- breakPointId,
- singleInfo.breakPointId,
- appId,
- userId,
- traceId,
- actionType,
- );
- } else if (count == 1) {
- //console.log('handlejoysticktesttest:actionRequest-笔直'+new Date().getTime());
- return await this.moveDirect(
- playerPosition,
- closestDis,
- breakPointId,
- neighPoints[0].breakPointId,
- appId,
- userId,
- traceId,
- actionType,
- );
- } else if (count == 0) {
- this.reply['newUserStates'][0].playerState.player.position =
- JSON.parse(JSON.stringify(user.player.position));
- this.reply.actionResponses[0].actionType = actionType;
- //console.log('handlejoysticktesttest:actionRequest-停止'+new Date().getTime());
- return this.reply;
- }
- }
- //count == 2
- //超出范围了
- console.log(
- 'handlejoysticktesttest:距离:' +
- JSON.stringify(playerPosition) +
- '->' +
- JSON.stringify(breakPoint.position) +
- ':' +
- this.getDistance(playerPosition, breakPoint.position),
- );
- if (this.getDistance(playerPosition, breakPoint.position) > closestDis) {
- // let offsetAngle1 = Math.abs(_angle - neighPoints[0].angle);
- // if (neighPoints[0].angle == 0) {
- // offsetAngle1 = Math.min(offsetAngle1, Math.abs(_angle - 360));
- // }
- // let offsetAngle2 = Math.abs(_angle - neighPoints[1].angle);
- // if (neighPoints[1].angle == 0) {
- // offsetAngle2 = Math.min(offsetAngle2, Math.abs(_angle - 360));
- // }
- // if (offsetAngle1 > offsetAngle2) {
- // chooseBreakPointId = neighPoints[1].breakPointId;
- // } else {
- // chooseBreakPointId = neighPoints[0].breakPointId;
- // }
- if (closestNeighorId == null) {
- debugger;
- }
- chooseBreakPointId = closestNeighorId;
- return await this.moveCamera(
- breakPointId,
- chooseBreakPointId,
- appId,
- userId,
- traceId,
- actionType,
- );
- } else {
- this.reply['newUserStates'][0].playerState.player.position = JSON.parse(
- JSON.stringify(user.player.position),
- );
- this.reply.actionResponses[0].actionType = actionType;
- const cameraInfo = this.getCameraInfo();
- if (cameraInfo != null) {
- console.log('joystick自由--->合并');
- this.reply['newUserStates'][0].playerState.camera.position =
- cameraInfo.camera_position;
- this.reply['newUserStates'][0].playerState.camera.angle =
- cameraInfo.camera_angle;
- if (cameraInfo.mediaSrc) {
- this.reply.mediaSrc = cameraInfo.mediaSrc;
- this.reply.isIDR = cameraInfo.isIDR;
- }
- user.camera.position = JSON.parse(
- JSON.stringify(cameraInfo.camera_position),
- );
- // console.log(
- // 'handlejoystick-angle->:自由并过渡更新user.angle' +
- // cameraInfo.camera_angle.yaw,
- // );
- user.camera.angle.yaw = cameraInfo.camera_angle.yaw;
- this.sendingFrameForJoystick = true;
- } else {
- //console.log('joystick自由--->不合并');
- }
- //console.log('handlejoysticktesttest:actionRequest-自由'+new Date().getTime());
- console.log(
- 'handlejoysticktesttest:自由:' +
- this.reply.mediaSrc +
- '->' +
- JSON.stringify(user.player.position),
- );
- return this.reply;
- }
- } catch (error) {
- console.log('MoveService', error);
- return null;
- }
- }
- //沿着最合适的neighBreakPointId走
- async moveDirect(
- playerPosition,
- closestDis,
- breakPointId,
- neighBreakPointId,
- appId,
- userId,
- traceId,
- actionType,
- ) {
- const breakPoint = this.breakPointInfo[breakPointId];
- const user = this.users[userId];
- const player_Position = this.getTarget(
- playerPosition,
- breakPoint.position,
- this.breakPointInfo[neighBreakPointId].position,
- );
- // console.log('handlejoysticktesttest校验1线段:'+breakPointId+','+neighBreakPointId)
- // console.log('handlejoysticktesttest校验2计算后的:'+JSON.stringify(player_Position))
- // console.log('handlejoysticktesttest校验3人物变化前:'+JSON.stringify(user.player.position))
- // console.log('handlejoysticktesttest校验4人物本应该的坐标:'+JSON.stringify(playerPosition))
- if (player_Position != null) {
- playerPosition.x = player_Position.x;
- playerPosition.y = player_Position.y;
- user.player.position = JSON.parse(JSON.stringify(playerPosition));
- }
- if (this.getDistance(playerPosition, breakPoint.position) > closestDis) {
- //this.reply.moveOver = true;
- return await this.moveCamera(
- breakPointId,
- neighBreakPointId,
- appId,
- userId,
- traceId,
- actionType,
- );
- } else {
- this.reply['newUserStates'][0].playerState.player.position = JSON.parse(
- JSON.stringify(user.player.position),
- );
- this.reply.actionResponses[0].actionType = actionType;
- const cameraInfo = this.getCameraInfo();
- if (cameraInfo != null) {
- this.reply['newUserStates'][0].playerState.camera.position =
- cameraInfo.camera_position;
- this.reply['newUserStates'][0].playerState.camera.angle =
- cameraInfo.camera_angle;
- if (cameraInfo.mediaSrc) {
- this.reply.mediaSrc = cameraInfo.mediaSrc;
- this.reply.isIDR = cameraInfo.isIDR;
- }
- user.camera.position = JSON.parse(
- JSON.stringify(cameraInfo.camera_position),
- );
- console.log(
- 'handlejoystick-angle->:moveDirect更新user.angle' +
- cameraInfo.camera_angle.yaw,
- );
- user.camera.angle.yaw = cameraInfo.camera_angle.yaw;
- this.sendingFrameForJoystick = true;
- }
- return this.reply;
- }
- }
- async moveCamera(
- breakPointId,
- chooseBreakPointId,
- appId,
- userId,
- traceId,
- actionType,
- ) {
- const user = this.users[userId];
- this.reply['newUserStates'][0].playerState.player.position = JSON.parse(
- JSON.stringify(user.player.position),
- );
- if (chooseBreakPointId == null) {
- this.reply.actionResponses[0].actionType = actionType;
- return this.reply;
- }
- //判断人物离该邻接点的距离是否在最小路径内,如果是,跳到这个邻接点里
- //相机纠正加过渡
- else {
- if (chooseBreakPointId == user.breakPointId) {
- return this.reply;
- }
- const angle = user.camera.angle.yaw % 45; //纠正需要
- //通过user.camera.angle矫正相机的angle
- const checkReplys = await this.modeifyCameraAngle(
- angle,
- userId,
- traceId,
- actionType,
- );
- for (let i = 0; i < checkReplys.length; ++i) {
- let isIDR = false;
- if (i == 0 || i == checkReplys.length - 1) {
- isIDR = true;
- }
- this.addCameraInfo(
- checkReplys[i]['newUserStates'][0].playerState.camera.position,
- checkReplys[i]['newUserStates'][0].playerState.camera.angle,
- checkReplys[i].mediaSrc,
- isIDR,
- );
- }
- //replys.push(checkReplys);
- //读redis里的数据,按照frame_index的大小排序
- /*
- const key =
- 'moveframe:app_id:' +
- appId +
- ':start_break_point_id:' +
- breakPointId +
- ':end_break_point_id:' +
- chooseBreakPointId +
- ':angle:' +
- Math.floor(user.camera.angle.yaw / 45);
- const moveFramesRes = await this.cacheService.get(key);
- const moveFrames = JSON.parse(moveFramesRes);
- */
- const index = this.getMoveIndex(user.camera.angle.yaw);
- const moveFrames = await this.getMoveFrames(
- appId,
- breakPointId,
- chooseBreakPointId,
- index,
- );
- this.setCameraInfo(appId, moveFrames, breakPointId, chooseBreakPointId);
- user.breakPointId = chooseBreakPointId;
- const cameraInfo = this.getCameraInfo();
- if (cameraInfo != null) {
- this.reply['newUserStates'][0].playerState.camera.position =
- cameraInfo.camera_position;
- this.reply['newUserStates'][0].playerState.camera.angle =
- cameraInfo.camera_angle;
- user.camera.position = JSON.parse(
- JSON.stringify(cameraInfo.camera_position),
- );
- // console.log(
- // 'handlejoystick-angle->:刚刚过渡更新user.angle' +
- // cameraInfo.camera_angle.yaw,
- // );
- user.camera.angle.yaw = cameraInfo.camera_angle.yaw;
- if (cameraInfo.mediaSrc) {
- this.reply.mediaSrc = cameraInfo.mediaSrc;
- this.reply.isIDR = cameraInfo.isIDR;
- }
- this.sendingFrameForJoystick = true;
- }
- //console.log('handlejoysticktesttest:actionRequest-镜头过渡'+new Date().getTime());
- console.log(
- 'handlejoysticktesttest:actionRequest-镜头过渡:从' +
- breakPointId +
- '到' +
- chooseBreakPointId +
- ',' +
- JSON.stringify(user.player.position),
- );
- return this.reply;
- }
- }
- setCameraInfo(appId, moveFrames, startBreakPointId, endBreakPointId) {
- for (let i = 0; i < moveFrames.length; i += moveInterval) {
- moveFrames[i].endBreakPointId = endBreakPointId;
- moveFrames[i].mediaSrc =
- '/' +
- appId +
- '/' +
- startBreakPointId +
- '/' +
- moveFrames[i].file_name.substring(
- 0,
- moveFrames[i].file_name.indexOf('.'),
- ) +
- '/' +
- moveFrames[i].file_name +
- '?m=' +
- new Date().getTime();
- if (startBreakPointId > endBreakPointId) {
- moveFrames[i].mediaSrc =
- '/' +
- appId +
- '/' +
- endBreakPointId +
- '/' +
- moveFrames[i].file_name.substring(
- 0,
- moveFrames[i].file_name.indexOf('.'),
- ) +
- '/' +
- moveFrames[i].file_name +
- '?m=' +
- new Date().getTime();
- }
- if (i == 0 || i == moveFrames.length - 1) {
- moveFrames[i].isIDR = true;
- } else {
- moveFrames[i].isIDR = false;
- }
- this.cameraInfos.push(moveFrames[i]);
- }
- }
- addCameraInfo(cameraPosition, cameraAngle, mediaSrc, isIDR) {
- this.cameraInfos.push({
- camera_position: cameraPosition,
- camera_angle: cameraAngle,
- mediaSrc: mediaSrc,
- isIDR: isIDR,
- });
- }
- getCameraInfo() {
- if (this.cameraInfos.length > 0) {
- const item = this.cameraInfos.shift();
- return item;
- } else {
- return null;
- }
- }
- complementFrame(userId) {
- if (this.cameraInfos.length > 0) {
- console.log('20220627test-执行complementFrame,' + new Date().getTime());
- const user = this.users[userId];
- const cameraInfo = this.cameraInfos.shift();
- this.reply.traceIds = [];
- this.reply['newUserStates'][0].userId = userId;
- this.reply['actionResponses'][0].traceId = null;
- this.reply['newUserStates'][0].playerState.player.angle.yaw =
- user.player.angle.yaw;
- this.reply['newUserStates'][0].playerState.player.position = JSON.parse(
- JSON.stringify(user.player.position),
- );
- this.reply['newUserStates'][0].playerState.camera.angle = JSON.parse(
- JSON.stringify(cameraInfo.camera_angle),
- );
- this.reply['newUserStates'][0].playerState.camera.position = JSON.parse(
- JSON.stringify(cameraInfo.camera_position),
- );
- this.reply['newUserStates'][0].playerState.cameraCenter = JSON.parse(
- JSON.stringify(user.player.position),
- );
- this.reply['newUserStates'][0].renderInfo.isMoving = 0;
- this.reply['actionResponses'][0].traceId = null;
- if (cameraInfo.mediaSrc) {
- this.reply.mediaSrc = cameraInfo.mediaSrc;
- this.reply.isIDR = cameraInfo.isIDR;
- }
- user.camera.position = JSON.parse(
- JSON.stringify(cameraInfo.camera_position),
- );
- user.camera.angle = JSON.parse(JSON.stringify(cameraInfo.camera_angle));
- this.sendingFrameForJoystick = true;
- return this.reply;
- } else if (this.cameraInfos.length == 0) {
- if (this.reply['newUserStates'][0].renderInfo.isMoving == 1) {
- //console.log('handlejoysticktesttest:complementFrame-2 停止1'+new Date().getTime())
- return this.stopJoystick(userId);
- } else {
- //console.log('handlejoysticktesttest:complementFrame-2 停止2'+new Date().getTime())
- return null;
- }
- }
- }
- stopJoystick(userId) {
- console.log('20220627test:stopJoystick');
- this.reply.traceIds = [];
- const user = this.users[userId];
- this.reply['newUserStates'][0].userId = userId;
- this.reply['actionResponses'][0].traceId = null;
- this.reply['newUserStates'][0].playerState.player.angle.yaw =
- user.player.angle.yaw;
- this.reply['newUserStates'][0].playerState.player.position = JSON.parse(
- JSON.stringify(user.player.position),
- );
- this.reply['newUserStates'][0].playerState.camera.angle = JSON.parse(
- JSON.stringify(user.camera.angle),
- );
- this.reply['newUserStates'][0].playerState.camera.position = JSON.parse(
- JSON.stringify(user.camera.position),
- );
- this.reply['newUserStates'][0].playerState.cameraCenter = JSON.parse(
- JSON.stringify(user.player.position),
- );
- this.reply['newUserStates'][0].renderInfo.isMoving = 0;
- this.reply['actionResponses'][0].traceId = null;
- this.reply.mediaSrc = null;
- return this.reply;
- }
- async modeifyCameraAngle(angle, userId, traceId, actionType) {
- const checkReplys = [];
- if (angle > 22) {
- angle = 45 - angle;
- for (let i = 0; i < angle; ++i) {
- // console.warn('矫正一次:' + i);
- const reply = await this.rotateService.rotateForAngle(userId, 1);
- // console.warn(
- // '矫正:' + reply.newUserStates[0].playerState.camera.angle.yaw,
- // );
- reply.traceIds = [];
- reply.traceIds.push(traceId);
- const actionResponse = this.rotateService.createActionResponse(
- actionType,
- traceId,
- );
- reply.actionResponses = [];
- reply.actionResponses.push(actionResponse);
- checkReplys.push(reply);
- }
- } else if (angle != 0) {
- for (let i = angle; i > -1; --i) {
- // console.warn('矫正一次:' + i);
- const reply = await this.rotateService.rotateForAngle(userId, -1);
- // console.warn(
- // '矫正:' + reply.newUserStates[0].playerState.camera.angle.yaw,
- // );
- reply.traceIds = [];
- reply.traceIds.push(traceId);
- const actionResponse = this.rotateService.createActionResponse(
- actionType,
- traceId,
- );
- reply.actionResponses = [];
- reply.actionResponses.push(actionResponse);
- checkReplys.push(reply);
- }
- }
- return checkReplys;
- }
- getTarget(position, position1, position2) {
- const line = this.createLine1(position1, position2);
- const join = this.getJoinLinePoint(position, line);
- // if (this.isContainForSegment(join, position1, position2)) {
- // return join;
- // } else {
- // return null;
- // }
- return join;
- }
- getDistance(position1, position2) {
- return Math.sqrt(
- (position1.x - position2.x) * (position1.x - position2.x) +
- (position1.y - position2.y) * (position1.y - position2.y),
- );
- }
- isContainForSegment(point, startPoint, endPoint) {
- const minDis = 0.1;
- const dis1 =
- this.getDistance(startPoint, point) + this.getDistance(endPoint, point);
- const dis2 = this.getDistance(startPoint, endPoint);
- if (Math.abs(dis1 - dis2) < minDis) {
- return true;
- } else {
- return false;
- }
- }
- //两条线的交点
- getIntersectionPoint(parameter1, parameter2) {
- if (this.isParallel(parameter1, parameter2)) {
- return null;
- }
- if (
- typeof parameter1.a == 'undefined' &&
- typeof parameter2.a != 'undefined'
- ) {
- if (parameter1.x) {
- return {
- x: parameter1.x,
- y: parameter2.a * parameter1.x + parameter2.b,
- };
- } else if (parameter1.y) {
- return {
- x: (parameter1.y - parameter2.b) / parameter2.a,
- y: parameter1.y,
- };
- }
- } else if (
- typeof parameter2.a == 'undefined' &&
- typeof parameter1.a != 'undefined'
- ) {
- if (parameter2.x) {
- return {
- x: parameter2.x,
- y: parameter1.a * parameter2.x + parameter1.b,
- };
- } else if (parameter2.y) {
- return {
- x: (parameter2.y - parameter1.b) / parameter1.a,
- y: parameter2.y,
- };
- }
- } else if (
- typeof parameter2.a == 'undefined' &&
- typeof parameter1.a == 'undefined'
- ) {
- if (parameter1.hasOwnProperty('x') && parameter2.hasOwnProperty('y')) {
- return { x: parameter1.x, y: parameter2.y };
- } else if (
- parameter1.hasOwnProperty('y') &&
- parameter2.hasOwnProperty('x')
- ) {
- return { x: parameter2.x, y: parameter1.y };
- } else {
- return null;
- }
- }
- if (parameter1.a == parameter2.a) {
- return null;
- }
- const joinpointx =
- (parameter2.b - parameter1.b) / (parameter1.a - parameter2.a);
- const joinpointy =
- (parameter1.a * parameter2.b - parameter2.a * parameter1.b) /
- (parameter1.a - parameter2.a);
- const point = { x: joinpointx, y: joinpointy };
- return point;
- }
- // 垂直线
- getVerticalLine(line, point) {
- if (typeof line.a === 'undefined') {
- if (line.hasOwnProperty('x')) {
- return { y: point.y };
- } else if (line.hasOwnProperty('y')) {
- return { x: point.x };
- } else {
- return null;
- }
- } else if (line.a == 0) {
- return { x: point.x };
- } else {
- const tl = {} as any as VerticalLineType;
- tl.a = -1 / line.a;
- const result = this.createLine2(tl, point);
- return result;
- }
- }
- // 经过point且与line垂直的直线,该直线与line的交点
- getJoinLinePoint(point, line) {
- const verticalLine = this.getVerticalLine(line, point);
- const join = this.getIntersectionPoint(line, verticalLine);
- return join;
- }
- // 与lineA平行并且point在线上
- createLine2(lineA, point) {
- const parameter = {} as any as {
- x: number;
- y: number;
- a: number;
- b: number;
- };
- if (typeof lineA.a === 'undefined') {
- if (typeof lineA.x !== 'undefined') {
- parameter.x = point.x;
- } else if (typeof lineA.y !== 'undefined') {
- parameter.y = point.y;
- }
- } else {
- parameter.a = lineA.a;
- parameter.b = point.y - point.x * lineA.a;
- }
- return parameter;
- }
- createLine1(point1, point2) {
- if (point1.x == point2.x && point1.y == point2.y) {
- return null;
- } else if (this.getFixed(Math.abs(point1.x - point2.x)) == 0) {
- return { x: point1.x };
- } else if (this.getFixed(Math.abs(point1.y - point2.y)) == 0) {
- return { y: point1.y };
- }
- const parametera = (point1.y - point2.y) / (point1.x - point2.x);
- const parameterb =
- (point1.x * point2.y - point2.x * point1.y) / (point1.x - point2.x);
- if (this.getFixed(parametera) == 0) {
- return { y: this.getFixed(parameterb) };
- }
- const parameter = {
- a: this.getFixed(parametera),
- b: this.getFixed(parameterb),
- };
- return parameter;
- }
- //返回true表示平行
- isParallel(line1, line2) {
- if (typeof line1.a == 'undefined' && typeof line2.a == 'undefined') {
- if (line1.hasOwnProperty('x') && line2.hasOwnProperty('x')) {
- return true;
- } else if (line1.hasOwnProperty('y') && line2.hasOwnProperty('y')) {
- return true;
- } else {
- return false;
- }
- } else if (typeof line1.a == 'undefined' || typeof line2.a == 'undefined') {
- return false;
- } else if (this.getFixed(line1.a) == this.getFixed(line2.a)) {
- return true;
- } else {
- return false;
- }
- }
- getFixed(num) {
- const decimal = 2;
- return parseFloat(num.toFixed(decimal));
- }
- isPointInPoly(position, points) {
- const x = position.x;
- const y = position.y;
- let inside = false;
- for (let i = 0, j = points.length - 1; i < points.length; j = i++) {
- const pt1 = points[i];
- const pt2 = points[j];
- const xi = pt1.x;
- const yi = pt1.y;
- const xj = pt2.x;
- const yj = pt2.y;
- const intersect =
- yi > y != yj > y && x < ((xj - xi) * (y - yi)) / (yj - yi) + xi;
- if (intersect) inside = !inside;
- }
- return inside;
- }
- getMoveIndex(angle) {
- if (angle < 0) {
- angle = 360 + angle;
- }
- for (let i = 0; i < 8; ++i) {
- if (angle < 45 * i + 45 / 2) {
- return i;
- }
- }
- //超过了337
- return 0;
- }
- getOffsetAngle(angle1, angle2) {
- if (angle1 < 0) {
- angle1 = 360 + angle1;
- //return Math.min(Math.abs(angle1-angle2),Math.abs());
- }
- if (angle2 < 0) {
- angle2 = 360 + angle2;
- }
- return Math.min(Math.abs(angle1 - angle2), 360 - Math.abs(angle1 - angle2));
- }
- }
|