UserSelectionManager.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. [PunRPC]
  15. public void SetInt(int value)
  16. {
  17. _selectedIntValue = value;
  18. _endSelect = true;
  19. }
  20. protected void SetInt_RPC(int value)
  21. {
  22. photonView.RPC("SetInt", RpcTarget.All, value);
  23. }
  24. [PunRPC]
  25. public void SetBool(bool value)
  26. {
  27. _selectedIntValue = getIntFromBool(value);
  28. _endSelect = true;
  29. }
  30. protected void SetBool_RPC(bool value)
  31. {
  32. photonView.RPC("SetBool", RpcTarget.All, value);
  33. }
  34. internal int getIntFromBool(bool value)
  35. {
  36. return value ? 0 : 1;
  37. }
  38. internal bool getBoolFromInt(int value)
  39. {
  40. return value == 0;
  41. }
  42. public IEnumerator WaitForEndSelect()
  43. {
  44. yield return new WaitWhile(() => !_endSelect);
  45. _endSelect = false;
  46. GManager.instance.commandText.CloseCommandText();
  47. yield return new WaitWhile(() => GManager.instance.commandText.gameObject.activeSelf);
  48. }
  49. public void SetIntSelection(List<SelectionElement<int>> selectionElements, Player selectPlayer, string selectPlayerMessage, string notSelectPlayerMessage)
  50. {
  51. _endSelect = false;
  52. _selectedIntValue = 0;
  53. if (selectPlayer.isYou)
  54. {
  55. GManager.instance.commandText.OpenCommandText(selectPlayerMessage);
  56. List<Command_SelectCommand> command_SelectCommands = new List<Command_SelectCommand>();
  57. foreach (SelectionElement<int> selectionElement in selectionElements)
  58. {
  59. command_SelectCommands.Add(new Command_SelectCommand(selectionElement.Message, () => SetInt_RPC(selectionElement.Value), selectionElement.SpriteIndex));
  60. }
  61. GManager.instance.selectCommandPanel.SetUpCommandButton(command_SelectCommands);
  62. }
  63. else
  64. {
  65. GManager.instance.commandText.OpenCommandText(notSelectPlayerMessage);
  66. #region AIƒ‚�[ƒh
  67. if (GManager.instance.IsAI)
  68. {
  69. List<int> canSelectValue = new List<int>();
  70. foreach (SelectionElement<int> selectionElement in selectionElements)
  71. {
  72. canSelectValue.Add(selectionElement.Value);
  73. }
  74. if (canSelectValue.Count >= 1)
  75. {
  76. SetInt(canSelectValue[UnityEngine.Random.Range(0, canSelectValue.Count)]);
  77. }
  78. else
  79. {
  80. SetInt(0);
  81. }
  82. }
  83. #endregion
  84. }
  85. }
  86. public void SetBoolSelection(List<SelectionElement<bool>> selectionElements, Player selectPlayer, string selectPlayerMessage, string notSelectPlayerMessage)
  87. {
  88. if (selectPlayer.isYou)
  89. {
  90. GManager.instance.commandText.OpenCommandText(selectPlayerMessage);
  91. List<Command_SelectCommand> command_SelectCommands = new List<Command_SelectCommand>();
  92. foreach (SelectionElement<bool> selectionElement in selectionElements)
  93. {
  94. command_SelectCommands.Add(new Command_SelectCommand(selectionElement.Message, () => SetBool_RPC(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<bool> canSelectValue = new List<bool>();
  105. foreach (SelectionElement<bool> selectionElement in selectionElements)
  106. {
  107. canSelectValue.Add(selectionElement.Value);
  108. }
  109. if (canSelectValue.Count >= 1)
  110. {
  111. SetBool(canSelectValue[UnityEngine.Random.Range(0, canSelectValue.Count)]);
  112. }
  113. else
  114. {
  115. SetBool(false);
  116. }
  117. }
  118. #endregion
  119. }
  120. }
  121. }
  122. public class SelectionElement<T>
  123. {
  124. public SelectionElement(string message, T value, int spriteIndex)
  125. {
  126. this.Message = message;
  127. this.Value = value;
  128. this.SpriteIndex = spriteIndex;
  129. }
  130. public string Message { get; private set; }
  131. public T Value { get; private set; }
  132. public int SpriteIndex { get; private set; }
  133. }