174 lines
5.1 KiB
C#
174 lines
5.1 KiB
C#
using AibisDream.Framework;
|
|
using AibisDream.Utility;
|
|
using DG.Tweening;
|
|
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
|
|
namespace AibisDream.FixSystem
|
|
{
|
|
[RequireComponent(typeof(EventTriggerEx))]
|
|
public class Plug : MonoBehaviour, IInteraction
|
|
{
|
|
#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;
|
|
private EventTriggerEx _trigger;
|
|
|
|
#endregion
|
|
|
|
private bool _isDragging;
|
|
|
|
private void Awake()
|
|
{
|
|
InitComponentRef();
|
|
EventRegister();
|
|
}
|
|
|
|
private void InitComponentRef()
|
|
{
|
|
_cableSystem = transform.parent.GetComponent<CableSystem>();
|
|
_sprite = GetComponent<SpriteRenderer>();
|
|
_plugRootPos = transform.Find("Plug Root Pos");
|
|
_trigger = GetComponent<EventTriggerEx>();
|
|
}
|
|
|
|
private void EventRegister()
|
|
{
|
|
_trigger.Register(EventTriggerType.Drag, OnDrag);
|
|
_trigger.Register(EventTriggerType.PointerDown, OnPointerDown);
|
|
_trigger.Register(EventTriggerType.PointerUp, OnPointerUp);
|
|
}
|
|
|
|
public void InitPlug(ISocket initSocket)
|
|
{
|
|
InsertSocket(initSocket);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 插入某个socket
|
|
/// </summary>
|
|
/// <param name="targetSocket">目标socket</param>
|
|
public void InsertSocket(ISocket targetSocket)
|
|
{
|
|
// 修改插头位置
|
|
transform.position = targetSocket.GetSocketPos();
|
|
_cableSystem.CableRef.SetEndPos(transform);
|
|
|
|
// 处理插槽插入
|
|
targetSocket.PlugIn();
|
|
AudioManager.RandomPlayInteraction("plug_in");
|
|
_sprite.enabled = false;
|
|
|
|
_cableSystem.curSocket = targetSocket;
|
|
if (targetSocket is BodyModule bodyModule)
|
|
{
|
|
// 插入模块时触发插入事件
|
|
EnumEventSystem.Global.Send(EventEnum.PlugIn);
|
|
_cableSystem.curBodyModule = bodyModule;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 从某个Socket拔出来
|
|
/// </summary>
|
|
public void PullUpSocket()
|
|
{
|
|
// 修改线头位置
|
|
_cableSystem.CableRef.SetEndPos(_plugRootPos);
|
|
|
|
_cableSystem.curSocket?.PlugOut();
|
|
AudioManager.RandomPlayInteraction("plug_out");
|
|
_sprite.enabled = true;
|
|
|
|
if (_cableSystem.curSocket is BodyModule bodyModule)
|
|
{
|
|
// 从模块拔出时触发事件
|
|
EnumEventSystem.Global.Send(EventEnum.PlugOut);
|
|
_cableSystem.curBodyModule = null;
|
|
}
|
|
|
|
_cableSystem.curSocket = null;
|
|
}
|
|
|
|
private void OnDrag(BaseEventData eventData)
|
|
{
|
|
// 系统被锁定就返回
|
|
if (!_cableSystem.IsPlugAvailable()) return;
|
|
|
|
// 未被拖拽也返回
|
|
if (!_isDragging) return;
|
|
|
|
if (eventData is PointerEventData pointerData)
|
|
{
|
|
transform.position = CommonUtil.GetMouseWorldPos(pointerData.position, transform);
|
|
}
|
|
}
|
|
|
|
private void OnPointerDown(BaseEventData eventData)
|
|
{
|
|
// 系统被锁定就返回
|
|
if (!_cableSystem.IsPlugAvailable()) return;
|
|
|
|
// 开启拖拽
|
|
_isDragging = true;
|
|
// 进行插拔动作
|
|
if (_cableSystem.curSocket != null) PullUpSocket();
|
|
|
|
// 调整插头形状
|
|
AdjustPlugPos();
|
|
}
|
|
|
|
private void OnPointerUp(BaseEventData eventData)
|
|
{
|
|
// 停止拖拽
|
|
_isDragging = false;
|
|
|
|
// 寻找可吸附的Module
|
|
if (_cableSystem.TryFindClosestModule(transform.position, out var targetModule))
|
|
{
|
|
// 找到了就插入目标Module
|
|
InsertSocket(targetModule);
|
|
}
|
|
else
|
|
{
|
|
// 没找到就插回初始Socket,需要播动画
|
|
transform.DOMove(_cableSystem.InitSocket.GetSocketPos(), 0.1f).OnComplete(() =>
|
|
{
|
|
InsertSocket(_cableSystem.InitSocket);
|
|
});
|
|
}
|
|
}
|
|
|
|
private void AdjustPlugPos()
|
|
{
|
|
transform.position += _pickupOffset;
|
|
transform.rotation = _pickupRotationOffset;
|
|
}
|
|
|
|
void OnDrawGizmosSelected()
|
|
{
|
|
Gizmos.color = Color.yellow;
|
|
Gizmos.DrawWireSphere(transform.position, 0.5f);
|
|
}
|
|
|
|
public bool IsActive => true;
|
|
|
|
public bool IsAvailable => _cableSystem.BodyModuleSystem.isAvailable;
|
|
|
|
public GameObject GetGameObject()
|
|
{
|
|
return gameObject;
|
|
}
|
|
}
|
|
} |