227 lines
7.0 KiB
C#
227 lines
7.0 KiB
C#
using UnityEngine;
|
|
using DG.Tweening;
|
|
|
|
namespace AibisDream.FixSystem
|
|
{
|
|
public class CableSystem : MonoBehaviour
|
|
{
|
|
private const string CABLE_RETRACTED_KEY = "$global.cableRetracted";
|
|
|
|
#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();
|
|
LoadCableState();
|
|
}
|
|
|
|
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 LoadCableState()
|
|
{
|
|
if (StorageSystem.Instance.TryGetValue(CABLE_RETRACTED_KEY, out bool savedRetractedState))
|
|
{
|
|
if (isCableRetracted)
|
|
{
|
|
if (!savedRetractedState)
|
|
{
|
|
DropDownCable();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (savedRetractedState)
|
|
{
|
|
PullUpCable();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void SaveCableState()
|
|
{
|
|
Debug.Log("Save cableRetractedstate" + isCableRetracted);
|
|
StorageSystem.Instance.SetValue(CABLE_RETRACTED_KEY, isCableRetracted);
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
// 获取线缆方向并更新转盘旋转
|
|
if (CableRef != null && CableReelRef != null)
|
|
{
|
|
CableReelRef.UpdateRotation();
|
|
}
|
|
}
|
|
|
|
public void ResetPlug()
|
|
{
|
|
// 不在初始插孔里,就从当前插孔拔出来,插回初始插孔
|
|
PlugRef.PullUpSocket();
|
|
BodyModuleSystem.CloseModuleSlots(null);
|
|
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;
|
|
SaveCableState();
|
|
});
|
|
}
|
|
|
|
public void DropDownCable()
|
|
{
|
|
if (!isCableRetracted) return;
|
|
Debug.Log("DorpDown");
|
|
PlugRef.PullUpSocket();
|
|
CableReelRef.ResetRotation();
|
|
PlugRef.transform.DOMove(PlugRef.GetStartPos(), 0.1f).OnComplete(() =>
|
|
{
|
|
PlugRef.SwitchToPhysicCableState();
|
|
isCableRetracted = false;
|
|
SaveCableState();
|
|
// 添加一个短暂的延迟来确保状态正确设置
|
|
DOVirtual.DelayedCall(0.1f, () => {
|
|
Enable_plugInput();
|
|
});
|
|
});
|
|
// 只有在非收起状态时才切换到物理线缆状态
|
|
}
|
|
|
|
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;
|
|
isCableRetracted = true;
|
|
//SaveCableState();
|
|
}
|
|
}
|
|
} |