CardPanelManager.cs 3.2 KB

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