123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.EventSystems;
- using FSEvent;
- using FSBattle;
- using FSRole;
- using FSAssist;
- 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<RectTransform>().anchoredPosition;
- }
- /*
- * 鼠标按下时的回调
- */
- public void OnPointerDown(PointerEventData eventData) {
- // 给起始和移动参数保存值
- //startPos = eventData.position;
- movePos = eventData.position;
- }
- /*
- * 鼠标抬起时的回调
- */
- public void OnPointerUp(PointerEventData eventData) {
- for (int i = 0; i < BattleFieldManager.Instance.CpArray.Count; i++) {
- Dictionary<string, Transform> cpCard = (Dictionary<string, Transform>)BattleFieldManager.Instance.CpArray[i];
- Transform trs = cpCard[AssistConfig.Enemy];
- // 需要保证敌方卡片存在
- if (trs != null) {
- // 获取碰撞位置
- BoxCollider2D enemyBox = trs.GetComponent<BoxCollider2D>();
- // 我方碰撞位置
- BoxCollider2D roleBox = transform.GetComponent<BoxCollider2D>();
- // 当接触时
- if (enemyBox.bounds.Intersects(roleBox.bounds)) {
- Card myCard = this.GetComponent<Card>();
- Card enemyCard = trs.GetComponent<Card>();
- // 设置目标
- myCard.Target = enemyCard.Source;
- // 需要保证剩下的能量足够
- if (myCard.Source.CurrentAttr.Mp >= myCard.Cost) {
- myCard.Source.CurrentAttr.Mp -= myCard.Cost;
- // 排列新的位置
- transform.position = new Vector3(trs.position.x, Screen.height / 2 - AssistConfig.CpCardHeightSpace, 0);
- transform.localScale = new Vector3(AssistConfig.CardScale, AssistConfig.CardScale, AssistConfig.CardScale);
- transform.rotation = Quaternion.Euler(0, 0, 0);
- Dictionary<string, object> info = new Dictionary<string, object>();
- info.Add("card", this.transform);
- info.Add("index", i);
- EventListener.Instance.PostEvent(EventEnum.EVENT_ARMY_DEAL_CARD, info);
- info.Add("role", myCard.Source);
- EventListener.Instance.PostEvent(EventEnum.EVENT_UPDATE_PLAYER_ENGINE, info);
- } else {
- // 否则将卡片移回原位
- transform.position = StartPos;
- print("能量不足 ... " + myCard.Source.CurrentAttr.Mp + " - " + myCard.Cost);
- }
- return;
- }
- print("没有碰触到目标卡片 ... ");
- transform.position = StartPos;
- }
- }
- //for (int i = 0; i < BattleFieldManager.Instance.EnemyCpArray.Count; i++) {
- // Transform trs = (Transform)BattleFieldManager.Instance.EnemyCpArray[i];
- // // 获取地方比较牌的碰撞位置
- // BoxCollider2D enemyBox = trs.GetComponent<BoxCollider2D>();
- // // 我方的碰撞位置
- // BoxCollider2D roleBox = transform.GetComponent<BoxCollider2D>();
- // if (enemyBox.bounds.Intersects(roleBox.bounds)) {
- // print("碰撞了,选择它了 ... ");
- // BattleFieldManager.Instance.ArmyCpArray.Insert(i, transform);
- // }
- //}
- //foreach (Transform trs in BattleFieldManager.Instance.RoleArray) {
- // //Vector3 screenTrs = Camera.main.WorldToScreenPoint(trs.position);
- // BoxCollider roleCollider = trs.GetComponent<BoxCollider>();
- // // 再将世界碰撞区域转为屏幕上的
- // 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<BoxCollider2D>();
- // if (roleBounds.Intersects(cardBox.bounds)) {
- // Card card = this.GetComponent<Card>();
- // if (card != null) {
- // print("出牌 ... ");
- // Role role = trs.GetComponent<Role>();
- // if (role != null) {
- // card.Target = role;
- // //card.OnExecute();
- // // 需要保证剩下的能量足够
- // if (role.CurrentAttr.Mp >= card.Cost) {
- // role.CurrentAttr.Mp -= card.Cost;
- // Dictionary<string, object> info = new Dictionary<string, object>();
- // 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;
- }
- }
- }
|