CardEffectCommons.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System;
  4. using System.Linq;
  5. using UnityEngine;
  6. using UnityEditor;
  7. public partial class CardEffectCommons
  8. {
  9. #region Play cards as new permanents
  10. public static IEnumerator PlayPermanentCards(List<CardSource> cardSources, ICardEffect activateClass, bool payCost, bool isTapped, SelectCardEffect.Root root, bool activateETB, bool isBreedingArea = false)
  11. {
  12. if (cardSources == null) yield break;
  13. cardSources = cardSources
  14. .Filter(cardSource => cardSource != null)
  15. .Filter(cardSource => CanPlayAsNewPermanent(
  16. cardSource: cardSource,
  17. payCost: payCost,
  18. cardEffect: activateClass,
  19. isBreedingArea: isBreedingArea));
  20. if (cardSources.Count == 0) yield break;
  21. PlayCardClass playCardClass = new PlayCardClass(
  22. cardSources: cardSources,
  23. hashtable: CardEffectHashtable(activateClass),
  24. payCost: payCost,
  25. targetPermanent: null,
  26. isTapped: isTapped,
  27. root: root,
  28. activateETB: activateETB);
  29. if (isBreedingArea)
  30. {
  31. playCardClass.SetIsBreedingArea();
  32. }
  33. yield return ContinuousController.instance.StartCoroutine(playCardClass.PlayCard());
  34. }
  35. #endregion
  36. #region Play option cards
  37. public static IEnumerator PlayOptionCards(List<CardSource> cardSources, ICardEffect activateClass, bool payCost, SelectCardEffect.Root root,
  38. bool setAddSecurityEndOption = false)
  39. {
  40. if (cardSources == null) yield break;
  41. cardSources = cardSources
  42. .Filter(cardSource => cardSource != null)
  43. .Filter(cardSource => !cardSource.CanNotPlayThisOption);
  44. if (cardSources.Count == 0) yield break;
  45. PlayCardClass playCard = new PlayCardClass(
  46. cardSources: cardSources,
  47. hashtable: CardEffectHashtable(activateClass),
  48. payCost: payCost,
  49. targetPermanent: null,
  50. isTapped: false,
  51. root: root,
  52. activateETB: true);
  53. if (activateClass != null)
  54. {
  55. playCard.SetShowEffect();
  56. }
  57. if (setAddSecurityEndOption)
  58. {
  59. playCard.SetAddSecurityEndOption();
  60. }
  61. yield return ContinuousController.instance.StartCoroutine(playCard.PlayCard());
  62. }
  63. #endregion
  64. #region Play Delay Option cards as new permanents
  65. public static IEnumerator PlaceDelayOptionCards(CardSource card, ICardEffect cardEffect, SelectCardEffect.Root root = SelectCardEffect.Root.Execution)
  66. {
  67. if (CanPlayAsNewPermanent(cardSource: card, payCost: false, cardEffect: cardEffect, isPlayOption: true))
  68. {
  69. PlayPermanentClass playPermanent = new PlayPermanentClass(
  70. cardSources: new List<CardSource>() { card },
  71. hashtable: CardEffectHashtable(cardEffect),
  72. targetPermanent: null,
  73. isTapped: false,
  74. root: root,
  75. ActivateETB: true);
  76. playPermanent.SetISPlayOption();
  77. yield return ContinuousController.instance.StartCoroutine(playPermanent.PlayPermanent());
  78. if (IsExistOnBattleArea(card))
  79. {
  80. card.PermanentOfThisCard().IsPlayedOptionPermanent = true;
  81. }
  82. }
  83. }
  84. #endregion
  85. #region Play 1 Token
  86. public static IEnumerator PlayToken(CEntity_Base tokenData, ICardEffect activateClass, bool isOwnerPermanent, bool isTapped)
  87. {
  88. if (activateClass == null) yield break;
  89. if (activateClass.EffectSourceCard == null) yield break;
  90. if (tokenData == null) yield break;
  91. CardSource card = activateClass.EffectSourceCard;
  92. Player player = isOwnerPermanent ? card.Owner : card.Owner.Enemy;
  93. if (card.Owner.fieldCardFrames.Count((frame) => frame.IsEmptyFrame() && frame.IsBattleAreaFrame()) >= 1)
  94. {
  95. CardSource tokenCard = CardObjectController.CreateCardSource(
  96. player.PlayerID,
  97. tokenData,
  98. true);
  99. if (CanPlayAsNewPermanent(cardSource: tokenCard, payCost: false, cardEffect: activateClass))
  100. {
  101. yield return ContinuousController.instance.StartCoroutine(new PlayCardClass(
  102. cardSources: new List<CardSource>() { tokenCard },
  103. hashtable: CardEffectHashtable(activateClass),
  104. payCost: false,
  105. targetPermanent: null,
  106. isTapped: isTapped,
  107. root: SelectCardEffect.Root.None,
  108. activateETB: true).PlayCard());
  109. }
  110. }
  111. }
  112. #endregion
  113. #region Play 1 [Diaboromon] Token
  114. public static IEnumerator PlayDiaboromonToken(ICardEffect activateClass)
  115. {
  116. yield return ContinuousController.instance.StartCoroutine(PlayToken(
  117. tokenData: ContinuousController.instance.DiaboromonToken,
  118. activateClass: activateClass,
  119. isOwnerPermanent: true,
  120. isTapped: false
  121. ));
  122. }
  123. #endregion
  124. #region Play 1 [Amon of Crimson Flame] Token
  125. public static IEnumerator PlayAmonToken(ICardEffect activateClass)
  126. {
  127. yield return ContinuousController.instance.StartCoroutine(PlayToken(
  128. tokenData: ContinuousController.instance.AmonToken,
  129. activateClass: activateClass,
  130. isOwnerPermanent: true,
  131. isTapped: false
  132. ));
  133. }
  134. #endregion
  135. #region Play 1 [Umon of Blue Thunder] Token
  136. public static IEnumerator PlayUmonToken(ICardEffect activateClass)
  137. {
  138. yield return ContinuousController.instance.StartCoroutine(PlayToken(
  139. tokenData: ContinuousController.instance.UmonToken,
  140. activateClass: activateClass,
  141. isOwnerPermanent: true,
  142. isTapped: false
  143. ));
  144. }
  145. #endregion
  146. #region Play 1 [Fujitsumon] Token
  147. public static IEnumerator PlayFujitsumonToken(ICardEffect activateClass, bool isOwnerPermanent)
  148. {
  149. yield return ContinuousController.instance.StartCoroutine(PlayToken(
  150. tokenData: ContinuousController.instance.FujitsumonToken,
  151. activateClass: activateClass,
  152. isOwnerPermanent: isOwnerPermanent,
  153. isTapped: true
  154. ));
  155. }
  156. #endregion
  157. #region Security effect of "add this card to hand"
  158. public static IEnumerator AddThisCardToHand(CardSource card1, ICardEffect activateClass)
  159. {
  160. yield return new WaitForSeconds(0.5f);
  161. yield return ContinuousController.instance.StartCoroutine(card1.Owner.brainStormObject.CloseBrainstrorm(card1));
  162. yield return ContinuousController.instance.StartCoroutine(CardObjectController.AddHandCards(new List<CardSource>() { card1 }, false, activateClass));
  163. }
  164. #endregion
  165. #region Delete target permanents, and the effect determines whether the permanent has been deleted or not
  166. public static IEnumerator DeletePeremanentAndProcessAccordingToResult(List<Permanent> targetPermanents, ICardEffect activateClass, Func<List<Permanent>, IEnumerator> successProcess, Func<IEnumerator> failureProcess)
  167. {
  168. DestroyPermanentsClass destroyPermanentsClass = new DestroyPermanentsClass(targetPermanents, CardEffectHashtable(activateClass));
  169. yield return ContinuousController.instance.StartCoroutine(destroyPermanentsClass.Destroy());
  170. if (targetPermanents.Some((permanent) => destroyPermanentsClass.IsDestroyed(permanent)))
  171. {
  172. if (successProcess != null)
  173. {
  174. yield return ContinuousController.instance.StartCoroutine(successProcess(destroyPermanentsClass.DestroyedPermanents));
  175. }
  176. }
  177. else
  178. {
  179. if (failureProcess != null)
  180. {
  181. yield return ContinuousController.instance.StartCoroutine(failureProcess());
  182. }
  183. }
  184. }
  185. #endregion
  186. #region Bounce target 1 permanent, and the effect determines whether the permanent has been bounced or not
  187. public static IEnumerator BouncePeremanentAndProcessAccordingToResult(List<Permanent> targetPermanents, ICardEffect activateClass, IEnumerator successProcess, IEnumerator failureProcess)
  188. {
  189. HandBounceClaass bouncePermanentClass = new HandBounceClaass(targetPermanents, CardEffectHashtable(activateClass));
  190. yield return ContinuousController.instance.StartCoroutine(bouncePermanentClass.Bounce());
  191. if (targetPermanents.Some((permanent) => bouncePermanentClass.IsBounced(permanent)))
  192. {
  193. if (successProcess != null)
  194. {
  195. yield return ContinuousController.instance.StartCoroutine(successProcess);
  196. }
  197. }
  198. else
  199. {
  200. if (failureProcess != null)
  201. {
  202. yield return ContinuousController.instance.StartCoroutine(failureProcess);
  203. }
  204. }
  205. }
  206. #endregion
  207. #region Deck Bounce target 1 permanent, and the effect determines whether the permanent has been bounced or not
  208. public static IEnumerator DeckBouncePeremanentAndProcessAccordingToResult(List<Permanent> targetPermanents, ICardEffect activateClass, IEnumerator successProcess, IEnumerator failureProcess)
  209. {
  210. DeckBottomBounceClass deckBounceClass = new DeckBottomBounceClass(targetPermanents, CardEffectHashtable(activateClass));
  211. yield return ContinuousController.instance.StartCoroutine(deckBounceClass.DeckBounce());
  212. if (targetPermanents.Some((permanent) => deckBounceClass.IsDeckBounced(permanent)))
  213. {
  214. if (successProcess != null)
  215. {
  216. yield return ContinuousController.instance.StartCoroutine(successProcess);
  217. }
  218. }
  219. else
  220. {
  221. if (failureProcess != null)
  222. {
  223. yield return ContinuousController.instance.StartCoroutine(failureProcess);
  224. }
  225. }
  226. }
  227. #endregion
  228. #region Trash digivolution cards from top or bottom
  229. public static IEnumerator TrashDigivolutionCardsFromTopOrBottom(Permanent targetPermanent, int trashCount, bool isFromTop, ICardEffect activateClass)
  230. {
  231. if (activateClass == null) yield break;
  232. if (targetPermanent == null) yield break;
  233. if (targetPermanent.TopCard == null) yield break;
  234. if (targetPermanent.DigivolutionCards.Count((cardSource) => !cardSource.CanNotTrashFromDigivolutionCards(activateClass)) == 0) yield break;
  235. if (targetPermanent.TopCard.CanNotBeAffected(activateClass)) yield break;
  236. if (trashCount <= 0) yield break;
  237. List<CardSource> trashTargetCards = new List<CardSource>();
  238. for (int i = 0; i < trashCount; i++)
  239. {
  240. if (targetPermanent.DigivolutionCards.Count >= i + 1)
  241. {
  242. int index = isFromTop ? i : targetPermanent.DigivolutionCards.Count - 1 - i;
  243. CardSource trashTargetCard = targetPermanent.DigivolutionCards[index];
  244. trashTargetCards.Add(trashTargetCard);
  245. }
  246. }
  247. yield return ContinuousController.instance.StartCoroutine(new ITrashDigivolutionCards(targetPermanent, trashTargetCards, activateClass).TrashDigivolutionCards());
  248. }
  249. #endregion
  250. #region this Option card's Main effect
  251. public static ActivateClass OptionMainEffect(CardSource card) => card.EffectList(EffectTiming.OptionSkill).Find(cardEffect => cardEffect != null && cardEffect is ActivateClass && cardEffect.EffectDiscription.Contains("[Main]")) as ActivateClass;
  252. #endregion
  253. #region Add Option's Security effect that "Activate this card's Main effect" to cardEffects
  254. public static void AddActivateMainOptionSecurityEffect(CardSource card, ref List<ICardEffect> cardEffects, string effectName, string effectDiscription = "", Func<ICardEffect, IEnumerator> afterMainEffect = null)
  255. {
  256. if (OptionMainEffect(card) == null) return;
  257. cardEffects.Add(CardEffectFactory.ActivateMainOptionSecurityEffect(card, effectName, effectDiscription, afterMainEffect));
  258. }
  259. #endregion
  260. #region Target permanent Digivolves into Digimon card from hand or trash
  261. public static IEnumerator DigivolveIntoHandOrTrashCard(
  262. Permanent targetPermanent,
  263. Func<CardSource, bool> cardCondition,
  264. bool payCost,
  265. (int reduceCost, Func<CardSource, bool> reduceCostCardCondition)? reduceCostTuple,
  266. (int fixedCost, Func<CardSource, bool> fixedCostCardCondition)? fixedCostTuple,
  267. int ignoreDigivolutionRequirementFixedCost,
  268. bool isHand,
  269. ICardEffect activateClass,
  270. IEnumerator successProcess,
  271. bool ignoreLevel = false)
  272. {
  273. if (targetPermanent == null) yield break;
  274. if (targetPermanent.TopCard == null) yield break;
  275. if (activateClass == null) yield break;
  276. if (activateClass.EffectSourceCard == null) yield break;
  277. Player owner = targetPermanent.TopCard.Owner;
  278. SelectCardEffect.Root root = isHand ? SelectCardEffect.Root.Hand : SelectCardEffect.Root.Trash;
  279. bool ignoreDigivolutionRequirement = ignoreDigivolutionRequirementFixedCost >= 0;
  280. int fixedCost = -1;
  281. if (fixedCostTuple != null)
  282. {
  283. fixedCost = fixedCostTuple.Value.fixedCost;
  284. }
  285. if (ignoreDigivolutionRequirement)
  286. {
  287. fixedCost = ignoreDigivolutionRequirementFixedCost;
  288. }
  289. bool CanSelectCardCondition(CardSource cardSource)
  290. {
  291. if (cardSource.IsDigimon)
  292. {
  293. if (cardCondition == null || cardCondition(cardSource))
  294. {
  295. if (!cardSource.CanNotEvolve(targetPermanent))
  296. {
  297. if (ignoreLevel
  298. || cardSource.CanPlayCardTargetFrame(
  299. frame: targetPermanent.PermanentFrame,
  300. PayCost: payCost,
  301. cardEffect: activateClass,
  302. root: root,
  303. fixedCost: fixedCost))
  304. {
  305. return true;
  306. }
  307. }
  308. }
  309. }
  310. return false;
  311. }
  312. #region reduce cost
  313. Func<EffectTiming, ICardEffect> getChangeCostEffect = null;
  314. if (reduceCostTuple != null)
  315. {
  316. bool PermanentCondition(Permanent permanent)
  317. {
  318. return permanent == targetPermanent;
  319. }
  320. bool CardCondition(CardSource cardSource)
  321. {
  322. if (cardSource != null)
  323. {
  324. if (cardSource.Owner == owner)
  325. {
  326. if (reduceCostTuple.Value.reduceCostCardCondition == null || reduceCostTuple.Value.reduceCostCardCondition(cardSource))
  327. {
  328. return true;
  329. }
  330. }
  331. }
  332. return false;
  333. }
  334. getChangeCostEffect = ChangeDigivolutionCostPlayerEffect(
  335. permanentCondition: PermanentCondition,
  336. cardCondition: CardCondition,
  337. rootCondition: null,
  338. changeValue: -reduceCostTuple.Value.reduceCost,
  339. activateClass: activateClass,
  340. setFixedCost: false);
  341. if (getChangeCostEffect != null)
  342. {
  343. AddEffectToPlayer(
  344. effectDuration: EffectDuration.UntilCalculateFixedCost,
  345. card: targetPermanent.TopCard,
  346. cardEffect: null,
  347. timing: EffectTiming.None,
  348. getCardEffect: getChangeCostEffect);
  349. }
  350. }
  351. #endregion
  352. #region set fixed cost
  353. Func<EffectTiming, ICardEffect> getFixedCostEffect = null;
  354. if (fixedCostTuple != null)
  355. {
  356. bool PermanentCondition(Permanent permanent)
  357. {
  358. return permanent == targetPermanent;
  359. }
  360. bool CardCondition(CardSource cardSource)
  361. {
  362. if (cardSource != null)
  363. {
  364. if (cardSource.Owner == owner)
  365. {
  366. if (fixedCostTuple.Value.fixedCostCardCondition == null || fixedCostTuple.Value.fixedCostCardCondition(cardSource))
  367. {
  368. return true;
  369. }
  370. }
  371. }
  372. return false;
  373. }
  374. getFixedCostEffect = ChangeDigivolutionCostPlayerEffect(
  375. permanentCondition: PermanentCondition,
  376. cardCondition: CardCondition,
  377. rootCondition: null,
  378. changeValue: fixedCost,
  379. activateClass: activateClass,
  380. setFixedCost: true);
  381. if (getChangeCostEffect != null)
  382. {
  383. AddEffectToPlayer(
  384. effectDuration: EffectDuration.UntilCalculateFixedCost,
  385. card: targetPermanent.TopCard,
  386. cardEffect: null,
  387. timing: EffectTiming.None,
  388. getCardEffect: getFixedCostEffect);
  389. }
  390. }
  391. #endregion
  392. #region ignore digivolution requirement
  393. Func<EffectTiming, ICardEffect> getIgnoreDigivolutionRequirementEffect = null;
  394. if (ignoreDigivolutionRequirement)
  395. {
  396. bool PermanentCondition(Permanent permanent)
  397. {
  398. return permanent == targetPermanent;
  399. }
  400. bool CardCondition(CardSource cardSource)
  401. {
  402. if (cardSource == null)
  403. {
  404. return false;
  405. }
  406. if (cardSource.Owner != owner)
  407. {
  408. return false;
  409. }
  410. if (cardCondition == null || cardCondition(cardSource))
  411. {
  412. return true;
  413. }
  414. return false;
  415. }
  416. getIgnoreDigivolutionRequirementEffect = GainIgnoreDigivolutionRequirementPlayerEffect(
  417. permanentCondition: PermanentCondition,
  418. cardCondition: CardCondition,
  419. ignoreDigivolutionRequirement: ignoreDigivolutionRequirement,
  420. digivolutionCost: fixedCost,
  421. activateClass: activateClass);
  422. if (getIgnoreDigivolutionRequirementEffect != null)
  423. {
  424. AddEffectToPlayer(
  425. effectDuration: EffectDuration.UntilCalculateFixedCost,
  426. card: targetPermanent.TopCard,
  427. cardEffect: null,
  428. timing: EffectTiming.None,
  429. getCardEffect: getIgnoreDigivolutionRequirementEffect);
  430. }
  431. }
  432. #endregion
  433. List<CardSource> selectedCards = new List<CardSource>();
  434. IEnumerator SelectCardCoroutine(CardSource cardSource)
  435. {
  436. selectedCards.Add(cardSource);
  437. yield return null;
  438. }
  439. if (isHand)
  440. {
  441. if (owner.HandCards.Count(CanSelectCardCondition) >= 1)
  442. {
  443. int maxCount = 1;
  444. SelectHandEffect selectHandEffect = GManager.instance.GetComponent<SelectHandEffect>();
  445. selectHandEffect.SetUp(
  446. selectPlayer: owner,
  447. canTargetCondition: CanSelectCardCondition,
  448. canTargetCondition_ByPreSelecetedList: null,
  449. canEndSelectCondition: null,
  450. maxCount: maxCount,
  451. canNoSelect: true,
  452. canEndNotMax: false,
  453. isShowOpponent: true,
  454. selectCardCoroutine: SelectCardCoroutine,
  455. afterSelectCardCoroutine: null,
  456. mode: SelectHandEffect.Mode.Custom,
  457. cardEffect: activateClass);
  458. selectHandEffect.SetUpCustomMessage("Select 1 card to digivolve.", "The opponent is selecting 1 card to digivolve.");
  459. selectHandEffect.SetUpCustomMessage_ShowCard("Selected Card");
  460. yield return ContinuousController.instance.StartCoroutine(selectHandEffect.Activate());
  461. }
  462. }
  463. else
  464. {
  465. if (HasMatchConditionOwnersCardInTrash(targetPermanent.TopCard, CanSelectCardCondition))
  466. {
  467. int maxCount = 1;
  468. SelectCardEffect selectCardEffect = GManager.instance.GetComponent<SelectCardEffect>();
  469. selectCardEffect.SetUp(
  470. canTargetCondition: CanSelectCardCondition,
  471. canTargetCondition_ByPreSelecetedList: null,
  472. canEndSelectCondition: null,
  473. canNoSelect: () => true,
  474. selectCardCoroutine: SelectCardCoroutine,
  475. afterSelectCardCoroutine: null,
  476. message: "Select 1 card to digivolve.",
  477. maxCount: maxCount,
  478. canEndNotMax: false,
  479. isShowOpponent: true,
  480. mode: SelectCardEffect.Mode.Custom,
  481. root: SelectCardEffect.Root.Trash,
  482. customRootCardList: null,
  483. canLookReverseCard: true,
  484. selectPlayer: owner,
  485. cardEffect: activateClass);
  486. selectCardEffect.SetUpCustomMessage("Select 1 card to digivolve.", "The opponent is selecting 1 card to digivolve.");
  487. selectCardEffect.SetUpCustomMessage_ShowCard("Selected Card");
  488. yield return ContinuousController.instance.StartCoroutine(selectCardEffect.Activate());
  489. }
  490. }
  491. PlayCardClass playCardClass = new PlayCardClass(
  492. cardSources: selectedCards,
  493. hashtable: CardEffectHashtable(activateClass),
  494. payCost: payCost,
  495. targetPermanent: targetPermanent,
  496. isTapped: false,
  497. root: root,
  498. activateETB: true);
  499. if (ignoreLevel) playCardClass.SetIgnoreLevel();
  500. if (!ignoreDigivolutionRequirement && fixedCost >= 0) playCardClass.SetFixedCost(fixedCost);
  501. yield return ContinuousController.instance.StartCoroutine(playCardClass.PlayCard());
  502. #region release effect
  503. if (getChangeCostEffect != null) owner.UntilCalculateFixedCostEffect.Remove(getChangeCostEffect);
  504. #endregion
  505. #region release effect
  506. if (getFixedCostEffect != null) owner.UntilCalculateFixedCostEffect.Remove(getFixedCostEffect);
  507. #endregion
  508. #region release effect
  509. if (getIgnoreDigivolutionRequirementEffect != null) owner.UntilCalculateFixedCostEffect.Remove(getIgnoreDigivolutionRequirementEffect);
  510. #endregion
  511. if (selectedCards.Count >= 1)
  512. {
  513. if (IsDigivolvedByTheEffect(targetPermanent, selectedCards[0], activateClass))
  514. {
  515. if (successProcess != null)
  516. {
  517. yield return ContinuousController.instance.StartCoroutine(successProcess);
  518. }
  519. }
  520. }
  521. }
  522. #endregion
  523. #region Get the effect given by EffectTiming
  524. public static Func<EffectTiming, ICardEffect> GetCardEffectByEffectTiming(EffectTiming timing, ICardEffect cardEffect) => (_timing) => _timing == timing ? cardEffect : null;
  525. #endregion
  526. }