This commit is contained in:
2024-11-23 21:20:10 +08:00
parent c4a0b4a377
commit 55ceaf239e
36 changed files with 706 additions and 2162 deletions
@@ -0,0 +1,79 @@
using System;
using UnityEngine;
namespace AibisDream.Framework
{
public class MoveableSystem : MonoBehaviour
{
private bool _isDragging;
private IMoveable _dragObj;
private Vector3 _offset;
private Camera _camera;
private void Start()
{
_camera = Camera.main;
}
private void Update()
{
// 鼠标按下
if (Input.GetMouseButtonDown(0))
{
OnMouseClickDown();
}
// 鼠标按住
if (Input.GetMouseButton(0) && _isDragging)
{
OnMouseHolding();
}
// 鼠标松开
if (Input.GetMouseButtonUp(0))
{
OnMouseClickUp();
}
}
private void OnMouseClickDown()
{
// 按下时发出检查射线
Ray ray = _camera.ScreenPointToRay(Input.mousePosition);
RaycastHit2D hit = Physics2D.Raycast(ray.origin, ray.direction);
// 检测命中的物体
if (hit.collider && hit.collider.CompareTag("Moveable"))
{
_dragObj = hit.collider.gameObject.GetComponent<IMoveable>();
if (_dragObj == null) return;
_dragObj.StartMove();
_offset = hit.collider.transform.position - GetMouseAsWorldPoint();
_isDragging = true;
}
}
private void OnMouseHolding()
{
_dragObj.Move(GetMouseAsWorldPoint() + _offset);
}
private void OnMouseClickUp()
{
if (!_isDragging) return;
_dragObj?.StopMove();
_dragObj = null;
_isDragging = false;
}
private Vector3 GetMouseAsWorldPoint()
{
Vector3 mousePoint = Input.mousePosition;
mousePoint.z = _camera.nearClipPlane;
return _camera.ScreenToWorldPoint(mousePoint);
}
}
}