CardPrefab_CreateDeck.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  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.cardKind != CardKind.DigiEgg)
  160. {
  161. for (int i = CostIcons.Count-1; i > -1; i--)
  162. {
  163. CardColor cardColor = CardColor.None;
  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 == CardKind.Digimon || cEntity_Base.cardKind == CardKind.DigiEgg) && cEntity_Base.Level >= 1)
  190. {
  191. if (LevelIcons.Count >= 1)
  192. {
  193. for (int i = 0; i < CostIcons.Count; i++)
  194. {
  195. int cardColorIndex = i < cEntity_Base.cardColors.Count ? i : 0;
  196. CardColor cardColor = cEntity_Base.cardColors[cardColorIndex];
  197. if (i < cEntity_Base.cardColors.Count)
  198. {
  199. cardColor = cEntity_Base.cardColors[i];
  200. LevelIcons[i].color = DataBase.CardColor_ColorDarkDictionary[cardColor];
  201. LevelIcons[i].gameObject.SetActive(true);
  202. }
  203. else
  204. {
  205. LevelIcons[i].gameObject.SetActive(false);
  206. }
  207. }
  208. LevelText.color = Color.white;
  209. LevelText.transform.parent.gameObject.SetActive(true);
  210. LevelText.text = $"Lv.{cEntity_Base.Level}";
  211. }
  212. }
  213. else
  214. {
  215. LevelText.transform.parent.gameObject.SetActive(false);
  216. }
  217. }
  218. if (EvoCostIcons != null)
  219. {
  220. if (cEntity_Base.cardKind == CardKind.Digimon)
  221. {
  222. if (EvoCostIcons.Count >= 1)
  223. {
  224. for (int i = 0; i < EvoCostIcons.Count; i++)
  225. {
  226. if (i < cEntity_Base.EvoCosts.Count)
  227. {
  228. EvoCostIcons[i].sprite = WhiteCircle;
  229. EvoCostIcons[i].transform.parent.gameObject.SetActive(true);
  230. CardColor cardColor = CardColor.None;
  231. cardColor = cEntity_Base.EvoCosts[i].CardColor;
  232. EvoCostIcons[i].color = DataBase.CardColor_ColorDarkDictionary[cardColor];
  233. EvoCostTexts_Level[i].text = $"Lv.{cEntity_Base.EvoCosts[i].Level}";
  234. EvoCostTexts_Memory[i].text = $"{cEntity_Base.EvoCosts[i].MemoryCost}";
  235. if (cardColor == CardColor.Yellow || cardColor == CardColor.White)
  236. {
  237. EvoCostTexts_Level[i].color = Color.black;
  238. EvoCostTexts_Memory[i].color = Color.black;
  239. }
  240. else
  241. {
  242. EvoCostTexts_Level[i].color = Color.white;
  243. EvoCostTexts_Memory[i].color = Color.white;
  244. }
  245. }
  246. else
  247. {
  248. EvoCostIcons[i].transform.parent.gameObject.SetActive(false);
  249. }
  250. }
  251. if (cEntity_Base.EvoCosts.Count == 7)
  252. {
  253. int cost = cEntity_Base.EvoCosts[0].MemoryCost;
  254. int level = cEntity_Base.EvoCosts[0].Level;
  255. bool sameCost = true;
  256. for (int i = 0; i < cEntity_Base.EvoCosts.Count; i++)
  257. {
  258. if (cEntity_Base.EvoCosts[i].MemoryCost != cost || cEntity_Base.EvoCosts[i].Level != level)
  259. {
  260. sameCost = false;
  261. break;
  262. }
  263. }
  264. if (sameCost)
  265. {
  266. for (int i = 0; i < EvoCostIcons.Count; i++)
  267. {
  268. EvoCostIcons[i].transform.parent.gameObject.SetActive(false);
  269. if (i == 0)
  270. {
  271. EvoCostIcons[i].sprite = RainbowCircle;
  272. EvoCostIcons[i].transform.parent.gameObject.SetActive(true);
  273. EvoCostIcons[i].color = Color.white;
  274. EvoCostTexts_Level[i].text = $"Lv.{cEntity_Base.EvoCosts[i].Level}";
  275. EvoCostTexts_Memory[i].text = $"{cEntity_Base.EvoCosts[i].MemoryCost}";
  276. EvoCostTexts_Level[i].color = Color.white;
  277. EvoCostTexts_Memory[i].color = Color.white;
  278. }
  279. }
  280. }
  281. }
  282. }
  283. }
  284. else
  285. {
  286. for (int i = 0; i < EvoCostIcons.Count; i++)
  287. {
  288. EvoCostIcons[i].transform.parent.gameObject.SetActive(false);
  289. }
  290. }
  291. }
  292. if (LimitObject != null && BanObject != null && LimitText != null)
  293. {
  294. LimitObject.SetActive(false);
  295. BanObject.SetActive(false);
  296. int maxCount = DeckBuildingRule.MaxCount_BanList(cEntity_Base);
  297. if (maxCount <= cEntity_Base.MaxCountInDeck)
  298. {
  299. if (maxCount == 1)
  300. {
  301. LimitObject.SetActive(true);
  302. LimitText.gameObject.SetActive(true);
  303. LimitText.text = $"{maxCount}";
  304. }
  305. else if (maxCount == 0)
  306. {
  307. BanObject.SetActive(true);
  308. }
  309. }
  310. }
  311. }
  312. }
  313. public void HideDeckCardTab()
  314. {
  315. CardImage.color = new Color(1, 1, 1, 0);
  316. CardImage.GetComponent<EventTrigger>().enabled = false;
  317. Outline.SetActive(false);
  318. if (CardImage.GetComponent<UIShiny>() != null)
  319. {
  320. CardImage.GetComponent<UIShiny>().enabled = false;
  321. }
  322. if (CostText != null)
  323. {
  324. CostText.transform.parent.gameObject.SetActive(false);
  325. }
  326. if (LevelText != null)
  327. {
  328. LevelText.transform.parent.gameObject.SetActive(false);
  329. }
  330. if (EvoCostIcons != null)
  331. {
  332. if (EvoCostIcons.Count >= 1)
  333. {
  334. for (int i = 0; i < EvoCostIcons.Count; i++)
  335. {
  336. EvoCostIcons[i].transform.parent.gameObject.SetActive(false);
  337. }
  338. }
  339. }
  340. }
  341. public void SetCover(bool active)
  342. {
  343. foreach (GameObject g in Cover)
  344. {
  345. g.SetActive(active);
  346. }
  347. }
  348. public void CheckCover(DeckData deckData)
  349. {
  350. SetCover(CanNotAddThisCard(deckData));
  351. }
  352. public bool CanNotAddThisCard(DeckData deckData)
  353. {
  354. if (deckData.DeckCards().Count >= 70)
  355. {
  356. return true;
  357. }
  358. if (deckData.DigitamaDeckCards().Count >= 10)
  359. {
  360. return true;
  361. }
  362. if (cEntity_Base != null)
  363. {
  364. if (cEntity_Base.cardKind == CardKind.DigiEgg)
  365. {
  366. if (cEntity_Base.SameCardIDCount(deckData.DigitamaDeckCards()) >= cEntity_Base.MaxCountInDeck)
  367. {
  368. return true;
  369. }
  370. }
  371. else
  372. {
  373. if (cEntity_Base.SameCardIDCount(deckData.DeckCards()) >= cEntity_Base.MaxCountInDeck)
  374. {
  375. return true;
  376. }
  377. }
  378. if (!DeckBuildingRule.CanAddCard(cEntity_Base, deckData))
  379. {
  380. return true;
  381. }
  382. }
  383. return false;
  384. }
  385. public void OnClick()
  386. {
  387. OnClickAction?.Invoke();
  388. }
  389. public void OnBeginDrag()
  390. {
  391. OnBeginDragAction?.Invoke(this);
  392. }
  393. float _timer = 0f;
  394. private void Update()
  395. {
  396. if (AddRemoveButtonParent.activeSelf)
  397. {
  398. _OnEnter();
  399. }
  400. _timer += Time.deltaTime;
  401. if (_timer > Time.deltaTime * 6)
  402. {
  403. _timer = 0f;
  404. if (isActive && CardImage.gameObject.activeSelf)
  405. {
  406. if (cEntity_Base != null && Cover_Standard != null)
  407. {
  408. Cover_Standard.SetActive(!cEntity_Base.IsStandardValid);
  409. }
  410. }
  411. }
  412. }
  413. public Coroutine _OnEnterCoroutine = null;
  414. public void OnEnter()
  415. {
  416. Outline.SetActive(true);
  417. anim.SetInteger("Open", 1);
  418. anim.SetInteger("Close", 0);
  419. uIShiny.enabled = true;
  420. _OnEnter();
  421. }
  422. IEnumerator OnEnterCoroutine()
  423. {
  424. int count = 0;
  425. while (true)
  426. {
  427. yield return new WaitForSeconds(Time.deltaTime);
  428. bool isRay = false;
  429. List<RaycastResult> results = new List<RaycastResult>();
  430. PointerEventData pointer = new PointerEventData(EventSystem.current);
  431. // マウスポインタの位置にレイ飛ばし、ヒットしたものを保存
  432. pointer.position = Input.mousePosition;
  433. EventSystem.current.RaycastAll(pointer, results);
  434. // ヒットしたUIの名前
  435. foreach (RaycastResult target in results)
  436. {
  437. if (target.gameObject == this.CardImage.gameObject)
  438. {
  439. isRay = true;
  440. }
  441. }
  442. if (isRay)
  443. {
  444. _OnEnter();
  445. count++;
  446. if (count > 10)
  447. {
  448. _OnEnterCoroutine = null;
  449. yield break;
  450. }
  451. }
  452. }
  453. }
  454. public void _OnEnter()
  455. {
  456. OnEnterAction?.Invoke(this);
  457. }
  458. public void OnExit()
  459. {
  460. #if !UNITY_EDITOR && UNITY_ANDROID
  461. _OnExit();
  462. return;
  463. #endif
  464. return;
  465. bool isRay = false;
  466. List<RaycastResult> results = new List<RaycastResult>();
  467. PointerEventData pointer = new PointerEventData(EventSystem.current);
  468. // マウスポインタの位置にレイ飛ばし、ヒットしたものを保存
  469. pointer.position = Input.mousePosition;
  470. EventSystem.current.RaycastAll(pointer, results);
  471. // ヒットしたUIの名前
  472. foreach (RaycastResult target in results)
  473. {
  474. if (target.gameObject == AddButton.gameObject || target.gameObject == RemoveButton.gameObject)
  475. {
  476. return;
  477. }
  478. }
  479. _OnExit();
  480. //StartCoroutine(OnExitCoroutine());
  481. }
  482. IEnumerator OnExitCoroutine()
  483. {
  484. while (true)
  485. {
  486. yield return new WaitForSeconds(Time.deltaTime);
  487. bool isRay = false;
  488. List<RaycastResult> results = new List<RaycastResult>();
  489. PointerEventData pointer = new PointerEventData(EventSystem.current);
  490. // マウスポインタの位置にレイ飛ばし、ヒットしたものを保存
  491. pointer.position = Input.mousePosition;
  492. EventSystem.current.RaycastAll(pointer, results);
  493. // ヒットしたUIの名前
  494. foreach (RaycastResult target in results)
  495. {
  496. if (target.gameObject == this.CardImage.gameObject)
  497. {
  498. isRay = true;
  499. }
  500. }
  501. if (!isRay)
  502. {
  503. _OnExit();
  504. yield break;
  505. }
  506. }
  507. }
  508. public void _OnExit()
  509. {
  510. Outline.SetActive(false);
  511. anim.SetInteger("Open", 0);
  512. anim.SetInteger("Close", 1);
  513. uIShiny.enabled = false;
  514. OnExitAction?.Invoke(this);
  515. OffAddRemoveButton();
  516. }
  517. }