CardPrefab_CreateDeck.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using System.Linq;
  6. using UnityEngine.Events;
  7. using Shapes2D;
  8. using System;
  9. using Coffee.UIEffects;
  10. using UnityEngine.EventSystems;
  11. using TMPro;
  12. public class CardPrefab_CreateDeck : MonoBehaviour
  13. {
  14. [Header("Card Image")]
  15. public Image CardImage;
  16. [Header("Scroll Rect")]
  17. public List<ScrollRect> scroll = new List<ScrollRect>();
  18. [Header("Cover")]
  19. public List<GameObject> Cover = new List<GameObject>();
  20. [Header("Cover Standard")]
  21. public GameObject Cover_Standard;
  22. [Header("Outline")]
  23. public GameObject Outline;
  24. [Header("Anim")]
  25. public Animator anim;
  26. [Header("Cost Icons")]
  27. public List<Image> CostIcons = new List<Image>();
  28. [Header("Cost Text")]
  29. public TextMeshProUGUI CostText;
  30. [Header("Level Icons")]
  31. public List<Image> LevelIcons;
  32. [Header("Level Text")]
  33. public TextMeshProUGUI LevelText;
  34. [Header("Evo Cost Icons")]
  35. public List<Image> EvoCostIcons;
  36. [Header("Evo Cost Text Level")]
  37. public List<TextMeshProUGUI> EvoCostTexts_Level;
  38. [Header("Evo Cost Text Memory")]
  39. public List<TextMeshProUGUI> EvoCostTexts_Memory;
  40. [Header("Quantity Limit")]
  41. public GameObject LimitObject;
  42. public Text LimitText;
  43. [Header("Ban")]
  44. public GameObject BanObject;
  45. [Header("White Circle")]
  46. public Sprite WhiteCircle;
  47. [Header("Rainbow Circle")]
  48. public Sprite RainbowCircle;
  49. public Transform Parent;
  50. public CEntity_Base cEntity_Base { get; set; }
  51. public UnityAction OnClickAction;
  52. public UnityAction<CardPrefab_CreateDeck> OnEnterAction;
  53. public UnityAction<CardPrefab_CreateDeck> OnExitAction;
  54. public UnityAction<CardPrefab_CreateDeck> OnBeginDragAction;
  55. public bool isActive = true;
  56. public GameObject AddRemoveButtonParent;
  57. public Button AddButton;
  58. public Button RemoveButton;
  59. public UIShiny uIShiny;
  60. private void Start()
  61. {
  62. Outline.SetActive(false);
  63. if (CardImage.GetComponent<UIShiny>() != null)
  64. {
  65. CardImage.GetComponent<UIShiny>().enabled = false;
  66. }
  67. OffAddRemoveButton();
  68. if (CostText != null)
  69. {
  70. CostText.transform.parent.gameObject.SetActive(false);
  71. }
  72. if (LevelText != null)
  73. {
  74. LevelText.transform.parent.gameObject.SetActive(false);
  75. }
  76. if (EvoCostIcons != null)
  77. {
  78. if (EvoCostIcons.Count >= 1)
  79. {
  80. for (int i = 0; i < EvoCostIcons.Count; i++)
  81. {
  82. EvoCostIcons[i].transform.parent.gameObject.SetActive(false);
  83. EvoCostIcons[i].transform.parent.parent.gameObject.SetActive(true);
  84. }
  85. }
  86. }
  87. if (LimitObject != null)
  88. {
  89. LimitObject.SetActive(false);
  90. }
  91. if (BanObject != null)
  92. {
  93. BanObject.SetActive(false);
  94. }
  95. }
  96. public UnityAction<CardPrefab_CreateDeck> OnClickAddButtonAction;
  97. public void OnClickAddButton()
  98. {
  99. OnClickAddButtonAction?.Invoke(this);
  100. }
  101. public UnityAction<CardPrefab_CreateDeck> OnClickRemoveButtonAction;
  102. public void OnClickRemoveButton()
  103. {
  104. OnClickRemoveButtonAction?.Invoke(this);
  105. }
  106. public void SetupAddRemoveButton(DeckData deckData)
  107. {
  108. #if !UNITY_EDITOR && UNITY_ANDROID
  109. return;
  110. #endif
  111. if (AddRemoveButtonParent != null)
  112. {
  113. AddRemoveButtonParent.SetActive(true);
  114. }
  115. bool AddButtonInteractable = false;
  116. if (deckData != null && cEntity_Base != null)
  117. {
  118. AddButtonInteractable = !CanNotAddThisCard(deckData);
  119. }
  120. if (AddButton != null)
  121. {
  122. AddButton.interactable = AddButtonInteractable;
  123. AddButton.transform.parent.gameObject.SetActive(AddButtonInteractable);
  124. }
  125. if (transform.parent != null)
  126. {
  127. for (int i = 0; i < transform.parent.childCount; i++)
  128. {
  129. CardPrefab_CreateDeck cardPrefab_CreateDeck = transform.parent.GetChild(i).GetComponent<CardPrefab_CreateDeck>();
  130. if (cardPrefab_CreateDeck != null && cardPrefab_CreateDeck != this)
  131. {
  132. cardPrefab_CreateDeck.OffAddRemoveButton();
  133. }
  134. }
  135. }
  136. }
  137. public void OffAddRemoveButton()
  138. {
  139. if (AddRemoveButtonParent != null)
  140. {
  141. AddRemoveButtonParent.SetActive(false);
  142. }
  143. }
  144. public void SetUpCardPrefab_CreateDeck(CEntity_Base _cEntity_Base)
  145. {
  146. cEntity_Base = _cEntity_Base;
  147. //ShowCardImage();
  148. }
  149. public async void ShowCardImage()
  150. {
  151. if (this.cEntity_Base != null)
  152. {
  153. //カード画像
  154. CardImage.color = new Color(1, 1, 1, 1);
  155. CardImage.GetComponent<EventTrigger>().enabled = true;
  156. CardImage.sprite = await cEntity_Base.GetCardSprite();
  157. if (CostText != null)
  158. {
  159. if (cEntity_Base.HasCost)
  160. {
  161. for (int i = CostIcons.Count-1; i > -1; i--)
  162. {
  163. CardColor cardColor;
  164. int value = CostIcons.Count - i - 1;
  165. if (i >= CostIcons.Count - cEntity_Base.cardColors.Count)
  166. {
  167. float fillAmount = (float)((CostIcons.Count - i) / (float)cEntity_Base.cardColors.Count);
  168. cardColor = cEntity_Base.cardColors[value];
  169. CostIcons[i].color = DataBase.CardColor_ColorDarkDictionary[cardColor];
  170. CostIcons[i].fillAmount = fillAmount;
  171. CostIcons[i].gameObject.SetActive(true);
  172. }
  173. else
  174. {
  175. CostIcons[i].gameObject.SetActive(false);
  176. }
  177. }
  178. CostText.color = Color.white;
  179. CostText.transform.parent.gameObject.SetActive(true);
  180. CostText.text = $"{cEntity_Base.PlayCost}";
  181. }
  182. else
  183. {
  184. CostText.transform.parent.gameObject.SetActive(false);
  185. }
  186. }
  187. if (LevelIcons != null)
  188. {
  189. if ((cEntity_Base.cardKind.Contains(CardKind.Digimon) || cEntity_Base.cardKind.Contains(CardKind.DigiEgg)) && cEntity_Base.Level >= 1)
  190. {
  191. if (LevelIcons.Count >= 1)
  192. {
  193. for (int i = 0; i < CostIcons.Count; i++)
  194. {
  195. CardColor cardColor;
  196. if (i < cEntity_Base.cardColors.Count)
  197. {
  198. cardColor = cEntity_Base.cardColors[i];
  199. LevelIcons[i].color = DataBase.CardColor_ColorDarkDictionary[cardColor];
  200. LevelIcons[i].gameObject.SetActive(true);
  201. }
  202. else
  203. {
  204. LevelIcons[i].gameObject.SetActive(false);
  205. }
  206. }
  207. LevelText.color = Color.white;
  208. LevelText.transform.parent.gameObject.SetActive(true);
  209. LevelText.text = $"Lv.{cEntity_Base.Level}";
  210. }
  211. }
  212. else
  213. {
  214. LevelText.transform.parent.gameObject.SetActive(false);
  215. }
  216. }
  217. if (EvoCostIcons != null)
  218. {
  219. if (cEntity_Base.cardKind.Contains(CardKind.Digimon))
  220. {
  221. if (EvoCostIcons.Count >= 1)
  222. {
  223. for (int i = 0; i < EvoCostIcons.Count; i++)
  224. {
  225. if (i < cEntity_Base.EvoCosts.Count)
  226. {
  227. EvoCostIcons[i].sprite = WhiteCircle;
  228. EvoCostIcons[i].transform.parent.gameObject.SetActive(true);
  229. CardColor cardColor;
  230. cardColor = cEntity_Base.EvoCosts[i].CardColor;
  231. EvoCostIcons[i].color = DataBase.CardColor_ColorDarkDictionary[cardColor];
  232. EvoCostTexts_Level[i].text = $"Lv.{cEntity_Base.EvoCosts[i].Level}";
  233. EvoCostTexts_Memory[i].text = $"{cEntity_Base.EvoCosts[i].MemoryCost}";
  234. if (cardColor == CardColor.Yellow || cardColor == CardColor.White)
  235. {
  236. EvoCostTexts_Level[i].color = Color.black;
  237. EvoCostTexts_Memory[i].color = Color.black;
  238. }
  239. else
  240. {
  241. EvoCostTexts_Level[i].color = Color.white;
  242. EvoCostTexts_Memory[i].color = Color.white;
  243. }
  244. }
  245. else
  246. {
  247. EvoCostIcons[i].transform.parent.gameObject.SetActive(false);
  248. }
  249. }
  250. if (cEntity_Base.EvoCosts.Count == 7)
  251. {
  252. int cost = cEntity_Base.EvoCosts[0].MemoryCost;
  253. int level = cEntity_Base.EvoCosts[0].Level;
  254. bool sameCost = true;
  255. for (int i = 0; i < cEntity_Base.EvoCosts.Count; i++)
  256. {
  257. if (cEntity_Base.EvoCosts[i].MemoryCost != cost || cEntity_Base.EvoCosts[i].Level != level)
  258. {
  259. sameCost = false;
  260. break;
  261. }
  262. }
  263. if (sameCost)
  264. {
  265. for (int i = 0; i < EvoCostIcons.Count; i++)
  266. {
  267. EvoCostIcons[i].transform.parent.gameObject.SetActive(false);
  268. if (i == 0)
  269. {
  270. EvoCostIcons[i].sprite = RainbowCircle;
  271. EvoCostIcons[i].transform.parent.gameObject.SetActive(true);
  272. EvoCostIcons[i].color = Color.white;
  273. EvoCostTexts_Level[i].text = $"Lv.{cEntity_Base.EvoCosts[i].Level}";
  274. EvoCostTexts_Memory[i].text = $"{cEntity_Base.EvoCosts[i].MemoryCost}";
  275. EvoCostTexts_Level[i].color = Color.white;
  276. EvoCostTexts_Memory[i].color = Color.white;
  277. }
  278. }
  279. }
  280. }
  281. }
  282. }
  283. else
  284. {
  285. for (int i = 0; i < EvoCostIcons.Count; i++)
  286. {
  287. EvoCostIcons[i].transform.parent.gameObject.SetActive(false);
  288. }
  289. }
  290. }
  291. if (LimitObject != null && BanObject != null && LimitText != null)
  292. {
  293. LimitObject.SetActive(false);
  294. BanObject.SetActive(false);
  295. int maxCount = DeckBuildingRule.MaxCount_BanList(cEntity_Base);
  296. if (maxCount <= cEntity_Base.MaxCountInDeck)
  297. {
  298. if (maxCount == 1)
  299. {
  300. LimitObject.SetActive(true);
  301. LimitText.gameObject.SetActive(true);
  302. LimitText.text = $"{maxCount}";
  303. }
  304. else if (maxCount == 0)
  305. {
  306. BanObject.SetActive(true);
  307. }
  308. }
  309. }
  310. }
  311. }
  312. public void HideDeckCardTab()
  313. {
  314. CardImage.color = new Color(1, 1, 1, 0);
  315. CardImage.GetComponent<EventTrigger>().enabled = false;
  316. Outline.SetActive(false);
  317. if (CardImage.GetComponent<UIShiny>() != null)
  318. {
  319. CardImage.GetComponent<UIShiny>().enabled = false;
  320. }
  321. if (CostText != null)
  322. {
  323. CostText.transform.parent.gameObject.SetActive(false);
  324. }
  325. if (LevelText != null)
  326. {
  327. LevelText.transform.parent.gameObject.SetActive(false);
  328. }
  329. if (EvoCostIcons != null)
  330. {
  331. if (EvoCostIcons.Count >= 1)
  332. {
  333. for (int i = 0; i < EvoCostIcons.Count; i++)
  334. {
  335. EvoCostIcons[i].transform.parent.gameObject.SetActive(false);
  336. }
  337. }
  338. }
  339. }
  340. public void SetCover(bool active)
  341. {
  342. foreach (GameObject g in Cover)
  343. {
  344. g.SetActive(active);
  345. }
  346. }
  347. public void CheckCover(DeckData deckData)
  348. {
  349. SetCover(CanNotAddThisCard(deckData));
  350. }
  351. public bool CanNotAddThisCard(DeckData deckData)
  352. {
  353. if (deckData.DeckCards().Count >= 70)
  354. {
  355. return true;
  356. }
  357. if (deckData.DigitamaDeckCards().Count >= 10)
  358. {
  359. return true;
  360. }
  361. if (cEntity_Base != null)
  362. {
  363. if (cEntity_Base.cardKind.Contains(CardKind.DigiEgg))
  364. {
  365. if (cEntity_Base.SameCardIDCount(deckData.DigitamaDeckCards()) >= cEntity_Base.MaxCountInDeck)
  366. {
  367. return true;
  368. }
  369. }
  370. else
  371. {
  372. if (cEntity_Base.SameCardIDCount(deckData.DeckCards()) >= cEntity_Base.MaxCountInDeck)
  373. {
  374. return true;
  375. }
  376. }
  377. if (!DeckBuildingRule.CanAddCard(cEntity_Base, deckData))
  378. {
  379. return true;
  380. }
  381. }
  382. return false;
  383. }
  384. public void OnClick()
  385. {
  386. OnClickAction?.Invoke();
  387. }
  388. public void OnBeginDrag()
  389. {
  390. OnBeginDragAction?.Invoke(this);
  391. }
  392. float _timer = 0f;
  393. private void Update()
  394. {
  395. if (AddRemoveButtonParent.activeSelf)
  396. {
  397. _OnEnter();
  398. }
  399. _timer += Time.deltaTime;
  400. if (_timer > Time.deltaTime * 6)
  401. {
  402. _timer = 0f;
  403. if (isActive && CardImage.gameObject.activeSelf)
  404. {
  405. if (cEntity_Base != null && Cover_Standard != null)
  406. {
  407. Cover_Standard.SetActive(!cEntity_Base.IsStandardValid);
  408. }
  409. }
  410. }
  411. }
  412. public Coroutine _OnEnterCoroutine = null;
  413. public void OnEnter()
  414. {
  415. Outline.SetActive(true);
  416. anim.SetInteger("Open", 1);
  417. anim.SetInteger("Close", 0);
  418. uIShiny.enabled = true;
  419. _OnEnter();
  420. }
  421. IEnumerator OnEnterCoroutine()
  422. {
  423. int count = 0;
  424. while (true)
  425. {
  426. yield return new WaitForSeconds(Time.deltaTime);
  427. bool isRay = false;
  428. List<RaycastResult> results = new List<RaycastResult>();
  429. PointerEventData pointer = new PointerEventData(EventSystem.current)
  430. {
  431. // マウスポインタの位置にレイ飛ばし、ヒットしたものを保存
  432. position = Input.mousePosition
  433. };
  434. EventSystem.current.RaycastAll(pointer, results);
  435. // ヒットしたUIの名前
  436. foreach (RaycastResult target in results)
  437. {
  438. if (target.gameObject == this.CardImage.gameObject)
  439. {
  440. isRay = true;
  441. }
  442. }
  443. if (isRay)
  444. {
  445. _OnEnter();
  446. count++;
  447. if (count > 10)
  448. {
  449. _OnEnterCoroutine = null;
  450. yield break;
  451. }
  452. }
  453. }
  454. }
  455. public void _OnEnter()
  456. {
  457. OnEnterAction?.Invoke(this);
  458. }
  459. public void OnExit()
  460. {
  461. #if !UNITY_EDITOR && UNITY_ANDROID
  462. _OnExit();
  463. return;
  464. #endif
  465. return;
  466. //bool isRay = false;
  467. //List<RaycastResult> results = new List<RaycastResult>();
  468. //PointerEventData pointer = new PointerEventData(EventSystem.current);
  469. //// マウスポインタの位置にレイ飛ばし、ヒットしたものを保存
  470. //pointer.position = Input.mousePosition;
  471. //EventSystem.current.RaycastAll(pointer, results);
  472. //// ヒットしたUIの名前
  473. //foreach (RaycastResult target in results)
  474. //{
  475. // if (target.gameObject == AddButton.gameObject || target.gameObject == RemoveButton.gameObject)
  476. // {
  477. // return;
  478. // }
  479. //}
  480. //_OnExit();
  481. ////StartCoroutine(OnExitCoroutine());
  482. }
  483. IEnumerator OnExitCoroutine()
  484. {
  485. while (true)
  486. {
  487. yield return new WaitForSeconds(Time.deltaTime);
  488. bool isRay = false;
  489. List<RaycastResult> results = new List<RaycastResult>();
  490. PointerEventData pointer = new PointerEventData(EventSystem.current)
  491. {
  492. // マウスポインタの位置にレイ飛ばし、ヒットしたものを保存
  493. position = Input.mousePosition
  494. };
  495. EventSystem.current.RaycastAll(pointer, results);
  496. // ヒットしたUIの名前
  497. foreach (RaycastResult target in results)
  498. {
  499. if (target.gameObject == this.CardImage.gameObject)
  500. {
  501. isRay = true;
  502. }
  503. }
  504. if (!isRay)
  505. {
  506. _OnExit();
  507. yield break;
  508. }
  509. }
  510. }
  511. public void _OnExit()
  512. {
  513. Outline.SetActive(false);
  514. anim.SetInteger("Open", 0);
  515. anim.SetInteger("Close", 1);
  516. uIShiny.enabled = false;
  517. OnExitAction?.Invoke(this);
  518. OffAddRemoveButton();
  519. }
  520. }