CardPrefab_CreateDeck.cs 18 KB

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