SelectAttackEffect.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  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. foreach (Player player in GManager.instance.turnStateMachine.gameContext.Players)
  183. {
  184. GManager.instance.turnStateMachine.OffFieldCardTarget(player);
  185. GManager.instance.turnStateMachine.OffHandCardTarget(player);
  186. }
  187. GManager.instance.turnStateMachine.IsSelecting = true;
  188. Player attackOwner = _attacker?.TopCard != null ? _attacker.TopCard.Owner : null;
  189. if (_attacker != null)
  190. {
  191. if (_attacker.TopCard != null)
  192. {
  193. if (_attacker.CanAttack(_cardEffect, _withoutTap, _isVortex))
  194. {
  195. if (_attacker.TopCard.Owner.isYou)
  196. {
  197. #region Select Attack Target
  198. if (!string.IsNullOrEmpty(_customMessage))
  199. {
  200. GManager.instance.commandText.OpenCommandText(_customMessage);
  201. }
  202. else
  203. {
  204. if (CanAttackDigimon())
  205. {
  206. GManager.instance.commandText.OpenCommandText("Which target will you attack?");
  207. }
  208. else
  209. {
  210. GManager.instance.commandText.OpenCommandText("Will you attack to the opponent?");
  211. }
  212. }
  213. #endregion
  214. Permanent selectedPermanent = null;
  215. #region Can Attack Player
  216. if (CanAttackPlayer())
  217. {
  218. if (_attacker.TopCard.Owner.Enemy.SecurityCards.Count >= 1)
  219. {
  220. _attacker.TopCard.Owner.Enemy.securityObject.securityBreakGlass.ShowBlueMatarial();
  221. }
  222. _attacker.TopCard.Owner.Enemy.securityObject.SetSecurityAttackObject();
  223. _attacker.TopCard.Owner.Enemy.securityObject.SetSecurityOutline(true);
  224. _attacker.TopCard.Owner.Enemy.securityObject.AddClickTarget(() =>
  225. {
  226. selectedPermanent = null;
  227. EndSelect_RPC();
  228. });
  229. }
  230. #endregion
  231. List<FieldPermanentCard> candidates = new List<FieldPermanentCard>();
  232. foreach (Player player in GManager.instance.turnStateMachine.gameContext.Players)
  233. {
  234. foreach (Permanent permanent in player.GetFieldPermanents())
  235. {
  236. if (CanTarget(permanent))
  237. {
  238. permanent.ShowingPermanentCard.AddClickTarget(OnClickFieldPermanentCard);
  239. candidates.Add(permanent.ShowingPermanentCard);
  240. }
  241. }
  242. }
  243. if (candidates.Count >= 1)
  244. {
  245. GManager.instance.hideCannotSelectObject.SetUpHideCannotSelectObject(candidates, false);
  246. }
  247. CheckEndSelect();
  248. #region Selecting field permanent
  249. void OnClickFieldPermanentCard(FieldPermanentCard fieldPermanentCard)
  250. {
  251. if (selectedPermanent == fieldPermanentCard.ThisPermanent)
  252. {
  253. selectedPermanent = null;
  254. }
  255. else
  256. {
  257. selectedPermanent = fieldPermanentCard.ThisPermanent;
  258. }
  259. CheckEndSelect();
  260. }
  261. #endregion
  262. #region End Selectiong "Not Attack"
  263. void CheckEndSelect()
  264. {
  265. #region CanEndSelect
  266. if (CanEndSelect(selectedPermanent))
  267. {
  268. }
  269. else
  270. {
  271. }
  272. #endregion
  273. #region Visually Select Target
  274. foreach (Player player in GManager.instance.turnStateMachine.gameContext.Players)
  275. {
  276. foreach (Permanent permanent in player.GetFieldPermanents())
  277. {
  278. permanent.ShowingPermanentCard.RemoveSelectEffect();
  279. if (CanTarget(permanent))
  280. {
  281. if (selectedPermanent == permanent)
  282. {
  283. permanent.ShowingPermanentCard.OnSelectEffect(1.1f);
  284. permanent.ShowingPermanentCard.SetOrangeOutline();
  285. }
  286. else
  287. {
  288. permanent.ShowingPermanentCard.OnSelectEffect(1.1f);
  289. permanent.ShowingPermanentCard.SetBlueOutline();
  290. }
  291. }
  292. }
  293. }
  294. #endregion
  295. if (selectedPermanent == null)
  296. {
  297. if (_canSelectNotAttack)
  298. {
  299. GManager.instance.BackButton.OpenSelectCommandButton("Not Attack", () => { photonView.RPC("SetAttackTarget", RpcTarget.All, attackOwner.PlayerID, false, NopIndex); }, 0);
  300. }
  301. GManager.instance.selectCommandPanel.CloseSelectCommandPanel();
  302. }
  303. else
  304. {
  305. GManager.instance.selectCommandPanel.SetUpCommandButton(new List<Command_SelectCommand>() { new Command_SelectCommand("End Selection", EndSelect_RPC, 0) });
  306. GManager.instance.BackButton.CloseSelectCommandButton();
  307. }
  308. }
  309. #endregion
  310. #region End Selection RPC
  311. void EndSelect_RPC()
  312. {
  313. foreach (Player player in GManager.instance.turnStateMachine.gameContext.Players)
  314. {
  315. foreach (Permanent permanent in player.GetFieldPermanents())
  316. {
  317. permanent.ShowingPermanentCard.RemoveSelectEffect();
  318. permanent.ShowingPermanentCard.RemoveClickTarget();
  319. }
  320. }
  321. bool isTurnPlayer = false;
  322. int PermanentIndex = SecurityIndex;
  323. if (selectedPermanent != null)
  324. {
  325. if (selectedPermanent.TopCard != null)
  326. {
  327. isTurnPlayer = selectedPermanent.TopCard.Owner == GManager.instance.turnStateMachine.gameContext.TurnPlayer;
  328. PermanentIndex = selectedPermanent.TopCard.Owner.GetFieldPermanents().IndexOf(selectedPermanent);
  329. }
  330. }
  331. photonView.RPC("SetAttackTarget", RpcTarget.All, attackOwner.PlayerID, isTurnPlayer, PermanentIndex);
  332. GManager.instance.BackButton.CloseSelectCommandButton();
  333. }
  334. #endregion
  335. }
  336. else
  337. {
  338. #region Showing Enemy Message
  339. if (!string.IsNullOrEmpty(_customMessage_Enemy))
  340. {
  341. GManager.instance.commandText.OpenCommandText(_customMessage_Enemy);
  342. }
  343. else
  344. {
  345. GManager.instance.commandText.OpenCommandText("The opponent is selecting the attack target.");
  346. }
  347. #endregion
  348. #region AI
  349. if (GManager.instance.IsAI)
  350. {
  351. List<Permanent> AttackcTargetCandidates = new List<Permanent>();
  352. foreach (Player player in GManager.instance.turnStateMachine.gameContext.Players_ForTurnPlayer)
  353. {
  354. foreach (Permanent permanent in player.GetFieldPermanents())
  355. {
  356. if (CanTarget(permanent))
  357. {
  358. AttackcTargetCandidates.Add(permanent);
  359. }
  360. }
  361. }
  362. Permanent selectedPermanent = null;
  363. if (AttackcTargetCandidates.Count >= 1)
  364. {
  365. if (!_attacker.CanAttackTargetDigimon(null, _cardEffect, _withoutTap, _isVortex) || (_attacker.TopCard.Owner.Enemy.SecurityCards.Count >= 3 && RandomUtility.IsSucceedProbability(0.5f)))
  366. {
  367. selectedPermanent = AttackcTargetCandidates[UnityEngine.Random.Range(0, AttackcTargetCandidates.Count)];
  368. }
  369. }
  370. bool isTurnPlayer = false;
  371. int PermanentIndex = SecurityIndex;
  372. if (selectedPermanent != null)
  373. {
  374. if (selectedPermanent.TopCard != null)
  375. {
  376. isTurnPlayer = selectedPermanent.TopCard.Owner == GManager.instance.turnStateMachine.gameContext.TurnPlayer;
  377. PermanentIndex = selectedPermanent.TopCard.Owner.GetFieldPermanents().IndexOf(selectedPermanent);
  378. }
  379. }
  380. SetAttackTarget(attackOwner.PlayerID, isTurnPlayer, PermanentIndex);
  381. }
  382. #endregion
  383. }
  384. }
  385. }
  386. }
  387. //Wait until selection ends
  388. yield return new WaitUntil(() => attackOwner.HasPlayerSelection());
  389. _defender = null;
  390. bool isTargetTurnPlayer = false;
  391. int targetIndex = SecurityIndex;
  392. PermanentSelection permanentSelection = attackOwner.DequeuePlayerSelection<PermanentSelection>();
  393. if (permanentSelection != null)
  394. {
  395. if (permanentSelection.IsTurnPlayerList != null && permanentSelection.IsTurnPlayerList.Length > 0 &&
  396. permanentSelection.PermanentIDList != null && permanentSelection.PermanentIDList.Length > 0)
  397. {
  398. isTargetTurnPlayer = permanentSelection.IsTurnPlayerList[0];
  399. targetIndex = permanentSelection.PermanentIDList[0];
  400. }
  401. _noSelect = targetIndex == NopIndex;
  402. if (_noSelect)
  403. {
  404. GManager.instance.selectCommandPanel.CloseSelectCommandPanel();
  405. }
  406. else if (targetIndex != SecurityIndex)
  407. {
  408. Player player = null;
  409. if (isTargetTurnPlayer)
  410. {
  411. player = GManager.instance.turnStateMachine.gameContext.TurnPlayer;
  412. }
  413. else
  414. {
  415. player = GManager.instance.turnStateMachine.gameContext.NonTurnPlayer;
  416. }
  417. if (targetIndex >= 0 && targetIndex < player.GetFieldPermanents().Count)
  418. {
  419. _defender = player.GetFieldPermanents()[targetIndex];
  420. }
  421. }
  422. }
  423. #region Clean up visual selections and UI
  424. foreach (Player player in GManager.instance.turnStateMachine.gameContext.Players)
  425. {
  426. GManager.instance.turnStateMachine.OffFieldCardTarget(player);
  427. GManager.instance.turnStateMachine.OffHandCardTarget(player);
  428. foreach (Permanent permanent in player.GetFieldPermanents())
  429. {
  430. permanent.ShowingPermanentCard.RemoveSelectEffect();
  431. }
  432. player.securityObject.OffShowSecurityAttackObject();
  433. player.securityObject.RemoveClickTarget();
  434. if (_defender != null || _noSelect)
  435. {
  436. player.securityObject.securityBreakGlass.gameObject.SetActive(false);
  437. }
  438. }
  439. GManager.instance.hideCannotSelectObject.Close();
  440. GManager.instance.selectCommandPanel.CloseSelectCommandPanel();
  441. GManager.instance.BackButton.CloseSelectCommandButton();
  442. GManager.instance.commandText.CloseCommandText();
  443. yield return new WaitWhile(() => GManager.instance.commandText.gameObject.activeSelf);
  444. #endregion
  445. if (!_noSelect)
  446. {
  447. if (CanEndSelect(_defender))
  448. {
  449. yield return ContinuousController.instance.StartCoroutine(GManager.instance.attackProcess.Attack(_attacker, _defender, _cardEffect, _withoutTap, _beforeOnAttackCoroutine));
  450. if(_afterOnAttackCoroutine != null)
  451. yield return ContinuousController.instance.StartCoroutine(_afterOnAttackCoroutine());
  452. }
  453. }
  454. }
  455. }
  456. #region ‘I‘ðŒˆ’è
  457. [PunRPC]
  458. public void SetAttackTarget(int playerID, bool isTurnPlayer, int permanentIndex)
  459. {
  460. bool[] isTurnPlayerList = new bool[] { isTurnPlayer };
  461. int[] permanentIndexList = new int[] { permanentIndex };
  462. Player selectionPlayer = GManager.instance.GetPlayerFromID(playerID);
  463. if (selectionPlayer == null)
  464. {
  465. return;
  466. }
  467. selectionPlayer.QueuePlayerSelection(new PermanentSelection(isTurnPlayerList, permanentIndexList));
  468. }
  469. #endregion
  470. }