FieldPermanentCard.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Events;
  5. using UnityEngine.UI;
  6. using DG.Tweening;
  7. using UnityEngine.EventSystems;
  8. using TMPro;
  9. using System;
  10. public class FieldPermanentCard : MonoBehaviour
  11. {
  12. [Header("選択状態アウトライン")]
  13. public Image Outline_Select;
  14. [Header("カード画像")]
  15. public Image CardImage;
  16. [Header("ブロッカーエフェクト")]
  17. public GameObject BlockerEffect;
  18. [Header("エナジー個数Text")]
  19. public Text EnergyCountText;
  20. [Header("コライダー")]
  21. public GameObject Collider;
  22. [Header("アニメーター")]
  23. public Animator anim;
  24. [Header("コマンドパネル")]
  25. public CommandPanel fieldUnitCommandPanel;
  26. [Header("スキル名Text")]
  27. public Text SkillNameText;
  28. [Header("DPテキスト")]
  29. public Text DPText;
  30. [Header("DP枠")]
  31. public List<Image> DPBackground_color = new List<Image>();
  32. [Header("進化元枚テキスト")]
  33. public Text EvoRootCountText;
  34. [Header("進化元枚枠")]
  35. public Image EvoRootCountBackground;
  36. [Header("レベルテキスト")]
  37. public TextMeshProUGUI LevelText;
  38. [Header("表示親")]
  39. public GameObject Parent;
  40. [Header("スキル使用エフェクト")]
  41. public GameObject UsingSkillEffect;
  42. [Header("進化元追加エフェクト")]
  43. public ParticleSystem addDigivolutionCardsEffect;
  44. //初期スケール
  45. public Vector3 StartScale { get; set; }
  46. [Header("パーマネント番号テキスト")]
  47. public TextMeshProUGUI permanentIndexText;
  48. [Header("タップオブジェクト")]
  49. public GameObject TapObject;
  50. [Header("ダメージテキスト")]
  51. public Text DirectStrikeText;
  52. [Header("召喚酔い")]
  53. public GameObject SummonSicknessObject;
  54. [Header("進化予定オブジェクト")]
  55. public GameObject WillEvolutionObject;
  56. [Header("消滅予定オブジェクト")]
  57. public GameObject WillBeDeletedObject;
  58. [Header("デッキバウンス予定オブジェクト")]
  59. public GameObject WillBeDeckBounceObject;
  60. [Header("手札バウンス予定オブジェクト")]
  61. public GameObject WillBeHandBounceObject;
  62. [Header("場を離れる予定オブジェクト")]
  63. public GameObject WillRemoveFieldObject;
  64. [Header("アンタップ予定オブジェクト")]
  65. public GameObject WillUntapObject;
  66. public List<TextCardColorMaterial> textCardColorMaterials = new List<TextCardColorMaterial>();
  67. public UnityAction<FieldPermanentCard> OnClickAction;
  68. public Permanent ThisPermanent { get; set; }
  69. public bool IsEffectPlaying { get; set; }
  70. private void Awake()
  71. {
  72. RemoveSelectEffect();
  73. CloseCommandPanel();
  74. Outline_Select.gameObject.SetActive(false);
  75. OffUsingSkillEffect();
  76. OffSkillName();
  77. if (BlockerEffect != null)
  78. {
  79. BlockerEffect.gameObject.SetActive(false);
  80. }
  81. if (EvoRootCountText != null)
  82. {
  83. EvoRootCountText.transform.parent.gameObject.SetActive(false);
  84. }
  85. if (DirectStrikeText != null)
  86. {
  87. DirectStrikeText.transform.parent.parent.gameObject.SetActive(true);
  88. DirectStrikeText.transform.parent.gameObject.SetActive(false);
  89. }
  90. if (SummonSicknessObject != null)
  91. {
  92. SummonSicknessObject.gameObject.SetActive(false);
  93. }
  94. if (WillEvolutionObject != null)
  95. {
  96. WillEvolutionObject.SetActive(false);
  97. }
  98. if (WillBeDeletedObject != null)
  99. {
  100. WillBeDeletedObject.SetActive(false);
  101. }
  102. if (WillBeDeckBounceObject != null)
  103. {
  104. WillBeDeckBounceObject.SetActive(false);
  105. }
  106. if (WillBeHandBounceObject != null)
  107. {
  108. WillBeHandBounceObject.SetActive(false);
  109. }
  110. if (WillRemoveFieldObject != null)
  111. {
  112. WillRemoveFieldObject.SetActive(false);
  113. }
  114. if (WillUntapObject != null)
  115. {
  116. WillUntapObject.SetActive(false);
  117. }
  118. OffPermanentIndexText();
  119. }
  120. public IEnumerator ShowAddDigivolutionCardEffect()
  121. {
  122. if (addDigivolutionCardsEffect != null)
  123. {
  124. addDigivolutionCardsEffect.gameObject.SetActive(true);
  125. addDigivolutionCardsEffect.Play();
  126. }
  127. yield return new WaitForSeconds(0.1f);
  128. }
  129. public void OffPermanentIndexText()
  130. {
  131. if (permanentIndexText != null)
  132. {
  133. permanentIndexText.transform.parent.gameObject.SetActive(false);
  134. }
  135. }
  136. public void SetPermanentIndexText(List<Permanent> permanents)
  137. {
  138. if (permanentIndexText != null)
  139. {
  140. if (ThisPermanent != null)
  141. {
  142. if (ThisPermanent.TopCard != null)
  143. {
  144. int index = permanents.IndexOf(ThisPermanent) + 1;
  145. permanentIndexText.transform.parent.parent.gameObject.SetActive(true);
  146. permanentIndexText.transform.parent.gameObject.SetActive(true);
  147. permanentIndexText.text = $"{index}";
  148. }
  149. }
  150. }
  151. }
  152. void RotateSkillNameText()
  153. {
  154. SkillNameText.transform.parent.localRotation = Quaternion.Euler(0, 0, -1 * this.transform.localRotation.eulerAngles.z);
  155. }
  156. public void OnSkillName(ICardEffect cardEffect)
  157. {
  158. if (SkillNameText != null && ThisPermanent != null)
  159. {
  160. RotateSkillNameText();
  161. SkillNameText.transform.parent.gameObject.SetActive(true);
  162. SkillNameText.text = cardEffect.EffectName;
  163. }
  164. }
  165. public void OffSkillName()
  166. {
  167. if (SkillNameText != null)
  168. {
  169. SkillNameText.transform.parent.gameObject.SetActive(false);
  170. }
  171. }
  172. public void OffUsingSkillEffect()
  173. {
  174. if (UsingSkillEffect != null)
  175. {
  176. UsingSkillEffect.SetActive(false);
  177. }
  178. }
  179. public void OnUsingSkillEffect()
  180. {
  181. if (UsingSkillEffect != null)
  182. {
  183. UsingSkillEffect.SetActive(true);
  184. }
  185. }
  186. float _validPressTime = 0.5f;
  187. float _requiredTime = 0.0f;
  188. bool _pressing = false;
  189. #region Processing on click
  190. #region クリックされた時の処理
  191. public void OnClick()
  192. {
  193. if (ThisPermanent != null)
  194. {
  195. if (ThisPermanent.TopCard != null)
  196. {
  197. if (Input.GetMouseButtonUp(0))
  198. {
  199. OnClickAction?.Invoke(this);
  200. }
  201. else if (Input.GetMouseButtonUp(1))
  202. {
  203. OnRightClicked();
  204. }
  205. }
  206. }
  207. }
  208. void OnRightClicked()
  209. {
  210. if (!ThisPermanent.TopCard.IsFlipped || ThisPermanent.TopCard.Owner.isYou)
  211. {
  212. GManager.instance.pokemonDetail.OpenUnitDetail(ThisPermanent);
  213. }
  214. }
  215. #endregion
  216. #region クリックされた時の処理を追加
  217. public void AddClickTarget(UnityAction<FieldPermanentCard> _OnClickAction)
  218. {
  219. OnClickAction = _OnClickAction;
  220. }
  221. #endregion
  222. #region クリックされた時の処理を削除
  223. public void RemoveClickTarget()
  224. {
  225. OnClickAction = null;
  226. }
  227. #endregion
  228. #endregion
  229. #region Set permanent data
  230. public void SetPermanentData(Permanent permanent, bool updateIsTapped)
  231. {
  232. ThisPermanent = permanent;
  233. ShowPermanentData(updateIsTapped);
  234. }
  235. #endregion
  236. #region Reflect permanent data on UI
  237. bool _oldTurnSuspendedCards = false;
  238. public async void ShowPermanentData(bool updateIsTapped)
  239. {
  240. if (ThisPermanent == null)
  241. {
  242. return;
  243. }
  244. if (ThisPermanent.TopCard != null)
  245. {
  246. // reverse opponent's card
  247. if (!ThisPermanent.TopCard.Owner.isYou)
  248. {
  249. if (ContinuousController.instance != null)
  250. {
  251. if (ContinuousController.instance.reverseOpponentsCards)
  252. {
  253. Parent.transform.localRotation = Quaternion.Euler(new Vector3(0, 0, 180));
  254. }
  255. else
  256. {
  257. Parent.transform.localRotation = Quaternion.Euler(new Vector3(0, 0, 0));
  258. }
  259. }
  260. }
  261. // card image
  262. if (ThisPermanent.TopCard.IsFlipped)
  263. {
  264. CardImage.sprite = ContinuousController.instance.ReverseCard;
  265. }
  266. else
  267. {
  268. CardImage.sprite = await ThisPermanent.TopCard.GetCardSprite();
  269. }
  270. CardImage.gameObject.SetActive(true);
  271. //タップ
  272. if (ThisPermanent.IsSuspended)
  273. {
  274. if (TapObject != null)
  275. {
  276. TapObject.SetActive(true);
  277. }
  278. if (updateIsTapped)
  279. {
  280. bool turnSuspendedCards = ContinuousController.instance != null && ContinuousController.instance.turnSuspendedCards;
  281. if (anim != null)
  282. {
  283. if (ThisPermanent.OldIsSuspended != ThisPermanent.IsSuspended || _oldTurnSuspendedCards != turnSuspendedCards)
  284. {
  285. ThisPermanent.OldIsSuspended = ThisPermanent.IsSuspended;
  286. _oldTurnSuspendedCards = turnSuspendedCards;
  287. if (turnSuspendedCards)
  288. {
  289. anim.SetInteger("Tap", 1);
  290. }
  291. else
  292. {
  293. anim.SetInteger("Tap", -1);
  294. }
  295. }
  296. }
  297. }
  298. }
  299. else
  300. {
  301. if (TapObject != null)
  302. {
  303. TapObject.SetActive(false);
  304. }
  305. if (ContinuousController.instance != null)
  306. {
  307. if (ContinuousController.instance.turnSuspendedCards)
  308. {
  309. if (ThisPermanent.OldIsSuspended != ThisPermanent.IsSuspended)
  310. {
  311. ThisPermanent.OldIsSuspended = ThisPermanent.IsSuspended;
  312. if (anim != null)
  313. {
  314. anim.SetInteger("Tap", -1);
  315. }
  316. }
  317. }
  318. }
  319. }
  320. //DP
  321. if (ThisPermanent.IsDigimon && ThisPermanent.DP >= 0)
  322. {
  323. DPText.transform.parent.gameObject.SetActive(true);
  324. DPText.text = ThisPermanent.DP.ToString();
  325. for (int i = 0; i < DPBackground_color.Count; i++)
  326. {
  327. CardColor cardColor = CardColor.None;
  328. if (i < ThisPermanent.TopCard.CardColors.Count)
  329. {
  330. cardColor = ThisPermanent.TopCard.CardColors[i];
  331. }
  332. else
  333. {
  334. cardColor = ThisPermanent.TopCard.CardColors[0];
  335. }
  336. DPBackground_color[i].color = DataBase.CardColor_ColorLightDictionary[cardColor];
  337. }
  338. }
  339. else
  340. {
  341. DPText.transform.parent.gameObject.SetActive(false);
  342. }
  343. //Level
  344. if (ThisPermanent.IsDigimon && ThisPermanent.TopCard.HasLevel)
  345. {
  346. LevelText.transform.parent.gameObject.SetActive(true);
  347. LevelText.text = ThisPermanent.Level.ToString();
  348. foreach (TextCardColorMaterial textCardColorMaterial in textCardColorMaterials)
  349. {
  350. if (textCardColorMaterial.cardColor == ThisPermanent.TopCard.CardColors[0])
  351. {
  352. LevelText.fontSharedMaterial = new Material(textCardColorMaterial.material);
  353. break;
  354. }
  355. }
  356. }
  357. else
  358. {
  359. LevelText.transform.parent.gameObject.SetActive(false);
  360. }
  361. if (EvoRootCountText != null)
  362. {
  363. if (ThisPermanent.DigivolutionCards.Count >= 1)
  364. {
  365. EvoRootCountText.transform.parent.gameObject.SetActive(true);
  366. EvoRootCountText.text = $"×{ThisPermanent.DigivolutionCards.Count}";
  367. EvoRootCountBackground.color = DataBase.CardColor_ColorLightDictionary[ThisPermanent.TopCard.CardColors[0]];
  368. }
  369. else
  370. {
  371. EvoRootCountText.transform.parent.gameObject.SetActive(false);
  372. }
  373. }
  374. if (SummonSicknessObject != null)
  375. {
  376. bool isActive = false;
  377. if (ThisPermanent.EnterFieldTurnCount == GManager.instance.turnStateMachine.TurnCount)
  378. {
  379. if (GManager.instance.turnStateMachine.gameContext.TurnPlayer == ThisPermanent.TopCard.Owner)
  380. {
  381. if (!ThisPermanent.HasRush)
  382. {
  383. if (ThisPermanent.TopCard.Owner.GetBattleAreaPermanents().Contains(ThisPermanent))
  384. {
  385. isActive = true;
  386. }
  387. }
  388. }
  389. }
  390. SummonSicknessObject.SetActive(isActive);
  391. }
  392. if (BlockerEffect != null)
  393. {
  394. bool isActive = false;
  395. if (ThisPermanent.IsDigimon)
  396. {
  397. if (ThisPermanent.HasBlocker)
  398. {
  399. //if (thisPermanent.TopCard.Owner.GetBattleAreaPermanents().Contains(thisPermanent))
  400. {
  401. isActive = true;
  402. }
  403. }
  404. }
  405. BlockerEffect.SetActive(isActive);
  406. }
  407. }
  408. }
  409. #endregion
  410. #region Automatically reflect card data
  411. int _frameCount = 0;
  412. int _updateFrame = 75;
  413. public bool destroyed { get; set; } = false;
  414. public bool skipDestroy { get; set; } = false;
  415. public bool skipUpdate { get; set; } = false;
  416. //TODO: Pretty poor gargage collection, need to optimize - MB
  417. private void LateUpdate()
  418. {
  419. if (skipUpdate)
  420. {
  421. return;
  422. }
  423. #region exception check
  424. if (destroyed)
  425. {
  426. return;
  427. }
  428. if (ThisPermanent == null)
  429. {
  430. return;
  431. }
  432. if (ThisPermanent.TopCard == null)
  433. {
  434. if (!destroyed && !skipDestroy)
  435. {
  436. StartCoroutine(DestroyCoroutine());
  437. return;
  438. }
  439. }
  440. #endregion
  441. //Skill name
  442. if (SkillNameText.transform.parent.gameObject.activeSelf)
  443. {
  444. RotateSkillNameText();
  445. }
  446. if (DirectStrikeText != null)
  447. {
  448. if (ThisPermanent.IsSuspended)
  449. {
  450. DirectStrikeText.transform.parent.parent.transform.localPosition = new Vector2(DirectStrikeText.transform.parent.parent.transform.localPosition.x, 24);
  451. }
  452. else
  453. {
  454. DirectStrikeText.transform.parent.parent.transform.localPosition = new Vector2(DirectStrikeText.transform.parent.parent.transform.localPosition.x, 78);
  455. }
  456. DirectStrikeText.transform.parent.parent.localRotation = Quaternion.Euler(0, 0, -1 * this.transform.localRotation.eulerAngles.z);
  457. }
  458. if (ThisPermanent.TopCard.Owner.GetFieldPermanents().Count >= ThisPermanent.TopCard.Owner.fieldCardFrames.Count * 0.7f)
  459. {
  460. _updateFrame = 115;
  461. }
  462. else
  463. {
  464. _updateFrame = 75;
  465. }
  466. #region Get long press
  467. #if !UNITY_EDITOR && UNITY_ANDROID
  468. if (pressing)
  469. {
  470. if(requiredTime < Time.time)
  471. {
  472. OnRightClicked();
  473. pressing = false;
  474. }
  475. }
  476. #endif
  477. #endregion
  478. #region Reflected only once every few frames
  479. _frameCount++;
  480. if (_frameCount < _updateFrame)
  481. {
  482. return;
  483. }
  484. else
  485. {
  486. _frameCount = 0;
  487. }
  488. #endregion
  489. ShowPermanentData(true);
  490. }
  491. #endregion
  492. public void PointerDown(BaseEventData eventData)
  493. {
  494. if (!_pressing)
  495. {
  496. _pressing = true;
  497. _requiredTime = Time.time + _validPressTime;
  498. }
  499. else
  500. {
  501. _pressing = false;
  502. }
  503. }
  504. public void PointerUp(BaseEventData eventData)
  505. {
  506. if (_pressing)
  507. {
  508. _pressing = false;
  509. }
  510. }
  511. public void PointerExit(BaseEventData eventData)
  512. {
  513. if (_pressing)
  514. {
  515. _pressing = false;
  516. }
  517. }
  518. #region このオブジェクトを削除
  519. IEnumerator DestroyCoroutine()
  520. {
  521. yield return null;
  522. destroyed = true;
  523. Destroy(this.gameObject);
  524. }
  525. #endregion
  526. #region 選択状態
  527. public bool isExpand { get; set; }
  528. #region 選択状態
  529. public void OnSelectEffect(float expand)
  530. {
  531. if (isExpand)
  532. {
  533. return;
  534. }
  535. Outline_Select.gameObject.SetActive(true);
  536. if (this.gameObject.activeSelf)
  537. {
  538. StartCoroutine(ExpandCoroutine(expand));
  539. }
  540. isExpand = true;
  541. SetOrangeOutline();
  542. }
  543. #endregion
  544. #region オレンジアウトライン
  545. public void SetOrangeOutline()
  546. {
  547. Outline_Select.color = DataBase.SelectColor_Orange;
  548. }
  549. #endregion
  550. #region ブルーアウトライン
  551. public void SetBlueOutline()
  552. {
  553. Outline_Select.color = DataBase.SelectColor_Blue;
  554. }
  555. #endregion
  556. #region 大きくする
  557. IEnumerator ExpandCoroutine(float expand)
  558. {
  559. float ExpandTime = 0.06f;
  560. float targetScale = StartScale.x * expand;
  561. float expandSpeed = (targetScale - transform.localScale.x) / ExpandTime;
  562. while (transform.localScale.x < targetScale)
  563. {
  564. transform.localScale += new Vector3(expandSpeed * Time.deltaTime, expandSpeed * Time.deltaTime, 0);
  565. yield return new WaitForSeconds(Time.deltaTime);
  566. if (!isExpand)
  567. {
  568. transform.localScale = StartScale;
  569. yield break;
  570. }
  571. }
  572. transform.localScale = new Vector3(targetScale, targetScale, 1);
  573. if (!isExpand)
  574. {
  575. transform.localScale = StartScale;
  576. yield break;
  577. }
  578. }
  579. #endregion
  580. #region 選択状態リセット
  581. public void RemoveSelectEffect()
  582. {
  583. Outline_Select.gameObject.SetActive(false);
  584. if (!isExpand)
  585. {
  586. return;
  587. }
  588. transform.localScale = StartScale;
  589. isExpand = false;
  590. }
  591. #endregion
  592. #endregion
  593. #region キャンバス座標への変換
  594. public Vector3 GetLocalCanvasPosition()
  595. {
  596. return this.transform.localPosition + this.transform.parent.localPosition;
  597. }
  598. #endregion
  599. #region コマンドパネルを閉じる
  600. public void CloseCommandPanel()
  601. {
  602. if (fieldUnitCommandPanel != null)
  603. {
  604. fieldUnitCommandPanel.CloseCommandPanel();
  605. }
  606. }
  607. #endregion
  608. #region ドラッグ
  609. public UnityAction<FieldPermanentCard> OnBeginDragAction { get; set; }
  610. public UnityAction<FieldPermanentCard, List<DropArea>> OnDragAction { get; set; }
  611. public UnityAction<FieldPermanentCard, List<DropArea>> OnEndDragAction { get; set; }
  612. public void RemoveDragTarget()
  613. {
  614. this.OnBeginDragAction = null;
  615. this.OnDragAction = null;
  616. this.OnEndDragAction = null;
  617. }
  618. public void AddDragTarget(UnityAction<FieldPermanentCard> OnBeginDragAction, UnityAction<FieldPermanentCard, List<DropArea>> OnDragAction, UnityAction<FieldPermanentCard, List<DropArea>> OnEndDragAction)
  619. {
  620. this.OnBeginDragAction = OnBeginDragAction;
  621. this.OnDragAction = OnDragAction;
  622. this.OnEndDragAction = OnEndDragAction;
  623. }
  624. public void OnBeginDrag()
  625. {
  626. OnBeginDragAction?.Invoke(this);
  627. }
  628. public void OnDrag()
  629. {
  630. OnDragAction?.Invoke(this, Draggable.GetRaycastArea());
  631. }
  632. public void OnEndDrag()
  633. {
  634. OnEndDragAction?.Invoke(this, Draggable.GetRaycastArea());
  635. }
  636. #endregion
  637. }
  638. [System.Serializable]
  639. public class TextCardColorMaterial
  640. {
  641. public CardColor cardColor;
  642. public Material material;
  643. }