SelectAssemblyClass.cs 14 KB

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