CardAction.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. namespace FSCard {
  9. /*
  10. * 卡片组
  11. * 包含鼠标进入,拖拽等一系列事件的回调
  12. */
  13. public class CardAction : MonoBehaviour, IPointerEnterHandler, IPointerDownHandler, IPointerUpHandler, IBeginDragHandler, IDragHandler {
  14. // 给定一个参数保存鼠标按下时候的位置
  15. public Vector3 StartPos { set; get; }
  16. // 给定一个参数来保存鼠标移动时候的位置
  17. private Vector3 movePos;
  18. // Use this for initialization
  19. void Start() {
  20. }
  21. // Update is called once per frame
  22. void Update() {
  23. }
  24. /*
  25. * 鼠标进入时候的回调
  26. */
  27. public void OnPointerEnter(PointerEventData eventData) {
  28. Vector3 anchPos = GetComponent<RectTransform>().anchoredPosition;
  29. }
  30. /*
  31. * 鼠标按下时的回调
  32. */
  33. public void OnPointerDown(PointerEventData eventData) {
  34. // 给起始和移动参数保存值
  35. //startPos = eventData.position;
  36. movePos = eventData.position;
  37. }
  38. /*
  39. * 鼠标抬起时的回调
  40. */
  41. public void OnPointerUp(PointerEventData eventData) {
  42. //for (int i = 0; i < BattleFieldManager.Instance.EnemyCpArray.Count; i++) {
  43. // Transform trs = (Transform)BattleFieldManager.Instance.EnemyCpArray[i];
  44. // // 获取地方比较牌的碰撞位置
  45. // BoxCollider2D enemyBox = trs.GetComponent<BoxCollider2D>();
  46. // // 我方的碰撞位置
  47. // BoxCollider2D roleBox = transform.GetComponent<BoxCollider2D>();
  48. // if (enemyBox.bounds.Intersects(roleBox.bounds)) {
  49. // print("碰撞了,选择它了 ... ");
  50. // BattleFieldManager.Instance.ArmyCpArray.Insert(i, transform);
  51. // }
  52. //}
  53. foreach (Transform trs in BattleFieldManager.Instance.RoleArray) {
  54. //Vector3 screenTrs = Camera.main.WorldToScreenPoint(trs.position);
  55. BoxCollider roleCollider = trs.GetComponent<BoxCollider>();
  56. // 再将世界碰撞区域转为屏幕上的
  57. Vector2 roleMax = Camera.main.WorldToScreenPoint(roleCollider.bounds.max);
  58. Vector2 roleMin = Camera.main.WorldToScreenPoint(roleCollider.bounds.min);
  59. Bounds roleBounds = new Bounds();
  60. roleBounds.SetMinMax(roleMin, roleMax);
  61. BoxCollider2D cardBox = transform.GetComponent<BoxCollider2D>();
  62. if (roleBounds.Intersects(cardBox.bounds)) {
  63. Card card = this.GetComponent<Card>();
  64. if (card != null) {
  65. print("出牌 ... ");
  66. Role role = trs.GetComponent<Role>();
  67. if (role != null) {
  68. card.Target = role;
  69. //card.OnExecute();
  70. // 需要保证剩下的能量足够
  71. if (role.CurrentAttr.Mp >= card.Cost) {
  72. role.CurrentAttr.Mp -= card.Cost;
  73. Dictionary<string, object> info = new Dictionary<string, object>();
  74. info.Add("card", this.transform);
  75. EventListener.Instance.PostEvent(EventEnum.EVENT_ARMY_DEAL_CARD, info);
  76. info.Add("role", role);
  77. EventListener.Instance.PostEvent(EventEnum.EVENT_UPDATE_PLAYER_ENGINE, info);
  78. } else {
  79. // 否则将卡片移回原位
  80. transform.position = StartPos;
  81. }
  82. return;
  83. }
  84. }
  85. }
  86. }
  87. transform.position = StartPos;
  88. }
  89. public void OnBeginDrag(PointerEventData eventData) {
  90. print("开始拖拽 ... ");
  91. }
  92. /*
  93. * 鼠标拖拽时的回调
  94. */
  95. public void OnDrag(PointerEventData eventData) {
  96. // 首先记录下抬起时候的位置
  97. Vector3 endPos = eventData.position;
  98. // 计算出卡片拖拽时候的位置,由原位置加上最终位置减去移动位置
  99. Vector3 pos = new Vector3(transform.position.x + (endPos.x - movePos.x),
  100. transform.position.y + (endPos.y - movePos.y), 0);
  101. // 赋值给当前组件
  102. transform.position = pos;
  103. // 再更新移动位置
  104. movePos = endPos;
  105. }
  106. }
  107. }