You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
3.0 KiB

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class CardDrag : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IDragHandler
{
public int playerID;
public BattleCardState state = BattleCardState.inHand;
private Vector3 offset;
private bool isDragging = false;
public GameObject SummonBlock;
public GameObject[] playerBlocks;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
// 右键点击取消操作
if (isDragging && Input.GetMouseButtonDown(1))
{
CancelDrag();
}
}
public void OnPointerDown(PointerEventData eventData)
{
// 计算鼠标与卡牌的偏移量
offset = transform.position - Camera.main.ScreenToWorldPoint(Input.mousePosition);
isDragging = true;
if (BattleManager.Instance.gamePhase == GamePhase.playerAction)
//if(BattleManager.Instance.gamePhase.ToString() == "playerAction")
{
//在手牌时发起召唤请求
if (GetComponent<CardDisplay>().card is MonsterCard)
{
if (state == BattleCardState.inHand)
{
BattleManager.Instance.SummonRequst(playerID, gameObject);
//Debug.Log("Summon Requst has loaded");
}
}
//在场上点击时发起攻击请求'
}
}
public void OnPointerUp(PointerEventData eventData)
{
isDragging = false;
GameObject closestSlot = GetClosestSlot();
if (closestSlot != null && SummonBlock.activeInHierarchy)
{
BattleManager.Instance.SummonConfirm(closestSlot.transform);
}
// 这里可以重置卡牌的位置
}
public void OnDrag(PointerEventData eventData)
{
if (isDragging)
{
Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
transform.position = new Vector3(mousePosition.x + offset.x, mousePosition.y + offset.y, transform.position.z);
}
}
private GameObject GetClosestSlot()
{
GameObject[] blocks = playerBlocks;
GameObject closest = null;
float minDistance = Mathf.Infinity;
foreach (var block in blocks)
{
float distance = Vector3.Distance(transform.position, block.transform.position);
if (distance < minDistance)
{
minDistance = distance;
closest = block;
}
}
return closest;
}
private void SummonCard(GameObject slot)
{
Debug.Log($"召唤到 {slot.name}");
// 在这里添加更多逻辑,例如将卡牌实例移动到槽内
}
private void CancelDrag()
{
isDragging = false;
// 重置卡牌位置或执行其他取消逻辑
Debug.Log("取消拖动操作");
}
}