|
@@ -10,6 +10,7 @@ export class MoveService {
|
|
|
Joystick: 15,
|
|
|
};
|
|
|
private users = {};
|
|
|
+
|
|
|
private reply = {
|
|
|
traceIds: [],
|
|
|
vehicle: null,
|
|
@@ -75,124 +76,312 @@ export class MoveService {
|
|
|
],
|
|
|
actionResponses: [
|
|
|
{
|
|
|
- "actionType": 1,
|
|
|
- "pointType": 100,
|
|
|
- "extra": "",
|
|
|
- "traceId": "",
|
|
|
- "packetId": "",
|
|
|
- "nps": [],
|
|
|
- "peopleNum": 0,
|
|
|
- "zoneId": "",
|
|
|
- "echoMsg": "",
|
|
|
- "reserveDetail": null,
|
|
|
- "userWithAvatarList": [],
|
|
|
- "newUserStates": [],
|
|
|
- "code": 0,
|
|
|
- "msg": ""
|
|
|
- }
|
|
|
+ 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',
|
|
|
};
|
|
|
|
|
|
+ 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,
|
|
|
+ mediaSrc: null,
|
|
|
+ },
|
|
|
+ moveInfo: {},
|
|
|
+ // traceIds: [],
|
|
|
+ // actionResponses:[]
|
|
|
+ };
|
|
|
+
|
|
|
+ user.appId = app_id;
|
|
|
+ user.userId = userId;
|
|
|
+ user.breakPointId = 100;
|
|
|
+ this.users[userId] = user;
|
|
|
+ }
|
|
|
+
|
|
|
async move(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 = [100,99,97]; //需要计算路径
|
|
|
- const angle = user.camera.angle.yaw%45; //纠正需要
|
|
|
- const index = Math.round(user.camera.angle.yaw/45); //过渡需要
|
|
|
+ const path = [100, 101, 102]; //需要计算路径
|
|
|
+ const angle = user.camera.angle.yaw % 45; //纠正需要
|
|
|
+ const index = Math.round(user.camera.angle.yaw / 45); //过渡需要
|
|
|
|
|
|
- let replys = [];
|
|
|
- let traceIds = [];
|
|
|
- traceIds.push(traceId);
|
|
|
+ const replys = {};
|
|
|
+ const traceIds = [];
|
|
|
+ traceIds.push(traceId);
|
|
|
|
|
|
//纠正,旋转传到缓存里
|
|
|
- let checkReplys = [];
|
|
|
- for(let i=0;i<angle;++i){
|
|
|
- //let reply = rotateService.rotateForAngle(userId,(angle+i)%359);
|
|
|
- //reply.traceIds = [];
|
|
|
- //reply.traceIds.push(traceId);
|
|
|
- //let actionResponse = rotateService.createActionResponse(actionType,trace_id);
|
|
|
- //reply.actionResponses = [];
|
|
|
- //reply.actionResponses.push(actionResponse);
|
|
|
- //checkReplys.push(reply);
|
|
|
+ const checkReplys = [];
|
|
|
+ for (let i = 0; i < angle; ++i) {
|
|
|
+ //let reply = rotateService.rotateForAngle(userId,(angle+i)%359);
|
|
|
+ //reply.traceIds = [];
|
|
|
+ //reply.traceIds.push(traceId);
|
|
|
+ //let actionResponse = rotateService.createActionResponse(actionType,trace_id);
|
|
|
+ //reply.actionResponses = [];
|
|
|
+ //reply.actionResponses.push(actionResponse);
|
|
|
+ //checkReplys.push(reply);
|
|
|
}
|
|
|
- replys.push(checkReplys);
|
|
|
+ replys['P' + user.breakPointId + 'T' + user.breakPointId] = checkReplys;
|
|
|
+ //replys.push(checkReplys);
|
|
|
|
|
|
//过渡传到缓存里
|
|
|
this.reply.traceIds = traceIds;
|
|
|
this.reply['newUserStates'][0].userId = userId;
|
|
|
this.reply['actionResponses'][0].traceId = traceId;
|
|
|
-
|
|
|
- for(let i=0;i<path.length-1;++i){
|
|
|
+
|
|
|
+ for (let i = 0; i < path.length - 1; ++i) {
|
|
|
let pathReplys = [];
|
|
|
- let start_break_point_id = path[i];
|
|
|
- let end_break_point_id = path[i+1];
|
|
|
+ const start_break_point_id = path[i];
|
|
|
+ const end_break_point_id = path[i + 1];
|
|
|
//读redis里的数据,按照frame_index的大小排序
|
|
|
- let key = 'moveframe:app_id:'+appId+':start_break_point_id:'+start_break_point_id+':end_break_point_id:'+end_break_point_id+':angle:'+index;
|
|
|
- let moveFrames = await this.cacheService.get(key);
|
|
|
- if(moveFrames == null){
|
|
|
+ const key =
|
|
|
+ 'moveframe:app_id:' +
|
|
|
+ appId +
|
|
|
+ ':start_break_point_id:' +
|
|
|
+ start_break_point_id +
|
|
|
+ ':end_break_point_id:' +
|
|
|
+ end_break_point_id +
|
|
|
+ ':angle:' +
|
|
|
+ index;
|
|
|
+ const moveFramesRes = await this.cacheService.get(key);
|
|
|
+ if (moveFramesRes == null) {
|
|
|
return replys;
|
|
|
}
|
|
|
-
|
|
|
+ const moveFrames = JSON.parse(moveFramesRes);
|
|
|
//读redis里的数据
|
|
|
- const startBreakPoint = await this.cacheService.get('breakpoints:app_id:'+appId+':break_point_id:'+start_break_point_id);
|
|
|
- const endBreakPoint = await this.cacheService.get('breakpoints:app_id:'+appId+':break_point_id:'+end_break_point_id);
|
|
|
-
|
|
|
- pathReplys = this.createCacheReplys(moveFrames,traceId,userId,startBreakPoint.position,endBreakPoint.position);
|
|
|
- replys.push(pathReplys);
|
|
|
- }
|
|
|
+ const startBreakPointRes = await this.cacheService.get(
|
|
|
+ 'breakpoints:app_id:' +
|
|
|
+ appId +
|
|
|
+ ':break_point_id:' +
|
|
|
+ start_break_point_id,
|
|
|
+ );
|
|
|
|
|
|
+ const startBreakPoint = JSON.parse(startBreakPointRes);
|
|
|
+
|
|
|
+ const endBreakPointRes = await this.cacheService.get(
|
|
|
+ 'breakpoints:app_id:' +
|
|
|
+ appId +
|
|
|
+ ':break_point_id:' +
|
|
|
+ end_break_point_id,
|
|
|
+ );
|
|
|
+ const endBreakPoint = JSON.parse(endBreakPointRes);
|
|
|
+ pathReplys = this.createCacheReplys(
|
|
|
+ moveFrames,
|
|
|
+ traceId,
|
|
|
+ userId,
|
|
|
+ start_break_point_id,
|
|
|
+ startBreakPoint.position,
|
|
|
+ endBreakPoint.position,
|
|
|
+ );
|
|
|
+ //replys.push(pathReplys);
|
|
|
+ replys['P' + start_break_point_id + 'T' + end_break_point_id] =
|
|
|
+ pathReplys;
|
|
|
+ }
|
|
|
return replys;
|
|
|
-
|
|
|
} catch (error) {
|
|
|
console.log('MoveService', error);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- createCacheReplys(moveFrames,traceId,userId,startPosition,endPosition){
|
|
|
+ createCacheReplys(
|
|
|
+ moveFrames,
|
|
|
+ traceId,
|
|
|
+ userId,
|
|
|
+ breakPointId,
|
|
|
+ startPosition,
|
|
|
+ endPosition,
|
|
|
+ ) {
|
|
|
+ const replys = [];
|
|
|
|
|
|
- let replys = [];
|
|
|
- for(let i = 1;i<moveFrames.length;++i){
|
|
|
- let moveFrame = moveFrames[i];
|
|
|
+ const angle =
|
|
|
+ -1 *
|
|
|
+ this.getAngle(
|
|
|
+ startPosition,
|
|
|
+ {
|
|
|
+ x: startPosition.x + 1,
|
|
|
+ y: startPosition.y,
|
|
|
+ },
|
|
|
+ endPosition,
|
|
|
+ );
|
|
|
|
|
|
- let reply = JSON.parse(JSON.stringify(this.reply));
|
|
|
- reply.traceIds.push(traceId);
|
|
|
- reply['newUserStates'][0].userId = userId;
|
|
|
-
|
|
|
- reply['newUserStates'][0].playerState.player.position = {
|
|
|
- x:(endPosition.x - startPosition.x)/moveFrames.length*i,
|
|
|
- y:(endPosition.y - startPosition.y)/moveFrames.length*i,
|
|
|
- z:(endPosition.z - startPosition.z)/moveFrames.length*i
|
|
|
- }
|
|
|
- reply['newUserStates'][0].playerState.player.angle = this.users[userId].player.angle;
|
|
|
-
|
|
|
- reply['newUserStates'][0].playerState.camera.position = moveFrame.camera_position;
|
|
|
- reply['newUserStates'][0].playerState.camera.angle = moveFrame.camera_angle;
|
|
|
- reply['newUserStates'][0].playerState.cameraCenter = reply['newUserStates'][0].playerState.player.position;
|
|
|
-
|
|
|
- reply['actionResponses'][0].traceId = traceId;
|
|
|
- replys.push(reply);
|
|
|
+ for (let i = 1; i < moveFrames.length; i += 5) {
|
|
|
+ const moveFrame = moveFrames[i];
|
|
|
+
|
|
|
+ const reply = JSON.parse(JSON.stringify(this.reply));
|
|
|
+ reply.traceIds.push(traceId);
|
|
|
+ reply['newUserStates'][0].userId = userId;
|
|
|
+
|
|
|
+ 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.camera.position =
|
|
|
+ moveFrame.camera_position;
|
|
|
+ reply['newUserStates'][0].playerState.camera.angle =
|
|
|
+ moveFrame.camera_angle;
|
|
|
+ reply['newUserStates'][0].playerState.cameraCenter =
|
|
|
+ reply['newUserStates'][0].playerState.player.position;
|
|
|
+
|
|
|
+ reply['actionResponses'][0].traceId = traceId;
|
|
|
+
|
|
|
+ reply.mediaSrc =
|
|
|
+ '/' +
|
|
|
+ '0000000001' +
|
|
|
+ '/' +
|
|
|
+ breakPointId +
|
|
|
+ '/' +
|
|
|
+ moveFrame.file_name.substring(0, moveFrame.file_name.indexOf('.')) +
|
|
|
+ '/' +
|
|
|
+ moveFrame.file_name +
|
|
|
+ '?m=' +
|
|
|
+ new Date().getTime();
|
|
|
+
|
|
|
+ replys.push(reply);
|
|
|
}
|
|
|
return replys;
|
|
|
-
|
|
|
}
|
|
|
|
|
|
//需要通知user,人物和相机走到哪一个呼吸点位了
|
|
|
- updateUser(userId,breakPointId,lastReply){
|
|
|
- let user = this.users[userId];
|
|
|
+ updateUser(userId, breakPointId, lastReply) {
|
|
|
+ const user = this.users[userId];
|
|
|
user.breakPointId = breakPointId;
|
|
|
|
|
|
- user.player.position = lastReply['newUserStates'][0].playerState.player.position;
|
|
|
+ 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.position =
|
|
|
+ lastReply['newUserStates'][0].playerState.camera.position;
|
|
|
user.camera.angle = lastReply['newUserStates'][0].playerState.camera.angle;
|
|
|
}
|
|
|
+
|
|
|
+ async 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 breakPoints = [];
|
|
|
+ for (let i = 0; i < breakPoints.length; ++i) {
|
|
|
+ const position = breakPoints[i].position;
|
|
|
+ reply['nps'].push(position);
|
|
|
+ }
|
|
|
+ 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 user = this.users[userId];
|
|
|
+ user.breakPointId = breakPointId;
|
|
|
+ const appId = user.appId;
|
|
|
+
|
|
|
+ const breakPointRes = await this.cacheService.get('breakpoints:app_id:' +appId +':break_point_id:' +breakPointId);
|
|
|
+ const breakPoint = JSON.parse(breakPointRes);
|
|
|
+
|
|
|
+ user.player.position = breakPoint.position;
|
|
|
+ user.player.angle = playerAngle;
|
|
|
+
|
|
|
+ const rotateDataRes ='rotateframe:app_id:' +appId +':frame_index:' + cameraAngle.yaw +':break_point_id:' +breakPointId;
|
|
|
+ const rotateData = await this.cacheService.get(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 ='/' +'0000000001' +'/' +breakPointId +'/' +rotateData.directory +'/' +rotateData.fileName +'?m=' +new Date().getTime();
|
|
|
+ return reply;
|
|
|
+ }
|
|
|
}
|