-
Notifications
You must be signed in to change notification settings - Fork 1
/
AOdinMultiplayerAdapter.cs
83 lines (73 loc) · 3.49 KB
/
AOdinMultiplayerAdapter.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
using System.Collections;
using OdinNative.Odin.Room;
using UnityEngine;
namespace ODIN_Sample.Scripts.Runtime.Odin
{
/// <summary>
/// It is used to implement the adapter pattern for interfacing between ODIN and the Multiplayer solution
/// (e.g. Photon, Mirror, Unity Netcode, etc.) of your choice. This class should be the connection between your
/// multiplayer game logic and the ODIN logic, representing a single player (either local or remote). It will
/// couple the transmissions of a player in an ODIN room with the visual representation in your game.
/// </summary>
/// <remarks>
/// A concrete sample for implementing Photon can be found in the script <c>PhotonToOdinAdapter</c>.
/// </remarks>
public abstract class AOdinMultiplayerAdapter : MonoBehaviour
{
protected virtual void OnEnable()
{
if (IsLocalUser()) StartCoroutine(OnConnectionBehaviour());
}
protected virtual void OnDisable()
{
if (IsLocalUser()) OdinHandler.Instance.OnRoomJoined.RemoveListener(OnRoomJoined);
}
/// <summary>
/// Behaviour to start, once connection was established. Runs as a coroutine, until user data is available.
/// </summary>
/// <returns></returns>
protected virtual IEnumerator OnConnectionBehaviour()
{
while (!OdinHandler.Instance || null == OdinHandler.Instance.GetUserData())
yield return null;
if (OdinHandler.Instance.HasConnections && IsLocalUser())
foreach (Room instanceRoom in OdinHandler.Instance.Rooms)
UpdateUniqueUserId(instanceRoom);
OdinHandler.Instance.OnRoomJoined.AddListener(OnRoomJoined);
}
/// <summary>
/// Called when a ODIN room was joined.
/// </summary>
/// <param name="roomJoinedEventArgs">Room join arguments</param>
protected virtual void OnRoomJoined(RoomJoinedEventArgs roomJoinedEventArgs)
{
if (IsLocalUser())
UpdateUniqueUserId(roomJoinedEventArgs.Room);
}
/// <summary>
/// Returns a unique user id, usually provided by your multiplayer solution. This will couple transmissions by
/// a certain user in an ODIN room to their visual/physical representation in your game.
/// </summary>
/// <remarks>
/// E.g. in photon you could use the photonView's ViewID.
/// </remarks>
/// <returns>A unique user Id.</returns>
public abstract string GetUniqueUserId();
/// <summary>
/// Whether the user represented by <see cref="GetUniqueUserId" /> is a local or remote user.
/// </summary>
/// <returns>True, if it is a local user, false otherwise.</returns>
public abstract bool IsLocalUser();
/// <summary>
/// Transmits the unique user Id to the given ODIN room. Will be automatically called in OnRoomJoined.
/// </summary>
/// <param name="newRoom">The room for which the unique user Id should be updated.</param>
protected virtual void UpdateUniqueUserId(Room newRoom)
{
OdinSampleUserData userData = OdinHandler.Instance.GetUserData().ToOdinSampleUserData();
userData.uniqueUserId = GetUniqueUserId();
newRoom.UpdatePeerUserData(userData.ToUserData());
OdinHandler.Instance.UpdateUserData(userData.ToUserData());
}
}
}