예제 #1
0
        private void SendCurrentSnapshot(int to)
        {
            SnapshotMsg snapshot = new SnapshotMsg(0);

            KeyValuePair <int, Snapshot>[] objs = netobjects.ToArray();
            foreach (KeyValuePair <int, Snapshot> obj in objs)
            {
                Debug.Log("HAS MOVED");
                snapshot.objID.Add(obj.Value.objID);
                snapshot.textureType.Add(obj.Value.textureType);
                Vector3 pos = obj.Value.pos;
                snapshot.positions.Add(pos);
                snapshot.angular_speed.Add(Quaternion.identity);
                snapshot.linear_speed.Add(Vector3.zero);

                Quaternion rot = obj.Value.rot;
                snapshot.rotation.Add(rot);

                //snapshot.linear_speed.Add((pos - lastPosition[obj.Value.objID]) / (float)time);
                //snapshot.linear_speed.Add(AngleShift(rot, lastAngle[obj.Value.objID]).eulerAngles / (float)time);


                //lastPosition.Add(obj.Value.objID, pos);
                //lastAngle.Add(obj.Value.objID, rot);
                obj.Value.hasMoved = false;
            }
            if (snapshot.objID.Count > 0)
            {
                KeyValuePair <int, PlayerData>[] players = users.ToArray();
                foreach (KeyValuePair <int, PlayerData> playerData in players)
                {
                    //Debug.Log("StructChange");
                    snapshot.to = playerData.Value.clientData.id;
                    Send(snapshot.GetBytes(), playerData.Value.clientData.id);
                }
            }
        }
예제 #2
0
        private void SendSnapshot()
        {
            Debug.Log("thread started");
            while (true)
            {
                Dictionary <int, Vector3>    lastPosition = new Dictionary <int, Vector3>();    //check if mem leak
                Dictionary <int, Quaternion> lastAngle    = new Dictionary <int, Quaternion>(); //check if mem leak

                DateTime prevTime = DateTime.Now;
                DateTime curTime;
                double   time = 0;
                while (netobjects.Count > 1)
                {
                    curTime = DateTime.Now;
                    time   += (curTime - prevTime).TotalSeconds;
                    //Debug.Log(time);
                    if (time > 0.005f)
                    {
                        Debug.Log("IN TIME");
                        SnapshotMsg snapshot = new SnapshotMsg(0);

                        try
                        {
                            KeyValuePair <int, Snapshot>[] objs = netobjects.ToArray();



                            foreach (KeyValuePair <int, Snapshot> obj in objs)
                            {
                                if (obj.Value.hasMoved)
                                {
                                    Debug.Log("HAS MOVED");
                                    snapshot.objID.Add(obj.Value.objID);
                                    snapshot.textureType.Add(obj.Value.textureType);
                                    Vector3 pos = obj.Value.pos;
                                    snapshot.positions.Add(pos);
                                    snapshot.angular_speed.Add(Quaternion.identity);
                                    snapshot.linear_speed.Add(Vector3.zero);

                                    Quaternion rot = obj.Value.rot;
                                    snapshot.rotation.Add(rot);

                                    //snapshot.linear_speed.Add((pos - lastPosition[obj.Value.objID]) / (float)time);
                                    //snapshot.linear_speed.Add(AngleShift(rot, lastAngle[obj.Value.objID]).eulerAngles / (float)time);


                                    //lastPosition.Add(obj.Value.objID, pos);
                                    //lastAngle.Add(obj.Value.objID, rot);
                                    obj.Value.hasMoved = false;
                                }
                            }
                            if (snapshot.objID.Count > 0)
                            {
                                KeyValuePair <int, PlayerData>[] players = users.ToArray();
                                foreach (KeyValuePair <int, PlayerData> playerData in players)
                                {
                                    //Debug.Log("StructChange");
                                    snapshot.to = playerData.Value.clientData.id;
                                    Send(snapshot.GetBytes(), playerData.Value.clientData.id);
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            Debug.Log(e);
                            Debug.Log(string.Format("ERROR IN SNAPSHOT {0}", netobjects.Count));
                        }
                        //Snapshot(snapshot);
                        time = 0;
                    }
                    prevTime = DateTime.Now;


                    SpinWait.SpinUntil(() => netobjects.Count > 1);
                }
            }
        }