SelectAssemblyClass.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. GManager.instance.turnStateMachine.isSync = true;
  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. yield return GManager.instance.photonWaitController.StartWait("SelectAssemblys");
  156. if (selectedAssemblyCards.Count >= AssemblyCondition.elementCount)
  157. {
  158. yield return ContinuousController.instance.StartCoroutine(GManager.instance.GetComponent<Effects>().ShowCardEffect2(selectedAssemblyCards, "Assembly Cards", false, true));
  159. }
  160. if (_endSelectAssembly)
  161. {
  162. _endSelectAssembly = false;
  163. //break; TODO: Removed for not triggering Assembly in all situations
  164. }
  165. bool canSelectTrash = false;
  166. if (CardEffectCommons.MatchConditionOwnersCardCountInTrash(card, (cardSource) => CanSelectAssembly(element, cardSource, card)) >= element.ElementCount)
  167. {
  168. canSelectTrash = true;
  169. }
  170. if (canSelectTrash)
  171. {
  172. yield return ContinuousController.instance.StartCoroutine(SelectTrashCard(element, card));
  173. }
  174. }
  175. }
  176. if (selectedAssemblyCards.Count >= 1)
  177. {
  178. yield return new WaitForSeconds(0.4f);
  179. }
  180. GManager.instance.GetComponent<Effects>().OffShowCard2();
  181. GManager.instance.turnStateMachine.isSync = false;
  182. }
  183. #endregion
  184. #region Select Trash Card
  185. IEnumerator SelectTrashCard(AssemblyConditionElement AssemblyConditionElement, CardSource card)
  186. {
  187. bool CanSelectCardCondition(CardSource cardSource) => CanSelectAssembly(AssemblyConditionElement, cardSource, card);
  188. if (CardEffectCommons.MatchConditionOwnersCardCountInTrash(card, CanSelectCardCondition) >= AssemblyConditionElement.ElementCount)
  189. {
  190. int maxCount = AssemblyConditionElement.ElementCount;
  191. SelectCardEffect selectCardEffect = GManager.instance.GetComponent<SelectCardEffect>();
  192. selectCardEffect.SetUp(
  193. canTargetCondition: CanSelectCardCondition,
  194. canTargetCondition_ByPreSelecetedList: AssemblyConditionElement.CanTargetCondition_ByPreSelecetedList,
  195. canEndSelectCondition: null,
  196. canNoSelect: () => true,
  197. selectCardCoroutine: SelectCardCoroutine,
  198. afterSelectCardCoroutine: AfterSelectCardCoroutine,
  199. message: $"<color=#FF633E>Assembly</color>: Select {AssemblyConditionElement.selectMessage} from trash.",
  200. maxCount: maxCount,
  201. canEndNotMax: false,
  202. isShowOpponent: true,
  203. mode: SelectCardEffect.Mode.Custom,
  204. root: SelectCardEffect.Root.Trash,
  205. customRootCardList: card.Owner.TrashCards.Except(selectedAssemblyCards).ToList(),//don't include cards already chosen
  206. canLookReverseCard: true,
  207. selectPlayer: card.Owner,
  208. cardEffect: null);
  209. selectCardEffect.SetUpCustomMessage($"Select {AssemblyConditionElement.selectMessage}.", $"The opponent is selecting {AssemblyConditionElement.selectMessage}.");
  210. selectCardEffect.SetUpCustomMessage_ShowCard("Selected Trash Card");
  211. selectCardEffect.SetAssembly();
  212. yield return StartCoroutine(selectCardEffect.Activate());
  213. IEnumerator SelectCardCoroutine(CardSource cardSource)
  214. {
  215. selectedAssemblyCards.Add(cardSource);
  216. yield return null;
  217. }
  218. IEnumerator AfterSelectCardCoroutine(List<CardSource> cardSources)
  219. {
  220. if (cardSources.Count == 0)
  221. {
  222. if (AssemblyConditionElement != null)
  223. {
  224. if (AssemblyConditionElement.CanTargetCondition_ByPreSelecetedList != null || AssemblyConditionElement.skipAllIfNoSelect)
  225. {
  226. EndSelectAssembly();
  227. }
  228. }
  229. }
  230. yield return null;
  231. }
  232. }
  233. }
  234. #endregion
  235. #region End Select Assembly
  236. IEnumerator EndSelectAssembly()
  237. {
  238. _endSelectAssembly = true;
  239. yield return null;
  240. }
  241. #endregion
  242. #region Add Digivolution Cards
  243. public IEnumerator AddDigivolutiuonCards(CardSource card)
  244. {
  245. if (card != null)
  246. {
  247. if (card == playCard)
  248. {
  249. if (card.PermanentOfThisCard() != null)
  250. {
  251. if (selectedAssemblyCards.Count == playCard.assemblyCondition.elementCount)
  252. {
  253. yield return ContinuousController.instance.StartCoroutine(GManager.instance.GetComponent<Effects>().ShowCardEffect(selectedAssemblyCards, "Assembly Cards", true, true));
  254. foreach (CardSource cardSource in selectedAssemblyCards)
  255. {
  256. if (isTrashCard(cardSource))
  257. {
  258. yield return ContinuousController.instance.StartCoroutine(GManager.instance.GetComponent<Effects>().CreateAssemblySelectCardEffect(null, player: cardSource.Owner));
  259. yield return ContinuousController.instance.StartCoroutine(card.PermanentOfThisCard().AddDigivolutionCardsBottom(new List<CardSource>() { cardSource }, null));
  260. }
  261. }
  262. }
  263. }
  264. }
  265. }
  266. ResetSelectAssemblyClass();
  267. yield return null;
  268. }
  269. #endregion
  270. #region Add Digivolution Card
  271. public IEnumerator AddDigivolutiuonCardsByEffect(CardSource card)
  272. {
  273. if (addDigivolutionCardInfos.Count >= 1)
  274. {
  275. if (card != null)
  276. {
  277. if (card.PermanentOfThisCard() != null)
  278. {
  279. List<CardSource> addedCards = new List<CardSource>();
  280. foreach (AddDigivolutionCardsInfo info in addDigivolutionCardInfos)
  281. {
  282. List<CardSource> underTamerCards = new List<CardSource>();
  283. List<Permanent> digimonPermanents = new List<Permanent>();
  284. List<CardSource> trashCards = new List<CardSource>();
  285. foreach (CardSource cardSource in info.cardSources)
  286. {
  287. if (isTrashCard(cardSource))
  288. {
  289. trashCards.Add(cardSource);
  290. addedCards.Add(cardSource);
  291. }
  292. }
  293. if (trashCards.Count >= 1)
  294. {
  295. yield return ContinuousController.instance.StartCoroutine(card.PermanentOfThisCard().AddDigivolutionCardsBottom(trashCards, info.cardEffect));
  296. }
  297. }
  298. yield return ContinuousController.instance.StartCoroutine(GManager.instance.GetComponent<Effects>().ShowCardEffect2(addedCards, "Digivolution Cards", true, true));
  299. }
  300. }
  301. }
  302. addDigivolutionCardInfos = new List<AddDigivolutionCardsInfo>();
  303. yield return null;
  304. }
  305. #endregion
  306. bool _endSelectAssembly = false;
  307. }