UserSelectionManager.cs 6.1 KB

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