Beispiel #1
0
 public void MySyncVar(HandlePacket.Packet pkt)
 {
     HandlePacket.coord kk = pkt.State.state_base.pos;
     if (!isServer)
     {
         if (pkt.State.state_base.name.ToString() != hp.myname.ToString())
         {
             if (isYanni)
             {
                 heidi.transform.position = new Vector3(Convert.ToSingle(kk.x), Convert.ToSingle(kk.y), Convert.ToSingle(kk.z));
             }
             if (isHeidi)
             {
                 yanni.transform.position = new Vector3(Convert.ToSingle(kk.x), Convert.ToSingle(kk.y), Convert.ToSingle(kk.z));
             }
         }
     }
     else
     {
         //DisplayPacket(pkt.C, pkt.State);
         if (pkt.State.state_base.name.ToString() == hp.myname.ToString())
         {
             yanni.transform.position = new Vector3(Convert.ToSingle(kk.x), Convert.ToSingle(kk.y), Convert.ToSingle(kk.z));
         }
     }
 }
Beispiel #2
0
 public void Add(HandlePacket.Packet newItem)
 {
     if (queue.Count == this.size)
     {
         queue.Dequeue();
     }
     queue.Enqueue(newItem);
 }
Beispiel #3
0
        void Update()
        {
            if (Time.deltaTime < 0.014)
            {
                m0++;
            }
            else if (Time.deltaTime < 0.022)
            {
                m1++;
            }
            else if (Time.deltaTime > 0.03)
            {
                m2++;
            }

            for (UInt32 i = 1; i < 10; i++)
            {
                if ((!IdList.Contains(i)) || i == m_Id)
                {
                    continue;
                }
                else
                {
                    if (MsgQueue[i].queue.Count == 0)
                    {
                        Debug.Log("Using Extrapolation");
                        if (PlayerList[i] == null)
                        {
                            Debug.Log("null");
                        }
                        else
                        {
                            Extrapolation(i);
                        }
                    }
                    else
                    {
                        Debug.Log("Using Received Packets");
                        while (MsgQueue[i].queue.Count != 0)
                        {
                            HandlePacket.Packet reader = MsgQueue[i].Read();
                            if (reader != null)
                            {
                                MySyncVar(reader);
                            }
                        }
                    }
                }
            }

            if ((!isServer) || (isHost))
            {
                buffer = ClientWritePacket(EgoVehicle, counter);
                BroadcastMessage(buffer, ClientList);
                counter++;
            }
        }
Beispiel #4
0
        public void MySyncVar(HandlePacket.Packet pkt)
        {
            UInt32 id = ParseID(pkt);

            if (PlayerList[id] == null)
            {
                GenerateObject(pkt);
            }
            UInt32 _no = pkt.C.frameNo;

            if (_no < maxNo)
            {
                n++;
                Extrapolation(id);
            }
            else
            {
                maxNo = _no;
                Vector3 vel_c = ParseCoord(pkt, 2, true);
                CurrentVelocity[id] = vel_c / 2.23693629f;

                Vector3 vel_a = ParseCoord(pkt, 2, false);

                if (dummyRigid)
                {
                    RigidList[id].velocity        = vel_c / 2.23693629f;
                    RigidList[id].angularVelocity = vel_a;
                }

                Vector3 pos_c = ParseCoord(pkt, 1, true);
                switch (smoothFlag)
                {
                case 0:
                    GeoList[id].transform.position = pos_c;
                    break;

                case 1:
                    GeoList[id].transform.position = Vector3.SmoothDamp(CurrentPosition[id], pos_c, ref vel_c, Time.deltaTime);
                    break;

                case 2:
                    GeoList[id].transform.position = Vector3.Lerp(CurrentPosition[id], pos_c, Time.deltaTime * vel_c.magnitude);
                    break;
                }
                CurrentPosition[id] = GeoList[id].transform.position;
                Debug.Log("Get Position: " + CurrentPosition[id]);

                Vector3 pos_a = ParseCoord(pkt, 1, false);
                GeoList[id].transform.eulerAngles = pos_a;
                CurrentRotation[id] = GeoList[id].transform.eulerAngles;
            }
        }
Beispiel #5
0
        public void GenerateObject(HandlePacket.Packet pkt)
        {
            UInt32  id            = ParseID(pkt);
            Vector3 SpawnLocation = ParseCoord(pkt, 1, true);
            Vector3 SpawnRotation = ParseCoord(pkt, 1, false);

            PlayerList[id]      = Instantiate(myDummy, SpawnLocation, Quaternion.Euler(SpawnRotation.x, SpawnRotation.y, SpawnRotation.z)) as GameObject;
            CurrentPosition[id] = SpawnLocation;
            GeoList[id]         = PlayerList[id].transform.GetChild(4).gameObject;
            if (dummyRigid)
            {
                RigidList[id] = PlayerList[id].GetComponent <Rigidbody>();
            }
        }
Beispiel #6
0
        //void Init()
        //{
        //    //DontDestroyOnLoad(gameObject);
        //}

        void Update()
        {
            while (MsgQueue.queue.Count != 0)
            {
                HandlePacket.Packet reader = MsgQueue.Read();
                if (reader != null)
                {
                    MySyncVar(reader);
                }
            }

            if (!isServer)
            {
                counter++;
                buffer = ClientWritePacket(theGO, counter);
                BroadcastMessage(buffer, ClientList);
            }
        }
Beispiel #7
0
        public void OnReceive(IAsyncResult ar)
        {
            try
            {
                Debug.Log("In");
                buffer = Receiver.EndReceive(ar, ref RefPoint);
                Debug.Log("End Receiving from: " + RefPoint.ToString());

                if (isServer || isHost)
                {
                    // Add clients
                    IPEP = Conversion(RefPoint);
                    AddClient(IPEP);
                    BroadcastMessage(buffer, ClientList);
                }

                HandlePacket.Packet pkt_n = ParsePacket(buffer, lens);
                UInt32 ID = ParseID(pkt_n);
                if (!IdList.Contains(ID))
                {
                    IdList.Add(ID);
                    Debug.Log("New Object");
                }
                MsgQueue[ID].Add(pkt_n);

                // Test Latency on the same laptop
                //if(id == m_Id)
                //{
                //    DateTime time = GetTime(pkt_n);
                //    TimeSpan interval = testTime - time;
                //    Debug.Log("Latency is " + String.Format("{0}.{1}", interval.Seconds, interval.Milliseconds.ToString().PadLeft(3, '0')) + "s");
                //}

                // Start to receive again
                StartToReceive();
            }
            catch (ArgumentException)
            {
            }
        }
Beispiel #8
0
        public Vector3 ParseCoord(HandlePacket.Packet pkt, int flag1, bool flag2)
        {
            // flag1 is the option for "pos"(1) and "speed"(2), flag2 stands for "angular"(0) and "coordinates"(1)
            HandlePacket.coord pos = pkt.State.state_base.pos;
            HandlePacket.coord speed = pkt.State.state_ext.speed;
            Vector3            pos_c, pos_a, speed_c, speed_a = new Vector3();

            pos_c   = new Vector3(-Convert.ToSingle(pos.y), Convert.ToSingle(pos.z), Convert.ToSingle(pos.x));
            pos_a   = new Vector3(Convert.ToSingle(pos.p * 180f / Mathf.PI), Convert.ToSingle(-pos.h * 180f / Mathf.PI), Convert.ToSingle(pos.r * 180f / Mathf.PI));
            speed_c = new Vector3(-Convert.ToSingle(speed.y), Convert.ToSingle(speed.z), Convert.ToSingle(speed.x));
            speed_a = new Vector3(Convert.ToSingle(speed.p), Convert.ToSingle(-speed.h), Convert.ToSingle(speed.r));
            if (flag1 == 1)
            {
                if (flag2)
                {
                    return(pos_c);
                }
                else
                {
                    return(pos_a);
                }
            }
            else if (flag1 == 2)
            {
                if (flag2)
                {
                    return(speed_c);
                }
                else
                {
                    return(speed_a);
                }
            }
            else
            {
                Debug.Log("No such a flag");
                return(new Vector3(0, 0, 0));
            }
        }
Beispiel #9
0
        public void OnReceive(IAsyncResult ar)
        {
            try
            {
                buffer = Receiver.EndReceive(ar, ref RefPoint);
                Debug.Log("End Receiving");
                // Add clients
                IPEP = Conversion(RefPoint);
                AddClient(IPEP);
                if (isServer)
                {
                    BroadcastMessage(buffer, ClientList);
                }

                HandlePacket.Packet pkt_n = ParsePacket(buffer, lens);
                MsgQueue.Add(pkt_n);

                // Start to receive again
                StartToReceive();
            }
            catch (ArgumentException)
            {
            }
        }
Beispiel #10
0
 public HandlePacket.Packet ParsePacket(byte[] stream, int[] lens)
 {
     HandlePacket.Packet pkt   = new HandlePacket.Packet();
     HandlePacket.Packet pkt_n = pkt.Parse(stream, lens);
     return(pkt_n);
 }
Beispiel #11
0
        public UInt32 ParseID(HandlePacket.Packet pkt)
        {
            UInt32 theId = pkt.State.state_base.id;

            return(theId);
        }