CardAction.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. foreach (Transform trs in BattleFieldManager.Instance.RoleArray) {
  43. //Vector3 screenTrs = Camera.main.WorldToScreenPoint(trs.position);
  44. BoxCollider roleCollider = trs.GetComponent<BoxCollider>();
  45. // 再将世界碰撞区域转为屏幕上的
  46. Vector2 roleMax = Camera.main.WorldToScreenPoint(roleCollider.bounds.max);
  47. Vector2 roleMin = Camera.main.WorldToScreenPoint(roleCollider.bounds.min);
  48. Bounds roleBounds = new Bounds();
  49. roleBounds.SetMinMax(roleMin, roleMax);
  50. BoxCollider2D cardBox = transform.GetComponent<BoxCollider2D>();
  51. if (roleBounds.Intersects(cardBox.bounds)) {
  52. Card card = this.GetComponent<Card>();
  53. if (card != null) {
  54. print("出牌 ... ");
  55. Role role = trs.GetComponent<Role>();
  56. if (role != null) {
  57. card.Target = role;
  58. //card.OnExecute();
  59. // 需要保证剩下的能量足够
  60. if (role.CurrentAttr.Mp >= card.Cost) {
  61. role.CurrentAttr.Mp -= card.Cost;
  62. Dictionary<string, object> info = new Dictionary<string, object>();
  63. info.Add("card", this.transform);
  64. EventListener.Instance.PostEvent(EventEnum.EVENT_ARMY_DEAL_CARD, info);
  65. info.Add("role", role);
  66. EventListener.Instance.PostEvent(EventEnum.EVENT_UPDATE_PLAYER_ENGINE, info);
  67. } else {
  68. // 否则将卡片移回原位
  69. transform.position = StartPos;
  70. }
  71. return;
  72. }
  73. }
  74. }
  75. }
  76. transform.position = StartPos;
  77. }
  78. public void OnBeginDrag(PointerEventData eventData) {
  79. print("开始拖拽 ... ");
  80. }
  81. /*
  82. * 鼠标拖拽时的回调
  83. */
  84. public void OnDrag(PointerEventData eventData) {
  85. // 首先记录下抬起时候的位置
  86. Vector3 endPos = eventData.position;
  87. // 计算出卡片拖拽时候的位置,由原位置加上最终位置减去移动位置
  88. Vector3 pos = new Vector3(transform.position.x + (endPos.x - movePos.x),
  89. transform.position.y + (endPos.y - movePos.y), 0);
  90. // 赋值给当前组件
  91. transform.position = pos;
  92. // 再更新移动位置
  93. movePos = endPos;
  94. }
  95. }
  96. }