private FightRoomDTO fightRoomDTO = null;//缓存战斗房间信息 /// <summary> /// 处理网络消息 /// </summary> /// <param name="sm"></param> public override void OnMessageReceived(SocketModel sm) { switch (sm.command) { case FightProtocol.START_BRO: fightRoomDTO = sm.GetMessage <FightRoomDTO>(); CreateBattelField(fightRoomDTO); Debug.LogError("收到创建战场信息"); break; case FightProtocol.ATK_TO_POS_SCMD: PosDTO atkToPosDTO = sm.GetMessage <PosDTO>(); MgrCenter.Instance.SendMsg(Msgs.GetMsgPos((ushort)SoilderFightEvent.SoilderAtkToPosServerCmd, atkToPosDTO)); break; case FightProtocol.MOVE_TARGET_SRES: PosDTO moveToTargetDTO = sm.GetMessage <PosDTO>(); MgrCenter.Instance.SendMsg(Msgs.GetMsgPos((ushort)MoveEvent.MoveServerRes, moveToTargetDTO)); break; case FightProtocol.SKILL_UP_SRES: break; case FightProtocol.POS_SYNC_BRO: PosSyncDTO posSyncDTO = sm.GetMessage <PosSyncDTO>(); //得到的PosSync是服务器处理时认可的服务器时间 //这里将它转化成client认可的时间 //floating = client - server //client = server + floating long timeStampClient = GetTimeStampClient(posSyncDTO.timeStamp); MgrCenter.Instance.SendMsg(Msgs.GetMsgPosSync((ushort)MoveEvent.PosSync, posSyncDTO.instanceId, new Vector3(posSyncDTO.x, posSyncDTO.y, posSyncDTO.z), new Vector3(posSyncDTO.dirX, posSyncDTO.dirY, posSyncDTO.dirZ), timeStampClient)); break; case FightProtocol.SKILL_BRO: break; case FightProtocol.ATTACK_BRO: break; case FightProtocol.DAMAGE_BRO: Damage(sm.GetMessage <DamageDTO>()); break; case FightProtocol.IDLE_BRO: int idleEntityId = sm.GetMessage <int>(); MgrCenter.Instance.SendMsg(Msgs.GetMsgInt((ushort)SoilderFightEvent.SoilderIdleServerAllow, idleEntityId)); break; } }
/// <summary> /// 根据预测进行移动 /// </summary> /// <param name="token"></param> /// <param name="moveDTO"></param> private void Move(UserToken token, PosSyncDTO moveDTO) { DelayAndFloating delayAndFloating = userBiz.GetDelayAndFloating(token); //time: floating = server - client //server = floating + client long time = delayAndFloating.floating + moveDTO.timeStamp;//将client的发送时间,转换为server时间 float timeBtwClientSendAndNow = (DateTime.Now.Ticks - time) * 0.0000001f; float speed = instances[moveDTO.instanceId].speed; if (timeBtwClientSendAndNow < 0) { Console.WriteLine("time floating is negative : " + timeBtwClientSendAndNow.ToString()); DelayCheckDTO delayCheckDTO = new DelayCheckDTO(5); delayCheckDTO.timeStamps.Add(DateTime.Now.Ticks); Send(token, Protocol.Protocol.TYPE_TIME, area, TimeProtocol.CHECK_SREQ, delayCheckDTO); } else { Console.WriteLine("time floating is : " + timeBtwClientSendAndNow.ToString()); //TODO 如果位置与之前存储的位置相差太大,就发送位置校正消息 Vector3 posNow = GetFuturePos(new Vector3(moveDTO.x, moveDTO.y, moveDTO.z), new Vector3(moveDTO.dirX, moveDTO.dirY, moveDTO.dirZ), timeBtwClientSendAndNow, speed); if (!instancePos.ContainsKey(moveDTO.instanceId)) { instancePos.Add(moveDTO.instanceId, new EntityMoveInfo(posNow, true)); } else { instancePos[moveDTO.instanceId].cVector3.x = posNow.x; instancePos[moveDTO.instanceId].cVector3.y = posNow.y; instancePos[moveDTO.instanceId].cVector3.z = posNow.z; instancePos[moveDTO.instanceId].moveStatus = MoveStatus.Moving; } PosSyncDTO posSyncDTO = new PosSyncDTO(moveDTO.instanceId, posNow.x, posNow.y, posNow.z, moveDTO.dirX, moveDTO.dirY, moveDTO.dirZ, DateTime.Now.Ticks); brocast(FightProtocol.POS_SYNC_BRO, posSyncDTO, token); } }