FieldPermanentCard.cs 21 KB

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