AttackProcess.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  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: true,
  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. selectPermanentEffect.SetUpCustomBackButtonMessage("Not Block");
  252. yield return ContinuousController.instance.StartCoroutine(selectPermanentEffect.Activate());
  253. IEnumerator SelectPermanentCoroutine(Permanent permanent)
  254. {
  255. selectedPermanent = permanent;
  256. yield return null;
  257. }
  258. if (selectedPermanent != null)
  259. {
  260. yield return ContinuousController.instance.StartCoroutine(SwitchDefender(null, true, selectedPermanent));
  261. }
  262. }
  263. #endregion
  264. GManager.instance.turnStateMachine.IsSelecting = true;
  265. //end attack
  266. if (IsEndAttack)
  267. {
  268. goto EndAttack;
  269. }
  270. if (AttackingPermanent.TopCard == null)
  271. {
  272. goto EndAttack;
  273. }
  274. AttackingPermanent.ShowingPermanentCard.SetOrangeOutline();
  275. AttackingPermanent.ShowingPermanentCard.Outline_Select.gameObject.SetActive(true);
  276. if (DefendingPermanent != null)
  277. {
  278. if (DefendingPermanent.TopCard != null)
  279. {
  280. DefendingPermanent.ShowingPermanentCard.SetOrangeOutline();
  281. DefendingPermanent.ShowingPermanentCard.Outline_Select.gameObject.SetActive(true);
  282. }
  283. }
  284. #region there is no Defending Permanent
  285. if (DefendingPermanent == null)
  286. {
  287. if (AttackingPermanent.Strike >= 1)
  288. {
  289. //if there is no security, end the game
  290. if (AttackingPermanent.TopCard.Owner.Enemy.SecurityCards.Count == 0)
  291. {
  292. GManager.instance.turnStateMachine.EndGame(AttackingPermanent.TopCard.Owner, false);
  293. yield break;
  294. }
  295. }
  296. DoSecurityCheck = true;
  297. }
  298. #endregion
  299. #region there is Defending Permanent
  300. else
  301. {
  302. if (DefendingPermanent.TopCard == null || AttackingPermanent == null || AttackingPermanent.TopCard == null)
  303. {
  304. goto EndAttack;
  305. }
  306. DefendingPermanent.TopCard.Owner.securityObject.securityBreakGlass.gameObject.SetActive(false);
  307. DefendingPermanent.ShowingPermanentCard.SetOrangeOutline();
  308. DefendingPermanent.ShowingPermanentCard.Outline_Select.gameObject.SetActive(true);
  309. // battle
  310. IBattle battle = new IBattle(AttackingPermanent: AttackingPermanent, DefendingPermanent: DefendingPermanent, null);
  311. yield return ContinuousController.instance.StartCoroutine(battle.Battle());
  312. #region effect when determine whether to do security check
  313. Hashtable hashtable = new Hashtable()
  314. {
  315. {"battle", battle}
  316. };
  317. List<SkillInfo> skillInfos_Pierce = AutoProcessing.GetSkillInfos(hashtable, EffectTiming.OnDetermineDoSecurityCheck)
  318. .Filter(skillInfo => skillInfo.CardEffect != null && skillInfo.CardEffect.CanActivate(skillInfo.Hashtable));
  319. if (skillInfos_Pierce.Count >= 1)
  320. {
  321. GManager.instance.autoProcessing.PutStackedSkill(skillInfos_Pierce[0]);
  322. }
  323. #endregion
  324. yield return ContinuousController.instance.StartCoroutine(GManager.instance.autoProcessing.TriggeredSkillProcess(true, null));
  325. GManager.instance.turnStateMachine.IsSelecting = true;
  326. }
  327. #endregion
  328. if (AttackingPermanent.TopCard == null)
  329. {
  330. goto EndAttack;
  331. }
  332. #region do security check
  333. if (DoSecurityCheck && AttackingPermanent.TopCard.Owner.Enemy.SecurityCards.Count >= 1)
  334. {
  335. //security check process
  336. yield return ContinuousController.instance.StartCoroutine(new ISecurityCheck(
  337. AttackingPermanent: AttackingPermanent,
  338. player: GManager.instance.turnStateMachine.gameContext.NonTurnPlayer).SecurityCheck());
  339. }
  340. #endregion
  341. }
  342. else
  343. {
  344. if (beforeOnAttackCoroutine != null)
  345. {
  346. yield return ContinuousController.instance.StartCoroutine(beforeOnAttackCoroutine());
  347. }
  348. }
  349. #endregion
  350. #region End Attack
  351. EndAttack:;
  352. // [On End Attack] effect
  353. if (AttackingPermanent != null && AttackingPermanent.TopCard != null)
  354. {
  355. yield return ContinuousController.instance.StartCoroutine(GManager.instance.autoProcessing.StackSkillInfos(effectHashtable, EffectTiming.OnEndAttack));
  356. }
  357. #region reset effects which continues until the end of attack
  358. foreach (Player player in GManager.instance.turnStateMachine.gameContext.Players_ForTurnPlayer)
  359. {
  360. foreach (Permanent permanent in player.GetFieldPermanents())
  361. {
  362. permanent.UntilEndAttackEffects = new List<Func<EffectTiming, ICardEffect>>();
  363. }
  364. }
  365. #endregion
  366. GManager.instance.turnStateMachine.IsSelecting = true;
  367. #endregion
  368. DoSecurityCheck = false;
  369. IsBlocking = false;
  370. SecurityDigimon = null;
  371. IsAttacking = false;
  372. IsEndAttack = false;
  373. }
  374. public IEnumerator SwitchDefender(ICardEffect cardEffect, bool isBlock, Permanent newDefendingPermanent)
  375. {
  376. if (AttackingPermanent == null) yield break;
  377. if (AttackingPermanent.TopCard == null) yield break;
  378. if (newDefendingPermanent != null && newDefendingPermanent.TopCard == null) yield break;
  379. if (!AttackingPermanent.CanSwitchAttackTarget) yield break;
  380. Permanent oldDefendingPermanent = DefendingPermanent;
  381. DefendingPermanent = newDefendingPermanent;
  382. IsBlocking = isBlock;
  383. if (DefendingPermanent != null)
  384. {
  385. if (DefendingPermanent.ShowingPermanentCard != null)
  386. {
  387. DefendingPermanent.ShowingPermanentCard.SetOrangeOutline();
  388. DefendingPermanent.ShowingPermanentCard.Outline_Select.gameObject.SetActive(true);
  389. }
  390. }
  391. Hashtable hashtable = new Hashtable(){
  392. {"AttackingPermanent", AttackingPermanent},
  393. {"DefendingPermanent", DefendingPermanent},
  394. {"CardEffect", cardEffect}
  395. };
  396. if (isBlock)
  397. {
  398. hashtable.Add("IsBlock", isBlock);
  399. }
  400. if (isBlock)
  401. {
  402. if (DefendingPermanent != null)
  403. {
  404. // add log
  405. string log1 = $"\nBlock:\n{DefendingPermanent.TopCard.BaseENGCardNameFromEntity}({DefendingPermanent.TopCard.CardID})\n";
  406. GManager.instance.playLog.AddLogString(log1);
  407. // suspend
  408. yield return ContinuousController.instance.StartCoroutine(new SuspendPermanentsClass(new List<Permanent>() { DefendingPermanent }, hashtable).Tap());
  409. // the effects when blocking
  410. yield return ContinuousController.instance.StartCoroutine(GManager.instance.autoProcessing.StackSkillInfos(
  411. hashtable,
  412. EffectTiming.OnBlockAnyone));
  413. }
  414. }
  415. if (AttackingPermanent != null)
  416. {
  417. if (AttackingPermanent.TopCard == null)
  418. {
  419. IsBlocking = false;
  420. yield break;
  421. }
  422. }
  423. if (DefendingPermanent != null)
  424. {
  425. if (DefendingPermanent.TopCard == null)
  426. {
  427. IsBlocking = false;
  428. yield break;
  429. }
  430. }
  431. if (AttackingPermanent != null)
  432. {
  433. // target arrow
  434. for (int i = 0; i < 3; i++)
  435. {
  436. GManager.instance.OffTargetArrow();
  437. yield return null;
  438. }
  439. if (DefendingPermanent != null)
  440. {
  441. DefendingPermanent.ShowingPermanentCard.SetOrangeOutline();
  442. DefendingPermanent.ShowingPermanentCard.Outline_Select.gameObject.SetActive(true);
  443. yield return GManager.instance.OnTargetArrow(
  444. AttackingPermanent.PermanentFrame.GetLocalCanvasPosition() + AttackingPermanent.TopCard.Owner.playerUIObjectParent.localPosition,
  445. DefendingPermanent.PermanentFrame.GetLocalCanvasPosition() + DefendingPermanent.TopCard.Owner.playerUIObjectParent.localPosition,
  446. null,
  447. null);
  448. }
  449. else
  450. {
  451. yield return GManager.instance.OnTargetArrow(
  452. AttackingPermanent.PermanentFrame.GetLocalCanvasPosition() + AttackingPermanent.TopCard.Owner.playerUIObjectParent.localPosition,
  453. GManager.instance.turnStateMachine.gameContext.NonTurnPlayer.SecurityAttackLocalCanvasPosition + GManager.instance.turnStateMachine.gameContext.NonTurnPlayer.playerUIObjectParent.localPosition,
  454. null,
  455. null);
  456. }
  457. AttackingPermanent.ShowingPermanentCard.SetOrangeOutline();
  458. AttackingPermanent.ShowingPermanentCard.Outline_Select.gameObject.SetActive(true);
  459. }
  460. if (newDefendingPermanent != oldDefendingPermanent)
  461. {
  462. // the effects when attack target switched
  463. yield return ContinuousController.instance.StartCoroutine(GManager.instance.autoProcessing.StackSkillInfos(
  464. hashtable,
  465. EffectTiming.OnAttackTargetChanged));
  466. }
  467. }
  468. }