TrashLinkedCards.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System.Linq;
  5. using Photon;
  6. using System;
  7. using Photon.Pun;
  8. public partial class CardEffectCommons
  9. {
  10. public static IEnumerator SelectTrashLinkedCards(Func<Permanent, bool> permanentCondition, Func<CardSource, bool> cardCondition, int maxCount, bool canNoTrash, bool isFromOnly1Permanent, ICardEffect activateClass, string selectString = "Digimon", Func<Permanent, List<CardSource>, IEnumerator> afterSelectionCoroutine = null)
  11. {
  12. if (maxCount <= 0) yield break;
  13. if (activateClass == null) yield break;
  14. CardSource card = activateClass.EffectSourceCard;
  15. if (card == null) yield break;
  16. bool CanSelectPermanentCondition(Permanent permanent)
  17. {
  18. if (IsPermanentExistsOnBattleArea(permanent))
  19. {
  20. if (permanentCondition == null || permanentCondition(permanent))
  21. {
  22. if (!permanent.TopCard.CanNotBeAffected(activateClass))
  23. {
  24. return true;
  25. }
  26. }
  27. }
  28. return false;
  29. }
  30. bool CanSelectCardCondition(CardSource cardSource)
  31. {
  32. if (cardCondition == null || cardCondition(cardSource))
  33. {
  34. return true;
  35. }
  36. return false;
  37. }
  38. int permanentSum =
  39. GManager.instance.turnStateMachine.gameContext.Players_ForTurnPlayer
  40. .Map(player => player.GetBattleAreaPermanents().Filter(CanSelectPermanentCondition))
  41. .Flat().Count();
  42. int linkedCardsSum =
  43. GManager.instance.turnStateMachine.gameContext.Players_ForTurnPlayer
  44. .Map(player => player.GetBattleAreaPermanents().Filter(CanSelectPermanentCondition))
  45. .Flat()
  46. .Map(permanent => permanent.LinkedCards.Map(link => link.Source).Count(CanSelectCardCondition))
  47. .Sum();
  48. int maxLinkDiscardCount = Math.Min(linkedCardsSum, maxCount);
  49. int linkDiscardedCount = 0;
  50. void EndSelection() => linkDiscardedCount = linkedCardsSum;
  51. bool NotSelectYet() => linkDiscardedCount == 0;
  52. int permanentSelectedCount = 0;
  53. while (true)
  54. {
  55. if (isFromOnly1Permanent)
  56. {
  57. if (permanentSelectedCount >= 1)
  58. {
  59. break;
  60. }
  61. }
  62. if (linkDiscardedCount >= maxLinkDiscardCount)
  63. {
  64. break;
  65. }
  66. if (permanentSum < 1)
  67. {
  68. break;
  69. }
  70. else
  71. {
  72. permanentSelectedCount++;
  73. maxCount = Math.Min(1, permanentSum);
  74. SelectPermanentEffect selectPermanentEffect = GManager.instance.GetComponent<SelectPermanentEffect>();
  75. selectPermanentEffect.SetUp(
  76. selectPlayer: card.Owner,
  77. canTargetCondition: CanSelectPermanentCondition,
  78. canTargetCondition_ByPreSelecetedList: null,
  79. canEndSelectCondition: null,
  80. maxCount: maxCount,
  81. canNoSelect: canNoTrash && NotSelectYet(),
  82. canEndNotMax: false,
  83. selectPermanentCoroutine: SelectPermanentCoroutine,
  84. afterSelectPermanentCoroutine: AfterSelectPermanentCoroutine,
  85. mode: SelectPermanentEffect.Mode.Custom,
  86. cardEffect: activateClass);
  87. selectPermanentEffect.SetUpCustomMessage($"Select 1 {selectString} that will trash digivolution cards.", $"The opponent is selecting 1 {selectString} that will trash digivolution cards.");
  88. yield return ContinuousController.instance.StartCoroutine(selectPermanentEffect.Activate());
  89. IEnumerator SelectPermanentCoroutine(Permanent permanent)
  90. {
  91. Permanent selectedPermanent = permanent;
  92. if (selectedPermanent.DigivolutionCards.Count(CanSelectCardCondition) >= 1)
  93. {
  94. int trashMaxCountOfTargetPermanent = Math.Min(
  95. maxLinkDiscardCount - linkDiscardedCount,
  96. selectedPermanent.DigivolutionCards.Count(CanSelectCardCondition));
  97. List<CardSource> selectedCards = new List<CardSource>();
  98. SelectCardEffect selectCardEffect = GManager.instance.GetComponent<SelectCardEffect>();
  99. selectCardEffect.SetUp(
  100. canTargetCondition: CanSelectCardCondition,
  101. canTargetCondition_ByPreSelecetedList: null,
  102. canEndSelectCondition: null,
  103. canNoSelect: () => canNoTrash && NotSelectYet(),
  104. selectCardCoroutine: SelectCardCoroutine,
  105. afterSelectCardCoroutine: null,
  106. message: "Select linked cards to trash.",
  107. maxCount: trashMaxCountOfTargetPermanent,
  108. canEndNotMax: trashMaxCountOfTargetPermanent >= 2 && !isFromOnly1Permanent,
  109. isShowOpponent: true,
  110. mode: SelectCardEffect.Mode.Custom,
  111. root: SelectCardEffect.Root.Custom,
  112. customRootCardList: selectedPermanent.LinkedCards.Map(linked => linked.Source),
  113. canLookReverseCard: true,
  114. selectPlayer: card.Owner,
  115. cardEffect: activateClass);
  116. selectCardEffect.SetUpCustomMessage("Select linked cards to trash.", "The opponent is selecting linked cards to trash.");
  117. yield return ContinuousController.instance.StartCoroutine(selectCardEffect.Activate());
  118. IEnumerator SelectCardCoroutine(CardSource cardSource)
  119. {
  120. selectedCards.Add(cardSource);
  121. yield return null;
  122. }
  123. yield return ContinuousController.instance.StartCoroutine(new ITrashDigivolutionCards(
  124. selectedPermanent,
  125. selectedCards,
  126. activateClass).TrashDigivolutionCards());
  127. linkDiscardedCount += selectedCards.Count;
  128. if (canNoTrash && NotSelectYet())
  129. {
  130. EndSelection();
  131. }
  132. if (afterSelectionCoroutine != null)
  133. yield return ContinuousController.instance.StartCoroutine(afterSelectionCoroutine(selectedPermanent, selectedCards));
  134. }
  135. }
  136. IEnumerator AfterSelectPermanentCoroutine(List<Permanent> permanents)
  137. {
  138. if (permanents.Count == 0)
  139. {
  140. if (canNoTrash && NotSelectYet())
  141. {
  142. EndSelection();
  143. }
  144. }
  145. yield return null;
  146. }
  147. }
  148. }
  149. }
  150. }