| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
- using Photon.Pun;
- using System;
- using System.Collections.Generic;
- using System.Collections;
- using UnityEngine;
- using System.Linq;
- public class UserSelectionManager : MonoBehaviourPunCallbacks
- {
- bool _endSelect = false;
- int _selectedIntValue = 0;
- bool _isLocal = false;
- bool _selectedBoolValue => getBoolFromInt(_selectedIntValue);
- public int SelectedIntValue => _selectedIntValue;
- public bool SelectedBoolValue => _selectedBoolValue;
- Player _selectPlayer;
- [PunRPC]
- public void SetIntForPlayer(int playerID, int value)
- {
- Player selectionPlayer = GManager.instance.GetPlayerFromID(playerID);
- if (selectionPlayer == null)
- {
- return;
- }
- selectionPlayer.QueuePlayerSelection(new ValueSelection(value));
- }
- public void SetInt(int value)
- {
- _selectedIntValue = value;
- _endSelect = true;
- }
- protected void SetInt_RPC(int playerID, int value)
- {
- photonView.RPC("SetIntForPlayer", RpcTarget.All, playerID, value);
- }
- [PunRPC]
- public void SetBoolForPlayer(int playerID, bool value)
- {
- Player selectionPlayer = GManager.instance.GetPlayerFromID(playerID);
- if (selectionPlayer == null)
- {
- return;
- }
- selectionPlayer.QueuePlayerSelection(new ValueSelection(value));
- }
- public void SetBool(bool value)
- {
- _selectedIntValue = getIntFromBool(value);
- _endSelect = true;
- }
- protected void SetBool_RPC(int playerID, bool value)
- {
- photonView.RPC("SetBoolForPlayer", RpcTarget.All, playerID, value);
- }
- internal int getIntFromBool(bool value)
- {
- return value ? 1 : 0;
- }
- internal bool getBoolFromInt(int value)
- {
- return value != 0;
- }
- public IEnumerator WaitForEndSelect()
- {
- if (_selectPlayer != null)
- {
- yield return new WaitUntil(() => _selectPlayer.HasPlayerSelection());
- ValueSelection valueSeletion = _selectPlayer.DequeuePlayerSelection<ValueSelection>();
- if (valueSeletion != null)
- {
- _selectedIntValue = valueSeletion.ValueAsInt();
- }
- }
- else
- {
- yield return new WaitWhile(() => !_endSelect);
- }
- _endSelect = false;
- _selectPlayer = null;
- GManager.instance.commandText.CloseCommandText();
- yield return new WaitWhile(() => GManager.instance.commandText.gameObject.activeSelf);
- }
- public void SetIntSelection(List<SelectionElement<int>> selectionElements, Player selectPlayer, string selectPlayerMessage, string notSelectPlayerMessage, bool IsLocal = false)
- {
- _endSelect = false;
- _selectedIntValue = 0;
- _selectPlayer = selectPlayer;
- _isLocal = IsLocal;
- if (selectPlayer.isYou)
- {
- GManager.instance.commandText.OpenCommandText(selectPlayerMessage);
- List<Command_SelectCommand> command_SelectCommands = new List<Command_SelectCommand>();
- foreach (SelectionElement<int> selectionElement in selectionElements)
- {
- command_SelectCommands.Add(new Command_SelectCommand(selectionElement.Message, () => SendSelection(selectionElement.Value), selectionElement.SpriteIndex));
- }
- GManager.instance.selectCommandPanel.SetUpCommandButton(command_SelectCommands);
- }
- else
- {
- GManager.instance.commandText.OpenCommandText(notSelectPlayerMessage);
- #region AIƒ‚�[ƒh
- if (GManager.instance.IsAI)
- {
- List<int> canSelectValue = new List<int>();
- foreach (SelectionElement<int> selectionElement in selectionElements)
- {
- canSelectValue.Add(selectionElement.Value);
- }
- int value = canSelectValue.Count >= 1 ? canSelectValue[UnityEngine.Random.Range(0, canSelectValue.Count)] : 0;
- SendSelection(value);
- }
- #endregion
- }
- void SendSelection(int value)
- {
- if (_isLocal)
- {
- SetIntForPlayer(selectPlayer.PlayerID, value);
- }
- else
- {
- SetInt_RPC(selectPlayer.PlayerID, value);
- }
- }
- }
- public void SetBoolSelection(List<SelectionElement<bool>> selectionElements, Player selectPlayer, string selectPlayerMessage, string notSelectPlayerMessage, bool IsLocal = false)
- {
- _endSelect = false;
- _selectedIntValue = 0;
- _selectPlayer = selectPlayer;
- _isLocal = IsLocal;
- if (selectPlayer.isYou)
- {
- GManager.instance.commandText.OpenCommandText(selectPlayerMessage);
- List<Command_SelectCommand> command_SelectCommands = new List<Command_SelectCommand>();
- foreach (SelectionElement<bool> selectionElement in selectionElements)
- {
- command_SelectCommands.Add(new Command_SelectCommand(selectionElement.Message, () => SendSelection(selectionElement.Value), selectionElement.SpriteIndex));
- }
- GManager.instance.selectCommandPanel.SetUpCommandButton(command_SelectCommands);
- }
- else
- {
- GManager.instance.commandText.OpenCommandText(notSelectPlayerMessage);
- #region AIƒ‚�[ƒh
- if (GManager.instance.IsAI)
- {
- List<bool> canSelectValue = new List<bool>();
- foreach (SelectionElement<bool> selectionElement in selectionElements)
- {
- canSelectValue.Add(selectionElement.Value);
- }
- bool value = canSelectValue.Count >= 1 ? canSelectValue[UnityEngine.Random.Range(0, canSelectValue.Count)] : false;
- SendSelection(value);
- }
- #endregion
- }
- void SendSelection(bool value)
- {
- if (_isLocal)
- {
- SetBoolForPlayer(selectPlayer.PlayerID, value);
- }
- else
- {
- SetBool_RPC(selectPlayer.PlayerID, value);
- }
- }
- }
- }
- public class SelectionElement<T>
- {
- public SelectionElement(string message, T value, int spriteIndex)
- {
- this.Message = message;
- this.Value = value;
- this.SpriteIndex = spriteIndex;
- }
- public string Message { get; private set; }
- public T Value { get; private set; }
- public int SpriteIndex { get; private set; }
- }
|