Ejemplo n.º 1
0
        public void SetMoveValueByNet() //9 从服务器器获取运动和开火状态并设置
        {
            while (this._gameing)       //正在游戏中
            //读包头
            {
                var buf = new byte[1024];
                int n1  = this._socket.Receive(buf, 12, 0);
                if (n1 != 12)
                {
                    Debug.LogError("消息长度出错");
                    continue;
                }
                string headmsg = Encoding.UTF8.GetString(buf, 0, n1);
                //Debug.LogWarning(headmsg);
                MsgHead head = JsonUtility.FromJson <MsgHead>(headmsg);

                int n2 = this._socket.Receive(buf, head.size, 0);
                if (n2 != head.size)
                {
                    Debug.LogError("消息长度出错");
                    continue;
                }
                string data = Encoding.UTF8.GetString(buf, 0, n2);
                //Debug.LogWarning(data);

                PlayerMsg move = JsonUtility.FromJson <PlayerMsg>(data);
                if (move.order == MOVEOrder.MOVE_X || move.order == MOVEOrder.MOVE_Y)//如果是移动指令
                {
                    FrameList.Enqueue(move);
                }
                else if (move.order == MOVEOrder.Fire)//如果是开火指令
                {
                    //开火同步函数..没写..
                }
            }
        }
Ejemplo n.º 2
0
        public void Operate()  //10. 进行移动操作
        //1. 修正数值
        {
            t.text = " ";

            if (FrameList.Count != 0)
            {
                Server_id++;
                X = My - Cline_id; // X表示上次状态运行了几帧
                int flag = Cline_id;
                Cline_id = My;     //Cline_i 表示客户端在第几帧帧运动状态改变的
                PlayerMsg move = FrameList.Dequeue();
                Y = move.num;
                int Z = Y - X;
                if (Z != 0)//补帧
                {
                    buzhen++;
                    foreach (long ID in config.name_Map.Keys)
                    {
                        GameObject player = GameObject.Find(config.Get_Name_by_id(ID));
                        if (player == null)
                        {
                            Debug.LogError("this.GameObject错误");
                            return;
                        }

                        //获取移动数值
                        float _Q = config.Get_xy_by_id(ID).x;
                        float _V = config.Get_xy_by_id(ID).y;

                        if (_Q == 0 && _V == 0)
                        {
                            continue;
                        }


                        Quaternion old_Q  = player.transform.rotation;
                        Vector3    oldpos = player.transform.position;

                        if (Z < 0)
                        {
                            _Q = -_Q;
                            _V = -_V;
                            Z  = -Z;

                            //移动
                            if (_V != 0)
                            {
                                Vector3 tarPos = oldpos + player.transform.forward * _V;
                                player.transform.position = Vector3.Lerp(oldpos, tarPos, Time.deltaTime * Z * 5.0f);
                            }

                            //旋转
                            if (_Q != 0)
                            {
                                Quaternion targetAngels = Quaternion.Euler(0, player.transform.localEulerAngles.y + _Q * 90, 0);
                                player.transform.rotation = Quaternion.Slerp(old_Q, targetAngels, Time.deltaTime * Z);
                            }
                        }
                        else
                        {
                            //旋转
                            if (_Q != 0)
                            {
                                Quaternion targetAngels = Quaternion.Euler(0, player.transform.localEulerAngles.y + _Q * 90, 0);
                                player.transform.rotation = Quaternion.Slerp(old_Q, targetAngels, Time.deltaTime * Z);
                            }
                            //移动
                            if (_V != 0)
                            {
                                Vector3 tarPos = oldpos + player.transform.forward * _V;
                                player.transform.position = Vector3.Lerp(oldpos, tarPos, Time.deltaTime * Z * 5.0f);
                            }
                        }

                        Debug.LogError("补帧: " + "Z: " + Z + " _V: " + _V + " _Q: " + _Q);
                        Debug.LogError("信息1: " + "服务器当前帧数:" + Move.my + " 客户端当前帧数: " + My);
                        Debug.LogError("信息2: " + "服务器改变状态帧数:" + Move.F + " 客户端改变状态帧数: " + Cline_id + "服务器上次:" + Move.o + " 客户端上次: " + flag);
                        Debug.LogError("信息3: " + "服务器状态维持帧数:" + move.num + " 客户端状态维持帧数: " + X);
                        Debug.LogError(" old_V: " + oldpos.x + " " + oldpos.y + " " + oldpos.z);

                        Debug.LogError(" old_Q: " + old_Q.x + " " + old_Q.y + " " + old_Q.z + " " + old_Q.w);
                        Debug.LogError(" new_Q: " + player.transform.rotation.x + " " + player.transform.rotation.y + " " + player.transform.rotation.z + " " + player.transform.rotation.w);
                    }
                }

                if (move.order == MOVEOrder.SITE)
                {
                    config.xyz_Set(move.ID, move.V, move.Q);
                }
                else
                {
                    config.Set_xy(move.ID, move.q, move.v);
                }
            }

            if (this._gameing)//初始化已完成,正在游戏中
            //2. 进行移动
            {
                foreach (long ID in config.name_Map.Keys)
                {
                    GameObject player = GameObject.Find(config.Get_Name_by_id(ID));
                    if (player == null)
                    {
                        Debug.LogError("this.GameObject错误");
                        return;
                    }

                    //获取移动数值
                    float _Q = config.Get_xy_by_id(ID).x;
                    float _V = config.Get_xy_by_id(ID).y;
                    t.text += "\n" + "移动数值修改:  " + "_Q: " + _Q + "  " + "_V: " + _V;

                    //旋转
                    if (_Q != 0)
                    {
                        Quaternion targetAngels = Quaternion.Euler(0, player.transform.localEulerAngles.y + _Q * 90.0F, 0);
                        player.transform.rotation = Quaternion.Slerp(player.transform.rotation, targetAngels, Time.deltaTime);
                    }

                    //移动
                    if (_V != 0)
                    {
                        Vector3 oldpos = player.transform.position;
                        Vector3 tarPos = oldpos + player.transform.forward * _V;
                        player.transform.position = Vector3.Lerp(oldpos, tarPos, Time.deltaTime * 5.0f);
                    }
                    t.text += "\n" + ID + ": " + player.transform.position.x + " , " + player.transform.position.y + " , " + player.transform.position.z;
                    t.text += "---" + player.transform.rotation.x + " , " + player.transform.rotation.y + " , " + player.transform.rotation.z + " , " + player.transform.rotation.w;
                }

                t.text += "\n" + "当前是服务器第 " + Server_id + " 帧 " + " 客户端更新状态在第 " + Cline_id + " 帧 ";

                t.text += "\n" + "客户端上次状态维持了 " + X + " 帧 " + " 服务器上次状态维持了 " + Y + " 帧";
                t.text += "\n" + "补帧次数 " + buzhen;
                My++;
            }
        }