Files
aibis-dream/Assets/Scripts/FixSystemNew/BodyModule/CableSystem.cs
T
2025-05-30 18:01:13 +08:00

197 lines
6.0 KiB
C#

using UnityEngine;
using DG.Tweening;
using System;
namespace AibisDream.FixSystem
{
public class CableSystem : MonoBehaviour
{
#region 组件索引
public Plug PlugRef { get; private set; }
public Cable CableRef { get; private set; }
public PhysicCable PhysicCableRef { get; private set; }
public Transform CableRootPos { get; private set; }
public CableReel CableReelRef { get; private set; }
public BodyModuleSystem BodyModuleSystem { get; private set; }
#endregion
#region 系统状态
public bool isInDialog;
public bool isActive = true;
public bool isCableRetracted = true; // 线缆是否收起
[SerializeField] private Transform retractedPos; // 收起位置
[SerializeField] private float dropDownDuration = 0.5f; // 放下动画时长
[SerializeField] private float swingDuration = 0.3f; // 摆动动画时长
[SerializeField] private float swingAngle = 15f; // 摆动角度
[HideInInspector] public ISocket curSocket;
[HideInInspector] public BodyModule curBodyModule;
#endregion
private void Awake()
{
InitComponentRefs();
}
private void Start()
{
InitSystem();
}
private void OnEnable()
{
DialogController.OnDialogueStart += HandleDialogueStart;
DialogController.OnDialogueComplete += HandleDialogueComplete;
}
private void OnDisable()
{
DialogController.OnDialogueStart -= HandleDialogueStart;
DialogController.OnDialogueComplete -= HandleDialogueComplete;
}
private void InitComponentRefs()
{
PlugRef = transform.Find("Plug").GetComponent<Plug>();
CableRef = transform.Find("Cable").GetComponent<Cable>();
CableReelRef = transform.Find("CableReel").GetComponent<CableReel>();
CableRootPos = CableReelRef.transform.Find("Cable Root Pos").transform;
PhysicCableRef = transform.Find("PhysicCable").GetComponent<PhysicCable>();
BodyModuleSystem = transform.parent.GetComponent<BodyModuleSystem>();
}
private void InitSystem()
{
if (isCableRetracted)
{
SetRetractCable();
}
else
{
PlugRef.ReturnToStartPosition();
}
}
private void Update()
{
// 获取线缆方向并更新转盘旋转
if (CableRef != null && CableReelRef != null)
{
CableReelRef.UpdateRotation();
}
if (Input.GetKeyDown(KeyCode.Space))
{
if (isCableRetracted)
{
DropDownCable();
}
else
{
PullUpCable();
}
}
}
public void ResetPlug()
{
Debug.Log("ResetPlug");
// 不在初始插孔里,就从当前插孔拔出来,插回初始插孔
PlugRef.PullUpSocket();
PlugRef.ReturnToStartPosition();
}
/// <summary>
/// 寻找最接近的Module
/// </summary>
/// <param name="sourcePos">源位置</param>
/// <param name="targetModule">最近的Module</param>
/// <returns>是否能找到目标范围内的Module</returns>
public bool TryFindClosestModule(Vector3 sourcePos, out BodyModule targetModule)
{
return BodyModuleSystem.TryFindClosestModule(sourcePos, out targetModule);
}
#region 系统可用状态
private void HandleDialogueStart()
{
isInDialog = true;
}
private void HandleDialogueComplete()
{
isInDialog = false;
}
public void Disable_plugInput()
{
isActive = false;
}
public void Enable_plugInput()
{
isActive = true;
}
/// <summary>
/// 查询插线系统是否可用
/// </summary>
/// <returns></returns>
public bool IsPlugAvailable()
{
return isActive && !isInDialog;
}
#endregion
public void PullUpCable()
{
if (isCableRetracted) return;
//禁用plug交互
Disable_plugInput();
ResetPlug();
PlugRef.SetPhysicCableActive(false);
PlugRef.GetComponent<SpriteRenderer>().sortingLayerName = CableReelRef.GetComponent<SpriteRenderer>().sortingLayerName;
PlugRef.GetComponent<SpriteRenderer>().sortingOrder = 41;
PlugRef.transform.DOMove(retractedPos.position, 0.1f).OnComplete(() =>
{
PhysicCableRef.GetComponent<LineRenderer>().enabled = false;
isCableRetracted = true;
});
}
public void DropDownCable()
{
if (!isCableRetracted) return;
PlugRef.PullUpSocket();
CableReelRef.ResetRotation();
PlugRef.transform.DOMove(PlugRef.GetStartPos(), 0.1f).OnComplete(() =>
{
PlugRef.SwitchToPhysicCableState();
isCableRetracted = false;
});
// 只有在非收起状态时才切换到物理线缆状态
}
public void SetRetractCable()
{
// 禁用物理线缆和普通线缆
PhysicCableRef.GetComponent<LineRenderer>().enabled = false;
CableRef.HideCable();
// 设置Plug位置和层级
PlugRef.transform.position = retractedPos.position;
PlugRef.transform.rotation = retractedPos.rotation;
PlugRef.GetComponent<SpriteRenderer>().sortingLayerName = CableReelRef.GetComponent<SpriteRenderer>().sortingLayerName;
PlugRef.GetComponent<SpriteRenderer>().sortingOrder = 41;
}
}
}