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.

82 lines
2.0 KiB

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class Drag : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IDragHandler
{
private Vector2 distance;
private bool isDragging = false;
private bool cancelDrag = false;
void Update()
{
}
public void OnPointerDown(PointerEventData eventData)
{
if (BattleManager.Instance.gamePhase == GamePhase.playerAction)
{
isDragging = true;
}
if (!cancelDrag)
{
distance = new Vector2(transform.position.x, transform.position.y) - new Vector2(Input.mousePosition.x, Input.mousePosition.y);
}
}
public void OnPointerUp(PointerEventData eventData)
{
if (isDragging)
{
StopDragging();
MatchToNearestBlock();//匹配最近格子并进行召唤
}
}
public void OnDrag(PointerEventData eventData)
{
if (isDragging)
{
transform.position = new Vector2(Input.mousePosition.x, Input.mousePosition.y) + distance;
}
}
private void StopDragging()
{
isDragging = false;
cancelDrag = true;
}
private void MatchToNearestBlock()
{
Block[] blocks = FindObjectsOfType<Block>(); // 获取所有的Block
Block nearestBlock = null;
float closestDistance = Mathf.Infinity;
Vector2 currentPos = transform.position;
// 遍历所有格子,找到距离最近的
foreach (Block block in blocks)
{
float distance = Vector2.Distance(currentPos, block.transform.position);
if (distance < closestDistance)
{
closestDistance = distance;
nearestBlock = block;
}
}
// 如果找到最近的格子,则召唤
if (nearestBlock != null && nearestBlock.SummonBlock.activeInHierarchy)
{
BattleManager.Instance.SummonConfirm(nearestBlock.transform);
}
}
}