CardAction.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.EventSystems;
  5. using FSEvent;
  6. using FSBattle;
  7. using FSRole;
  8. using FSAssist;
  9. namespace FSCard {
  10. /*
  11. * 卡片组
  12. * 包含鼠标进入,拖拽等一系列事件的回调
  13. */
  14. public class CardAction : MonoBehaviour, IPointerEnterHandler, IPointerDownHandler, IPointerUpHandler, IBeginDragHandler, IDragHandler {
  15. // 给定一个参数保存鼠标按下时候的位置
  16. public Vector3 StartPos { set; get; }
  17. // 给定一个参数来保存鼠标移动时候的位置
  18. private Vector3 movePos;
  19. // Use this for initialization
  20. void Start() {
  21. }
  22. // Update is called once per frame
  23. void Update() {
  24. }
  25. /*
  26. * 鼠标进入时候的回调
  27. */
  28. public void OnPointerEnter(PointerEventData eventData) {
  29. Vector3 anchPos = GetComponent<RectTransform>().anchoredPosition;
  30. }
  31. /*
  32. * 鼠标按下时的回调
  33. */
  34. public void OnPointerDown(PointerEventData eventData) {
  35. // 给起始和移动参数保存值
  36. //startPos = eventData.position;
  37. movePos = eventData.position;
  38. }
  39. /*
  40. * 鼠标抬起时的回调
  41. */
  42. public void OnPointerUp(PointerEventData eventData) {
  43. for (int i = 0; i < BattleFieldManager.Instance.CpArray.Count; i++) {
  44. Dictionary<string, Transform> cpCard = (Dictionary<string, Transform>)BattleFieldManager.Instance.CpArray[i];
  45. Transform trs = cpCard[AssistConfig.Enemy];
  46. // 需要保证敌方卡片存在
  47. if (trs != null) {
  48. // 获取碰撞位置
  49. BoxCollider2D enemyBox = trs.GetComponent<BoxCollider2D>();
  50. // 我方碰撞位置
  51. BoxCollider2D roleBox = transform.GetComponent<BoxCollider2D>();
  52. // 当接触时
  53. if (enemyBox.bounds.Intersects(roleBox.bounds)) {
  54. Card myCard = this.GetComponent<Card>();
  55. Card enemyCard = trs.GetComponent<Card>();
  56. // 设置目标
  57. myCard.Target = enemyCard.Source;
  58. // 需要保证剩下的能量足够
  59. if (myCard.Source.CurrentAttr.Mp >= myCard.Cost) {
  60. myCard.Source.CurrentAttr.Mp -= myCard.Cost;
  61. // 排列新的位置
  62. transform.position = new Vector3(trs.position.x, Screen.height / 2 - AssistConfig.CpCardHeightSpace, 0);
  63. transform.localScale = new Vector3(AssistConfig.CardScale, AssistConfig.CardScale, AssistConfig.CardScale);
  64. transform.rotation = Quaternion.Euler(0, 0, 0);
  65. Dictionary<string, object> info = new Dictionary<string, object>();
  66. info.Add("card", this.transform);
  67. info.Add("index", i);
  68. EventListener.Instance.PostEvent(EventEnum.EVENT_ARMY_DEAL_CARD, info);
  69. info.Add("role", myCard.Source);
  70. EventListener.Instance.PostEvent(EventEnum.EVENT_UPDATE_PLAYER_ENGINE, info);
  71. } else {
  72. // 否则将卡片移回原位
  73. transform.position = StartPos;
  74. print("能量不足 ... " + myCard.Source.CurrentAttr.Mp + " - " + myCard.Cost);
  75. }
  76. return;
  77. }
  78. print("没有碰触到目标卡片 ... ");
  79. transform.position = StartPos;
  80. }
  81. }
  82. //for (int i = 0; i < BattleFieldManager.Instance.EnemyCpArray.Count; i++) {
  83. // Transform trs = (Transform)BattleFieldManager.Instance.EnemyCpArray[i];
  84. // // 获取地方比较牌的碰撞位置
  85. // BoxCollider2D enemyBox = trs.GetComponent<BoxCollider2D>();
  86. // // 我方的碰撞位置
  87. // BoxCollider2D roleBox = transform.GetComponent<BoxCollider2D>();
  88. // if (enemyBox.bounds.Intersects(roleBox.bounds)) {
  89. // print("碰撞了,选择它了 ... ");
  90. // BattleFieldManager.Instance.ArmyCpArray.Insert(i, transform);
  91. // }
  92. //}
  93. //foreach (Transform trs in BattleFieldManager.Instance.RoleArray) {
  94. // //Vector3 screenTrs = Camera.main.WorldToScreenPoint(trs.position);
  95. // BoxCollider roleCollider = trs.GetComponent<BoxCollider>();
  96. // // 再将世界碰撞区域转为屏幕上的
  97. // Vector2 roleMax = Camera.main.WorldToScreenPoint(roleCollider.bounds.max);
  98. // Vector2 roleMin = Camera.main.WorldToScreenPoint(roleCollider.bounds.min);
  99. // Bounds roleBounds = new Bounds();
  100. // roleBounds.SetMinMax(roleMin, roleMax);
  101. // BoxCollider2D cardBox = transform.GetComponent<BoxCollider2D>();
  102. // if (roleBounds.Intersects(cardBox.bounds)) {
  103. // Card card = this.GetComponent<Card>();
  104. // if (card != null) {
  105. // print("出牌 ... ");
  106. // Role role = trs.GetComponent<Role>();
  107. // if (role != null) {
  108. // card.Target = role;
  109. // //card.OnExecute();
  110. // // 需要保证剩下的能量足够
  111. // if (role.CurrentAttr.Mp >= card.Cost) {
  112. // role.CurrentAttr.Mp -= card.Cost;
  113. // Dictionary<string, object> info = new Dictionary<string, object>();
  114. // info.Add("card", this.transform);
  115. // EventListener.Instance.PostEvent(EventEnum.EVENT_ARMY_DEAL_CARD, info);
  116. // info.Add("role", role);
  117. // EventListener.Instance.PostEvent(EventEnum.EVENT_UPDATE_PLAYER_ENGINE, info);
  118. // } else {
  119. // // 否则将卡片移回原位
  120. // transform.position = StartPos;
  121. // }
  122. // return;
  123. // }
  124. // }
  125. // }
  126. //}
  127. //transform.position = StartPos;
  128. }
  129. public void OnBeginDrag(PointerEventData eventData) {
  130. print("开始拖拽 ... ");
  131. }
  132. /*
  133. * 鼠标拖拽时的回调
  134. */
  135. public void OnDrag(PointerEventData eventData) {
  136. // 首先记录下抬起时候的位置
  137. Vector3 endPos = eventData.position;
  138. // 计算出卡片拖拽时候的位置,由原位置加上最终位置减去移动位置
  139. Vector3 pos = new Vector3(transform.position.x + (endPos.x - movePos.x),
  140. transform.position.y + (endPos.y - movePos.y), 0);
  141. // 赋值给当前组件
  142. transform.position = pos;
  143. // 再更新移动位置
  144. movePos = endPos;
  145. }
  146. }
  147. }