UserSelectionManager.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. using Photon.Pun;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Collections;
  5. using UnityEngine;
  6. using System.Linq;
  7. public class UserSelectionManager : MonoBehaviourPunCallbacks
  8. {
  9. bool _endSelect = false;
  10. int _selectedIntValue = 0;
  11. bool _isLocal = false;
  12. bool _selectedBoolValue => getBoolFromInt(_selectedIntValue);
  13. public int SelectedIntValue => _selectedIntValue;
  14. public bool SelectedBoolValue => _selectedBoolValue;
  15. Player _selectPlayer;
  16. [PunRPC]
  17. public void SetIntForPlayer(int playerID, int value)
  18. {
  19. Player selectionPlayer = GManager.instance.GetPlayerFromID(playerID);
  20. if (selectionPlayer == null)
  21. {
  22. return;
  23. }
  24. selectionPlayer.QueuePlayerSelection(new ValueSelection(value));
  25. }
  26. public void SetInt(int value)
  27. {
  28. _selectedIntValue = value;
  29. _endSelect = true;
  30. }
  31. protected void SetInt_RPC(int playerID, int value)
  32. {
  33. photonView.RPC("SetIntForPlayer", RpcTarget.All, playerID, value);
  34. }
  35. [PunRPC]
  36. public void SetBoolForPlayer(int playerID, bool value)
  37. {
  38. Player selectionPlayer = GManager.instance.GetPlayerFromID(playerID);
  39. if (selectionPlayer == null)
  40. {
  41. return;
  42. }
  43. selectionPlayer.QueuePlayerSelection(new ValueSelection(value));
  44. }
  45. public void SetBool(bool value)
  46. {
  47. _selectedIntValue = getIntFromBool(value);
  48. _endSelect = true;
  49. }
  50. protected void SetBool_RPC(int playerID, bool value)
  51. {
  52. photonView.RPC("SetBoolForPlayer", RpcTarget.All, playerID, value);
  53. }
  54. internal int getIntFromBool(bool value)
  55. {
  56. return value ? 1 : 0;
  57. }
  58. internal bool getBoolFromInt(int value)
  59. {
  60. return value != 0;
  61. }
  62. public IEnumerator WaitForEndSelect()
  63. {
  64. if (_selectPlayer != null)
  65. {
  66. yield return new WaitUntil(() => _selectPlayer.HasPlayerSelection());
  67. ValueSelection valueSeletion = _selectPlayer.DequeuePlayerSelection<ValueSelection>();
  68. if (valueSeletion != null)
  69. {
  70. _selectedIntValue = valueSeletion.ValueAsInt();
  71. }
  72. }
  73. else
  74. {
  75. yield return new WaitWhile(() => !_endSelect);
  76. }
  77. _endSelect = false;
  78. _selectPlayer = null;
  79. GManager.instance.commandText.CloseCommandText();
  80. yield return new WaitWhile(() => GManager.instance.commandText.gameObject.activeSelf);
  81. }
  82. public void SetIntSelection(List<SelectionElement<int>> selectionElements, Player selectPlayer, string selectPlayerMessage, string notSelectPlayerMessage, bool IsLocal = false)
  83. {
  84. _endSelect = false;
  85. _selectedIntValue = 0;
  86. _selectPlayer = selectPlayer;
  87. _isLocal = IsLocal;
  88. if (selectPlayer.isYou)
  89. {
  90. GManager.instance.commandText.OpenCommandText(selectPlayerMessage);
  91. List<Command_SelectCommand> command_SelectCommands = new List<Command_SelectCommand>();
  92. foreach (SelectionElement<int> selectionElement in selectionElements)
  93. {
  94. command_SelectCommands.Add(new Command_SelectCommand(selectionElement.Message, () => SendSelection(selectionElement.Value), selectionElement.SpriteIndex));
  95. }
  96. GManager.instance.selectCommandPanel.SetUpCommandButton(command_SelectCommands);
  97. }
  98. else
  99. {
  100. GManager.instance.commandText.OpenCommandText(notSelectPlayerMessage);
  101. #region AIƒ‚�[ƒh
  102. if (GManager.instance.IsAI)
  103. {
  104. List<int> canSelectValue = new List<int>();
  105. foreach (SelectionElement<int> selectionElement in selectionElements)
  106. {
  107. canSelectValue.Add(selectionElement.Value);
  108. }
  109. int value = canSelectValue.Count >= 1 ? canSelectValue[UnityEngine.Random.Range(0, canSelectValue.Count)] : 0;
  110. SendSelection(value);
  111. }
  112. #endregion
  113. }
  114. void SendSelection(int value)
  115. {
  116. if (_isLocal)
  117. {
  118. SetIntForPlayer(selectPlayer.PlayerID, value);
  119. }
  120. else
  121. {
  122. SetInt_RPC(selectPlayer.PlayerID, value);
  123. }
  124. }
  125. }
  126. public void SetBoolSelection(List<SelectionElement<bool>> selectionElements, Player selectPlayer, string selectPlayerMessage, string notSelectPlayerMessage, bool IsLocal = false)
  127. {
  128. _endSelect = false;
  129. _selectedIntValue = 0;
  130. _selectPlayer = selectPlayer;
  131. _isLocal = IsLocal;
  132. if (selectPlayer.isYou)
  133. {
  134. GManager.instance.commandText.OpenCommandText(selectPlayerMessage);
  135. List<Command_SelectCommand> command_SelectCommands = new List<Command_SelectCommand>();
  136. foreach (SelectionElement<bool> selectionElement in selectionElements)
  137. {
  138. command_SelectCommands.Add(new Command_SelectCommand(selectionElement.Message, () => SendSelection(selectionElement.Value), selectionElement.SpriteIndex));
  139. }
  140. GManager.instance.selectCommandPanel.SetUpCommandButton(command_SelectCommands);
  141. }
  142. else
  143. {
  144. GManager.instance.commandText.OpenCommandText(notSelectPlayerMessage);
  145. #region AIƒ‚�[ƒh
  146. if (GManager.instance.IsAI)
  147. {
  148. List<bool> canSelectValue = new List<bool>();
  149. foreach (SelectionElement<bool> selectionElement in selectionElements)
  150. {
  151. canSelectValue.Add(selectionElement.Value);
  152. }
  153. bool value = canSelectValue.Count >= 1 ? canSelectValue[UnityEngine.Random.Range(0, canSelectValue.Count)] : false;
  154. SendSelection(value);
  155. }
  156. #endregion
  157. }
  158. void SendSelection(bool value)
  159. {
  160. if (_isLocal)
  161. {
  162. SetBoolForPlayer(selectPlayer.PlayerID, value);
  163. }
  164. else
  165. {
  166. SetBool_RPC(selectPlayer.PlayerID, value);
  167. }
  168. }
  169. }
  170. }
  171. public class SelectionElement<T>
  172. {
  173. public SelectionElement(string message, T value, int spriteIndex)
  174. {
  175. this.Message = message;
  176. this.Value = value;
  177. this.SpriteIndex = spriteIndex;
  178. }
  179. public string Message { get; private set; }
  180. public T Value { get; private set; }
  181. public int SpriteIndex { get; private set; }
  182. }