CardPrefab_CreateDeck.cs 18 KB

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