SelectAssemblyClass.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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. private static WaitForSeconds _waitForSeconds0_4 = new WaitForSeconds(0.4f);
  12. public List<CardSource> selectedAssemblyCards { get; private set; } = new List<CardSource>();
  13. public List<AddDigivolutionCardsInfo> addDigivolutionCardInfos { get; private set; } = new List<AddDigivolutionCardsInfo>();
  14. public List<CardSource> excludedCards { get; private set; } = new List<CardSource>();
  15. public CardSource playCard { get; private set; } = null;
  16. public void ResetSelectAssemblyClass()
  17. {
  18. selectedAssemblyCards = new List<CardSource>();
  19. addDigivolutionCardInfos = new List<AddDigivolutionCardsInfo>();
  20. playCard = null;
  21. }
  22. public void AddDigivolutionCardInfos(AddDigivolutionCardsInfo digivolutionCardsInfo)
  23. {
  24. addDigivolutionCardInfos.Add(digivolutionCardsInfo);
  25. }
  26. public void SetExcludedCards(List<CardSource> excluded)
  27. {
  28. excludedCards = excluded;
  29. }
  30. #region Is Trash Card
  31. bool isTrashCard(CardSource cardSource)
  32. {
  33. if (cardSource != null)
  34. {
  35. if (CardEffectCommons.IsExistOnTrash(cardSource))
  36. {
  37. return true;
  38. }
  39. }
  40. return false;
  41. }
  42. #endregion
  43. #region Can Select Assembly
  44. bool CanSelectAssembly(AssemblyConditionElement element, CardSource targetCard, CardSource card)
  45. {
  46. if (excludedCards.Contains(targetCard))
  47. return false;
  48. if (card != targetCard)
  49. {
  50. if (card != null)
  51. {
  52. if (element != null)
  53. {
  54. if (element.CardCondition != null)
  55. {
  56. if (targetCard != null)
  57. {
  58. if (card.assemblyCondition != null)
  59. {
  60. if (!targetCard.IsToken)
  61. {
  62. if (!selectedAssemblyCards.Contains(targetCard))
  63. {
  64. if (addDigivolutionCardInfos.Count((addDigivolutionCardInfo) => addDigivolutionCardInfo.cardSources.Contains(targetCard)) == 0)
  65. {
  66. if (element.CanTargetCondition_ByPreSelecetedList != null)
  67. {
  68. if (!element.CanTargetCondition_ByPreSelecetedList(selectedAssemblyCards, targetCard))
  69. {
  70. return false;
  71. }
  72. }
  73. if (element.CardCondition(targetCard))
  74. {
  75. return true;
  76. }
  77. if (targetCard.PermanentOfThisCard() != null)
  78. {
  79. if (targetCard == targetCard.PermanentOfThisCard().TopCard)
  80. {
  81. if (targetCard.PermanentOfThisCard().CanSubstituteForAssemblyCondition(card))
  82. {
  83. return true;
  84. }
  85. }
  86. }
  87. }
  88. }
  89. }
  90. }
  91. }
  92. }
  93. }
  94. }
  95. }
  96. return false;
  97. }
  98. #endregion
  99. #region CanFulfillConditions
  100. bool CanFulfillConditions(CardSource card)
  101. {
  102. if (card != null)
  103. {
  104. if (card.HasAssembly)
  105. {
  106. AssemblyCondition AssemblyCondition = card.assemblyCondition;
  107. if (AssemblyCondition.elements != null)
  108. {
  109. if (AssemblyCondition.elements.Count == 1)
  110. return CardEffectCommons.MatchConditionOwnersCardCountInTrash(card, (cardSource) => CanSelectAssembly(AssemblyCondition.elements[0], cardSource, card)) >= AssemblyCondition.elementCount;
  111. else
  112. return CanFulfillEachElementCondition(card, AssemblyCondition);
  113. }
  114. }
  115. }
  116. return false;
  117. }
  118. bool CanFulfillEachElementCondition(CardSource card, AssemblyCondition AssemblyCondition, int index = 0, List<CardSource> usedCards = null, int currentElementCount = 0)
  119. {
  120. if (index < 0 || AssemblyCondition.elements == null)
  121. return false;
  122. if (index >= AssemblyCondition.elements.Count)
  123. return true;
  124. usedCards ??= new List<CardSource>();
  125. AssemblyConditionElement currentElement = AssemblyCondition.elements[index];
  126. List<CardSource> validCards = card.Owner.TrashCards.Filter(currentElement.CardCondition).Except(usedCards).ToList();
  127. foreach(CardSource validCard in validCards)
  128. {
  129. List<CardSource> addedSoFar = new List<CardSource>() { validCard };
  130. addedSoFar.AddRange(usedCards);
  131. if (currentElementCount + 1 >= currentElement.ElementCount)
  132. {
  133. if(CanFulfillEachElementCondition(card, AssemblyCondition, index+1, addedSoFar, 0))
  134. return true;
  135. }
  136. else
  137. {
  138. if (CanFulfillEachElementCondition(card, AssemblyCondition, index, addedSoFar, currentElementCount+1))
  139. return true;
  140. }
  141. }
  142. return false;
  143. }
  144. #endregion
  145. #region Select
  146. public IEnumerator Select(CardSource card)
  147. {
  148. selectedAssemblyCards = new List<CardSource>();
  149. playCard = card;
  150. if (CanFulfillConditions(card))
  151. {
  152. AssemblyCondition AssemblyCondition = card.assemblyCondition;
  153. foreach(AssemblyConditionElement element in AssemblyCondition.elements)
  154. {
  155. if (selectedAssemblyCards.Count >= AssemblyCondition.elementCount)
  156. {
  157. yield return ContinuousController.instance.StartCoroutine(GManager.instance.GetComponent<Effects>().ShowCardEffect2(selectedAssemblyCards, "Assembly Cards", false, true));
  158. }
  159. if (_endSelectAssembly)
  160. {
  161. _endSelectAssembly = false;
  162. //break; TODO: Removed for not triggering Assembly in all situations
  163. }
  164. bool canSelectTrash = false;
  165. if (CardEffectCommons.MatchConditionOwnersCardCountInTrash(card, (cardSource) => CanSelectAssembly(element, cardSource, card)) >= element.ElementCount)
  166. {
  167. canSelectTrash = true;
  168. }
  169. if (canSelectTrash)
  170. {
  171. yield return ContinuousController.instance.StartCoroutine(SelectTrashCard(element, card));
  172. }
  173. }
  174. }
  175. if (selectedAssemblyCards.Count >= 1)
  176. {
  177. yield return _waitForSeconds0_4;
  178. }
  179. GManager.instance.GetComponent<Effects>().OffShowCard2();
  180. }
  181. #endregion
  182. #region Select Trash Card
  183. IEnumerator SelectTrashCard(AssemblyConditionElement AssemblyConditionElement, CardSource card)
  184. {
  185. bool CanSelectCardCondition(CardSource cardSource) => CanSelectAssembly(AssemblyConditionElement, cardSource, card);
  186. if (CardEffectCommons.MatchConditionOwnersCardCountInTrash(card, CanSelectCardCondition) >= AssemblyConditionElement.ElementCount)
  187. {
  188. int maxCount = AssemblyConditionElement.ElementCount;
  189. SelectCardEffect selectCardEffect = GManager.instance.GetComponent<SelectCardEffect>();
  190. selectCardEffect.SetUp(
  191. canTargetCondition: CanSelectCardCondition,
  192. canTargetCondition_ByPreSelecetedList: AssemblyConditionElement.CanTargetCondition_ByPreSelecetedList,
  193. canEndSelectCondition: null,
  194. canNoSelect: () => true,
  195. selectCardCoroutine: SelectCardCoroutine,
  196. afterSelectCardCoroutine: AfterSelectCardCoroutine,
  197. message: $"<color=#FF633E>Assembly</color>: Select {AssemblyConditionElement.selectMessage} from trash.",
  198. maxCount: maxCount,
  199. canEndNotMax: false,
  200. isShowOpponent: true,
  201. mode: SelectCardEffect.Mode.Custom,
  202. root: SelectCardEffect.Root.Trash,
  203. customRootCardList: card.Owner.TrashCards.Except(selectedAssemblyCards).ToList(),//don't include cards already chosen
  204. canLookReverseCard: true,
  205. selectPlayer: card.Owner,
  206. cardEffect: null);
  207. selectCardEffect.SetUpCustomMessage($"Select {AssemblyConditionElement.selectMessage}.", $"The opponent is selecting {AssemblyConditionElement.selectMessage}.");
  208. selectCardEffect.SetUpCustomMessage_ShowCard("Selected Trash Card");
  209. selectCardEffect.SetAssembly();
  210. yield return StartCoroutine(selectCardEffect.Activate());
  211. IEnumerator SelectCardCoroutine(CardSource cardSource)
  212. {
  213. selectedAssemblyCards.Add(cardSource);
  214. yield return null;
  215. }
  216. IEnumerator AfterSelectCardCoroutine(List<CardSource> cardSources)
  217. {
  218. if (cardSources.Count == 0)
  219. {
  220. if (AssemblyConditionElement != null)
  221. {
  222. if (AssemblyConditionElement.CanTargetCondition_ByPreSelecetedList != null || AssemblyConditionElement.skipAllIfNoSelect)
  223. {
  224. yield return StartCoroutine(EndSelectAssembly());
  225. }
  226. }
  227. }
  228. yield return null;
  229. }
  230. }
  231. }
  232. #endregion
  233. #region End Select Assembly
  234. IEnumerator EndSelectAssembly()
  235. {
  236. _endSelectAssembly = true;
  237. yield return null;
  238. }
  239. #endregion
  240. #region Add Digivolution Cards
  241. public IEnumerator AddDigivolutiuonCards(CardSource card)
  242. {
  243. if (card != null)
  244. {
  245. if (card == playCard)
  246. {
  247. if (card.PermanentOfThisCard() != null)
  248. {
  249. if (selectedAssemblyCards.Count == playCard.assemblyCondition.elementCount)
  250. {
  251. yield return ContinuousController.instance.StartCoroutine(GManager.instance.GetComponent<Effects>().ShowCardEffect(selectedAssemblyCards, "Assembly Cards", true, true));
  252. foreach (CardSource cardSource in selectedAssemblyCards)
  253. {
  254. if (isTrashCard(cardSource))
  255. {
  256. yield return ContinuousController.instance.StartCoroutine(GManager.instance.GetComponent<Effects>().CreateAssemblySelectCardEffect(null, player: cardSource.Owner));
  257. yield return ContinuousController.instance.StartCoroutine(card.PermanentOfThisCard().AddDigivolutionCardsBottom(new List<CardSource>() { cardSource }, null));
  258. }
  259. }
  260. }
  261. }
  262. }
  263. }
  264. ResetSelectAssemblyClass();
  265. yield return null;
  266. }
  267. #endregion
  268. #region Add Digivolution Card
  269. public IEnumerator AddDigivolutiuonCardsByEffect(CardSource card)
  270. {
  271. if (addDigivolutionCardInfos.Count >= 1)
  272. {
  273. if (card != null)
  274. {
  275. if (card.PermanentOfThisCard() != null)
  276. {
  277. List<CardSource> addedCards = new List<CardSource>();
  278. foreach (AddDigivolutionCardsInfo info in addDigivolutionCardInfos)
  279. {
  280. List<CardSource> trashCards = new List<CardSource>();
  281. foreach (CardSource cardSource in info.cardSources)
  282. {
  283. if (isTrashCard(cardSource))
  284. {
  285. trashCards.Add(cardSource);
  286. addedCards.Add(cardSource);
  287. }
  288. }
  289. if (trashCards.Count >= 1)
  290. {
  291. yield return ContinuousController.instance.StartCoroutine(card.PermanentOfThisCard().AddDigivolutionCardsBottom(trashCards, info.cardEffect));
  292. }
  293. }
  294. yield return ContinuousController.instance.StartCoroutine(GManager.instance.GetComponent<Effects>().ShowCardEffect2(addedCards, "Digivolution Cards", true, true));
  295. }
  296. }
  297. }
  298. addDigivolutionCardInfos = new List<AddDigivolutionCardsInfo>();
  299. yield return null;
  300. }
  301. #endregion
  302. bool _endSelectAssembly = false;
  303. }