Fieldモジュールは、接続するサーバーを選択することなく、接続されているすべてのクライアントがお互いに「見える」仮想世界を作ります。
視界に入ったクライアント同士は、自動的にリアルタイムメッセージ交換ができるようになります。
Fieldのデフォルトサイズは37,7970,000平方メートル(日本とほぼ同じ面積)で、すべてのクライアントの視界は100メートルに設定されています。
Fieldモジュールのセットアップ
Fieldモジュールには TCPまたはUDP のインスタンスが設定されている必要があります。
TCPクライアントを使用する
Diarkis.Tcp tcp = new Diarkis.Tcp();
Diarkis.Modules.Field field = new Diarkis.Modules.Field();
field.SetupAsTcp(tcp);
UDPクライアントを使用する
Diarkis.Udp udp = new Diarkis.Udp();
Diarkis.Modules.Field field = new Diarkis.Modules.Field();
field.SetupAsTcp(udp);
Fieldを使い始めるには
Fieldモジュールを使い始めるには、まず初期化処理を行います。
以下はFieldの初期化コードの例です。
// We receive a response from the server once the client is successfully initialized on the server
field.OnResponseSyncInit += HandleDiarkisFieldInit;
// Initialize the client by sending its current X and Y coordinates along with the Z (It is not height, but Z represents dimension)
// syncLimit controls the maximum number of remote clients to synchronize with
int syncLimit = 30;
// customFilterID executes custom filter function on the server (This is not available for Diarkis Cloud)
int customFilterID = 0;
// message will be sent to all remote clients that are within the field of view
field.SyncInit(x, y, z, syncLimit, customFilterID, message);
// The event is raised when the client is successfully initialized on the server by SyncInit
private void HandleDiarkisFieldInit(List<byte[]> remoteMessages)
{
// remoteMessages contains messages of the remote clients within the field of view
}
Field上でリモートクライアントと同期する方法
一定間隔で Sync メソッドを呼び出すことで、クライアントの座標を同期させたり、リモートクライアントにメッセージを送信することができます。
Fieldモジュールにおける「座標0」の扱い
座標0のクライアントは、0以外の座標のクライアントの視界に入ることはありません。
Fieldモジュールにおける「負の座標」の扱い
負の座標値を持つクライアントは、正の座標値を持つクライアントの視界と重なることはありません。
注: Sync を使用する前に SyncInit を呼び出さなければなりません。
// This is how to receive remote clients' messages sent by their Sync
field.OnSync += HandleDiarkisFieldSync;
// Sending its current X and Y coordinates along with the Z (It is not height, but Z represents dimension)
// syncLimit controls the maximum number of remote clients to synchronize with
int syncLimit = 30;
// customFilterID executes custom filter function on the server (This is not available for Diarkis Cloud)
int customFilterID = 0;
// message will be sent to all remote clients that are within the field of view
// for UDP, reliable=true will make the message an RUDP packet
bool reliable = false;
field.Sync(x, y, z, syncLimit, customFilterID, message, reliable);
// message is the byte array sent from remote clients
private void HandleDiarkisFieldSync(byte[] message)
{
// remoteMessages contains messages of the remote clients within the field of view
}
Fieldからの退出を通知する
Fieldから退出する際、Fieldからクライアントがいなくなったことを他のリモートクライアントに通知したい場合、Diarkisでは以下のようなコードでリモートクライアントからの通知を処理するイベントリスナーを設定することができます。
// This is the event handler for remote client disappearance
field.OnDisappear += HandleDiarkisFieldDisappear;
// Notify all remote clients within the field of view that you are no longer in the field
field.Disappear();
private void HandleDiarkisFieldDisappear(string uid)
{
// uid is the User ID of the client disappeared
}
おわりに
Fieldの概念は、リアルタイムプリケーションにおいて主要な機能であるオープンワールド・MMOのような世界を実装するために必要な概念の1つです。
Diarkisは分散アーキテクチャを取りながらもFieldがサーバごとに分断されないという点が大きな特徴であり、この特性により高い「拡張性」「耐障害性」を実現しています。