SelectAttackEffect.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. using Photon.Pun;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using UnityEngine;
  7. public class SelectAttackEffect : MonoBehaviourPunCallbacks
  8. {
  9. const int SecurityIndex = -1;
  10. const int NopIndex = -2;
  11. public void SetUp
  12. (
  13. Permanent attacker,
  14. Func<bool> canAttackPlayerCondition,
  15. Func<Permanent, bool> defenderCondition,
  16. ICardEffect cardEffect)
  17. {
  18. _attacker = attacker;
  19. _canAttackPlayerCondition = canAttackPlayerCondition;
  20. _defenderCondition = defenderCondition;
  21. _cardEffect = cardEffect;
  22. _canSelectNotAttack = true;
  23. _withoutTap = false;
  24. _isVortex = false;
  25. _customMessage = null;
  26. _customMessage_Enemy = null;
  27. _beforeOnAttackCoroutine = null;
  28. _afterOnAttackCoroutine = null;
  29. }
  30. public void SetUpCustomMessage(string customMessage, string customMessage_Enemy)
  31. {
  32. _customMessage = customMessage;
  33. _customMessage_Enemy = customMessage_Enemy;
  34. }
  35. public void SetCanNotSelectNotAttack()
  36. {
  37. _canSelectNotAttack = false;
  38. }
  39. public void SetWithoutTap()
  40. {
  41. _withoutTap = true;
  42. }
  43. public void SetIsVortex()
  44. {
  45. _isVortex = true;
  46. }
  47. public void SetBeforeOnAttackCoroutine(Func<IEnumerator> beforeOnAttackCoroutine)
  48. {
  49. _beforeOnAttackCoroutine = beforeOnAttackCoroutine;
  50. }
  51. public void SetAfterOnAttackCoroutine(Func<IEnumerator> afterOnAttackCoroutine)
  52. {
  53. _afterOnAttackCoroutine = afterOnAttackCoroutine;
  54. }
  55. Permanent _attacker = null;
  56. Func<bool> _canAttackPlayerCondition = null;
  57. Func<Permanent, bool> _defenderCondition = null;
  58. bool _canSelectNotAttack = true;
  59. bool _withoutTap = false;
  60. bool _isVortex = false;
  61. bool _noSelect = false;
  62. string _customMessage = null;
  63. string _customMessage_Enemy = null;
  64. Permanent _defender = null;
  65. ICardEffect _cardEffect = null;
  66. Func<IEnumerator> _beforeOnAttackCoroutine = null;
  67. Func<IEnumerator> _afterOnAttackCoroutine = null;
  68. #region ƒp[ƒ}ƒlƒ“ƒg‚ð‘I‘ð‚Å‚«‚é‚©
  69. bool CanTarget(Permanent permanent)
  70. {
  71. if (_attacker != null)
  72. {
  73. if (_attacker.TopCard != null)
  74. {
  75. if (_attacker.CanAttack(_cardEffect, _withoutTap, _isVortex))
  76. {
  77. if (_attacker.CanAttackTargetDigimon(permanent, _cardEffect, _withoutTap, _isVortex))
  78. {
  79. if (_defenderCondition != null)
  80. {
  81. if (!_defenderCondition(permanent))
  82. {
  83. return false;
  84. }
  85. }
  86. return true;
  87. }
  88. }
  89. }
  90. }
  91. return false;
  92. }
  93. #endregion
  94. #region UŒ‚‚Å‚«‚鑊Žèƒp[ƒ}ƒlƒ“ƒg‚ª‚¢‚é‚©
  95. public bool CanAttackDigimon()
  96. {
  97. foreach (Player player in GManager.instance.turnStateMachine.gameContext.Players)
  98. {
  99. foreach (Permanent permanent in player.GetFieldPermanents())
  100. {
  101. if (CanTarget(permanent))
  102. {
  103. return true;
  104. }
  105. }
  106. }
  107. return false;
  108. }
  109. #endregion
  110. #region ƒvƒŒƒCƒ„[‚ɍUŒ‚‚ª‰Â”\‚©
  111. bool CanAttackPlayer()
  112. {
  113. if (_attacker != null)
  114. {
  115. if (_attacker.TopCard != null)
  116. {
  117. if (_attacker.CanAttack(_cardEffect, _withoutTap, _isVortex))
  118. {
  119. if (_attacker.CanAttackTargetDigimon(null, _cardEffect, _withoutTap, _isVortex))
  120. {
  121. if (_canAttackPlayerCondition != null)
  122. {
  123. if (!_canAttackPlayerCondition())
  124. {
  125. return false;
  126. }
  127. }
  128. return true;
  129. }
  130. }
  131. }
  132. }
  133. return false;
  134. }
  135. #endregion
  136. #region ‘I‘ð‚ª‰Â”\‚©‚Ç‚¤‚©
  137. public bool active()
  138. {
  139. if (!CanAttackDigimon())
  140. {
  141. if (!CanAttackPlayer())
  142. {
  143. return false;
  144. }
  145. }
  146. if (GManager.instance.attackProcess.IsAttacking)
  147. {
  148. return false;
  149. }
  150. return true;
  151. }
  152. #endregion
  153. #region I—¹‚Å‚«‚é‚©”»’è
  154. bool CanEndSelect(Permanent selectedPermanent)
  155. {
  156. if (selectedPermanent == null)
  157. {
  158. if (!CanAttackPlayer())
  159. {
  160. return false;
  161. }
  162. }
  163. else
  164. {
  165. if (!CanAttackDigimon())
  166. {
  167. return false;
  168. }
  169. }
  170. return true;
  171. }
  172. #endregion
  173. public IEnumerator Activate()
  174. {
  175. _defender = null;
  176. _noSelect = false;
  177. if (_attacker == null) yield break;
  178. if (_attacker.TopCard == null) yield break;
  179. if (!_attacker.CanAttack(_cardEffect, _withoutTap, _isVortex)) yield break;
  180. if (active())
  181. {
  182. yield return GManager.instance.photonWaitController.StartWait("SelectAttackEffect");
  183. foreach (Player player in GManager.instance.turnStateMachine.gameContext.Players)
  184. {
  185. GManager.instance.turnStateMachine.OffFieldCardTarget(player);
  186. GManager.instance.turnStateMachine.OffHandCardTarget(player);
  187. }
  188. GManager.instance.turnStateMachine.IsSelecting = true;
  189. Player attackOwner = _attacker?.TopCard != null ? _attacker.TopCard.Owner : null;
  190. if (_attacker != null)
  191. {
  192. if (_attacker.TopCard != null)
  193. {
  194. if (_attacker.CanAttack(_cardEffect, _withoutTap, _isVortex))
  195. {
  196. if (_attacker.TopCard.Owner.isYou)
  197. {
  198. #region Select Attack Target
  199. if (!string.IsNullOrEmpty(_customMessage))
  200. {
  201. GManager.instance.commandText.OpenCommandText(_customMessage);
  202. }
  203. else
  204. {
  205. if (CanAttackDigimon())
  206. {
  207. GManager.instance.commandText.OpenCommandText("Which target will you attack?");
  208. }
  209. else
  210. {
  211. GManager.instance.commandText.OpenCommandText("Will you attack to the opponent?");
  212. }
  213. }
  214. #endregion
  215. Permanent selectedPermanent = null;
  216. #region Can Attack Player
  217. if (CanAttackPlayer())
  218. {
  219. if (_attacker.TopCard.Owner.Enemy.SecurityCards.Count >= 1)
  220. {
  221. _attacker.TopCard.Owner.Enemy.securityObject.securityBreakGlass.ShowBlueMatarial();
  222. }
  223. _attacker.TopCard.Owner.Enemy.securityObject.SetSecurityAttackObject();
  224. _attacker.TopCard.Owner.Enemy.securityObject.SetSecurityOutline(true);
  225. _attacker.TopCard.Owner.Enemy.securityObject.AddClickTarget(() =>
  226. {
  227. selectedPermanent = null;
  228. EndSelect_RPC();
  229. });
  230. }
  231. #endregion
  232. List<FieldPermanentCard> candidates = new List<FieldPermanentCard>();
  233. foreach (Player player in GManager.instance.turnStateMachine.gameContext.Players)
  234. {
  235. foreach (Permanent permanent in player.GetFieldPermanents())
  236. {
  237. if (CanTarget(permanent))
  238. {
  239. permanent.ShowingPermanentCard.AddClickTarget(OnClickFieldPermanentCard);
  240. candidates.Add(permanent.ShowingPermanentCard);
  241. }
  242. }
  243. }
  244. if (candidates.Count >= 1)
  245. {
  246. GManager.instance.hideCannotSelectObject.SetUpHideCannotSelectObject(candidates, false);
  247. }
  248. CheckEndSelect();
  249. #region Selecting field permanent
  250. void OnClickFieldPermanentCard(FieldPermanentCard fieldPermanentCard)
  251. {
  252. if (selectedPermanent == fieldPermanentCard.ThisPermanent)
  253. {
  254. selectedPermanent = null;
  255. }
  256. else
  257. {
  258. selectedPermanent = fieldPermanentCard.ThisPermanent;
  259. }
  260. CheckEndSelect();
  261. }
  262. #endregion
  263. #region End Selectiong "Not Attack"
  264. void CheckEndSelect()
  265. {
  266. #region CanEndSelect
  267. if (CanEndSelect(selectedPermanent))
  268. {
  269. }
  270. else
  271. {
  272. }
  273. #endregion
  274. #region Visually Select Target
  275. foreach (Player player in GManager.instance.turnStateMachine.gameContext.Players)
  276. {
  277. foreach (Permanent permanent in player.GetFieldPermanents())
  278. {
  279. permanent.ShowingPermanentCard.RemoveSelectEffect();
  280. if (CanTarget(permanent))
  281. {
  282. if (selectedPermanent == permanent)
  283. {
  284. permanent.ShowingPermanentCard.OnSelectEffect(1.1f);
  285. permanent.ShowingPermanentCard.SetOrangeOutline();
  286. }
  287. else
  288. {
  289. permanent.ShowingPermanentCard.OnSelectEffect(1.1f);
  290. permanent.ShowingPermanentCard.SetBlueOutline();
  291. }
  292. }
  293. }
  294. }
  295. #endregion
  296. if (selectedPermanent == null)
  297. {
  298. if (_canSelectNotAttack)
  299. {
  300. GManager.instance.BackButton.OpenSelectCommandButton("Not Attack", () => { photonView.RPC("SetAttackTarget", RpcTarget.All, attackOwner.PlayerID, false, NopIndex); }, 0);
  301. }
  302. GManager.instance.selectCommandPanel.CloseSelectCommandPanel();
  303. }
  304. else
  305. {
  306. GManager.instance.selectCommandPanel.SetUpCommandButton(new List<Command_SelectCommand>() { new Command_SelectCommand("End Selection", EndSelect_RPC, 0) });
  307. GManager.instance.BackButton.CloseSelectCommandButton();
  308. }
  309. }
  310. #endregion
  311. #region End Selection RPC
  312. void EndSelect_RPC()
  313. {
  314. foreach (Player player in GManager.instance.turnStateMachine.gameContext.Players)
  315. {
  316. foreach (Permanent permanent in player.GetFieldPermanents())
  317. {
  318. permanent.ShowingPermanentCard.RemoveSelectEffect();
  319. permanent.ShowingPermanentCard.RemoveClickTarget();
  320. }
  321. }
  322. bool isTurnPlayer = false;
  323. int PermanentIndex = SecurityIndex;
  324. if (selectedPermanent != null)
  325. {
  326. if (selectedPermanent.TopCard != null)
  327. {
  328. isTurnPlayer = selectedPermanent.TopCard.Owner == GManager.instance.turnStateMachine.gameContext.TurnPlayer;
  329. PermanentIndex = selectedPermanent.TopCard.Owner.GetFieldPermanents().IndexOf(selectedPermanent);
  330. }
  331. }
  332. photonView.RPC("SetAttackTarget", RpcTarget.All, attackOwner.PlayerID, isTurnPlayer, PermanentIndex);
  333. GManager.instance.BackButton.CloseSelectCommandButton();
  334. }
  335. #endregion
  336. }
  337. else
  338. {
  339. #region Showing Enemy Message
  340. if (!string.IsNullOrEmpty(_customMessage_Enemy))
  341. {
  342. GManager.instance.commandText.OpenCommandText(_customMessage_Enemy);
  343. }
  344. else
  345. {
  346. GManager.instance.commandText.OpenCommandText("The opponent is selecting the attack target.");
  347. }
  348. #endregion
  349. #region AI
  350. if (GManager.instance.IsAI)
  351. {
  352. List<Permanent> AttackcTargetCandidates = new List<Permanent>();
  353. foreach (Player player in GManager.instance.turnStateMachine.gameContext.Players_ForTurnPlayer)
  354. {
  355. foreach (Permanent permanent in player.GetFieldPermanents())
  356. {
  357. if (CanTarget(permanent))
  358. {
  359. AttackcTargetCandidates.Add(permanent);
  360. }
  361. }
  362. }
  363. Permanent selectedPermanent = null;
  364. if (AttackcTargetCandidates.Count >= 1)
  365. {
  366. if (!_attacker.CanAttackTargetDigimon(null, _cardEffect, _withoutTap, _isVortex) || (_attacker.TopCard.Owner.Enemy.SecurityCards.Count >= 3 && RandomUtility.IsSucceedProbability(0.5f)))
  367. {
  368. selectedPermanent = AttackcTargetCandidates[UnityEngine.Random.Range(0, AttackcTargetCandidates.Count)];
  369. }
  370. }
  371. bool isTurnPlayer = false;
  372. int PermanentIndex = SecurityIndex;
  373. if (selectedPermanent != null)
  374. {
  375. if (selectedPermanent.TopCard != null)
  376. {
  377. isTurnPlayer = selectedPermanent.TopCard.Owner == GManager.instance.turnStateMachine.gameContext.TurnPlayer;
  378. PermanentIndex = selectedPermanent.TopCard.Owner.GetFieldPermanents().IndexOf(selectedPermanent);
  379. }
  380. }
  381. SetAttackTarget(attackOwner.PlayerID, isTurnPlayer, PermanentIndex);
  382. }
  383. #endregion
  384. }
  385. }
  386. }
  387. }
  388. //Wait until selection ends
  389. yield return new WaitUntil(() => attackOwner.HasPlayerSelection());
  390. _defender = null;
  391. bool isTargetTurnPlayer = false;
  392. int targetIndex = SecurityIndex;
  393. PermanentSelection permanentSelection = attackOwner.DequeuePlayerSelection<PermanentSelection>();
  394. if (permanentSelection != null)
  395. {
  396. if (permanentSelection.IsTurnPlayerList != null && permanentSelection.IsTurnPlayerList.Length > 0 &&
  397. permanentSelection.PermanentIDList != null && permanentSelection.PermanentIDList.Length > 0)
  398. {
  399. isTargetTurnPlayer = permanentSelection.IsTurnPlayerList[0];
  400. targetIndex = permanentSelection.PermanentIDList[0];
  401. }
  402. _noSelect = targetIndex == NopIndex;
  403. if (_noSelect)
  404. {
  405. GManager.instance.selectCommandPanel.CloseSelectCommandPanel();
  406. }
  407. else if (targetIndex != SecurityIndex)
  408. {
  409. Player player = null;
  410. if (isTargetTurnPlayer)
  411. {
  412. player = GManager.instance.turnStateMachine.gameContext.TurnPlayer;
  413. }
  414. else
  415. {
  416. player = GManager.instance.turnStateMachine.gameContext.NonTurnPlayer;
  417. }
  418. if (targetIndex >= 0 && targetIndex < player.GetFieldPermanents().Count)
  419. {
  420. _defender = player.GetFieldPermanents()[targetIndex];
  421. }
  422. }
  423. }
  424. #region Clean up visual selections and UI
  425. foreach (Player player in GManager.instance.turnStateMachine.gameContext.Players)
  426. {
  427. GManager.instance.turnStateMachine.OffFieldCardTarget(player);
  428. GManager.instance.turnStateMachine.OffHandCardTarget(player);
  429. foreach (Permanent permanent in player.GetFieldPermanents())
  430. {
  431. permanent.ShowingPermanentCard.RemoveSelectEffect();
  432. }
  433. player.securityObject.OffShowSecurityAttackObject();
  434. player.securityObject.RemoveClickTarget();
  435. if (_defender != null || _noSelect)
  436. {
  437. player.securityObject.securityBreakGlass.gameObject.SetActive(false);
  438. }
  439. }
  440. GManager.instance.hideCannotSelectObject.Close();
  441. GManager.instance.selectCommandPanel.CloseSelectCommandPanel();
  442. GManager.instance.BackButton.CloseSelectCommandButton();
  443. GManager.instance.commandText.CloseCommandText();
  444. yield return new WaitWhile(() => GManager.instance.commandText.gameObject.activeSelf);
  445. #endregion
  446. if (!_noSelect)
  447. {
  448. if (CanEndSelect(_defender))
  449. {
  450. yield return ContinuousController.instance.StartCoroutine(GManager.instance.attackProcess.Attack(_attacker, _defender, _cardEffect, _withoutTap, _beforeOnAttackCoroutine));
  451. if(_afterOnAttackCoroutine != null)
  452. yield return ContinuousController.instance.StartCoroutine(_afterOnAttackCoroutine());
  453. }
  454. }
  455. }
  456. }
  457. #region ‘I‘ðŒˆ’è
  458. [PunRPC]
  459. public void SetAttackTarget(int playerID, bool isTurnPlayer, int permanentIndex)
  460. {
  461. bool[] isTurnPlayerList = new bool[] { isTurnPlayer };
  462. int[] permanentIndexList = new int[] { permanentIndex };
  463. Player selectionPlayer = GManager.instance.GetPlayerFromID(playerID);
  464. if (selectionPlayer == null)
  465. {
  466. return;
  467. }
  468. selectionPlayer.QueuePlayerSelection(new PermanentSelection(isTurnPlayerList, permanentIndexList));
  469. }
  470. #endregion
  471. }