AttackProcess.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Photon.Pun;
  5. using System;
  6. using System.Linq;
  7. using UnityEngine.UI;
  8. public class AttackProcess : MonoBehaviourPunCallbacks
  9. {
  10. public Permanent AttackingPermanent { get; private set; } = null;
  11. public Permanent DefendingPermanent { get; private set; } = null;
  12. public int AttackCount { get; set; } = 0;
  13. public bool IsAttacking { get; set; } = false;
  14. public bool IsBlocking { get; set; } = false;
  15. public CardSource SecurityDigimon { get; set; } = null;
  16. public bool DoSecurityCheck { get; set; } = false;
  17. public bool IsEndAttack { get; set; } = false;
  18. public bool UsedBlitz { get; set; } = false;
  19. void SetAttackerDefender(Permanent attackingPermanent, Permanent defendingPermanent)
  20. {
  21. AttackingPermanent = attackingPermanent;
  22. DefendingPermanent = defendingPermanent;
  23. }
  24. public IEnumerator Attack(Permanent attackingPermanent, Permanent defendingPermanent, ICardEffect attackEffect, bool withoutTap = false, Func<IEnumerator> beforeOnAttackCoroutine = null)
  25. {
  26. if (IsAttacking)
  27. {
  28. if (beforeOnAttackCoroutine != null)
  29. {
  30. yield return ContinuousController.instance.StartCoroutine(beforeOnAttackCoroutine());
  31. }
  32. yield break;
  33. }
  34. AttackingPermanent = null;
  35. DefendingPermanent = null;
  36. DoSecurityCheck = false;
  37. IsBlocking = false;
  38. SecurityDigimon = null;
  39. IsAttacking = false;
  40. IsEndAttack = false;
  41. SetAttackerDefender(attackingPermanent, defendingPermanent);
  42. Hashtable effectHashtable = CardEffectCommons.OnAttackCheckHashtableOfPermanent(AttackingPermanent, attackEffect);
  43. Hashtable counterEffectHashtable = CardEffectCommons.OnAttackCheckHashtableOfPermanent(new Permanent(AttackingPermanent.cardSources), attackEffect);
  44. IsAttacking = true;
  45. // check timing
  46. yield return ContinuousController.instance.StartCoroutine(GManager.instance.autoProcessing.AutoProcessCheck());
  47. GManager.instance.turnStateMachine.IsSelecting = true;
  48. // force to end attack
  49. if (IsEndAttack)
  50. {
  51. goto EndAttack;
  52. }
  53. #region Attack Process
  54. if (AttackingPermanent != null)
  55. {
  56. AttackCount++;
  57. GManager.instance.turnStateMachine.IsSelecting = true;
  58. if (DefendingPermanent != null)
  59. {
  60. DefendingPermanent.ShowingPermanentCard.SetOrangeOutline();
  61. DefendingPermanent.ShowingPermanentCard.Outline_Select.gameObject.SetActive(true);
  62. GManager.instance.turnStateMachine.gameContext.NonTurnPlayer.securityObject.securityBreakGlass.gameObject.SetActive(false);
  63. }
  64. else
  65. {
  66. if (GManager.instance.turnStateMachine.gameContext.NonTurnPlayer.SecurityCards.Count >= 1)
  67. {
  68. GManager.instance.turnStateMachine.gameContext.NonTurnPlayer.securityObject.securityBreakGlass.ShowBlueMatarial();
  69. }
  70. }
  71. AttackingPermanent.ShowingPermanentCard.SetOrangeOutline();
  72. AttackingPermanent.ShowingPermanentCard.Outline_Select.gameObject.SetActive(true);
  73. // add log
  74. string log = $"\nAttack:\n{AttackingPermanent.TopCard.BaseENGCardNameFromEntity}({AttackingPermanent.TopCard.CardID})";
  75. if (DefendingPermanent != null)
  76. {
  77. log += $"\n�«\n{DefendingPermanent.TopCard.BaseENGCardNameFromEntity}({DefendingPermanent.TopCard.CardID})\n";
  78. }
  79. else
  80. {
  81. log += $"\n�«\nSecurity\n";
  82. }
  83. PlayLog.OnAddLog?.Invoke(log);
  84. List<SkillInfo> stackedSkillInfos = new List<SkillInfo>();
  85. if (GManager.instance.autoProcessing.executingMultipleSkills != null)
  86. {
  87. if (GManager.instance.autoProcessing.executingMultipleSkills.StackedSkillInfos.Count >= 1)
  88. {
  89. foreach (SkillInfo skillInfo in GManager.instance.autoProcessing.executingMultipleSkills.StackedSkillInfos)
  90. {
  91. stackedSkillInfos.Add(skillInfo);
  92. }
  93. }
  94. foreach (SkillInfo skillInfo in stackedSkillInfos)
  95. {
  96. GManager.instance.autoProcessing.executingMultipleSkills.StackedSkillInfos.Remove(skillInfo);
  97. }
  98. }
  99. // suspend
  100. if (!withoutTap)
  101. {
  102. Hashtable attackerTapHashtable = new Hashtable()
  103. {
  104. {"IsAttack", true}
  105. };
  106. yield return ContinuousController.instance.StartCoroutine(new SuspendPermanentsClass(
  107. new List<Permanent>() { AttackingPermanent },
  108. attackerTapHashtable).Tap());
  109. }
  110. // target arrow
  111. if (DefendingPermanent == null)
  112. {
  113. yield return GManager.instance.OnTargetArrow(
  114. AttackingPermanent.PermanentFrame.GetLocalCanvasPosition() + AttackingPermanent.TopCard.Owner.playerUIObjectParent.localPosition,
  115. GManager.instance.turnStateMachine.gameContext.NonTurnPlayer.SecurityAttackLocalCanvasPosition + GManager.instance.turnStateMachine.gameContext.NonTurnPlayer.playerUIObjectParent.localPosition,
  116. null,
  117. null);
  118. }
  119. else
  120. {
  121. yield return GManager.instance.OnTargetArrow(
  122. AttackingPermanent.PermanentFrame.GetLocalCanvasPosition() + AttackingPermanent.TopCard.Owner.playerUIObjectParent.localPosition,
  123. DefendingPermanent.PermanentFrame.GetLocalCanvasPosition() + DefendingPermanent.TopCard.Owner.playerUIObjectParent.localPosition,
  124. null,
  125. null);
  126. }
  127. // activate cutin effects
  128. yield return ContinuousController.instance.StartCoroutine(GManager.instance.autoProcessing.RuleProcess());
  129. //yield return ContinuousController.instance.StartCoroutine(GManager.instance.autoProcessing_CutIn.TriggeredSkillProcess(true, null));
  130. // callback that is processed before [On Attack]
  131. if (beforeOnAttackCoroutine != null)
  132. {
  133. yield return ContinuousController.instance.StartCoroutine(beforeOnAttackCoroutine());
  134. }
  135. // trigger [On Attack] effect
  136. yield return ContinuousController.instance.StartCoroutine(GManager.instance.autoProcessing_CutIn.StackSkillInfos(
  137. effectHashtable,
  138. EffectTiming.OnAllyAttack));
  139. // activate cutin effects
  140. yield return ContinuousController.instance.StartCoroutine(GManager.instance.autoProcessing.RuleProcess());
  141. yield return ContinuousController.instance.StartCoroutine(GManager.instance.autoProcessing_CutIn.TriggeredSkillProcess(true, null));
  142. if (stackedSkillInfos.Count >= 1)
  143. {
  144. foreach (SkillInfo skillInfo in stackedSkillInfos)
  145. {
  146. GManager.instance.autoProcessing.PutStackedSkill(skillInfo);
  147. }
  148. }
  149. GManager.instance.turnStateMachine.IsSelecting = true;
  150. if (AttackingPermanent.TopCard != null)
  151. {
  152. AttackingPermanent.ShowingPermanentCard.SetOrangeOutline();
  153. AttackingPermanent.ShowingPermanentCard.Outline_Select.gameObject.SetActive(true);
  154. }
  155. if (DefendingPermanent != null)
  156. {
  157. if (DefendingPermanent.TopCard != null)
  158. {
  159. DefendingPermanent.ShowingPermanentCard.SetOrangeOutline();
  160. DefendingPermanent.ShowingPermanentCard.Outline_Select.gameObject.SetActive(true);
  161. }
  162. }
  163. // check timing
  164. yield return ContinuousController.instance.StartCoroutine(GManager.instance.autoProcessing.AutoProcessCheck());
  165. GManager.instance.turnStateMachine.IsSelecting = true;
  166. // force to end attack
  167. if (IsEndAttack)
  168. {
  169. goto EndAttack;
  170. }
  171. if (AttackingPermanent.TopCard != null)
  172. {
  173. AttackingPermanent.ShowingPermanentCard.SetOrangeOutline();
  174. AttackingPermanent.ShowingPermanentCard.Outline_Select.gameObject.SetActive(true);
  175. }
  176. if (DefendingPermanent != null)
  177. {
  178. if (DefendingPermanent.TopCard != null)
  179. {
  180. DefendingPermanent.ShowingPermanentCard.SetOrangeOutline();
  181. DefendingPermanent.ShowingPermanentCard.Outline_Select.gameObject.SetActive(true);
  182. }
  183. }
  184. // trigger effects when counter timing (except [Counter] effects)
  185. yield return ContinuousController.instance.StartCoroutine(GManager.instance.autoProcessing_CutIn.StackSkillInfos(
  186. counterEffectHashtable,
  187. EffectTiming.OnCounterTiming,
  188. cardEffect => !cardEffect.IsCounterEffect));
  189. // activate cutin effects
  190. yield return ContinuousController.instance.StartCoroutine(GManager.instance.autoProcessing.RuleProcess());
  191. yield return ContinuousController.instance.StartCoroutine(GManager.instance.autoProcessing_CutIn.TriggeredSkillProcess(true, null));
  192. GManager.instance.turnStateMachine.IsSelecting = true;
  193. // force to end attack
  194. if (IsEndAttack)
  195. {
  196. goto EndAttack;
  197. }
  198. // trigger effects when counter timing ([Counter] effects)
  199. yield return ContinuousController.instance.StartCoroutine(GManager.instance.autoProcessing_CutIn.StackSkillInfos(
  200. counterEffectHashtable,
  201. EffectTiming.OnCounterTiming,
  202. cardEffect => cardEffect.IsCounterEffect));
  203. bool HasCounterEffect(List<SkillInfo> skillInfos, SkillInfo skillInfo)
  204. {
  205. return skillInfos.Count((skillInfo1) => skillInfo1.CardEffect.IsCounterEffect) >= 1;
  206. }
  207. // activate cutin effects
  208. yield return ContinuousController.instance.StartCoroutine(GManager.instance.autoProcessing.RuleProcess());
  209. yield return ContinuousController.instance.StartCoroutine(GManager.instance.autoProcessing_CutIn.TriggeredSkillProcess(true, HasCounterEffect));
  210. GManager.instance.turnStateMachine.IsSelecting = true;
  211. // force to end attack
  212. if (IsEndAttack)
  213. {
  214. goto EndAttack;
  215. }
  216. if (AttackingPermanent.TopCard == null)
  217. {
  218. goto EndAttack;
  219. }
  220. AttackingPermanent.ShowingPermanentCard.SetOrangeOutline();
  221. AttackingPermanent.ShowingPermanentCard.Outline_Select.gameObject.SetActive(true);
  222. if (DefendingPermanent != null)
  223. {
  224. if (DefendingPermanent.TopCard != null)
  225. {
  226. DefendingPermanent.ShowingPermanentCard.SetOrangeOutline();
  227. DefendingPermanent.ShowingPermanentCard.Outline_Select.gameObject.SetActive(true);
  228. }
  229. }
  230. #region select blocker
  231. bool CanSelectBlockerCondition(Permanent permanent)
  232. {
  233. if (CardEffectCommons.IsPermanentExistsOnOpponentBattleAreaDigimon(permanent, AttackingPermanent.TopCard))
  234. {
  235. if (permanent != DefendingPermanent)
  236. {
  237. if (permanent.HasBlocker && permanent.CanBlock(AttackingPermanent))
  238. {
  239. return true;
  240. }
  241. }
  242. }
  243. return false;
  244. }
  245. if (AttackingPermanent.TopCard.Owner.Enemy.GetBattleAreaDigimons().Count(CanSelectBlockerCondition) >= 1)
  246. {
  247. int maxCount = 1;
  248. Permanent selectedPermanent = null;
  249. SelectPermanentEffect selectPermanentEffect = GManager.instance.GetComponent<SelectPermanentEffect>();
  250. selectPermanentEffect.SetUp(
  251. selectPlayer: AttackingPermanent.TopCard.Owner.Enemy,
  252. canTargetCondition: CanSelectBlockerCondition,
  253. canTargetCondition_ByPreSelecetedList: null,
  254. canEndSelectCondition: null,
  255. maxCount: maxCount,
  256. canNoSelect: (!IsBlocking),
  257. canEndNotMax: false,
  258. selectPermanentCoroutine: SelectPermanentCoroutine,
  259. afterSelectPermanentCoroutine: null,
  260. mode: SelectPermanentEffect.Mode.Custom,
  261. cardEffect: null);
  262. selectPermanentEffect.SetUpCustomMessage("Select 1 Digimon that will block.", "The opponent is selecting 1 Digimon that will block.");
  263. if(!IsBlocking)
  264. selectPermanentEffect.SetUpCustomBackButtonMessage("Not Block");
  265. yield return ContinuousController.instance.StartCoroutine(selectPermanentEffect.Activate());
  266. IEnumerator SelectPermanentCoroutine(Permanent permanent)
  267. {
  268. selectedPermanent = permanent;
  269. yield return null;
  270. }
  271. if (selectedPermanent != null)
  272. {
  273. yield return ContinuousController.instance.StartCoroutine(SwitchDefender(null, true, selectedPermanent));
  274. }
  275. }
  276. #endregion
  277. GManager.instance.turnStateMachine.IsSelecting = true;
  278. //end attack
  279. if (IsEndAttack)
  280. {
  281. goto EndAttack;
  282. }
  283. if (AttackingPermanent.TopCard == null)
  284. {
  285. goto EndAttack;
  286. }
  287. AttackingPermanent.ShowingPermanentCard.SetOrangeOutline();
  288. AttackingPermanent.ShowingPermanentCard.Outline_Select.gameObject.SetActive(true);
  289. if (DefendingPermanent != null)
  290. {
  291. if (DefendingPermanent.TopCard != null)
  292. {
  293. DefendingPermanent.ShowingPermanentCard.SetOrangeOutline();
  294. DefendingPermanent.ShowingPermanentCard.Outline_Select.gameObject.SetActive(true);
  295. }
  296. }
  297. #region there is no Defending Permanent
  298. if (DefendingPermanent == null)
  299. {
  300. if (AttackingPermanent.Strike >= 1)
  301. {
  302. //if there is no security, end the game
  303. if (AttackingPermanent.TopCard.Owner.Enemy.SecurityCards.Count == 0)
  304. {
  305. GManager.instance.turnStateMachine.EndGame(AttackingPermanent.TopCard.Owner, false);
  306. yield break;
  307. }
  308. }
  309. DoSecurityCheck = true;
  310. }
  311. #endregion
  312. #region there is Defending Permanent
  313. else
  314. {
  315. if (DefendingPermanent.TopCard == null || AttackingPermanent == null || AttackingPermanent.TopCard == null)
  316. {
  317. goto EndAttack;
  318. }
  319. DefendingPermanent.TopCard.Owner.securityObject.securityBreakGlass.gameObject.SetActive(false);
  320. DefendingPermanent.ShowingPermanentCard.SetOrangeOutline();
  321. DefendingPermanent.ShowingPermanentCard.Outline_Select.gameObject.SetActive(true);
  322. // battle
  323. IBattle battle = new IBattle(AttackingPermanent: AttackingPermanent, DefendingPermanent: DefendingPermanent, null);
  324. yield return ContinuousController.instance.StartCoroutine(battle.Battle());
  325. /*#region effect when determine whether to do security check
  326. Hashtable hashtable = new Hashtable()
  327. {
  328. {"battle", battle}
  329. };
  330. List<SkillInfo> skillInfos_Pierce = AutoProcessing.GetSkillInfos(hashtable, EffectTiming.OnDetermineDoSecurityCheck)
  331. .Filter(skillInfo => skillInfo.CardEffect != null && skillInfo.CardEffect.CanActivate(skillInfo.Hashtable));
  332. if (skillInfos_Pierce.Count >= 1)
  333. {
  334. GManager.instance.autoProcessing.PutStackedSkill(skillInfos_Pierce[0]);
  335. }
  336. #endregion*/
  337. yield return ContinuousController.instance.StartCoroutine(GManager.instance.autoProcessing.TriggeredSkillProcess(true, null));
  338. GManager.instance.turnStateMachine.IsSelecting = true;
  339. }
  340. #endregion
  341. if (AttackingPermanent.TopCard == null)
  342. {
  343. goto EndAttack;
  344. }
  345. #region do security check
  346. if (DoSecurityCheck && AttackingPermanent.TopCard.Owner.Enemy.SecurityCards.Count >= 1)
  347. {
  348. //security check process
  349. yield return ContinuousController.instance.StartCoroutine(new ISecurityCheck(
  350. AttackingPermanent: AttackingPermanent,
  351. player: GManager.instance.turnStateMachine.gameContext.NonTurnPlayer).SecurityCheck());
  352. }
  353. #endregion
  354. }
  355. else
  356. {
  357. if (beforeOnAttackCoroutine != null)
  358. {
  359. yield return ContinuousController.instance.StartCoroutine(beforeOnAttackCoroutine());
  360. }
  361. }
  362. #endregion
  363. #region End Attack
  364. EndAttack:;
  365. // [On End Attack] effect
  366. if (AttackingPermanent != null && AttackingPermanent.TopCard != null)
  367. {
  368. yield return ContinuousController.instance.StartCoroutine(GManager.instance.autoProcessing.StackSkillInfos(effectHashtable, EffectTiming.OnEndAttack));
  369. }
  370. #region reset effects which continues until the end of attack
  371. foreach (Player player in GManager.instance.turnStateMachine.gameContext.Players_ForTurnPlayer)
  372. {
  373. foreach (Permanent permanent in player.GetFieldPermanents())
  374. {
  375. permanent.UntilEndAttackEffects = new List<Func<EffectTiming, ICardEffect>>();
  376. }
  377. }
  378. #endregion
  379. GManager.instance.turnStateMachine.IsSelecting = true;
  380. #endregion
  381. DoSecurityCheck = false;
  382. IsBlocking = false;
  383. SecurityDigimon = null;
  384. IsAttacking = false;
  385. IsEndAttack = false;
  386. }
  387. public IEnumerator SwitchDefender(ICardEffect cardEffect, bool isBlock, Permanent newDefendingPermanent)
  388. {
  389. if (AttackingPermanent == null) yield break;
  390. if (AttackingPermanent.TopCard == null) yield break;
  391. if (newDefendingPermanent != null && newDefendingPermanent.TopCard == null) yield break;
  392. if (!AttackingPermanent.CanSwitchAttackTarget) yield break;
  393. Permanent oldDefendingPermanent = DefendingPermanent;
  394. DefendingPermanent = newDefendingPermanent;
  395. IsBlocking = isBlock;
  396. if (DefendingPermanent != null)
  397. {
  398. if (DefendingPermanent.ShowingPermanentCard != null)
  399. {
  400. DefendingPermanent.ShowingPermanentCard.SetOrangeOutline();
  401. DefendingPermanent.ShowingPermanentCard.Outline_Select.gameObject.SetActive(true);
  402. }
  403. }
  404. Hashtable hashtable = new Hashtable(){
  405. {"AttackingPermanent", AttackingPermanent},
  406. {"DefendingPermanent", DefendingPermanent},
  407. {"CardEffect", cardEffect}
  408. };
  409. if (isBlock)
  410. {
  411. hashtable.Add("IsBlock", isBlock);
  412. }
  413. if (isBlock)
  414. {
  415. if (DefendingPermanent != null)
  416. {
  417. // add log
  418. string log1 = $"\nBlock:\n{DefendingPermanent.TopCard.BaseENGCardNameFromEntity}({DefendingPermanent.TopCard.CardID})\n";
  419. PlayLog.OnAddLog?.Invoke(log1);
  420. // suspend
  421. yield return ContinuousController.instance.StartCoroutine(new SuspendPermanentsClass(new List<Permanent>() { DefendingPermanent }, hashtable).Tap());
  422. // the effects when blocking
  423. yield return ContinuousController.instance.StartCoroutine(GManager.instance.autoProcessing.StackSkillInfos(
  424. hashtable,
  425. EffectTiming.OnBlockAnyone));
  426. }
  427. }
  428. if (AttackingPermanent != null)
  429. {
  430. if (AttackingPermanent.TopCard == null)
  431. {
  432. IsBlocking = false;
  433. yield break;
  434. }
  435. }
  436. if (DefendingPermanent != null)
  437. {
  438. if (DefendingPermanent.TopCard == null)
  439. {
  440. IsBlocking = false;
  441. yield break;
  442. }
  443. }
  444. if (AttackingPermanent != null)
  445. {
  446. // target arrow
  447. for (int i = 0; i < 3; i++)
  448. {
  449. GManager.instance.OffTargetArrow();
  450. yield return null;
  451. }
  452. if (DefendingPermanent != null)
  453. {
  454. DefendingPermanent.ShowingPermanentCard.SetOrangeOutline();
  455. DefendingPermanent.ShowingPermanentCard.Outline_Select.gameObject.SetActive(true);
  456. yield return GManager.instance.OnTargetArrow(
  457. AttackingPermanent.PermanentFrame.GetLocalCanvasPosition() + AttackingPermanent.TopCard.Owner.playerUIObjectParent.localPosition,
  458. DefendingPermanent.PermanentFrame.GetLocalCanvasPosition() + DefendingPermanent.TopCard.Owner.playerUIObjectParent.localPosition,
  459. null,
  460. null);
  461. }
  462. else
  463. {
  464. yield return GManager.instance.OnTargetArrow(
  465. AttackingPermanent.PermanentFrame.GetLocalCanvasPosition() + AttackingPermanent.TopCard.Owner.playerUIObjectParent.localPosition,
  466. GManager.instance.turnStateMachine.gameContext.NonTurnPlayer.SecurityAttackLocalCanvasPosition + GManager.instance.turnStateMachine.gameContext.NonTurnPlayer.playerUIObjectParent.localPosition,
  467. null,
  468. null);
  469. }
  470. AttackingPermanent.ShowingPermanentCard.SetOrangeOutline();
  471. AttackingPermanent.ShowingPermanentCard.Outline_Select.gameObject.SetActive(true);
  472. }
  473. if (newDefendingPermanent != oldDefendingPermanent)
  474. {
  475. // the effects when attack target switched
  476. yield return ContinuousController.instance.StartCoroutine(GManager.instance.autoProcessing.StackSkillInfos(
  477. hashtable,
  478. EffectTiming.OnAttackTargetChanged));
  479. }
  480. }
  481. }