using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; using FSEvent; using FSBattle; using FSRole; namespace FSCard { /* * 卡片组 * 包含鼠标进入,拖拽等一系列事件的回调 */ public class CardAction : MonoBehaviour, IPointerEnterHandler, IPointerDownHandler, IPointerUpHandler, IBeginDragHandler, IDragHandler { // 给定一个参数保存鼠标按下时候的位置 public Vector3 StartPos { set; get; } // 给定一个参数来保存鼠标移动时候的位置 private Vector3 movePos; // Use this for initialization void Start() { } // Update is called once per frame void Update() { } /* * 鼠标进入时候的回调 */ public void OnPointerEnter(PointerEventData eventData) { Vector3 anchPos = GetComponent().anchoredPosition; } /* * 鼠标按下时的回调 */ public void OnPointerDown(PointerEventData eventData) { // 给起始和移动参数保存值 //startPos = eventData.position; movePos = eventData.position; } /* * 鼠标抬起时的回调 */ public void OnPointerUp(PointerEventData eventData) { foreach (Transform trs in BattleFieldManager.Instance.RoleArray) { //Vector3 screenTrs = Camera.main.WorldToScreenPoint(trs.position); BoxCollider roleCollider = trs.GetComponent(); // 再将世界碰撞区域转为屏幕上的 Vector2 roleMax = Camera.main.WorldToScreenPoint(roleCollider.bounds.max); Vector2 roleMin = Camera.main.WorldToScreenPoint(roleCollider.bounds.min); Bounds roleBounds = new Bounds(); roleBounds.SetMinMax(roleMin, roleMax); BoxCollider2D cardBox = transform.GetComponent(); if (roleBounds.Intersects(cardBox.bounds)) { Card card = this.GetComponent(); if (card != null) { print("出牌 ... "); Role role = trs.GetComponent(); if (role != null) { card.Target = role; //card.OnExecute(); // 需要保证剩下的能量足够 if (role.CurrentAttr.Mp >= card.Cost) { role.CurrentAttr.Mp -= card.Cost; Dictionary info = new Dictionary(); info.Add("card", this.transform); EventListener.Instance.PostEvent(EventEnum.EVENT_ARMY_DEAL_CARD, info); info.Add("role", role); EventListener.Instance.PostEvent(EventEnum.EVENT_UPDATE_PLAYER_ENGINE, info); } else { // 否则将卡片移回原位 transform.position = StartPos; } return; } } } } transform.position = StartPos; } public void OnBeginDrag(PointerEventData eventData) { print("开始拖拽 ... "); } /* * 鼠标拖拽时的回调 */ public void OnDrag(PointerEventData eventData) { // 首先记录下抬起时候的位置 Vector3 endPos = eventData.position; // 计算出卡片拖拽时候的位置,由原位置加上最终位置减去移动位置 Vector3 pos = new Vector3(transform.position.x + (endPos.x - movePos.x), transform.position.y + (endPos.y - movePos.y), 0); // 赋值给当前组件 transform.position = pos; // 再更新移动位置 movePos = endPos; } } }