SelectAssemblyClass.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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 List<CardSource> selectedAssemblyCards = new List<CardSource>();
  12. public List<CardSource> PreviouslySelectedAssemblyCards { get; private set; } = new List<CardSource>();
  13. private List<PlayCardTuple> AllAssemblyCards = new List<PlayCardTuple>();
  14. void AddSelectedCard(CardSource cardSource)
  15. {
  16. selectedAssemblyCards.Add(cardSource);
  17. PreviouslySelectedAssemblyCards.Add(cardSource);
  18. }
  19. public List<CardSource> GetPreviouslySelected()
  20. {
  21. SelectDigiXrosClass selectDigiXrosClass = GManager.instance.GetComponent<SelectDigiXrosClass>();
  22. return AllAssemblyCards.Map(tuple => tuple.SelectedAssemblyCards).Flat().Concat(selectDigiXrosClass.PreviouslySelectedDigicrossCards).ToList();
  23. }
  24. private class PlayCardTuple
  25. {
  26. public CardSource PlayCard;
  27. public List<CardSource> SelectedAssemblyCards;
  28. public List<AddDigivolutionCardsInfo> AddDigivolutionCardInfos;
  29. }
  30. private PlayCardTuple GetTupleForCard(CardSource card)
  31. {
  32. return AllAssemblyCards.FirstOrDefault(tuple => tuple.PlayCard == card);
  33. }
  34. private PlayCardTuple GetOrMakeTupleForCard(CardSource card)
  35. {
  36. PlayCardTuple cardTuple = GetTupleForCard(card);
  37. if (cardTuple == null)
  38. {
  39. cardTuple = new()
  40. {
  41. PlayCard = card,
  42. SelectedAssemblyCards = new List<CardSource>(),
  43. AddDigivolutionCardInfos = new List<AddDigivolutionCardsInfo>()
  44. };
  45. AllAssemblyCards.Add(cardTuple);
  46. }
  47. return cardTuple;
  48. }
  49. public void ResetSelectAssemblyClass()
  50. {
  51. AllAssemblyCards = new List<PlayCardTuple>();
  52. selectedAssemblyCards = new List<CardSource>();
  53. PreviouslySelectedAssemblyCards = new List<CardSource>();
  54. }
  55. public void AddDigivolutionCardInfos(CardSource card, AddDigivolutionCardsInfo digivolutionCardsInfo)
  56. {
  57. PlayCardTuple cardTuple = GetOrMakeTupleForCard(card);
  58. cardTuple.AddDigivolutionCardInfos.Add(digivolutionCardsInfo);
  59. }
  60. #region Is Trash Card
  61. bool isTrashCard(CardSource cardSource)
  62. {
  63. if (cardSource != null)
  64. {
  65. if (CardEffectCommons.IsExistOnTrash(cardSource))
  66. {
  67. return true;
  68. }
  69. }
  70. return false;
  71. }
  72. #endregion
  73. #region Can Select Assembly
  74. bool CanSelectAssembly(AssemblyConditionElement element, CardSource targetCard, CardSource card)
  75. {
  76. if (card != targetCard)
  77. {
  78. if (card != null)
  79. {
  80. if (element != null)
  81. {
  82. if (element.CardCondition != null)
  83. {
  84. if (targetCard != null)
  85. {
  86. if (card.assemblyCondition != null)
  87. {
  88. if (!targetCard.IsToken)
  89. {
  90. if (!PreviouslySelectedAssemblyCards.Contains(targetCard))
  91. {
  92. List<AddDigivolutionCardsInfo> addDigivolutionCardInfos = GetAddDigivolutionCardInfos(card);
  93. if (addDigivolutionCardInfos.Count((addDigivolutionCardInfo) => addDigivolutionCardInfo.cardSources.Contains(targetCard)) == 0)
  94. {
  95. if (element.CanTargetCondition_ByPreSelecetedList != null)
  96. {
  97. if (!element.CanTargetCondition_ByPreSelecetedList(PreviouslySelectedAssemblyCards, targetCard))
  98. {
  99. return false;
  100. }
  101. }
  102. if (element.CardCondition(targetCard))
  103. {
  104. return true;
  105. }
  106. if (targetCard.PermanentOfThisCard() != null)
  107. {
  108. if (targetCard == targetCard.PermanentOfThisCard().TopCard)
  109. {
  110. if (targetCard.PermanentOfThisCard().CanSubstituteForAssemblyCondition(card))
  111. {
  112. return true;
  113. }
  114. }
  115. }
  116. }
  117. }
  118. }
  119. }
  120. }
  121. }
  122. }
  123. }
  124. }
  125. return false;
  126. }
  127. #endregion
  128. #region CanFulfillConditions
  129. bool CanFulfillConditions(CardSource card)
  130. {
  131. if (card != null)
  132. {
  133. if (card.HasAssembly)
  134. {
  135. AssemblyCondition AssemblyCondition = card.assemblyCondition;
  136. if (AssemblyCondition.elements != null)
  137. {
  138. if (AssemblyCondition.elements.Count == 1)
  139. return CardEffectCommons.MatchConditionOwnersCardCountInTrash(card, (cardSource) => CanSelectAssembly(AssemblyCondition.elements[0], cardSource, card)) >= AssemblyCondition.elementCount;
  140. else
  141. return CanFulfillEachElementCondition(card, AssemblyCondition);
  142. }
  143. }
  144. }
  145. return false;
  146. }
  147. bool CanFulfillEachElementCondition(CardSource card, AssemblyCondition AssemblyCondition, int index = 0, List<CardSource> usedCards = null, int currentElementCount = 0)
  148. {
  149. if (index < 0 || AssemblyCondition.elements == null)
  150. return false;
  151. if (index >= AssemblyCondition.elements.Count)
  152. return true;
  153. usedCards ??= PreviouslySelectedAssemblyCards;
  154. AssemblyConditionElement currentElement = AssemblyCondition.elements[index];
  155. List<CardSource> validCards = card.Owner.TrashCards.Filter(currentElement.CardCondition).Except(usedCards).ToList();
  156. foreach(CardSource validCard in validCards)
  157. {
  158. List<CardSource> addedSoFar = new List<CardSource>() { validCard };
  159. addedSoFar.AddRange(usedCards);
  160. if (currentElementCount + 1 >= currentElement.ElementCount)
  161. {
  162. if(CanFulfillEachElementCondition(card, AssemblyCondition, index+1, addedSoFar, 0))
  163. return true;
  164. }
  165. else
  166. {
  167. if (CanFulfillEachElementCondition(card, AssemblyCondition, index, addedSoFar, currentElementCount+1))
  168. return true;
  169. }
  170. }
  171. return false;
  172. }
  173. #endregion
  174. #region Select
  175. public IEnumerator Select(CardSource card)
  176. {
  177. GManager.instance.turnStateMachine.isSync = true;
  178. PlayCardTuple cardTuple = GetOrMakeTupleForCard(card);
  179. selectedAssemblyCards = cardTuple.SelectedAssemblyCards;
  180. PreviouslySelectedAssemblyCards = GetPreviouslySelected();
  181. if (CanFulfillConditions(card))
  182. {
  183. AssemblyCondition AssemblyCondition = card.assemblyCondition;
  184. foreach(AssemblyConditionElement element in AssemblyCondition.elements)
  185. {
  186. yield return GManager.instance.photonWaitController.StartWait("SelectAssemblys");
  187. if (selectedAssemblyCards.Count >= AssemblyCondition.elementCount)
  188. {
  189. yield return ContinuousController.instance.StartCoroutine(GManager.instance.GetComponent<Effects>().ShowCardEffect2(selectedAssemblyCards, "Assembly Cards", false, true));
  190. }
  191. if (_endSelectAssembly)
  192. {
  193. _endSelectAssembly = false;
  194. //break; TODO: Removed for not triggering Assembly in all situations
  195. }
  196. bool canSelectTrash = false;
  197. if (CardEffectCommons.MatchConditionOwnersCardCountInTrash(card, (cardSource) => CanSelectAssembly(element, cardSource, card)) >= element.ElementCount)
  198. {
  199. canSelectTrash = true;
  200. }
  201. if (canSelectTrash)
  202. {
  203. yield return ContinuousController.instance.StartCoroutine(SelectTrashCard(element, card));
  204. }
  205. }
  206. }
  207. if (selectedAssemblyCards.Count >= 1)
  208. {
  209. yield return new WaitForSeconds(0.4f);
  210. }
  211. GManager.instance.GetComponent<Effects>().OffShowCard2();
  212. GManager.instance.turnStateMachine.isSync = false;
  213. }
  214. #endregion
  215. #region Select Trash Card
  216. IEnumerator SelectTrashCard(AssemblyConditionElement AssemblyConditionElement, CardSource card)
  217. {
  218. bool CanSelectCardCondition(CardSource cardSource) => CanSelectAssembly(AssemblyConditionElement, cardSource, card);
  219. if (CardEffectCommons.MatchConditionOwnersCardCountInTrash(card, CanSelectCardCondition) >= AssemblyConditionElement.ElementCount)
  220. {
  221. int maxCount = AssemblyConditionElement.ElementCount;
  222. SelectCardEffect selectCardEffect = GManager.instance.GetComponent<SelectCardEffect>();
  223. selectCardEffect.SetUp(
  224. canTargetCondition: CanSelectCardCondition,
  225. canTargetCondition_ByPreSelecetedList: AssemblyConditionElement.CanTargetCondition_ByPreSelecetedList,
  226. canEndSelectCondition: null,
  227. canNoSelect: () => true,
  228. selectCardCoroutine: SelectCardCoroutine,
  229. afterSelectCardCoroutine: AfterSelectCardCoroutine,
  230. message: $"<color=#FF633E>Assembly</color>: Select {AssemblyConditionElement.selectMessage} from trash.",
  231. maxCount: maxCount,
  232. canEndNotMax: false,
  233. isShowOpponent: true,
  234. mode: SelectCardEffect.Mode.Custom,
  235. root: SelectCardEffect.Root.Trash,
  236. customRootCardList: card.Owner.TrashCards.Except(selectedAssemblyCards).ToList(),//don't include cards already chosen
  237. canLookReverseCard: true,
  238. selectPlayer: card.Owner,
  239. cardEffect: null);
  240. selectCardEffect.SetUpCustomMessage($"Select {AssemblyConditionElement.selectMessage}.", $"The opponent is selecting {AssemblyConditionElement.selectMessage}.");
  241. selectCardEffect.SetUpCustomMessage_ShowCard("Selected Trash Card");
  242. selectCardEffect.SetAssembly();
  243. yield return StartCoroutine(selectCardEffect.Activate());
  244. IEnumerator SelectCardCoroutine(CardSource cardSource)
  245. {
  246. selectedAssemblyCards.Add(cardSource);
  247. yield return null;
  248. }
  249. IEnumerator AfterSelectCardCoroutine(List<CardSource> cardSources)
  250. {
  251. if (cardSources.Count == 0)
  252. {
  253. if (AssemblyConditionElement != null)
  254. {
  255. if (AssemblyConditionElement.CanTargetCondition_ByPreSelecetedList != null || AssemblyConditionElement.skipAllIfNoSelect)
  256. {
  257. EndSelectAssembly();
  258. }
  259. }
  260. }
  261. yield return null;
  262. }
  263. }
  264. }
  265. #endregion
  266. #region End Select Assembly
  267. IEnumerator EndSelectAssembly()
  268. {
  269. _endSelectAssembly = true;
  270. yield return null;
  271. }
  272. #endregion
  273. #region Check if card was Assembled
  274. public bool WasAssembled(CardSource card)
  275. {
  276. return GetTupleForCard(card) != null;
  277. }
  278. #endregion
  279. #region Get Count of Digivolution Cards for Card
  280. public int GetSelectedCardCount(CardSource card)
  281. {
  282. PlayCardTuple cardTuple = GetTupleForCard(card);
  283. if (cardTuple != null)
  284. {
  285. return cardTuple.SelectedAssemblyCards.Count;
  286. }
  287. return 0;
  288. }
  289. #endregion
  290. #region Get AddDigivolutionCardInfos
  291. public List<AddDigivolutionCardsInfo> GetAddDigivolutionCardInfos(CardSource card)
  292. {
  293. PlayCardTuple cardTuple = GetOrMakeTupleForCard(card);
  294. return cardTuple.AddDigivolutionCardInfos;
  295. }
  296. #endregion
  297. #region Add Digivolution Cards
  298. public IEnumerator AddDigivolutiuonCards(CardSource card)
  299. {
  300. if (card != null)
  301. {
  302. PlayCardTuple cardTuple = GetTupleForCard(card);
  303. if (cardTuple != null)
  304. {
  305. if (card.PermanentOfThisCard() != null)
  306. {
  307. selectedAssemblyCards = cardTuple.SelectedAssemblyCards;
  308. if (selectedAssemblyCards.Count == cardTuple.PlayCard.assemblyCondition.elementCount)
  309. {
  310. yield return ContinuousController.instance.StartCoroutine(GManager.instance.GetComponent<Effects>().ShowCardEffect(selectedAssemblyCards, "Assembly Cards", true, true));
  311. foreach (CardSource cardSource in selectedAssemblyCards)
  312. {
  313. if (isTrashCard(cardSource))
  314. {
  315. yield return ContinuousController.instance.StartCoroutine(GManager.instance.GetComponent<Effects>().CreateAssemblySelectCardEffect(null, player: cardSource.Owner));
  316. yield return ContinuousController.instance.StartCoroutine(card.PermanentOfThisCard().AddDigivolutionCardsBottom(new List<CardSource>() { cardSource }, null));
  317. }
  318. }
  319. }
  320. }
  321. }
  322. }
  323. ResetSelectAssemblyClass();
  324. yield return null;
  325. }
  326. #endregion
  327. #region Add Digivolution Card
  328. public IEnumerator AddDigivolutiuonCardsByEffect(CardSource card)
  329. {
  330. PlayCardTuple cardTuple = GetTupleForCard(card);
  331. if (cardTuple != null)
  332. {
  333. List<AddDigivolutionCardsInfo> addDigivolutionCardInfos = cardTuple.AddDigivolutionCardInfos;
  334. if (addDigivolutionCardInfos.Count >= 1)
  335. {
  336. if (card != null)
  337. {
  338. if (card.PermanentOfThisCard() != null)
  339. {
  340. List<CardSource> addedCards = new List<CardSource>();
  341. foreach (AddDigivolutionCardsInfo info in addDigivolutionCardInfos)
  342. {
  343. List<CardSource> underTamerCards = new List<CardSource>();
  344. List<Permanent> digimonPermanents = new List<Permanent>();
  345. List<CardSource> trashCards = new List<CardSource>();
  346. foreach (CardSource cardSource in info.cardSources)
  347. {
  348. if (isTrashCard(cardSource))
  349. {
  350. trashCards.Add(cardSource);
  351. addedCards.Add(cardSource);
  352. }
  353. }
  354. if (trashCards.Count >= 1)
  355. {
  356. yield return ContinuousController.instance.StartCoroutine(card.PermanentOfThisCard().AddDigivolutionCardsBottom(trashCards, info.cardEffect));
  357. }
  358. }
  359. yield return ContinuousController.instance.StartCoroutine(GManager.instance.GetComponent<Effects>().ShowCardEffect2(addedCards, "Digivolution Cards", true, true));
  360. }
  361. }
  362. }
  363. }
  364. yield return null;
  365. }
  366. #endregion
  367. int _targetIndex = 0;
  368. bool _endSelect = false;
  369. bool _endSelectAssembly = false;
  370. [PunRPC]
  371. public void SetTargetAssemblysIndex(int targetIndex)
  372. {
  373. this._targetIndex = targetIndex;
  374. _endSelect = true;
  375. }
  376. }