CardPanelManager.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using FSEvent;
  6. public class CardPanelManager : MonoBehaviour {
  7. public Transform cardPanel;
  8. public GameObject cardPrefab;
  9. // 用来保存卡片的数组
  10. private ArrayList cards = new ArrayList();
  11. // Use this for initialization
  12. void Start () {
  13. //GameObject cardTransform = Instantiate(cardPrefab);
  14. //cardTransform.transform.SetParent(bg, false);
  15. //Image bg = cardPanel.add
  16. InitEvents();
  17. }
  18. // Update is called once per frame
  19. void Update () {
  20. }
  21. private void OnDestroy() {
  22. RemoveEvents();
  23. }
  24. /*
  25. * 注册监听事件
  26. */
  27. private void InitEvents() {
  28. print("Card Panel 注册事件 ... ");
  29. EventListener.Instance.RegisterEvent(EventEnum.EVENT_SHOW_CARD_PANEL, ShowCardPanel);
  30. EventListener.Instance.RegisterEvent(EventEnum.EVENT_CLEAR_CARDS, CleatCards);
  31. }
  32. /*
  33. * 移除监听事件
  34. */
  35. private void RemoveEvents() {
  36. }
  37. /*
  38. * 显示卡片面板
  39. */
  40. private void ShowCardPanel() {
  41. print("展示面板 ... ");
  42. cardPanel.gameObject.SetActive(true);
  43. ShufflingCard();
  44. }
  45. /*
  46. * 发牌
  47. */
  48. private void ShufflingCard() {
  49. // 如果手牌小于5,则
  50. if (cards.Count < 5) {
  51. AddCard();
  52. Invoke("ShufflingCard", 1.0f);
  53. }
  54. }
  55. /*
  56. * 添加卡牌
  57. */
  58. public void AddCard() {
  59. // 创建一个卡牌
  60. GameObject card = Instantiate(cardPrefab);
  61. // 将卡牌加到指定的图层中
  62. card.transform.SetParent(cardPanel, false);
  63. cards.Add(card.transform);
  64. ArrangeCards();
  65. }
  66. /*
  67. * 重新排列卡牌
  68. */
  69. private void ArrangeCards() {
  70. // 空隙
  71. float space = 50;
  72. // 配置的半径
  73. float radius = 300;
  74. // 获取偏移的角度
  75. float angle = Mathf.Atan(space / radius) * 180 / Mathf.PI;
  76. // 遍历数组
  77. for (int i = 0; i < cards.Count; i++) {
  78. // 重定义位置
  79. float loc = i - (float)(cards.Count - 1) / 2;
  80. Transform cardTransform = (Transform)cards[i];
  81. // 设置位置
  82. cardTransform.localPosition = new Vector3(loc * space, 0, 0);
  83. // 设置角度
  84. cardTransform.localRotation = Quaternion.Euler(0, 0, angle * (loc * -1));
  85. float scale = 1 - (Mathf.Abs(loc) / 5);
  86. cardTransform.localScale = new Vector3(scale, scale, 1);
  87. }
  88. }
  89. /*
  90. * 清理手牌
  91. */
  92. private void CleatCards() {
  93. foreach (Transform card in cards) {
  94. Destroy(card.gameObject);
  95. }
  96. cards.Clear();
  97. EventListener.Instance.PostEvent(EventEnum.EVENT_ENTER_PLAY_A_HAND_ROUND);
  98. }
  99. }