所謂幀同步就是,將玩家的操作統(tǒng)一進(jìn)行上傳,在由服務(wù)器按照預(yù)先設(shè)定的幀 統(tǒng)一轉(zhuǎn)發(fā)(注意:這里的幀是指服務(wù)器的定義.),在此期間客戶端本地進(jìn)行模擬,在接受到服務(wù)器發(fā)過來的幀號和數(shù)據(jù)之后,和現(xiàn)在的數(shù)據(jù)進(jìn)行比對,如果有偏差就快速的將數(shù)據(jù)變更向服務(wù)器數(shù)據(jù).
需要解決的問題
1.數(shù)據(jù)的一致性
由于浮點(diǎn)數(shù)并不精確,在傳輸過程中不可避免的會發(fā)生數(shù)據(jù)偏差,所以需要將傳輸?shù)臄?shù)據(jù)進(jìn)行一些處理.
1.傳統(tǒng)的解決方式:傳遞定點(diǎn)數(shù)
2.和服務(wù)器協(xié)定,采用不超過小數(shù)點(diǎn)后X位.
在本次案例中我們只進(jìn)行同步位置,我們采用了2的方式:
public struct Vector_T
{
public int x;
public int y;
public int z;
public Vector_T(float X, float Y, float Z)
{
x = (int)(X * 10000);
y = (int)(Y * 10000);
z = (int)(Z * 10000);
}
public Vector_T(int X, int Y, int Z)
{
x = X;
y = Y;
z = Z;
}
public Vector3 Real()
{
return new Vector3(x / 10000.0f, y / 10000.0f, z / 10000.0f);
}
public static Vector_T operator +(Vector_T a, Vector_T b)
{
return new Vector_T(a.x + b.x, a.y + b.y, a.z + b.z);
}
public static bool operator ==(Vector_T a, Vector_T b)
{
return a.x == b.x && a.y == b.y && a.z == b.z;
}
public static bool operator !=(Vector_T a, Vector_T b)
{
return !(a.x == b.x && a.y == b.y && a.z == b.z);
}
}
2.tcp包的黏連
tcp每次發(fā)過來的并不是按照我們發(fā)送的進(jìn)行處理,有時候他會粘連上后面幾次的不完整內(nèi)容.
1.解決方式,將每次發(fā)送之前在包頭定一個數(shù)據(jù)長度,每次讀取先讀4個字節(jié)的本條數(shù)據(jù)的長度,不如不足4個字節(jié)則進(jìn)行等待.讀完之后,數(shù)據(jù)向前挪動上一條數(shù)據(jù)的長度,繼續(xù)讀,直到?jīng)]有數(shù)據(jù)可讀為止
//發(fā)送數(shù)據(jù)的拼裝
public static byte[] GetBytes(string data)
{
byte[] dataBytes = Encoding.UTF8.GetBytes(data);
int dataLength = dataBytes.Length;
byte[] lengthBytes = BitConverter.GetBytes(dataLength);
byte[] newBytes = lengthBytes.Concat(dataBytes).ToArray();
return newBytes;
}
/// 解析數(shù)據(jù)的讀取
public void ReadMessage(int newDataAmount, Action<string> processDataCallback)
{
startIndex += newDataAmount;
while (true)
{
if (startIndex <= 4) return;//不足4字節(jié)等待
int count = BitConverter.ToInt32(data, 0);//讀取一個包頭,獲取本條數(shù)據(jù)的長度
if ((startIndex - 4) >= count)//如果總長度小于本條數(shù)據(jù)的長度就跳出,直到確定本條數(shù)據(jù)真真切切的包含在內(nèi)
{
string s = Encoding.UTF8.GetString(data, 4, count);
processDataCallback(s);//輸錄當(dāng)前幀的所有數(shù)據(jù)
Array.Copy(data, count + 4, data, 0, startIndex - 4 - count);//向前挪動
startIndex -= (count + 4);
}
else
{
break;
}
}
}
3.客戶端本地的模擬和接受
接受到數(shù)據(jù)之后,查看客戶端的模擬數(shù)據(jù)是不是和服務(wù)器相同,不同則快速的偏移向服務(wù)器數(shù)據(jù).
這部分代碼比較多,稍后我把工程上傳.
4.循環(huán)上述過程,這樣一個簡易的幀同步就完成了.下面放出代碼,有不足之處可以寫在下面
V2.0
https://gitee.com/QingTingWork_18457176955/FrameSynchronization