AttackProcess.cs 22 KB

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