Files
aibis-dream/Assets/Scripts/FixSystemNew/Plug.cs
T
2024-11-29 17:51:19 +08:00

151 lines
4.4 KiB
C#

using AibisDream.Utility;
using DG.Tweening;
using UnityEngine;
using UnityEngine.EventSystems;
namespace AibisDream.FixSystem
{
public class Plug : MonoBehaviour, IDragHandler, IPointerDownHandler, IPointerUpHandler
{
#region 插头当前信息
private Vector3 _startPos;
private Vector3 _startRotate;
private readonly Vector3 _pickupOffset = new(0, 0, 0);
private readonly Quaternion _pickupRotationOffset = Quaternion.Euler(0, 0, 30);
#endregion
#region 索引
private CableSystem _cableSystem;
private SpriteRenderer _sprite;
private Transform _plugRootPos;
#endregion
private bool _isDragging;
private void Awake()
{
InitComponentRef();
}
private void InitComponentRef()
{
_cableSystem = transform.parent.GetComponent<CableSystem>();
_sprite = GetComponent<SpriteRenderer>();
_plugRootPos = transform.Find("Plug Root Pos");
}
public void InitPlug(Socket initSocket)
{
InsertSocket(initSocket);
}
/// <summary>
/// 插入某个socket
/// </summary>
/// <param name="targetSocket">目标socket</param>
private void InsertSocket(Socket targetSocket)
{
// 修改插头位置
transform.position = targetSocket.transform.position;
_cableSystem.CableRef.SetEndPos(transform);
// 处理插槽插入
targetSocket.PlugIn();
AudioManager.RandomPlayInteraction("plug_in");
_sprite.enabled = false;
// 触发事件
_cableSystem.plugInEvent?.Invoke(targetSocket);
_cableSystem.curSocket = targetSocket;
}
/// <summary>
/// 从某个Socket拔出来
/// </summary>
private void PullUpSocket()
{
// 修改线头位置
_cableSystem.CableRef.SetEndPos(_plugRootPos);
_cableSystem.curSocket?.PlugOut();
AudioManager.RandomPlayInteraction("plug_out");
_sprite.enabled = true;
// 触发事件
_cableSystem.plugOutEvent?.Invoke(_cableSystem.curSocket);
_cableSystem.curSocket = null;
}
public void OnDrag(PointerEventData eventData)
{
// 系统被锁定就返回
if (!_cableSystem.IsPlugAvailable()) return;
// 未被拖拽也返回
if (!_isDragging) return;
// 这一行感觉不需要,毕竟传递的是引用
// _cableSystem.CableRef.SetEndPos(transform);
transform.position = CommonUtil.GetMouseWorldPos(eventData.position, transform);
}
public void OnPointerDown(PointerEventData eventData)
{
// 系统被锁定就返回
if (!_cableSystem.IsPlugAvailable()) return;
// 开启拖拽
_isDragging = true;
// 进行插拔动作
if (_cableSystem.curSocket) PullUpSocket();
_cableSystem.plugStartDrag?.Invoke();
// 调整插头形状
AdjustPlugPos();
}
public void OnPointerUp(PointerEventData eventData)
{
// 系统被锁定就返回
if (!_cableSystem.IsPlugAvailable()) return;
// 停止拖拽
_isDragging = false;
// 寻找可吸附的Module
if (_cableSystem.TryFindClosestModule(transform.position, out var targetModule))
{
// 找到了就插入目标Module
InsertSocket(targetModule.Socket);
_cableSystem.plugEndDrag?.Invoke();
}
else
{
// 没找到就插回初始Socket,需要播动画
transform.DOMove(_cableSystem.InitSocketPos, 0.1f).OnComplete(() =>
{
InsertSocket(_cableSystem.InitSocket);
_cableSystem.plugEndDrag?.Invoke();
});
}
}
private void AdjustPlugPos()
{
transform.position += _pickupOffset;
transform.rotation = _pickupRotationOffset;
}
void OnDrawGizmosSelected()
{
Gizmos.color = Color.yellow;
Gizmos.DrawWireSphere(transform.position, 0.5f);
}
}
}