SelectAssemblyClass.cs 14 KB

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