CardAction.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. print("敌方比较数组 : " + BattleFieldManager.Instance.CpArray.Count);
  44. for (int i = 0; i < BattleFieldManager.Instance.CpArray.Count; i++) {
  45. Dictionary<string, Transform> cpCard = (Dictionary<string, Transform>)BattleFieldManager.Instance.CpArray[i];
  46. // 需要保证敌方卡片存在
  47. if (cpCard.ContainsKey(AssistConfig.Enemy) && !cpCard.ContainsKey(AssistConfig.Army)) {
  48. Transform trs = cpCard[AssistConfig.Enemy];
  49. // 获取碰撞位置
  50. BoxCollider2D enemyBox = trs.GetComponent<BoxCollider2D>();
  51. // 我方碰撞位置
  52. BoxCollider2D roleBox = transform.GetComponent<BoxCollider2D>();
  53. // 当接触时
  54. if (enemyBox.bounds.Intersects(roleBox.bounds)) {
  55. Card myCard = this.GetComponent<Card>();
  56. Card enemyCard = trs.GetComponent<Card>();
  57. // 设置目标
  58. myCard.Target = enemyCard.Source;
  59. // 需要保证剩下的能量足够
  60. if (myCard.Source.CurrentAttr.Mp >= myCard.Cost) {
  61. myCard.Source.CurrentAttr.Mp -= myCard.Cost;
  62. // 排列新的位置
  63. transform.position = new Vector3(trs.position.x, Screen.height / 2 - AssistConfig.CpCardHeightSpace, 0);
  64. transform.localScale = new Vector3(AssistConfig.CardScale, AssistConfig.CardScale, AssistConfig.CardScale);
  65. transform.rotation = Quaternion.Euler(0, 0, 0);
  66. Dictionary<string, object> info = new Dictionary<string, object>();
  67. info.Add("card", this.transform);
  68. info.Add("index", i);
  69. EventListener.Instance.PostEvent(EventEnum.EVENT_ARMY_DEAL_CARD, info);
  70. info.Add("role", myCard.Source);
  71. EventListener.Instance.PostEvent(EventEnum.EVENT_UPDATE_PLAYER_ENGINE, info);
  72. } else {
  73. // 否则将卡片移回原位
  74. transform.position = StartPos;
  75. print("能量不足 ... " + myCard.Source.CurrentAttr.Mp + " - " + myCard.Cost);
  76. }
  77. return;
  78. }
  79. }
  80. }
  81. transform.position = StartPos;
  82. }
  83. public void OnBeginDrag(PointerEventData eventData) {
  84. print("开始拖拽 ... ");
  85. }
  86. /*
  87. * 鼠标拖拽时的回调
  88. */
  89. public void OnDrag(PointerEventData eventData) {
  90. // 首先记录下抬起时候的位置
  91. Vector3 endPos = eventData.position;
  92. // 计算出卡片拖拽时候的位置,由原位置加上最终位置减去移动位置
  93. Vector3 pos = new Vector3(transform.position.x + (endPos.x - movePos.x),
  94. transform.position.y + (endPos.y - movePos.y), 0);
  95. // 赋值给当前组件
  96. transform.position = pos;
  97. // 再更新移动位置
  98. movePos = endPos;
  99. }
  100. }
  101. }