SelectAssemblyClass.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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 (element.CanTargetCondition_ByPreSelecetedList != null)
  59. {
  60. if (!element.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 CanFulfillConditions
  92. bool CanFulfillConditions(CardSource card)
  93. {
  94. if (card != null)
  95. {
  96. if (card.HasAssembly)
  97. {
  98. AssemblyCondition AssemblyCondition = card.assemblyCondition;
  99. if (AssemblyCondition.elements != null)
  100. {
  101. if (AssemblyCondition.elements.Count == 1)
  102. return CardEffectCommons.MatchConditionOwnersCardCountInTrash(card, (cardSource) => CanSelectAssembly(AssemblyCondition.elements[0], cardSource, card)) >= AssemblyCondition.elementCount;
  103. else
  104. return CanFulfillEachElementCondition(card, AssemblyCondition);
  105. }
  106. }
  107. }
  108. return false;
  109. }
  110. bool CanFulfillEachElementCondition(CardSource card, AssemblyCondition AssemblyCondition, int index = 0, List<CardSource> usedCards = null, int currentElementCount = 0)
  111. {
  112. if (index < 0 || AssemblyCondition.elements == null)
  113. return false;
  114. if (index >= AssemblyCondition.elements.Count)
  115. return true;
  116. usedCards ??= new List<CardSource>();
  117. AssemblyConditionElement currentElement = AssemblyCondition.elements[index];
  118. List<CardSource> validCards = card.Owner.TrashCards.Filter(currentElement.CardCondition).Except(usedCards).ToList();
  119. foreach(CardSource validCard in validCards)
  120. {
  121. List<CardSource> addedSoFar = new List<CardSource>() { validCard };
  122. addedSoFar.AddRange(usedCards);
  123. if (currentElementCount + 1 >= currentElement.ElementCount)
  124. {
  125. if(CanFulfillEachElementCondition(card, AssemblyCondition, index+1, addedSoFar, 0))
  126. return true;
  127. }
  128. else
  129. {
  130. if (CanFulfillEachElementCondition(card, AssemblyCondition, index, addedSoFar, currentElementCount+1))
  131. return true;
  132. }
  133. }
  134. return false;
  135. }
  136. #endregion
  137. #region Select
  138. public IEnumerator Select(CardSource card)
  139. {
  140. selectedAssemblyCards = new List<CardSource>();
  141. playCard = card;
  142. if (CanFulfillConditions(card))
  143. {
  144. AssemblyCondition AssemblyCondition = card.assemblyCondition;
  145. foreach(AssemblyConditionElement element in AssemblyCondition.elements)
  146. {
  147. yield return GManager.instance.photonWaitController.StartWait("SelectAssemblys");
  148. if (selectedAssemblyCards.Count >= AssemblyCondition.elementCount)
  149. {
  150. yield return ContinuousController.instance.StartCoroutine(GManager.instance.GetComponent<Effects>().ShowCardEffect2(selectedAssemblyCards, "Assembly Cards", false, true));
  151. }
  152. if (_endSelectAssembly)
  153. {
  154. _endSelectAssembly = false;
  155. //break; TODO: Removed for not triggering Assembly in all situations
  156. }
  157. bool canSelectTrash = false;
  158. if (CardEffectCommons.MatchConditionOwnersCardCountInTrash(card, (cardSource) => CanSelectAssembly(element, cardSource, card)) >= element.ElementCount)
  159. {
  160. canSelectTrash = true;
  161. }
  162. if (canSelectTrash)
  163. {
  164. yield return ContinuousController.instance.StartCoroutine(SelectTrashCard(element, card));
  165. }
  166. }
  167. }
  168. if (selectedAssemblyCards.Count >= 1)
  169. {
  170. yield return new WaitForSeconds(0.4f);
  171. }
  172. GManager.instance.GetComponent<Effects>().OffShowCard2();
  173. }
  174. #endregion
  175. #region Select Trash Card
  176. IEnumerator SelectTrashCard(AssemblyConditionElement AssemblyConditionElement, CardSource card)
  177. {
  178. bool CanSelectCardCondition(CardSource cardSource) => CanSelectAssembly(AssemblyConditionElement, cardSource, card);
  179. if (CardEffectCommons.MatchConditionOwnersCardCountInTrash(card, CanSelectCardCondition) >= AssemblyConditionElement.ElementCount)
  180. {
  181. int maxCount = AssemblyConditionElement.ElementCount;
  182. SelectCardEffect selectCardEffect = GManager.instance.GetComponent<SelectCardEffect>();
  183. selectCardEffect.SetUp(
  184. canTargetCondition: CanSelectCardCondition,
  185. canTargetCondition_ByPreSelecetedList: AssemblyConditionElement.CanTargetCondition_ByPreSelecetedList,
  186. canEndSelectCondition: null,
  187. canNoSelect: () => true,
  188. selectCardCoroutine: SelectCardCoroutine,
  189. afterSelectCardCoroutine: AfterSelectCardCoroutine,
  190. message: $"<color=#FF633E>Assembly</color>: Select {AssemblyConditionElement.selectMessage} from trash.",
  191. maxCount: maxCount,
  192. canEndNotMax: false,
  193. isShowOpponent: true,
  194. mode: SelectCardEffect.Mode.Custom,
  195. root: SelectCardEffect.Root.Trash,
  196. customRootCardList: card.Owner.TrashCards.Except(selectedAssemblyCards).ToList(),//don't include cards already chosen
  197. canLookReverseCard: true,
  198. selectPlayer: card.Owner,
  199. cardEffect: null);
  200. selectCardEffect.SetUpCustomMessage($"Select {AssemblyConditionElement.selectMessage}.", $"The opponent is selecting {AssemblyConditionElement.selectMessage}.");
  201. selectCardEffect.SetUpCustomMessage_ShowCard("Selected Trash Card");
  202. selectCardEffect.SetAssembly();
  203. yield return StartCoroutine(selectCardEffect.Activate());
  204. IEnumerator SelectCardCoroutine(CardSource cardSource)
  205. {
  206. selectedAssemblyCards.Add(cardSource);
  207. yield return null;
  208. }
  209. IEnumerator AfterSelectCardCoroutine(List<CardSource> cardSources)
  210. {
  211. if (cardSources.Count == 0)
  212. {
  213. if (AssemblyConditionElement != null)
  214. {
  215. if (AssemblyConditionElement.CanTargetCondition_ByPreSelecetedList != null || AssemblyConditionElement.skipAllIfNoSelect)
  216. {
  217. EndSelectAssembly();
  218. }
  219. }
  220. }
  221. yield return null;
  222. }
  223. }
  224. }
  225. #endregion
  226. #region End Select Assembly
  227. IEnumerator EndSelectAssembly()
  228. {
  229. _endSelectAssembly = true;
  230. yield return null;
  231. }
  232. #endregion
  233. #region Add Digivolution Cards
  234. public IEnumerator AddDigivolutiuonCards(CardSource card)
  235. {
  236. if (card != null)
  237. {
  238. if (card == playCard)
  239. {
  240. if (card.PermanentOfThisCard() != null)
  241. {
  242. if (selectedAssemblyCards.Count == playCard.assemblyCondition.elementCount)
  243. {
  244. yield return ContinuousController.instance.StartCoroutine(GManager.instance.GetComponent<Effects>().ShowCardEffect(selectedAssemblyCards, "Assembly Cards", true, true));
  245. foreach (CardSource cardSource in selectedAssemblyCards)
  246. {
  247. if (isTrashCard(cardSource))
  248. {
  249. yield return ContinuousController.instance.StartCoroutine(GManager.instance.GetComponent<Effects>().CreateAssemblySelectCardEffect(null, player: cardSource.Owner));
  250. yield return ContinuousController.instance.StartCoroutine(card.PermanentOfThisCard().AddDigivolutionCardsBottom(new List<CardSource>() { cardSource }, null));
  251. }
  252. }
  253. }
  254. }
  255. }
  256. }
  257. ResetSelectAssemblyClass();
  258. yield return null;
  259. }
  260. #endregion
  261. #region Add Digivolution Card
  262. public IEnumerator AddDigivolutiuonCardsByEffect(CardSource card)
  263. {
  264. if (addDigivolutionCardInfos.Count >= 1)
  265. {
  266. if (card != null)
  267. {
  268. if (card.PermanentOfThisCard() != null)
  269. {
  270. List<CardSource> addedCards = new List<CardSource>();
  271. foreach (AddDigivolutionCardsInfo info in addDigivolutionCardInfos)
  272. {
  273. List<CardSource> underTamerCards = new List<CardSource>();
  274. List<Permanent> digimonPermanents = new List<Permanent>();
  275. List<CardSource> trashCards = new List<CardSource>();
  276. foreach (CardSource cardSource in info.cardSources)
  277. {
  278. if (isTrashCard(cardSource))
  279. {
  280. trashCards.Add(cardSource);
  281. addedCards.Add(cardSource);
  282. }
  283. }
  284. if (trashCards.Count >= 1)
  285. {
  286. yield return ContinuousController.instance.StartCoroutine(card.PermanentOfThisCard().AddDigivolutionCardsBottom(trashCards, info.cardEffect));
  287. }
  288. }
  289. yield return ContinuousController.instance.StartCoroutine(GManager.instance.GetComponent<Effects>().ShowCardEffect2(addedCards, "Digivolution Cards", true, true));
  290. }
  291. }
  292. }
  293. addDigivolutionCardInfos = new List<AddDigivolutionCardsInfo>();
  294. yield return null;
  295. }
  296. #endregion
  297. bool _endSelectAssembly = false;
  298. }