Assignment for RMIT Mixed Reality in 2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

195 lines
3.8 KiB

  1. namespace Oculus.Platform.Samples.VrHoops
  2. {
  3. using UnityEngine;
  4. using Oculus.Platform;
  5. using Oculus.Platform.Models;
  6. public class PlatformManager : MonoBehaviour
  7. {
  8. private static PlatformManager s_instance;
  9. private MatchmakingManager m_matchmaking;
  10. private P2PManager m_p2p;
  11. private LeaderboardManager m_leaderboards;
  12. private AchievementsManager m_achievements;
  13. private State m_currentState;
  14. // my Application-scoped Oculus ID
  15. private ulong m_myID;
  16. // my Oculus user name
  17. private string m_myOculusID;
  18. void Update()
  19. {
  20. m_p2p.UpdateNetwork();
  21. m_leaderboards.CheckForUpdates();
  22. }
  23. #region Initialization and Shutdown
  24. void Awake()
  25. {
  26. // make sure only one instance of this manager ever exists
  27. if (s_instance != null)
  28. {
  29. Destroy(gameObject);
  30. return;
  31. }
  32. s_instance = this;
  33. DontDestroyOnLoad(gameObject);
  34. Core.Initialize();
  35. m_matchmaking = new MatchmakingManager();
  36. m_p2p = new P2PManager();
  37. m_leaderboards = new LeaderboardManager();
  38. m_achievements = new AchievementsManager();
  39. }
  40. void Start()
  41. {
  42. // First thing we should do is perform an entitlement check to make sure
  43. // we successfully connected to the Oculus Platform Service.
  44. Entitlements.IsUserEntitledToApplication().OnComplete(IsEntitledCallback);
  45. }
  46. void IsEntitledCallback(Message msg)
  47. {
  48. if (msg.IsError)
  49. {
  50. TerminateWithError(msg);
  51. return;
  52. }
  53. // Next get the identity of the user that launched the Application.
  54. Users.GetLoggedInUser().OnComplete(GetLoggedInUserCallback);
  55. }
  56. void GetLoggedInUserCallback(Message<User> msg)
  57. {
  58. if (msg.IsError)
  59. {
  60. TerminateWithError(msg);
  61. return;
  62. }
  63. m_myID = msg.Data.ID;
  64. m_myOculusID = msg.Data.OculusID;
  65. TransitionToState(State.WAITING_TO_PRACTICE_OR_MATCHMAKE);
  66. Achievements.CheckForAchievmentUpdates();
  67. }
  68. // In this example, for most errors, we terminate the Application. A full App would do
  69. // something more graceful.
  70. public static void TerminateWithError(Message msg)
  71. {
  72. Debug.Log("Error: " + msg.GetError().Message);
  73. UnityEngine.Application.Quit();
  74. }
  75. public void QuitButtonPressed()
  76. {
  77. UnityEngine.Application.Quit();
  78. }
  79. void OnApplicationQuit()
  80. {
  81. // be a good matchmaking citizen and leave any queue immediately
  82. Matchmaking.LeaveQueue();
  83. }
  84. #endregion
  85. #region Properties
  86. public static MatchmakingManager Matchmaking
  87. {
  88. get { return s_instance.m_matchmaking; }
  89. }
  90. public static P2PManager P2P
  91. {
  92. get { return s_instance.m_p2p; }
  93. }
  94. public static LeaderboardManager Leaderboards
  95. {
  96. get { return s_instance.m_leaderboards; }
  97. }
  98. public static AchievementsManager Achievements
  99. {
  100. get { return s_instance.m_achievements; }
  101. }
  102. public static State CurrentState
  103. {
  104. get { return s_instance.m_currentState; }
  105. }
  106. public static ulong MyID
  107. {
  108. get
  109. {
  110. if (s_instance != null)
  111. {
  112. return s_instance.m_myID;
  113. }
  114. else
  115. {
  116. return 0;
  117. }
  118. }
  119. }
  120. public static string MyOculusID
  121. {
  122. get
  123. {
  124. if (s_instance != null && s_instance.m_myOculusID != null)
  125. {
  126. return s_instance.m_myOculusID;
  127. }
  128. else
  129. {
  130. return string.Empty;
  131. }
  132. }
  133. }
  134. #endregion
  135. #region State Management
  136. public enum State
  137. {
  138. // loading platform library, checking application entitlement,
  139. // getting the local user info
  140. INITIALIZING,
  141. // waiting on the user to join a matchmaking queue or play a practice game
  142. WAITING_TO_PRACTICE_OR_MATCHMAKE,
  143. // waiting for the match to start or viewing results
  144. MATCH_TRANSITION,
  145. // actively playing a practice match
  146. PLAYING_A_LOCAL_MATCH,
  147. // actively playing an online match
  148. PLAYING_A_NETWORKED_MATCH,
  149. };
  150. public static void TransitionToState(State newState)
  151. {
  152. if (s_instance && s_instance.m_currentState != newState)
  153. {
  154. s_instance.m_currentState = newState;
  155. }
  156. }
  157. #endregion
  158. }
  159. }