SelectAssemblyClass.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System.Linq;
  5. using Photon;
  6. using Photon.Pun;
  7. using System;
  8. using UnityEngine.Events;
  9. public class SelectAssemblyClass : MonoBehaviourPunCallbacks
  10. {
  11. public List<CardSource> selectedAssemblyCards { get; private set; } = new List<CardSource>();
  12. public List<AddDigivolutionCardsInfo> addDigivolutionCardInfos { get; private set; } = new List<AddDigivolutionCardsInfo>();
  13. public CardSource playCard { get; private set; } = null;
  14. public void ResetSelectAssemblyClass()
  15. {
  16. selectedAssemblyCards = new List<CardSource>();
  17. addDigivolutionCardInfos = new List<AddDigivolutionCardsInfo>();
  18. playCard = null;
  19. }
  20. public void AddDigivolutionCardInfos(AddDigivolutionCardsInfo digivolutionCardsInfo)
  21. {
  22. addDigivolutionCardInfos.Add(digivolutionCardsInfo);
  23. }
  24. #region Is Trash Card
  25. bool isTrashCard(CardSource cardSource)
  26. {
  27. if (cardSource != null)
  28. {
  29. if (CardEffectCommons.IsExistOnTrash(cardSource))
  30. {
  31. return true;
  32. }
  33. }
  34. return false;
  35. }
  36. #endregion
  37. #region Can Select Assembly
  38. bool CanSelectAssembly(AssemblyConditionElement element, CardSource targetCard, CardSource card)
  39. {
  40. if (card != targetCard)
  41. {
  42. if (card != null)
  43. {
  44. if (element != null)
  45. {
  46. if (element.CardCondition != null)
  47. {
  48. if (targetCard != null)
  49. {
  50. if (card.assemblyCondition != null)
  51. {
  52. if (!targetCard.IsToken)
  53. {
  54. if (!selectedAssemblyCards.Contains(targetCard))
  55. {
  56. if (addDigivolutionCardInfos.Count((addDigivolutionCardInfo) => addDigivolutionCardInfo.cardSources.Contains(targetCard)) == 0)
  57. {
  58. if (card.assemblyCondition.CanTargetCondition_ByPreSelecetedList != null)
  59. {
  60. if (!card.assemblyCondition.CanTargetCondition_ByPreSelecetedList(selectedAssemblyCards, targetCard))
  61. {
  62. return false;
  63. }
  64. }
  65. if (element.CardCondition(targetCard))
  66. {
  67. return true;
  68. }
  69. if (targetCard.PermanentOfThisCard() != null)
  70. {
  71. if (targetCard == targetCard.PermanentOfThisCard().TopCard)
  72. {
  73. if (targetCard.PermanentOfThisCard().CanSubstituteForAssemblyCondition(card))
  74. {
  75. return true;
  76. }
  77. }
  78. }
  79. }
  80. }
  81. }
  82. }
  83. }
  84. }
  85. }
  86. }
  87. }
  88. return false;
  89. }
  90. #endregion
  91. #region Select
  92. public IEnumerator Select(CardSource card)
  93. {
  94. GManager.instance.turnStateMachine.isSync = true;
  95. selectedAssemblyCards = new List<CardSource>();
  96. playCard = card;
  97. if (card != null)
  98. {
  99. if (card.HasAssembly)
  100. {
  101. AssemblyCondition AssemblyCondition = card.assemblyCondition;
  102. foreach (AssemblyConditionElement element in AssemblyCondition.elements)
  103. {
  104. yield return GManager.instance.photonWaitController.StartWait("SelectAssemblys");
  105. if (selectedAssemblyCards.Count >= 1)
  106. {
  107. yield return ContinuousController.instance.StartCoroutine(GManager.instance.GetComponent<Effects>().ShowCardEffect2(selectedAssemblyCards, "Assembly Cards", false, true));
  108. }
  109. if (_endSelectAssembly)
  110. {
  111. _endSelectAssembly = false;
  112. //break; TODO: Removed for not triggering Assembly in all situations
  113. }
  114. bool canSelectTrash = false;
  115. if (CardEffectCommons.HasMatchConditionOwnersCardInTrash(card, (cardSource) => CanSelectAssembly(element, cardSource, card)))
  116. {
  117. canSelectTrash = true;
  118. }
  119. Func<IEnumerator> _SelectTrashCard = () => SelectTrashCard(AssemblyCondition, element, card);
  120. Func<IEnumerator> _EndSelectAssembly = () => EndSelectAssembly();
  121. List<Func<IEnumerator>> actions = new List<Func<IEnumerator>>() { _SelectTrashCard, _EndSelectAssembly };
  122. List<Func<IEnumerator>> canSelectActions = new List<Func<IEnumerator>>();
  123. if (canSelectTrash)
  124. {
  125. canSelectActions.Add(_SelectTrashCard);
  126. }
  127. canSelectActions.Add(_EndSelectAssembly);
  128. if (canSelectActions.Count == 1)
  129. {
  130. if (AssemblyCondition.CanTargetCondition_ByPreSelecetedList != null || element.skipAllIfNoSelect)
  131. {
  132. break;
  133. }
  134. else
  135. {
  136. continue;
  137. }
  138. }
  139. else if (canSelectActions.Count == 2 && AssemblyCondition.CanTargetCondition_ByPreSelecetedList == null && !element.skipAllIfNoSelect)
  140. {
  141. SetTargetAssemblysIndex(actions.IndexOf(canSelectActions[0]));
  142. }
  143. else
  144. {
  145. if (card.Owner.isYou)
  146. {
  147. GManager.instance.commandText.OpenCommandText($"From which area will you select {element.selectMessage}?", assembly: true);
  148. List<Command_SelectCommand> command_SelectCommands = new List<Command_SelectCommand>();
  149. for (int i = 0; i < canSelectActions.Count; i++)
  150. {
  151. int k = actions.IndexOf(canSelectActions[i]);
  152. int spriteIndex = 0;
  153. string message = "";
  154. switch (k)
  155. {
  156. case 0:
  157. message = "Trash";
  158. break;
  159. case 1:
  160. message = "End Selection";
  161. spriteIndex = 1;
  162. break;
  163. }
  164. command_SelectCommands.Add(new Command_SelectCommand(message, () => photonView.RPC("SetTargetAssemblysIndex", RpcTarget.All, k), spriteIndex));
  165. }
  166. GManager.instance.selectCommandPanel.SetUpCommandButton(command_SelectCommands);
  167. }
  168. else
  169. {
  170. GManager.instance.commandText.OpenCommandText($"The opponent is choosing from which area to select {element.selectMessage}.", assembly: true);
  171. #region AI
  172. if (GManager.instance.IsAI)
  173. {
  174. List<int> indexes = new List<int>();
  175. for (int i = 0; i < canSelectActions.Count; i++)
  176. {
  177. int k = actions.IndexOf(canSelectActions[i]);
  178. indexes.Add(k);
  179. }
  180. SetTargetAssemblysIndex(UnityEngine.Random.Range(0, indexes.Count));
  181. }
  182. #endregion
  183. }
  184. }
  185. yield return new WaitWhile(() => !_endSelect);
  186. _endSelect = false;
  187. GManager.instance.commandText.CloseCommandText();
  188. yield return new WaitWhile(() => GManager.instance.commandText.gameObject.activeSelf);
  189. if (0 <= _targetIndex && _targetIndex <= actions.Count - 1)
  190. {
  191. yield return ContinuousController.instance.StartCoroutine(actions[_targetIndex]());
  192. if (!card.Owner.isYou && GManager.instance.IsAI)
  193. {
  194. yield return new WaitForSeconds(0.3f);
  195. }
  196. }
  197. }
  198. }
  199. }
  200. if (selectedAssemblyCards.Count >= 1)
  201. {
  202. yield return new WaitForSeconds(0.4f);
  203. }
  204. GManager.instance.GetComponent<Effects>().OffShowCard2();
  205. GManager.instance.turnStateMachine.isSync = false;
  206. }
  207. #endregion
  208. #region Select Trash Card
  209. IEnumerator SelectTrashCard(AssemblyCondition AssemblyCondition, AssemblyConditionElement element, CardSource card)
  210. {
  211. bool CanSelectCardCondition(CardSource cardSource) => CanSelectAssembly(element, cardSource, card);
  212. if (CardEffectCommons.HasMatchConditionOwnersCardInTrash(card, (cardSource) => CanSelectCardCondition(cardSource)))
  213. {
  214. int maxCount = 1;
  215. SelectCardEffect selectCardEffect = GManager.instance.GetComponent<SelectCardEffect>();
  216. selectCardEffect.SetUp(
  217. canTargetCondition: CanSelectCardCondition,
  218. canTargetCondition_ByPreSelecetedList: null,
  219. canEndSelectCondition: null,
  220. canNoSelect: () => true,
  221. selectCardCoroutine: SelectCardCoroutine,
  222. afterSelectCardCoroutine: AfterSelectCardCoroutine,
  223. message: $"<color=#FF633E>Assembly</color>: Select {element.selectMessage} from trash.",
  224. maxCount: maxCount,
  225. canEndNotMax: false,
  226. isShowOpponent: true,
  227. mode: SelectCardEffect.Mode.Custom,
  228. root: SelectCardEffect.Root.Trash,
  229. customRootCardList: null,
  230. canLookReverseCard: true,
  231. selectPlayer: card.Owner,
  232. cardEffect: null);
  233. selectCardEffect.SetUpCustomMessage($"Select {element.selectMessage}.", $"The opponent is selecting {element.selectMessage}.");
  234. selectCardEffect.SetUpCustomMessage_ShowCard("Selected Trash Card");
  235. selectCardEffect.SetAssembly();
  236. yield return StartCoroutine(selectCardEffect.Activate());
  237. IEnumerator SelectCardCoroutine(CardSource cardSource)
  238. {
  239. selectedAssemblyCards.Add(cardSource);
  240. yield return null;
  241. }
  242. IEnumerator AfterSelectCardCoroutine(List<CardSource> cardSources)
  243. {
  244. if (cardSources.Count == 0)
  245. {
  246. if (AssemblyCondition != null)
  247. {
  248. if (AssemblyCondition.CanTargetCondition_ByPreSelecetedList != null || element.skipAllIfNoSelect)
  249. {
  250. EndSelectAssembly();
  251. }
  252. }
  253. }
  254. yield return null;
  255. }
  256. }
  257. }
  258. #endregion
  259. #region End Select Assembly
  260. IEnumerator EndSelectAssembly()
  261. {
  262. _endSelectAssembly = true;
  263. yield return null;
  264. }
  265. #endregion
  266. #region Add Digivolution Cards
  267. public IEnumerator AddDigivolutiuonCards(CardSource card)
  268. {
  269. if (selectedAssemblyCards.Count >= 1)
  270. {
  271. if (card != null)
  272. {
  273. if (card == playCard)
  274. {
  275. if (card.PermanentOfThisCard() != null)
  276. {
  277. yield return ContinuousController.instance.StartCoroutine(GManager.instance.GetComponent<Effects>().ShowCardEffect(selectedAssemblyCards, "Assembly Cards", true, true));
  278. foreach (CardSource cardSource in selectedAssemblyCards)
  279. {
  280. if (isTrashCard(cardSource))
  281. {
  282. yield return ContinuousController.instance.StartCoroutine(GManager.instance.GetComponent<Effects>().CreateAssemblySelectCardEffect(null, player: cardSource.Owner));
  283. yield return ContinuousController.instance.StartCoroutine(card.PermanentOfThisCard().AddDigivolutionCardsBottom(new List<CardSource>() { cardSource }, null));
  284. }
  285. }
  286. }
  287. }
  288. }
  289. }
  290. ResetSelectAssemblyClass();
  291. yield return null;
  292. }
  293. #endregion
  294. #region Add Digivolution Card
  295. public IEnumerator AddDigivolutiuonCardsByEffect(CardSource card)
  296. {
  297. if (addDigivolutionCardInfos.Count >= 1)
  298. {
  299. if (card != null)
  300. {
  301. if (card.PermanentOfThisCard() != null)
  302. {
  303. List<CardSource> addedCards = new List<CardSource>();
  304. foreach (AddDigivolutionCardsInfo info in addDigivolutionCardInfos)
  305. {
  306. List<CardSource> underTamerCards = new List<CardSource>();
  307. List<Permanent> digimonPermanents = new List<Permanent>();
  308. List<CardSource> trashCards = new List<CardSource>();
  309. foreach (CardSource cardSource in info.cardSources)
  310. {
  311. if (isTrashCard(cardSource))
  312. {
  313. trashCards.Add(cardSource);
  314. addedCards.Add(cardSource);
  315. }
  316. }
  317. if (trashCards.Count >= 1)
  318. {
  319. yield return ContinuousController.instance.StartCoroutine(card.PermanentOfThisCard().AddDigivolutionCardsBottom(trashCards, info.cardEffect));
  320. }
  321. }
  322. yield return ContinuousController.instance.StartCoroutine(GManager.instance.GetComponent<Effects>().ShowCardEffect2(addedCards, "Digivolution Cards", true, true));
  323. }
  324. }
  325. }
  326. addDigivolutionCardInfos = new List<AddDigivolutionCardsInfo>();
  327. yield return null;
  328. }
  329. #endregion
  330. int _targetIndex = 0;
  331. bool _endSelect = false;
  332. bool _endSelectAssembly = false;
  333. [PunRPC]
  334. public void SetTargetAssemblysIndex(int targetIndex)
  335. {
  336. this._targetIndex = targetIndex;
  337. _endSelect = true;
  338. }
  339. }