SelectAssemblyClass.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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. yield return GManager.instance.photonWaitController.StartWait("SelectAssemblys");
  103. if (selectedAssemblyCards.Count >= AssemblyCondition.elementCount)
  104. {
  105. yield return ContinuousController.instance.StartCoroutine(GManager.instance.GetComponent<Effects>().ShowCardEffect2(selectedAssemblyCards, "Assembly Cards", false, true));
  106. }
  107. if (_endSelectAssembly)
  108. {
  109. _endSelectAssembly = false;
  110. //break; TODO: Removed for not triggering Assembly in all situations
  111. }
  112. bool canSelectTrash = false;
  113. if (CardEffectCommons.MatchConditionOwnersCardCountInTrash(card, (cardSource) => CanSelectAssembly(AssemblyCondition.element, cardSource, card)) >= AssemblyCondition.elementCount)
  114. {
  115. canSelectTrash = true;
  116. }
  117. if (canSelectTrash)
  118. {
  119. yield return ContinuousController.instance.StartCoroutine(SelectTrashCard(AssemblyCondition, card));
  120. }
  121. /*foreach (AssemblyConditionElement element in AssemblyCondition.elements)
  122. {
  123. yield return GManager.instance.photonWaitController.StartWait("SelectAssemblys");
  124. if (selectedAssemblyCards.Count >= 1)
  125. {
  126. yield return ContinuousController.instance.StartCoroutine(GManager.instance.GetComponent<Effects>().ShowCardEffect2(selectedAssemblyCards, "Assembly Cards", false, true));
  127. }
  128. if (_endSelectAssembly)
  129. {
  130. _endSelectAssembly = false;
  131. //break; TODO: Removed for not triggering Assembly in all situations
  132. }
  133. bool canSelectTrash = false;
  134. if (CardEffectCommons.HasMatchConditionOwnersCardInTrash(card, (cardSource) => CanSelectAssembly(element, cardSource, card)))
  135. {
  136. canSelectTrash = true;
  137. }
  138. Func<IEnumerator> _SelectTrashCard = () => SelectTrashCard(AssemblyCondition, element, card);
  139. Func<IEnumerator> _EndSelectAssembly = () => EndSelectAssembly();
  140. List<Func<IEnumerator>> actions = new List<Func<IEnumerator>>() { _SelectTrashCard, _EndSelectAssembly };
  141. List<Func<IEnumerator>> canSelectActions = new List<Func<IEnumerator>>();
  142. if (canSelectTrash)
  143. {
  144. canSelectActions.Add(_SelectTrashCard);
  145. }
  146. canSelectActions.Add(_EndSelectAssembly);
  147. if (canSelectActions.Count == 1)
  148. {
  149. if (AssemblyCondition.CanTargetCondition_ByPreSelecetedList != null || element.skipAllIfNoSelect)
  150. {
  151. break;
  152. }
  153. else
  154. {
  155. continue;
  156. }
  157. }
  158. else if (canSelectActions.Count == 2 && AssemblyCondition.CanTargetCondition_ByPreSelecetedList == null && !element.skipAllIfNoSelect)
  159. {
  160. SetTargetAssemblysIndex(actions.IndexOf(canSelectActions[0]));
  161. }
  162. else
  163. {
  164. if (card.Owner.isYou)
  165. {
  166. GManager.instance.commandText.OpenCommandText($"From which area will you select {element.selectMessage}?", assembly: true);
  167. List<Command_SelectCommand> command_SelectCommands = new List<Command_SelectCommand>();
  168. for (int i = 0; i < canSelectActions.Count; i++)
  169. {
  170. int k = actions.IndexOf(canSelectActions[i]);
  171. int spriteIndex = 0;
  172. string message = "";
  173. switch (k)
  174. {
  175. case 0:
  176. message = "Trash";
  177. break;
  178. case 1:
  179. message = "End Selection";
  180. spriteIndex = 1;
  181. break;
  182. }
  183. command_SelectCommands.Add(new Command_SelectCommand(message, () => photonView.RPC("SetTargetAssemblysIndex", RpcTarget.All, k), spriteIndex));
  184. }
  185. GManager.instance.selectCommandPanel.SetUpCommandButton(command_SelectCommands);
  186. }
  187. else
  188. {
  189. GManager.instance.commandText.OpenCommandText($"The opponent is choosing from which area to select {element.selectMessage}.", assembly: true);
  190. #region AI
  191. if (GManager.instance.IsAI)
  192. {
  193. List<int> indexes = new List<int>();
  194. for (int i = 0; i < canSelectActions.Count; i++)
  195. {
  196. int k = actions.IndexOf(canSelectActions[i]);
  197. indexes.Add(k);
  198. }
  199. SetTargetAssemblysIndex(UnityEngine.Random.Range(0, indexes.Count));
  200. }
  201. #endregion
  202. }
  203. }
  204. yield return new WaitWhile(() => !_endSelect);
  205. _endSelect = false;
  206. GManager.instance.commandText.CloseCommandText();
  207. yield return new WaitWhile(() => GManager.instance.commandText.gameObject.activeSelf);
  208. if (0 <= _targetIndex && _targetIndex <= actions.Count - 1)
  209. {
  210. yield return ContinuousController.instance.StartCoroutine(actions[_targetIndex]());
  211. if (!card.Owner.isYou && GManager.instance.IsAI)
  212. {
  213. yield return new WaitForSeconds(0.3f);
  214. }
  215. }
  216. }*/
  217. }
  218. }
  219. if (selectedAssemblyCards.Count >= 1)
  220. {
  221. yield return new WaitForSeconds(0.4f);
  222. }
  223. GManager.instance.GetComponent<Effects>().OffShowCard2();
  224. GManager.instance.turnStateMachine.isSync = false;
  225. }
  226. #endregion
  227. #region Select Trash Card
  228. IEnumerator SelectTrashCard(AssemblyCondition AssemblyCondition, CardSource card)
  229. {
  230. bool CanSelectCardCondition(CardSource cardSource) => CanSelectAssembly(AssemblyCondition.element, cardSource, card);
  231. if (CardEffectCommons.MatchConditionOwnersCardCountInTrash(card, CanSelectCardCondition) >= AssemblyCondition.elementCount)
  232. {
  233. int maxCount = AssemblyCondition.elementCount;
  234. SelectCardEffect selectCardEffect = GManager.instance.GetComponent<SelectCardEffect>();
  235. selectCardEffect.SetUp(
  236. canTargetCondition: CanSelectCardCondition,
  237. canTargetCondition_ByPreSelecetedList: AssemblyCondition.CanTargetCondition_ByPreSelecetedList,
  238. canEndSelectCondition: null,
  239. canNoSelect: () => true,
  240. selectCardCoroutine: SelectCardCoroutine,
  241. afterSelectCardCoroutine: AfterSelectCardCoroutine,
  242. message: $"<color=#FF633E>Assembly</color>: Select {AssemblyCondition.selectMessage} from trash.",
  243. maxCount: maxCount,
  244. canEndNotMax: false,
  245. isShowOpponent: true,
  246. mode: SelectCardEffect.Mode.Custom,
  247. root: SelectCardEffect.Root.Trash,
  248. customRootCardList: null,
  249. canLookReverseCard: true,
  250. selectPlayer: card.Owner,
  251. cardEffect: null);
  252. selectCardEffect.SetUpCustomMessage($"Select {AssemblyCondition.selectMessage}.", $"The opponent is selecting {AssemblyCondition.selectMessage}.");
  253. selectCardEffect.SetUpCustomMessage_ShowCard("Selected Trash Card");
  254. selectCardEffect.SetAssembly();
  255. yield return StartCoroutine(selectCardEffect.Activate());
  256. IEnumerator SelectCardCoroutine(CardSource cardSource)
  257. {
  258. selectedAssemblyCards.Add(cardSource);
  259. yield return null;
  260. }
  261. IEnumerator AfterSelectCardCoroutine(List<CardSource> cardSources)
  262. {
  263. if (cardSources.Count == 0)
  264. {
  265. if (AssemblyCondition != null)
  266. {
  267. if (AssemblyCondition.CanTargetCondition_ByPreSelecetedList != null || AssemblyCondition.element.skipAllIfNoSelect)
  268. {
  269. EndSelectAssembly();
  270. }
  271. }
  272. }
  273. yield return null;
  274. }
  275. }
  276. }
  277. #endregion
  278. #region End Select Assembly
  279. IEnumerator EndSelectAssembly()
  280. {
  281. _endSelectAssembly = true;
  282. yield return null;
  283. }
  284. #endregion
  285. #region Add Digivolution Cards
  286. public IEnumerator AddDigivolutiuonCards(CardSource card)
  287. {
  288. if (card != null)
  289. {
  290. if (card == playCard)
  291. {
  292. if (card.PermanentOfThisCard() != null)
  293. {
  294. if (selectedAssemblyCards.Count == playCard.assemblyCondition.elementCount)
  295. {
  296. yield return ContinuousController.instance.StartCoroutine(GManager.instance.GetComponent<Effects>().ShowCardEffect(selectedAssemblyCards, "Assembly Cards", true, true));
  297. foreach (CardSource cardSource in selectedAssemblyCards)
  298. {
  299. if (isTrashCard(cardSource))
  300. {
  301. yield return ContinuousController.instance.StartCoroutine(GManager.instance.GetComponent<Effects>().CreateAssemblySelectCardEffect(null, player: cardSource.Owner));
  302. yield return ContinuousController.instance.StartCoroutine(card.PermanentOfThisCard().AddDigivolutionCardsBottom(new List<CardSource>() { cardSource }, null));
  303. }
  304. }
  305. }
  306. }
  307. }
  308. }
  309. ResetSelectAssemblyClass();
  310. yield return null;
  311. }
  312. #endregion
  313. #region Add Digivolution Card
  314. public IEnumerator AddDigivolutiuonCardsByEffect(CardSource card)
  315. {
  316. if (addDigivolutionCardInfos.Count >= 1)
  317. {
  318. if (card != null)
  319. {
  320. if (card.PermanentOfThisCard() != null)
  321. {
  322. List<CardSource> addedCards = new List<CardSource>();
  323. foreach (AddDigivolutionCardsInfo info in addDigivolutionCardInfos)
  324. {
  325. List<CardSource> underTamerCards = new List<CardSource>();
  326. List<Permanent> digimonPermanents = new List<Permanent>();
  327. List<CardSource> trashCards = new List<CardSource>();
  328. foreach (CardSource cardSource in info.cardSources)
  329. {
  330. if (isTrashCard(cardSource))
  331. {
  332. trashCards.Add(cardSource);
  333. addedCards.Add(cardSource);
  334. }
  335. }
  336. if (trashCards.Count >= 1)
  337. {
  338. yield return ContinuousController.instance.StartCoroutine(card.PermanentOfThisCard().AddDigivolutionCardsBottom(trashCards, info.cardEffect));
  339. }
  340. }
  341. yield return ContinuousController.instance.StartCoroutine(GManager.instance.GetComponent<Effects>().ShowCardEffect2(addedCards, "Digivolution Cards", true, true));
  342. }
  343. }
  344. }
  345. addDigivolutionCardInfos = new List<AddDigivolutionCardsInfo>();
  346. yield return null;
  347. }
  348. #endregion
  349. int _targetIndex = 0;
  350. bool _endSelect = false;
  351. bool _endSelectAssembly = false;
  352. [PunRPC]
  353. public void SetTargetAssemblysIndex(int targetIndex)
  354. {
  355. this._targetIndex = targetIndex;
  356. _endSelect = true;
  357. }
  358. }