IngameControlPanel.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. using ExitGames.Client.Photon;
  2. using Photon.Realtime;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. namespace Photon.Pun.Demo.Procedural
  6. {
  7. /// <summary>
  8. /// The Ingame Control Panel basically controls the WorldGenerator.
  9. /// It is only interactable for the current MasterClient in the room.
  10. /// </summary>
  11. public class IngameControlPanel : MonoBehaviourPunCallbacks
  12. {
  13. private bool isSeedValid;
  14. private InputField seedInputField;
  15. private Dropdown worldSizeDropdown;
  16. private Dropdown clusterSizeDropdown;
  17. private Dropdown worldTypeDropdown;
  18. private Button generateWorldButton;
  19. #region UNITY
  20. /// <summary>
  21. /// When the object gets created, all necessary references are set up.
  22. /// Also the UI elements get set up properly in order to control the WorldGenerator.
  23. /// </summary>
  24. public void Awake()
  25. {
  26. isSeedValid = false;
  27. seedInputField = GetComponentInChildren<InputField>();
  28. seedInputField.characterLimit = 10;
  29. seedInputField.characterValidation = InputField.CharacterValidation.Integer;
  30. seedInputField.interactable = PhotonNetwork.PhotonServerSettings.StartInOfflineMode;
  31. seedInputField.onValueChanged.AddListener((string value) =>
  32. {
  33. int seed;
  34. if (int.TryParse(value, out seed))
  35. {
  36. isSeedValid = true;
  37. WorldGenerator.Instance.Seed = seed;
  38. }
  39. else
  40. {
  41. isSeedValid = false;
  42. Debug.LogError("Invalid Seed entered. Only numeric Seeds are allowed.");
  43. }
  44. });
  45. worldSizeDropdown = GetComponentsInChildren<Dropdown>()[0];
  46. worldSizeDropdown.interactable = PhotonNetwork.PhotonServerSettings.StartInOfflineMode;
  47. worldSizeDropdown.onValueChanged.AddListener((int value) =>
  48. {
  49. switch (value)
  50. {
  51. case 0:
  52. {
  53. WorldGenerator.Instance.WorldSize = WorldSize.Tiny;
  54. break;
  55. }
  56. case 1:
  57. {
  58. WorldGenerator.Instance.WorldSize = WorldSize.Small;
  59. break;
  60. }
  61. case 2:
  62. {
  63. WorldGenerator.Instance.WorldSize = WorldSize.Medium;
  64. break;
  65. }
  66. case 3:
  67. {
  68. WorldGenerator.Instance.WorldSize = WorldSize.Large;
  69. break;
  70. }
  71. }
  72. });
  73. clusterSizeDropdown = GetComponentsInChildren<Dropdown>()[1];
  74. clusterSizeDropdown.interactable = PhotonNetwork.PhotonServerSettings.StartInOfflineMode;
  75. clusterSizeDropdown.onValueChanged.AddListener((int value) =>
  76. {
  77. switch (value)
  78. {
  79. case 0:
  80. {
  81. WorldGenerator.Instance.ClusterSize = ClusterSize.Small;
  82. break;
  83. }
  84. case 1:
  85. {
  86. WorldGenerator.Instance.ClusterSize = ClusterSize.Medium;
  87. break;
  88. }
  89. case 2:
  90. {
  91. WorldGenerator.Instance.ClusterSize = ClusterSize.Large;
  92. break;
  93. }
  94. }
  95. });
  96. worldTypeDropdown = GetComponentsInChildren<Dropdown>()[2];
  97. worldTypeDropdown.interactable = PhotonNetwork.PhotonServerSettings.StartInOfflineMode;
  98. worldTypeDropdown.onValueChanged.AddListener((int value) =>
  99. {
  100. switch (value)
  101. {
  102. case 0:
  103. {
  104. WorldGenerator.Instance.WorldType = WorldType.Flat;
  105. break;
  106. }
  107. case 1:
  108. {
  109. WorldGenerator.Instance.WorldType = WorldType.Standard;
  110. break;
  111. }
  112. case 2:
  113. {
  114. WorldGenerator.Instance.WorldType = WorldType.Mountain;
  115. break;
  116. }
  117. }
  118. });
  119. generateWorldButton = GetComponentInChildren<Button>();
  120. generateWorldButton.interactable = PhotonNetwork.PhotonServerSettings.StartInOfflineMode;
  121. generateWorldButton.onClick.AddListener(() =>
  122. {
  123. if (!PhotonNetwork.InRoom)
  124. {
  125. Debug.LogError("Client is not in a room.");
  126. return;
  127. }
  128. if (!isSeedValid)
  129. {
  130. Debug.LogError("Invalid Seed entered. Only numeric Seeds are allowed.");
  131. return;
  132. }
  133. WorldGenerator.Instance.ConfirmAndUpdateProperties();
  134. });
  135. }
  136. #endregion
  137. #region PUN CALLBACKS
  138. /// <summary>
  139. /// Gets called when the local client has joined the room.
  140. /// Since only the MasterClient can control the WorldGenerator,
  141. /// we are checking if we have to make the UI controls available for the local client.
  142. /// </summary>
  143. public override void OnJoinedRoom()
  144. {
  145. seedInputField.interactable = PhotonNetwork.IsMasterClient;
  146. worldSizeDropdown.interactable = PhotonNetwork.IsMasterClient;
  147. clusterSizeDropdown.interactable = PhotonNetwork.IsMasterClient;
  148. worldTypeDropdown.interactable = PhotonNetwork.IsMasterClient;
  149. generateWorldButton.interactable = PhotonNetwork.IsMasterClient;
  150. }
  151. /// <summary>
  152. /// Gets called whenever the current MasterClient has left the room and a new one is selected.
  153. /// If the local client is the new MasterClient, we make the UI controls available for him.
  154. /// </summary>
  155. public override void OnMasterClientSwitched(Player newMasterClient)
  156. {
  157. seedInputField.interactable = newMasterClient.IsLocal;
  158. worldSizeDropdown.interactable = newMasterClient.IsLocal;
  159. clusterSizeDropdown.interactable = newMasterClient.IsLocal;
  160. worldTypeDropdown.interactable = newMasterClient.IsLocal;
  161. generateWorldButton.interactable = newMasterClient.IsLocal;
  162. }
  163. /// <summary>
  164. /// Gets called whenever the Custom Room Properties are updated.
  165. /// In this callback we are interested in the settings the MasterClient can apply to the WorldGenerator.
  166. /// If all possible settings are updated (and available within the updated properties), these settings are also used
  167. /// to update the Ingame Control Panel as well as the WorldGenerator on all clients.
  168. /// Afterwards the WorldGenerator creates a new world with the new settings.
  169. /// </summary>
  170. public override void OnRoomPropertiesUpdate(Hashtable propertiesThatChanged)
  171. {
  172. if (propertiesThatChanged.ContainsKey(WorldGenerator.Instance.SeedPropertiesKey) && propertiesThatChanged.ContainsKey(WorldGenerator.Instance.WorldSizePropertiesKey) && propertiesThatChanged.ContainsKey(WorldGenerator.Instance.ClusterSizePropertiesKey) && propertiesThatChanged.ContainsKey(WorldGenerator.Instance.WorldTypePropertiesKey))
  173. {
  174. // Updating Seed
  175. int seed = (int) propertiesThatChanged[WorldGenerator.Instance.SeedPropertiesKey];
  176. seedInputField.text = seed.ToString();
  177. // Updating World Size
  178. WorldSize worldSize = (WorldSize) propertiesThatChanged[WorldGenerator.Instance.WorldSizePropertiesKey];
  179. switch (worldSize)
  180. {
  181. case WorldSize.Tiny:
  182. {
  183. worldSizeDropdown.value = 0;
  184. break;
  185. }
  186. case WorldSize.Small:
  187. {
  188. worldSizeDropdown.value = 1;
  189. break;
  190. }
  191. case WorldSize.Medium:
  192. {
  193. worldSizeDropdown.value = 2;
  194. break;
  195. }
  196. case WorldSize.Large:
  197. {
  198. worldSizeDropdown.value = 3;
  199. break;
  200. }
  201. }
  202. // Updating Cluster Size
  203. ClusterSize clusterSize = (ClusterSize) propertiesThatChanged[WorldGenerator.Instance.ClusterSizePropertiesKey];
  204. switch (clusterSize)
  205. {
  206. case ClusterSize.Small:
  207. {
  208. clusterSizeDropdown.value = 0;
  209. break;
  210. }
  211. case ClusterSize.Medium:
  212. {
  213. clusterSizeDropdown.value = 1;
  214. break;
  215. }
  216. case ClusterSize.Large:
  217. {
  218. clusterSizeDropdown.value = 2;
  219. break;
  220. }
  221. }
  222. // Updating World Type
  223. WorldType worldType = (WorldType) propertiesThatChanged[WorldGenerator.Instance.WorldTypePropertiesKey];
  224. switch (worldType)
  225. {
  226. case WorldType.Flat:
  227. {
  228. worldTypeDropdown.value = 0;
  229. break;
  230. }
  231. case WorldType.Standard:
  232. {
  233. worldTypeDropdown.value = 1;
  234. break;
  235. }
  236. case WorldType.Mountain:
  237. {
  238. worldTypeDropdown.value = 2;
  239. break;
  240. }
  241. }
  242. // Generating a new world
  243. WorldGenerator.Instance.CreateWorld();
  244. }
  245. }
  246. #endregion
  247. }
  248. }