SelectAssemblyClass.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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 List<CardSource> excludedCards { get; private set; } = new List<CardSource>();
  14. public CardSource playCard { get; private set; } = null;
  15. public void ResetSelectAssemblyClass()
  16. {
  17. selectedAssemblyCards = new List<CardSource>();
  18. addDigivolutionCardInfos = new List<AddDigivolutionCardsInfo>();
  19. playCard = null;
  20. }
  21. public void AddDigivolutionCardInfos(AddDigivolutionCardsInfo digivolutionCardsInfo)
  22. {
  23. addDigivolutionCardInfos.Add(digivolutionCardsInfo);
  24. }
  25. public void SetExcludedCards(List<CardSource> excluded)
  26. {
  27. excludedCards = excluded;
  28. }
  29. #region Is Trash Card
  30. bool isTrashCard(CardSource cardSource)
  31. {
  32. if (cardSource != null)
  33. {
  34. if (CardEffectCommons.IsExistOnTrash(cardSource))
  35. {
  36. return true;
  37. }
  38. }
  39. return false;
  40. }
  41. #endregion
  42. #region Can Select Assembly
  43. bool CanSelectAssembly(AssemblyConditionElement element, CardSource targetCard, CardSource card)
  44. {
  45. if (excludedCards.Contains(targetCard))
  46. return false;
  47. if (card != targetCard)
  48. {
  49. if (card != null)
  50. {
  51. if (element != null)
  52. {
  53. if (element.CardCondition != null)
  54. {
  55. if (targetCard != null)
  56. {
  57. if (card.assemblyCondition != null)
  58. {
  59. if (!targetCard.IsToken)
  60. {
  61. if (!selectedAssemblyCards.Contains(targetCard))
  62. {
  63. if (addDigivolutionCardInfos.Count((addDigivolutionCardInfo) => addDigivolutionCardInfo.cardSources.Contains(targetCard)) == 0)
  64. {
  65. if (element.CanTargetCondition_ByPreSelecetedList != null)
  66. {
  67. if (!element.CanTargetCondition_ByPreSelecetedList(selectedAssemblyCards, targetCard))
  68. {
  69. return false;
  70. }
  71. }
  72. if (element.CardCondition(targetCard))
  73. {
  74. return true;
  75. }
  76. if (targetCard.PermanentOfThisCard() != null)
  77. {
  78. if (targetCard == targetCard.PermanentOfThisCard().TopCard)
  79. {
  80. if (targetCard.PermanentOfThisCard().CanSubstituteForAssemblyCondition(card))
  81. {
  82. return true;
  83. }
  84. }
  85. }
  86. }
  87. }
  88. }
  89. }
  90. }
  91. }
  92. }
  93. }
  94. }
  95. return false;
  96. }
  97. #endregion
  98. #region CanFulfillConditions
  99. bool CanFulfillConditions(CardSource card)
  100. {
  101. if (card != null)
  102. {
  103. if (card.HasAssembly)
  104. {
  105. AssemblyCondition AssemblyCondition = card.assemblyCondition;
  106. if (AssemblyCondition.elements != null)
  107. {
  108. if (AssemblyCondition.elements.Count == 1)
  109. return CardEffectCommons.MatchConditionOwnersCardCountInTrash(card, (cardSource) => CanSelectAssembly(AssemblyCondition.elements[0], cardSource, card)) >= AssemblyCondition.elementCount;
  110. else
  111. return CanFulfillEachElementCondition(card, AssemblyCondition);
  112. }
  113. }
  114. }
  115. return false;
  116. }
  117. bool CanFulfillEachElementCondition(CardSource card, AssemblyCondition AssemblyCondition, int index = 0, List<CardSource> usedCards = null, int currentElementCount = 0)
  118. {
  119. if (index < 0 || AssemblyCondition.elements == null)
  120. return false;
  121. if (index >= AssemblyCondition.elements.Count)
  122. return true;
  123. usedCards ??= new List<CardSource>();
  124. AssemblyConditionElement currentElement = AssemblyCondition.elements[index];
  125. List<CardSource> validCards = card.Owner.TrashCards.Filter(currentElement.CardCondition).Except(usedCards).ToList();
  126. foreach(CardSource validCard in validCards)
  127. {
  128. List<CardSource> addedSoFar = new List<CardSource>() { validCard };
  129. addedSoFar.AddRange(usedCards);
  130. if (currentElementCount + 1 >= currentElement.ElementCount)
  131. {
  132. if(CanFulfillEachElementCondition(card, AssemblyCondition, index+1, addedSoFar, 0))
  133. return true;
  134. }
  135. else
  136. {
  137. if (CanFulfillEachElementCondition(card, AssemblyCondition, index, addedSoFar, currentElementCount+1))
  138. return true;
  139. }
  140. }
  141. return false;
  142. }
  143. #endregion
  144. #region Select
  145. public IEnumerator Select(CardSource card)
  146. {
  147. selectedAssemblyCards = new List<CardSource>();
  148. playCard = card;
  149. if (CanFulfillConditions(card))
  150. {
  151. AssemblyCondition AssemblyCondition = card.assemblyCondition;
  152. foreach(AssemblyConditionElement element in AssemblyCondition.elements)
  153. {
  154. yield return GManager.instance.photonWaitController.StartWait("SelectAssemblys");
  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 new WaitForSeconds(0.4f);
  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. 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> underTamerCards = new List<CardSource>();
  281. List<Permanent> digimonPermanents = new List<Permanent>();
  282. List<CardSource> trashCards = new List<CardSource>();
  283. foreach (CardSource cardSource in info.cardSources)
  284. {
  285. if (isTrashCard(cardSource))
  286. {
  287. trashCards.Add(cardSource);
  288. addedCards.Add(cardSource);
  289. }
  290. }
  291. if (trashCards.Count >= 1)
  292. {
  293. yield return ContinuousController.instance.StartCoroutine(card.PermanentOfThisCard().AddDigivolutionCardsBottom(trashCards, info.cardEffect));
  294. }
  295. }
  296. yield return ContinuousController.instance.StartCoroutine(GManager.instance.GetComponent<Effects>().ShowCardEffect2(addedCards, "Digivolution Cards", true, true));
  297. }
  298. }
  299. }
  300. addDigivolutionCardInfos = new List<AddDigivolutionCardsInfo>();
  301. yield return null;
  302. }
  303. #endregion
  304. bool _endSelectAssembly = false;
  305. }