SelectAttackEffect.cs 20 KB

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