AttackProcess.cs 22 KB

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