-
Notifications
You must be signed in to change notification settings - Fork 1
/
Multiplayer.cs
397 lines (375 loc) · 14.5 KB
/
Multiplayer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Net;
using System.Net.Sockets;
using System;
using System.Threading;
using System.Linq;
//using ParrelSync;
//使い方
//1. このスクリプトをアタッチします。
//2. myPortは自分のポート、opponentPortは相手のポートです。テストでは2つのクライアントで交互になるように入力します。
//3. playerは自分のキャラクター、otherPlayerObjectsは相手のキャラクターです。あらかじめ用意してアタッチしましょう。
//4. ParrelSyncなどを使い2つのエディターを立ち上げます(ビルドとエディターでデバッグする方法もありますが、ParrelSyncなどを使った方が効率的なのでおススメです)。
//5. 各エディターでmyPortとopponentPortが交互になっているのを確認します(クライアントAで3000, 3001ならクライアントBでは3001, 3000)。
//6. 両方のエディターでプレイボタンを押し、スクリプトのcontext menuからRegisterを押します。
//7. 両方のRegisterを押すと同期が開始します。Playerオブジェクトを動かして一方のエディターのotherPlayerが動いているか確認しましょう。
//8. enableSmoothingを用いるとフレーム間の位置を線形補完します。結果、滑らかに見えます。
public class Multiplayer : MonoBehaviour
{
[SerializeField] int myPort;
[SerializeField] int opponentPort;
[SerializeField] bool enableSmoothing = false;
[SerializeField] Transform player;
[SerializeField] List<GameObject> otherPlayerObjects;
//UDP通信系
int sendPerSecond = 20;
UdpClient client;
Thread receiveThread;
Thread sendThread;
bool isSendTiming = false;
List<IPEndPoint> ackWaiting = new List<IPEndPoint>(4);
List<IPEndPoint> connectedPlayerEPs = new List<IPEndPoint>(4);
List<ReceivedUnit> messageStack = new List<ReceivedUnit>(15);
//ゲーム情報
List<PositionAndRotation> otherPlayerInfo = new List<PositionAndRotation>(3);
void Start()
{
client = new UdpClient(new IPEndPoint(IPAddress.Any, myPort));
receiveThread = new Thread(new ThreadStart(ThreadReceive));
receiveThread.Start();
}
public void RegisterOpponentPort(string IP = "127.0.0.1", int port = 0)
{
port = opponentPort;
byte[] message = UDPMessage.Ack.ToByte();
IPEndPoint opponentEP = new IPEndPoint(IPAddress.Parse(IP), port);
client.Send(message, message.Length, opponentEP);
ackWaiting.Add(opponentEP);
print($"IP: {IP}:{port} に接続要求");
}
[ContextMenu("Register")]
public void OnClickRegister()
{
RegisterOpponentPort(port: opponentPort);
}
/// <summary>
/// 受信用のスレッド。受信した際に情報をスタックに保存しておく。
/// </summary>
void ThreadReceive()
{
while (true)
{
IPEndPoint senderEP = null;
byte[] receivedBytes = client.Receive(ref senderEP);
Debug.Log($"受け取ったメッセージ長: {receivedBytes.Length}");
messageStack.Add(new ReceivedUnit(senderEP, receivedBytes));
}
}
/// <summary>
/// 送信用のスレッド。送信タイミングでisSendTimingをtrueにする。
/// </summary>
void ThreadSend()
{
while (true)
{
//OnUpdateSend();
isSendTiming = true;
Thread.Sleep(1000 / sendPerSecond);
}
}
/// <summary>
/// 受信したメッセージの内容によって処理を行う
/// </summary>
/// <param name="unit">受信情報</param>
void Parse(ReceivedUnit unit)
{
//なるべくメッセージごと1関数で実装する
UDPMessage type = unit.message.ToUDPMessage();
int ackRegisteredIndex = ackWaiting.IndexOfPort(unit.senderEP.Port);
int connectedIndex = connectedPlayerEPs.IndexOfPort(unit.senderEP.Port);
print("メッセージを受信");
switch (type)
{
case UDPMessage.Ack:
{
print(ackRegisteredIndex);
if (ackRegisteredIndex == -1) break;
connectedPlayerEPs.Add(unit.senderEP);
ackWaiting.RemoveAt(ackRegisteredIndex);
if (connectedPlayerEPs.Count == 1)
{
sendThread = new Thread(new ThreadStart(ThreadSend));
sendThread.Start();
}
print("他の人から接続がありました");
byte[] message = UDPMessage.AckComplete.ToByte();
client.SendAsync(message, message.Length, unit.senderEP);
otherPlayerObjects[connectedPlayerEPs.Count - 1].SetActive(true);
break;
}
case UDPMessage.AckComplete:
{
print(ackRegisteredIndex);
if (ackRegisteredIndex == -1) break;
connectedPlayerEPs.Add(unit.senderEP);
ackWaiting.RemoveAt(ackRegisteredIndex);
if (connectedPlayerEPs.Count == 1)
{
sendThread = new Thread(new ThreadStart(ThreadSend));
sendThread.Start();
}
print("他の人から接続がありました");
otherPlayerObjects[connectedPlayerEPs.Count - 1].SetActive(true);
break;
}
case UDPMessage.PosUpdate:
{
Vector3 pos = unit.message.ToVector3(4);
float Yrot = BitConverter.ToSingle(unit.message, 16);
if (otherPlayerInfo.Count <= connectedIndex)
{
//プレイヤーのSmoothedMovementがない場合
PositionAndRotation newPlayer = new PositionAndRotation(sendPerSecond);
newPlayer.UpdateInformation(pos, Yrot);
otherPlayerInfo.Add(newPlayer);
}
else
{
otherPlayerInfo[connectedIndex].UpdateInformation(pos, Yrot);
}
break;
}
default:
{
print("malformed packet!!!!!!!!!");
break;
}
}
}
/// <summary>
/// 通信相手全員に自分の状態を送る
/// </summary>
void BroadcastStatus()
{
//変化がない場合送らない
Vector3 posThisFlame = player.position;
float YthisFlame = player.eulerAngles.y;
//データを作る
byte[] message = UDPMessage.PosUpdate.ToByte();
message = message.Concat(posThisFlame.ToByte()).Concat(BitConverter.GetBytes(YthisFlame)).ToArray();
for (int i = 0; i < connectedPlayerEPs.Count; i++)
{
client.SendAsync(message, message.Length, connectedPlayerEPs[i]);
}
}
private void Update()
{
//送信タイミング
if (isSendTiming)
{
BroadcastStatus();
isSendTiming = false;
}
//受信メッセージがある場合
for (int i = 0; i < messageStack.Count; i++)
{
Parse(messageStack[i]);
messageStack.RemoveAt(i);
i--;
}
//各プレイヤーの位置アップデート
for (int i = 0; i < connectedPlayerEPs.Count; i++)
{
if (i >= otherPlayerInfo.Count) return;
if (enableSmoothing)
{
otherPlayerInfo[i].UpdateTime();
otherPlayerObjects[i].transform.position = otherPlayerInfo[i].GetLerpPosition();
otherPlayerObjects[i].transform.eulerAngles = new Vector3(0, otherPlayerInfo[i].GetLerpRotation(), 0);
}
else
{
otherPlayerObjects[i].transform.position = otherPlayerInfo[i].GetPosition();
otherPlayerObjects[i].transform.eulerAngles = new Vector3(0, otherPlayerInfo[i].GetRotation(), 0);
}
}
}
/// <summary>
/// 受信メッセージの情報を保存
/// </summary>
class ReceivedUnit
{
public IPEndPoint senderEP;
public byte[] message;
public ReceivedUnit(IPEndPoint Ep, byte[] Message)
{
senderEP = Ep;
message = Message;
}
}
/// <summary>
/// スレッド〇す用
/// これがないと連続してプレイ時にポートのバインドが解除されていない
/// </summary>
private void OnApplicationQuit()
{
if (sendThread != null) sendThread.Abort();
if (receiveThread != null) receiveThread.Abort();
if (client != null) client.Close();
}
/// <summary>
/// オンラインでGameObjectを特定する一つの方法
/// タグで候補を特定し、posでオブジェクトを一つに特定する
/// </summary>
/// <param name="tag">特定するオブジェクトのタグ</param>
/// <param name="pos">特定するオブジェクトの位置</param>
/// <param name="threshold">同じだとする位置の閾値、よほどのことがない限り0だと思うが</param>
/// <returns></returns>
public GameObject GetGameObjectWithTagAndPostion(string tag, Vector3 pos, float threshold = .5f)
{
GameObject[] candidates = GameObject.FindGameObjectsWithTag(tag);
foreach (GameObject i in candidates)
{
if ((i.transform.position - pos).magnitude < threshold) return (i);
}
return null;
}
/// <summary>
/// 受信する間の位置・回転を補完するためのクラス
/// 例えば同期レートが1秒に15回の場合60fpsでは4フレームの間止まって瞬間移動を繰り返すように見える。
/// したがって同期レートの2つの値を4分割すると4フレームの間も移動しているように見せることができる。
/// </summary>
class PositionAndRotation
{
public List<Vector3> position;
public List<float> rot;
public float timeFromLastInformation;
float timeToNextInformation;
public PositionAndRotation(int tickPerSecond)
{
position = new List<Vector3>();
rot = new List<float>();
timeFromLastInformation = 0;
timeToNextInformation = 1f / tickPerSecond;
}
/// <summary>
/// 補完する情報を更新する
/// </summary>
/// <param name="pos"></param>
/// <param name="Yrot"></param>
public void UpdateInformation(Vector3 pos, float Yrot)
{
timeFromLastInformation = 0;
if (position.Count == 2) position.RemoveAt(0);
position.Add(pos);
if (rot.Count == 0) rot.Add(Yrot);
else
{
//1,2 の場合は閾値を超えていたら更新、それ以外は前のパラメーター複製
float lastRot = rot[rot.Count - 1];
if (Mathf.Abs(Yrot - lastRot) < 45) rot.Add(lastRot);
else rot.Add(Yrot);
}
if (rot.Count == 3) rot.RemoveAt(0);
}
/// <summary>
/// 時間を更新する、これにより補完のタイミングが進む
/// </summary>
public void UpdateTime()
{
timeFromLastInformation += Time.deltaTime;
}
/// <summary>
/// 補完された位置を変えす
/// </summary>
/// <returns></returns>
public Vector3 GetLerpPosition()
{
if (position.Count == 0) return Vector3.zero;
else if (position.Count == 1) return position[0];
return LerpPos(position[0], position[1], timeFromLastInformation / timeToNextInformation);
}
public Vector3 GetPosition()
{
if (position.Count == 0) return Vector3.zero;
else if (position.Count == 1) return position[0];
return position[1];
}
/// <summary>
/// 補完された回転を返す
/// </summary>
/// <returns></returns>
public float GetLerpRotation()
{
if (rot.Count == 0) return 0;
else if (rot.Count == 1) return rot[0];
return Mathf.LerpAngle(rot[0], rot[1], timeFromLastInformation / timeToNextInformation);
}
public float GetRotation()
{
if (rot.Count == 0) return 0;
else if (rot.Count == 1) return rot[0];
return rot[1];
}
/// <summary>
/// Vector3を線形補完する
/// </summary>
/// <param name="from"></param>
/// <param name="end"></param>
/// <param name="a"></param>
/// <returns></returns>
Vector3 LerpPos(Vector3 from, Vector3 end, float a)
{
float x = Mathf.Lerp(from.x, end.x, a);
float z = Mathf.Lerp(from.z, end.z, a);
float y = Mathf.Lerp(from.y, end.y, a);
return new Vector3(x, y, z);
}
}
}
enum UDPMessage
{
Ack = 100001,
AckComplete,
PosUpdate,
AnimUpdate,
RightDoorOpen,
LeftDoorOpen,
}
static class MultiPlayerExt
{
public static byte[] ToByte(this UDPMessage udpm)
{
return BitConverter.GetBytes((int)udpm);
}
public static UDPMessage ToUDPMessage(this byte[] b, int startIndex = 0)
{
int number = BitConverter.ToInt32(b, startIndex);
return (UDPMessage)Enum.ToObject(typeof(UDPMessage), number);
}
public static byte[] ToByte(this Vector3 v)
{
byte[] x = BitConverter.GetBytes(v.x);
byte[] y = BitConverter.GetBytes(v.y);
byte[] z = BitConverter.GetBytes(v.z);
return x.Concat(y).Concat(z).ToArray();
}
public static Vector3 ToVector3(this byte[] b, int startIndex)
{
float x = BitConverter.ToSingle(b, startIndex);
float y = BitConverter.ToSingle(b, startIndex + 4);
float z = BitConverter.ToSingle(b, startIndex + 8);
return new Vector3(x, y, z);
}
public static int IndexOfPort(this List<IPEndPoint> eps, int targetPort)
{
int index = -1;
for (int i = 0; i < eps.Count; i++)
{
if (eps[i].Port == targetPort) index = i;
}
return index;
}
}