AttackProcess.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  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. // callback that is processed before [On Attack]
  128. if (beforeOnAttackCoroutine != null)
  129. {
  130. yield return ContinuousController.instance.StartCoroutine(beforeOnAttackCoroutine());
  131. }
  132. // trigger [On Attack] effect
  133. yield return ContinuousController.instance.StartCoroutine(GManager.instance.autoProcessing_CutIn.StackSkillInfos(
  134. effectHashtable,
  135. EffectTiming.OnAllyAttack));
  136. // activate cutin effects
  137. yield return ContinuousController.instance.StartCoroutine(GManager.instance.autoProcessing.RuleProcess());
  138. yield return ContinuousController.instance.StartCoroutine(GManager.instance.autoProcessing_CutIn.TriggeredSkillProcess(true, null));
  139. if (stackedSkillInfos.Count >= 1)
  140. {
  141. foreach (SkillInfo skillInfo in stackedSkillInfos)
  142. {
  143. GManager.instance.autoProcessing.PutStackedSkill(skillInfo);
  144. }
  145. }
  146. GManager.instance.turnStateMachine.IsSelecting = true;
  147. if (AttackingPermanent.TopCard != null)
  148. {
  149. AttackingPermanent.ShowingPermanentCard.SetOrangeOutline();
  150. AttackingPermanent.ShowingPermanentCard.Outline_Select.gameObject.SetActive(true);
  151. }
  152. if (DefendingPermanent != null)
  153. {
  154. if (DefendingPermanent.TopCard != null)
  155. {
  156. DefendingPermanent.ShowingPermanentCard.SetOrangeOutline();
  157. DefendingPermanent.ShowingPermanentCard.Outline_Select.gameObject.SetActive(true);
  158. }
  159. }
  160. // check timing
  161. yield return ContinuousController.instance.StartCoroutine(GManager.instance.autoProcessing.AutoProcessCheck());
  162. GManager.instance.turnStateMachine.IsSelecting = true;
  163. // force to end attack
  164. if (IsEndAttack)
  165. {
  166. goto EndAttack;
  167. }
  168. if (AttackingPermanent.TopCard != null)
  169. {
  170. AttackingPermanent.ShowingPermanentCard.SetOrangeOutline();
  171. AttackingPermanent.ShowingPermanentCard.Outline_Select.gameObject.SetActive(true);
  172. }
  173. if (DefendingPermanent != null)
  174. {
  175. if (DefendingPermanent.TopCard != null)
  176. {
  177. DefendingPermanent.ShowingPermanentCard.SetOrangeOutline();
  178. DefendingPermanent.ShowingPermanentCard.Outline_Select.gameObject.SetActive(true);
  179. }
  180. }
  181. // trigger effects when counter timing (except [Counter] effects)
  182. yield return ContinuousController.instance.StartCoroutine(GManager.instance.autoProcessing_CutIn.StackSkillInfos(
  183. counterEffectHashtable,
  184. EffectTiming.OnCounterTiming,
  185. cardEffect => !cardEffect.IsCounterEffect));
  186. // activate cutin effects
  187. yield return ContinuousController.instance.StartCoroutine(GManager.instance.autoProcessing.RuleProcess());
  188. yield return ContinuousController.instance.StartCoroutine(GManager.instance.autoProcessing_CutIn.TriggeredSkillProcess(true, null));
  189. GManager.instance.turnStateMachine.IsSelecting = true;
  190. // force to end attack
  191. if (IsEndAttack)
  192. {
  193. goto EndAttack;
  194. }
  195. // trigger effects when counter timing ([Counter] effects)
  196. yield return ContinuousController.instance.StartCoroutine(GManager.instance.autoProcessing_CutIn.StackSkillInfos(
  197. counterEffectHashtable,
  198. EffectTiming.OnCounterTiming,
  199. cardEffect => cardEffect.IsCounterEffect));
  200. bool HasCounterEffect(List<SkillInfo> skillInfos, SkillInfo skillInfo)
  201. {
  202. return skillInfos.Count((skillInfo1) => skillInfo1.CardEffect.IsCounterEffect) >= 1;
  203. }
  204. // activate cutin effects
  205. yield return ContinuousController.instance.StartCoroutine(GManager.instance.autoProcessing.RuleProcess());
  206. yield return ContinuousController.instance.StartCoroutine(GManager.instance.autoProcessing_CutIn.TriggeredSkillProcess(true, HasCounterEffect));
  207. GManager.instance.turnStateMachine.IsSelecting = true;
  208. // force to end attack
  209. if (IsEndAttack)
  210. {
  211. goto EndAttack;
  212. }
  213. if (AttackingPermanent.TopCard == null)
  214. {
  215. goto EndAttack;
  216. }
  217. AttackingPermanent.ShowingPermanentCard.SetOrangeOutline();
  218. AttackingPermanent.ShowingPermanentCard.Outline_Select.gameObject.SetActive(true);
  219. if (DefendingPermanent != null)
  220. {
  221. if (DefendingPermanent.TopCard != null)
  222. {
  223. DefendingPermanent.ShowingPermanentCard.SetOrangeOutline();
  224. DefendingPermanent.ShowingPermanentCard.Outline_Select.gameObject.SetActive(true);
  225. }
  226. }
  227. #region select blocker
  228. bool CanSelectBlockerCondition(Permanent permanent)
  229. {
  230. if (CardEffectCommons.IsPermanentExistsOnOpponentBattleAreaDigimon(permanent, AttackingPermanent.TopCard))
  231. {
  232. if (permanent != DefendingPermanent)
  233. {
  234. if (permanent.HasBlocker && permanent.CanBlock(AttackingPermanent))
  235. {
  236. return true;
  237. }
  238. }
  239. }
  240. return false;
  241. }
  242. if (AttackingPermanent.TopCard.Owner.Enemy.GetBattleAreaDigimons().Count(CanSelectBlockerCondition) >= 1)
  243. {
  244. int maxCount = 1;
  245. Permanent selectedPermanent = null;
  246. SelectPermanentEffect selectPermanentEffect = GManager.instance.GetComponent<SelectPermanentEffect>();
  247. selectPermanentEffect.SetUp(
  248. selectPlayer: AttackingPermanent.TopCard.Owner.Enemy,
  249. canTargetCondition: CanSelectBlockerCondition,
  250. canTargetCondition_ByPreSelecetedList: null,
  251. canEndSelectCondition: null,
  252. maxCount: maxCount,
  253. canNoSelect: (!IsBlocking),
  254. canEndNotMax: false,
  255. selectPermanentCoroutine: SelectPermanentCoroutine,
  256. afterSelectPermanentCoroutine: null,
  257. mode: SelectPermanentEffect.Mode.Custom,
  258. cardEffect: null);
  259. selectPermanentEffect.SetUpCustomMessage("Select 1 Digimon that will block.", "The opponent is selecting 1 Digimon that will block.");
  260. if(!IsBlocking)
  261. selectPermanentEffect.SetUpCustomBackButtonMessage("Not Block");
  262. yield return ContinuousController.instance.StartCoroutine(selectPermanentEffect.Activate());
  263. IEnumerator SelectPermanentCoroutine(Permanent permanent)
  264. {
  265. selectedPermanent = permanent;
  266. yield return null;
  267. }
  268. if (selectedPermanent != null)
  269. {
  270. yield return ContinuousController.instance.StartCoroutine(SwitchDefender(null, true, selectedPermanent));
  271. }
  272. }
  273. #endregion
  274. GManager.instance.turnStateMachine.IsSelecting = true;
  275. //end attack
  276. if (IsEndAttack)
  277. {
  278. goto EndAttack;
  279. }
  280. if (AttackingPermanent.TopCard == null)
  281. {
  282. goto EndAttack;
  283. }
  284. AttackingPermanent.ShowingPermanentCard.SetOrangeOutline();
  285. AttackingPermanent.ShowingPermanentCard.Outline_Select.gameObject.SetActive(true);
  286. if (DefendingPermanent != null)
  287. {
  288. if (DefendingPermanent.TopCard != null)
  289. {
  290. DefendingPermanent.ShowingPermanentCard.SetOrangeOutline();
  291. DefendingPermanent.ShowingPermanentCard.Outline_Select.gameObject.SetActive(true);
  292. }
  293. }
  294. #region there is no Defending Permanent
  295. if (DefendingPermanent == null)
  296. {
  297. if (AttackingPermanent.Strike >= 1)
  298. {
  299. //if there is no security, end the game
  300. if (AttackingPermanent.TopCard.Owner.Enemy.SecurityCards.Count == 0)
  301. {
  302. GManager.instance.turnStateMachine.EndGame(AttackingPermanent.TopCard.Owner, false);
  303. yield break;
  304. }
  305. }
  306. DoSecurityCheck = true;
  307. }
  308. #endregion
  309. #region there is Defending Permanent
  310. else
  311. {
  312. if (DefendingPermanent.TopCard == null || AttackingPermanent == null || AttackingPermanent.TopCard == null)
  313. {
  314. goto EndAttack;
  315. }
  316. DefendingPermanent.TopCard.Owner.securityObject.securityBreakGlass.gameObject.SetActive(false);
  317. DefendingPermanent.ShowingPermanentCard.SetOrangeOutline();
  318. DefendingPermanent.ShowingPermanentCard.Outline_Select.gameObject.SetActive(true);
  319. // battle
  320. IBattle battle = new IBattle(AttackingPermanent: AttackingPermanent, DefendingPermanent: DefendingPermanent, null);
  321. yield return ContinuousController.instance.StartCoroutine(battle.Battle());
  322. /*#region effect when determine whether to do security check
  323. Hashtable hashtable = new Hashtable()
  324. {
  325. {"battle", battle}
  326. };
  327. List<SkillInfo> skillInfos_Pierce = AutoProcessing.GetSkillInfos(hashtable, EffectTiming.OnDetermineDoSecurityCheck)
  328. .Filter(skillInfo => skillInfo.CardEffect != null && skillInfo.CardEffect.CanActivate(skillInfo.Hashtable));
  329. if (skillInfos_Pierce.Count >= 1)
  330. {
  331. GManager.instance.autoProcessing.PutStackedSkill(skillInfos_Pierce[0]);
  332. }
  333. #endregion*/
  334. yield return ContinuousController.instance.StartCoroutine(GManager.instance.autoProcessing.TriggeredSkillProcess(true, null));
  335. GManager.instance.turnStateMachine.IsSelecting = true;
  336. }
  337. #endregion
  338. if (AttackingPermanent.TopCard == null)
  339. {
  340. goto EndAttack;
  341. }
  342. #region do security check
  343. if (DoSecurityCheck && AttackingPermanent.TopCard.Owner.Enemy.SecurityCards.Count >= 1)
  344. {
  345. //security check process
  346. yield return ContinuousController.instance.StartCoroutine(new ISecurityCheck(
  347. AttackingPermanent: AttackingPermanent,
  348. player: GManager.instance.turnStateMachine.gameContext.NonTurnPlayer).SecurityCheck());
  349. }
  350. #endregion
  351. }
  352. else
  353. {
  354. if (beforeOnAttackCoroutine != null)
  355. {
  356. yield return ContinuousController.instance.StartCoroutine(beforeOnAttackCoroutine());
  357. }
  358. }
  359. #endregion
  360. #region End Attack
  361. EndAttack:;
  362. // [On End Attack] effect
  363. if (AttackingPermanent != null && AttackingPermanent.TopCard != null)
  364. {
  365. yield return ContinuousController.instance.StartCoroutine(GManager.instance.autoProcessing.StackSkillInfos(effectHashtable, EffectTiming.OnEndAttack));
  366. }
  367. #region reset effects which continues until the end of attack
  368. foreach (Player player in GManager.instance.turnStateMachine.gameContext.Players_ForTurnPlayer)
  369. {
  370. foreach (Permanent permanent in player.GetFieldPermanents())
  371. {
  372. permanent.UntilEndAttackEffects = new List<Func<EffectTiming, ICardEffect>>();
  373. }
  374. }
  375. #endregion
  376. GManager.instance.turnStateMachine.IsSelecting = true;
  377. #endregion
  378. DoSecurityCheck = false;
  379. IsBlocking = false;
  380. SecurityDigimon = null;
  381. IsAttacking = false;
  382. IsEndAttack = false;
  383. }
  384. public IEnumerator SwitchDefender(ICardEffect cardEffect, bool isBlock, Permanent newDefendingPermanent)
  385. {
  386. if (AttackingPermanent == null) yield break;
  387. if (AttackingPermanent.TopCard == null) yield break;
  388. if (newDefendingPermanent != null && newDefendingPermanent.TopCard == null) yield break;
  389. if (!AttackingPermanent.CanSwitchAttackTarget) yield break;
  390. Permanent oldDefendingPermanent = DefendingPermanent;
  391. DefendingPermanent = newDefendingPermanent;
  392. IsBlocking = isBlock;
  393. if (DefendingPermanent != null)
  394. {
  395. if (DefendingPermanent.ShowingPermanentCard != null)
  396. {
  397. DefendingPermanent.ShowingPermanentCard.SetOrangeOutline();
  398. DefendingPermanent.ShowingPermanentCard.Outline_Select.gameObject.SetActive(true);
  399. }
  400. }
  401. Hashtable hashtable = new Hashtable(){
  402. {"AttackingPermanent", AttackingPermanent},
  403. {"DefendingPermanent", DefendingPermanent},
  404. {"CardEffect", cardEffect}
  405. };
  406. if (isBlock)
  407. {
  408. hashtable.Add("IsBlock", isBlock);
  409. }
  410. if (isBlock)
  411. {
  412. if (DefendingPermanent != null)
  413. {
  414. // add log
  415. string log1 = $"\nBlock:\n{DefendingPermanent.TopCard.BaseENGCardNameFromEntity}({DefendingPermanent.TopCard.CardID})\n";
  416. PlayLog.OnAddLog?.Invoke(log1);
  417. // suspend
  418. yield return ContinuousController.instance.StartCoroutine(new SuspendPermanentsClass(new List<Permanent>() { DefendingPermanent }, hashtable).Tap());
  419. // the effects when blocking
  420. yield return ContinuousController.instance.StartCoroutine(GManager.instance.autoProcessing.StackSkillInfos(
  421. hashtable,
  422. EffectTiming.OnBlockAnyone));
  423. }
  424. }
  425. if (AttackingPermanent != null)
  426. {
  427. if (AttackingPermanent.TopCard == null)
  428. {
  429. IsBlocking = false;
  430. yield break;
  431. }
  432. }
  433. if (DefendingPermanent != null)
  434. {
  435. if (DefendingPermanent.TopCard == null)
  436. {
  437. IsBlocking = false;
  438. yield break;
  439. }
  440. }
  441. if (AttackingPermanent != null)
  442. {
  443. // target arrow
  444. for (int i = 0; i < 3; i++)
  445. {
  446. GManager.instance.OffTargetArrow();
  447. yield return null;
  448. }
  449. if (DefendingPermanent != null)
  450. {
  451. DefendingPermanent.ShowingPermanentCard.SetOrangeOutline();
  452. DefendingPermanent.ShowingPermanentCard.Outline_Select.gameObject.SetActive(true);
  453. yield return GManager.instance.OnTargetArrow(
  454. AttackingPermanent.PermanentFrame.GetLocalCanvasPosition() + AttackingPermanent.TopCard.Owner.playerUIObjectParent.localPosition,
  455. DefendingPermanent.PermanentFrame.GetLocalCanvasPosition() + DefendingPermanent.TopCard.Owner.playerUIObjectParent.localPosition,
  456. null,
  457. null);
  458. }
  459. else
  460. {
  461. yield return GManager.instance.OnTargetArrow(
  462. AttackingPermanent.PermanentFrame.GetLocalCanvasPosition() + AttackingPermanent.TopCard.Owner.playerUIObjectParent.localPosition,
  463. GManager.instance.turnStateMachine.gameContext.NonTurnPlayer.SecurityAttackLocalCanvasPosition + GManager.instance.turnStateMachine.gameContext.NonTurnPlayer.playerUIObjectParent.localPosition,
  464. null,
  465. null);
  466. }
  467. AttackingPermanent.ShowingPermanentCard.SetOrangeOutline();
  468. AttackingPermanent.ShowingPermanentCard.Outline_Select.gameObject.SetActive(true);
  469. }
  470. if (newDefendingPermanent != oldDefendingPermanent)
  471. {
  472. // the effects when attack target switched
  473. yield return ContinuousController.instance.StartCoroutine(GManager.instance.autoProcessing.StackSkillInfos(
  474. hashtable,
  475. EffectTiming.OnAttackTargetChanged));
  476. }
  477. }
  478. }