SelectCardEffect.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System.Linq;
  5. using Photon.Pun;
  6. using System;
  7. public class SelectCardEffect : MonoBehaviourPunCallbacks
  8. {
  9. public void SetUp(
  10. Func<CardSource, bool> canTargetCondition,
  11. Func<List<CardSource>, CardSource, bool> canTargetCondition_ByPreSelecetedList,
  12. Func<List<CardSource>, bool> canEndSelectCondition,
  13. Func<bool> canNoSelect,
  14. Func<CardSource, IEnumerator> selectCardCoroutine,
  15. Func<List<CardSource>, IEnumerator> afterSelectCardCoroutine,
  16. string message,
  17. int maxCount,
  18. bool canEndNotMax,
  19. bool isShowOpponent,
  20. Mode mode,
  21. Root root,
  22. List<CardSource> customRootCardList,
  23. bool canLookReverseCard,
  24. Player selectPlayer,
  25. ICardEffect cardEffect)
  26. {
  27. _canTargetCondition = canTargetCondition;
  28. _canTargetCondition_ByPreSelecetedList = canTargetCondition_ByPreSelecetedList;
  29. _canEndSelectCondition = canEndSelectCondition;
  30. _canNoSelect = canNoSelect;
  31. _selectCardCoroutine = selectCardCoroutine;
  32. _afterSelectCardCoroutine = afterSelectCardCoroutine;
  33. _message = message;
  34. _maxCount = maxCount;
  35. _canEndNotMax = canEndNotMax;
  36. _isShowOpponent = isShowOpponent;
  37. _mode = mode;
  38. _root = root;
  39. _customRootCardList = customRootCardList;
  40. _canLookReverseCard = canLookReverseCard;
  41. _selectPlayer = selectPlayer;
  42. _cardEffect = cardEffect;
  43. _customMessage = null;
  44. _customMessage_Enemy = null;
  45. _customMessage_ShowCard = null;
  46. _customCountText = null;
  47. _showReverseCard = true;
  48. _showCard = true;
  49. _isDigiXros = false;
  50. _isDeckBottom = false;
  51. _isDeckTop = false;
  52. _notAddLog = false;
  53. _isSecurity = false;
  54. _skillInfos = new List<SkillInfo>();
  55. _afterSelectIndexCoroutine = null;
  56. }
  57. public void SetIsDeckBottom()
  58. {
  59. _isDeckBottom = true;
  60. }
  61. public void SetIsDeckTop()
  62. {
  63. _isDeckTop = true;
  64. }
  65. public void SetNotShowCard()
  66. {
  67. _showCard = false;
  68. }
  69. public void SetNotAddLog()
  70. {
  71. _notAddLog = true;
  72. }
  73. public void SetDigiXros()
  74. {
  75. _isDigiXros = true;
  76. }
  77. public void SetIsSecurity()
  78. {
  79. _isSecurity = true;
  80. }
  81. public void SetUpSkillInfos(List<SkillInfo> skillInfos)
  82. {
  83. _skillInfos = skillInfos.Clone();
  84. }
  85. public void SetUpCustomMessage(string CustomMessage, string CustomMessage_Enemy)
  86. {
  87. _customMessage = CustomMessage;
  88. _customMessage_Enemy = CustomMessage_Enemy;
  89. }
  90. public void SetUpCustomMessage_ShowCard(string CustomMessage_ShowCard)
  91. {
  92. _customMessage_ShowCard = CustomMessage_ShowCard;
  93. }
  94. public void SetUpCustomCountText(string CustomCountText)
  95. {
  96. _customCountText = CustomCountText;
  97. }
  98. public void SetShowReverseCard()
  99. {
  100. _showReverseCard = false;
  101. }
  102. Func<CardSource, bool> _canTargetCondition = null;
  103. Func<List<CardSource>, CardSource, bool> _canTargetCondition_ByPreSelecetedList = null;
  104. Func<List<CardSource>, bool> _canEndSelectCondition = null;
  105. Func<bool> _canNoSelect = null;
  106. List<CardSource> _targetCards = new List<CardSource>();
  107. Func<CardSource, IEnumerator> _selectCardCoroutine = null;
  108. Func<List<CardSource>, IEnumerator> _afterSelectCardCoroutine = null;
  109. string _message = "";
  110. int _maxCount = 0;
  111. bool _canEndNotMax = false;
  112. bool _isShowOpponent = false;
  113. bool _canLookReverseCard = false;
  114. List<CardSource> _customRootCardList { get; set; } = new List<CardSource>();
  115. Player _selectPlayer = null;
  116. ICardEffect _cardEffect = null;
  117. bool _showReverseCard = true;
  118. bool _showCard = true;
  119. bool _notAddLog = false;
  120. bool _isDigiXros = false;
  121. bool _isSecurity = false;
  122. public enum Mode
  123. {
  124. AddHand,
  125. Discard,
  126. // PutLibraryTop,
  127. // PutLibraryBottom,
  128. Custom,
  129. }
  130. Mode _mode;
  131. public enum Root
  132. {
  133. Library,
  134. Trash,
  135. Clock,
  136. Security,
  137. Custom,
  138. Hand,
  139. Recollection,
  140. Execution,
  141. DigivolutionCards,
  142. EnemyHand,
  143. None,
  144. }
  145. Root _root;
  146. bool _endSelect = false;
  147. bool _isDeckBottom = false;
  148. bool _isDeckTop = false;
  149. string _customMessage = null;
  150. string _customMessage_Enemy = null;
  151. string _customMessage_ShowCard = null;
  152. string _customCountText = null;
  153. List<SkillInfo> _skillInfos = new List<SkillInfo>();
  154. List<int> _slectedInexesInList = new List<int>();
  155. Func<List<int>, IEnumerator> _afterSelectIndexCoroutine = null;
  156. public void SetUpAfterSelectIndexCoroutine(Func<List<int>, IEnumerator> AfterSelectIndexCoroutine)
  157. {
  158. _afterSelectIndexCoroutine = AfterSelectIndexCoroutine;
  159. }
  160. public virtual List<CardSource> RootCardList()
  161. {
  162. List<CardSource> RootCardList = new List<CardSource>();
  163. switch (_root)
  164. {
  165. case Root.Library:
  166. foreach (CardSource cardSource in _selectPlayer.LibraryCards)
  167. {
  168. RootCardList.Add(cardSource);
  169. }
  170. break;
  171. case Root.Trash:
  172. foreach (CardSource cardSource in _selectPlayer.TrashCards)
  173. {
  174. RootCardList.Add(cardSource);
  175. }
  176. break;
  177. case Root.Security:
  178. foreach (CardSource cardSource in _selectPlayer.SecurityCards)
  179. {
  180. RootCardList.Add(cardSource);
  181. }
  182. break;
  183. case Root.Recollection:
  184. foreach (CardSource cardSource in _selectPlayer.LostCards)
  185. {
  186. RootCardList.Add(cardSource);
  187. }
  188. break;
  189. case Root.EnemyHand:
  190. foreach (CardSource cardSource in RandomUtility.ShuffledDeckCards(_selectPlayer.Enemy.HandCards))
  191. {
  192. RootCardList.Add(cardSource);
  193. }
  194. break;
  195. case Root.Custom:
  196. foreach (CardSource cardSource in _customRootCardList)
  197. {
  198. RootCardList.Add(cardSource);
  199. }
  200. break;
  201. }
  202. return RootCardList;
  203. }
  204. public bool active()
  205. {
  206. if (RootCardList().Count > 0)
  207. {
  208. if (_root != Root.Library && _root != Root.Security && _root != Root.Custom)
  209. {
  210. if (RootCardList().Count((cardSource) => _canTargetCondition(cardSource)) > 0)
  211. {
  212. return true;
  213. }
  214. }
  215. else
  216. {
  217. return true;
  218. }
  219. }
  220. return false;
  221. }
  222. public virtual IEnumerator Activate()
  223. {
  224. bool oldIsSelecting = GManager.instance.turnStateMachine.IsSelecting;
  225. List<CardSource> handCards = new List<CardSource>();
  226. _targetCards = new List<CardSource>();
  227. _slectedInexesInList = new List<int>();
  228. yield return GManager.instance.photonWaitController.StartWait("SelectCardEffect");
  229. bool oldIsActiveOutline_AttackingPermanent = false;
  230. if (GManager.instance.attackProcess.AttackingPermanent != null)
  231. {
  232. if (GManager.instance.attackProcess.AttackingPermanent.ShowingPermanentCard != null)
  233. {
  234. oldIsActiveOutline_AttackingPermanent = GManager.instance.attackProcess.AttackingPermanent.ShowingPermanentCard.Outline_Select.gameObject.activeSelf;
  235. }
  236. }
  237. foreach (Player player in GManager.instance.turnStateMachine.gameContext.Players)
  238. {
  239. GManager.instance.turnStateMachine.OffFieldCardTarget(player);
  240. GManager.instance.turnStateMachine.OffHandCardTarget(player);
  241. }
  242. if (GManager.instance.attackProcess.AttackingPermanent != null)
  243. {
  244. if (GManager.instance.attackProcess.AttackingPermanent.ShowingPermanentCard != null)
  245. {
  246. GManager.instance.attackProcess.AttackingPermanent.ShowingPermanentCard.Outline_Select.gameObject.SetActive(oldIsActiveOutline_AttackingPermanent);
  247. }
  248. }
  249. if (_maxCount == 0)
  250. {
  251. _canNoSelect = () => true;
  252. }
  253. if (_root == Root.Security)
  254. {
  255. SetIsSecurity();
  256. }
  257. if (active())
  258. {
  259. if (_isSecurity)
  260. {
  261. GManager.instance.turnStateMachine.gameContext.IsSecurityLooking = true;
  262. }
  263. if (_selectPlayer.isYou)
  264. {
  265. if ((_isDeckBottom && ContinuousController.instance.autoDeckBottomOrder)
  266. || (_isDeckTop && ContinuousController.instance.autoDeckTopOrder))
  267. {
  268. AutoSelect();
  269. }
  270. else
  271. {
  272. List<int> targetCardIDs = new List<int>();
  273. GManager.instance.turnStateMachine.IsSelecting = true;
  274. #region Message Display
  275. if (!string.IsNullOrEmpty(_customMessage))
  276. {
  277. GManager.instance.commandText.OpenCommandText(_customMessage, _isDigiXros);
  278. }
  279. else
  280. {
  281. string message = "";
  282. switch (_mode)
  283. {
  284. case Mode.AddHand:
  285. message = "Select cards to add to your hand.";
  286. break;
  287. case Mode.Discard:
  288. message = "Select cards to trash.";
  289. break;
  290. case Mode.Custom:
  291. message = "Select cards.";
  292. break;
  293. }
  294. if (!string.IsNullOrEmpty(message))
  295. {
  296. GManager.instance.commandText.OpenCommandText(message, _isDigiXros);
  297. }
  298. }
  299. #endregion
  300. List<CardSource> RootCards = new List<CardSource>();
  301. foreach (CardSource cardSource in RootCardList())
  302. {
  303. RootCards.Add(cardSource);
  304. }
  305. #region 並び替え
  306. if (_root == Root.Library || _root == Root.Trash)
  307. {
  308. RootCards = DeckData.SortedCardsList(RootCards);
  309. List<CardSource> matchConditionCards = new List<CardSource>();
  310. List<CardSource> notMatchConditionCards = new List<CardSource>();
  311. foreach (CardSource cardSource in RootCards)
  312. {
  313. if (_canTargetCondition != null)
  314. {
  315. if (_canTargetCondition(cardSource))
  316. {
  317. matchConditionCards.Add(cardSource);
  318. }
  319. else
  320. {
  321. notMatchConditionCards.Add(cardSource);
  322. }
  323. }
  324. }
  325. RootCards = new List<CardSource>();
  326. foreach (CardSource cardSource in matchConditionCards)
  327. {
  328. RootCards.Add(cardSource);
  329. }
  330. foreach (CardSource cardSource in notMatchConditionCards)
  331. {
  332. RootCards.Add(cardSource);
  333. }
  334. }
  335. #endregion
  336. #region 効果発動中・発動待機中表示
  337. if (_cardEffect != null)
  338. {
  339. if (_cardEffect.EffectSourceCard != null)
  340. {
  341. if (_skillInfos.Count == 0)
  342. {
  343. if (_root == Root.Trash)
  344. {
  345. SkillInfo[] skillInfoArray = new SkillInfo[RootCards.Count];
  346. for (int i = 0; i < skillInfoArray.Length; i++)
  347. {
  348. if (0 <= i && i <= RootCards.Count - 1)
  349. {
  350. if (RootCards[i] == _cardEffect.EffectSourceCard)
  351. {
  352. ICardEffect cardEffect = new ChangeBaseDPClass();
  353. cardEffect.SetUpICardEffect("Effect Processing", null, RootCards[i]);
  354. skillInfoArray[i] = new SkillInfo(cardEffect, null, EffectTiming.None);
  355. }
  356. else
  357. {
  358. if (GManager.instance.autoProcessing.executingMultipleSkills != null)
  359. {
  360. bool isEffectWaiting(SkillInfo skillInfo)
  361. {
  362. if (skillInfo != null)
  363. {
  364. if (skillInfo.CardEffect != null)
  365. {
  366. if (skillInfo.CardEffect.EffectSourceCard != null)
  367. {
  368. if (skillInfo.CardEffect.EffectSourceCard == RootCards[i])
  369. {
  370. return true;
  371. }
  372. }
  373. }
  374. }
  375. return false;
  376. }
  377. if (GManager.instance.autoProcessing.executingMultipleSkills.StackedSkillInfos.Count(isEffectWaiting) >= 1)
  378. {
  379. ICardEffect cardEffect = new ChangeBaseDPClass();
  380. cardEffect.SetUpICardEffect("Effect Waiting", null, RootCards[i]);
  381. skillInfoArray[i] = new SkillInfo(cardEffect, null, EffectTiming.None);
  382. }
  383. }
  384. }
  385. }
  386. }
  387. _skillInfos = skillInfoArray.ToList();
  388. }
  389. }
  390. }
  391. }
  392. #endregion
  393. GManager.instance.selectCardPanel._customCountText = _customCountText;
  394. yield return StartCoroutine(GManager.instance.selectCardPanel.OpenSelectCardPanel(
  395. Message: _message,
  396. RootCardSources: RootCards,
  397. _CanTargetCondition: _canTargetCondition,
  398. _CanTargetCondition_ByPreSelecetedList: _canTargetCondition_ByPreSelecetedList,
  399. _CanEndSelectCondition: _canEndSelectCondition,
  400. _MaxCount: _maxCount,
  401. _CanEndNotMax: _canEndNotMax,
  402. _CanNoSelect: _canNoSelect,
  403. CanLookReverseCard: _canLookReverseCard,
  404. skillInfos: _skillInfos,
  405. root: _root));
  406. foreach (CardSource selectedCard in GManager.instance.selectCardPanel.SelectedList)
  407. {
  408. targetCardIDs.Add(selectedCard.CardIndex);
  409. }
  410. foreach (int selectedIndex in GManager.instance.selectCardPanel.SelectedIndex)
  411. {
  412. _slectedInexesInList.Add(selectedIndex);
  413. }
  414. photonView.RPC("SetTargetCard", RpcTarget.All, targetCardIDs.ToArray());
  415. }
  416. }
  417. else
  418. {
  419. if (!string.IsNullOrEmpty(_customMessage_Enemy))
  420. {
  421. GManager.instance.commandText.OpenCommandText(_customMessage_Enemy, _isDigiXros);
  422. }
  423. else
  424. {
  425. GManager.instance.commandText.OpenCommandText("The opponent is selecting cards.", _isDigiXros);
  426. }
  427. #region AI
  428. if (GManager.instance.IsAI)
  429. {
  430. AutoSelect();
  431. }
  432. #endregion
  433. }
  434. void AutoSelect()
  435. {
  436. List<CardSource> ValidCards = new List<CardSource>();
  437. foreach (CardSource cardSource in RootCardList())
  438. {
  439. if (_canTargetCondition(cardSource))
  440. {
  441. ValidCards.Add(cardSource);
  442. }
  443. }
  444. IList<int> indexList = Enumerable.Range(0, ValidCards.Count).ToList();
  445. if (ValidCards.Count >= _maxCount)
  446. {
  447. for (int i = 0; i < 1000; i++)
  448. {
  449. List<int> GetIndexes = indexList.GetRandom(_maxCount).ToList();
  450. List<CardSource> GetCards = new List<CardSource>();
  451. foreach (int index in GetIndexes)
  452. {
  453. GetCards.Add(ValidCards[index]);
  454. }
  455. if (_canEndSelectCondition != null)
  456. {
  457. if (!_canEndSelectCondition(GetCards))
  458. {
  459. continue;
  460. }
  461. }
  462. List<int> CardIDs = new List<int>();
  463. foreach (CardSource cardSource in GetCards)
  464. {
  465. CardIDs.Add(cardSource.CardIndex);
  466. }
  467. if (GManager.instance.IsAI)
  468. {
  469. SetTargetCard(CardIDs.ToArray());
  470. }
  471. else
  472. {
  473. photonView.RPC("SetTargetCard", RpcTarget.All, CardIDs.ToArray());
  474. }
  475. break;
  476. }
  477. }
  478. if (GManager.instance.IsAI)
  479. {
  480. _endSelect = true;
  481. }
  482. }
  483. yield return new WaitWhile(() => !_endSelect);
  484. _endSelect = false;
  485. GManager.instance.commandText.CloseCommandText();
  486. yield return new WaitWhile(() => GManager.instance.commandText.gameObject.activeSelf);
  487. #region 選択されたカードを表示
  488. if (_targetCards.Count > 0 && _showCard)
  489. {
  490. if (_targetCards.Count((cardSource) => cardSource.IsFlipped) > 0 && !_showReverseCard)
  491. {
  492. //表示しない
  493. }
  494. else
  495. {
  496. if (_isShowOpponent || (_selectPlayer.isYou && _targetCards.Count((cardSource) => cardSource.Owner == _selectPlayer) > 0))
  497. {
  498. if (!string.IsNullOrEmpty(_customMessage_ShowCard))
  499. {
  500. yield return ContinuousController.instance.StartCoroutine(GManager.instance.GetComponent<Effects>().ShowCardEffect(_targetCards, _customMessage_ShowCard, true, true));
  501. }
  502. else
  503. {
  504. switch (_mode)
  505. {
  506. case Mode.AddHand:
  507. yield return ContinuousController.instance.StartCoroutine(GManager.instance.GetComponent<Effects>().ShowCardEffect(_targetCards, "Cards added to hand", true, true));
  508. break;
  509. case Mode.Discard:
  510. yield return ContinuousController.instance.StartCoroutine(GManager.instance.GetComponent<Effects>().ShowCardEffect(_targetCards, "Cards put on the trash", true, true));
  511. break;
  512. case Mode.Custom:
  513. yield return ContinuousController.instance.StartCoroutine(GManager.instance.GetComponent<Effects>().ShowCardEffect(_targetCards, "Selected Cards", true, true));
  514. break;
  515. }
  516. }
  517. }
  518. }
  519. }
  520. #endregion
  521. Hashtable hashtable = CardEffectCommons.CardEffectHashtable(_cardEffect);
  522. string log = "";
  523. if (_mode == Mode.AddHand)
  524. {
  525. log += $"\nCard{Utils.PluralFormSuffix(_targetCards.Count)} added to hand:";
  526. }
  527. else if (_mode == Mode.AddHand)
  528. {
  529. log += $"\nTrash Card{Utils.PluralFormSuffix(_targetCards.Count)}:";
  530. }
  531. else
  532. {
  533. log += $"\nSelected Card{Utils.PluralFormSuffix(_targetCards.Count)}:";
  534. }
  535. if (_root == Root.Library)
  536. {
  537. yield return ContinuousController.instance.StartCoroutine(CardObjectController.Shuffle(_selectPlayer));
  538. }
  539. foreach (CardSource cardSource in _targetCards)
  540. {
  541. log += $"\n{cardSource.BaseENGCardNameFromEntity}({cardSource.CardID})";
  542. }
  543. switch (_mode)
  544. {
  545. case Mode.AddHand:
  546. foreach (CardSource cardSource in _targetCards)
  547. {
  548. cardSource.SetFace();
  549. if (cardSource.PermanentOfThisCard() != null)
  550. {
  551. if (cardSource.PermanentOfThisCard().DigivolutionCards.Contains(cardSource))
  552. {
  553. yield return ContinuousController.instance.StartCoroutine(GManager.instance.GetComponent<Effects>().RemoveDigivolveRootEffect(cardSource, cardSource.PermanentOfThisCard()));
  554. }
  555. }
  556. handCards.Add(cardSource);
  557. }
  558. break;
  559. case Mode.Discard:
  560. foreach (CardSource cardSource in _targetCards)
  561. {
  562. if (CardEffectCommons.IsExistOnHand(cardSource))
  563. {
  564. List<IDiscardHand> discardHands = _targetCards.Map(cardSource => new IDiscardHand(cardSource, hashtable));
  565. yield return ContinuousController.instance.StartCoroutine(new IDiscardHands(discardHands, _cardEffect).DiscardHands());
  566. }
  567. else
  568. yield return ContinuousController.instance.StartCoroutine(CardObjectController.AddTrashCard(cardSource));
  569. }
  570. break;
  571. case Mode.Custom:
  572. if (_selectCardCoroutine != null)
  573. {
  574. foreach (CardSource cardSource in _targetCards)
  575. {
  576. yield return StartCoroutine(_selectCardCoroutine(cardSource));
  577. }
  578. }
  579. break;
  580. }
  581. if (handCards.Count >= 1)
  582. {
  583. yield return ContinuousController.instance.StartCoroutine(CardObjectController.AddHandCards(handCards, false, _cardEffect));
  584. }
  585. #region ログ追加
  586. if (!_notAddLog)
  587. {
  588. if (_isShowOpponent || _selectPlayer.isYou)
  589. {
  590. if (_targetCards.Count >= 1)
  591. {
  592. log += "\n";
  593. GManager.instance.playLog.AddLogString(log);
  594. }
  595. }
  596. }
  597. #endregion
  598. }
  599. if (_afterSelectCardCoroutine != null)
  600. {
  601. yield return StartCoroutine(_afterSelectCardCoroutine(_targetCards));
  602. }
  603. if (_afterSelectIndexCoroutine != null)
  604. {
  605. yield return StartCoroutine(_afterSelectIndexCoroutine(_slectedInexesInList));
  606. }
  607. GManager.instance.turnStateMachine.gameContext.IsSecurityLooking = false;
  608. GManager.instance.turnStateMachine.IsSelecting = oldIsSelecting;
  609. }
  610. [PunRPC]
  611. public void SetTargetCard(int[] CardIDs)
  612. {
  613. _targetCards = new List<CardSource>();
  614. foreach (int CardID in CardIDs)
  615. {
  616. _targetCards.Add(GManager.instance.turnStateMachine.gameContext.ActiveCardList[CardID]);
  617. }
  618. _endSelect = true;
  619. }
  620. }