2025-10-20 11:02:10 +08:00
|
|
|
using System;
|
2025-10-19 19:38:52 +08:00
|
|
|
using UnityEngine;
|
|
|
|
|
using System.Collections.Generic;
|
2025-10-22 17:34:58 +08:00
|
|
|
using Script.Gameplay.Interface;
|
2025-10-19 19:38:52 +08:00
|
|
|
using Core;
|
|
|
|
|
|
|
|
|
|
namespace Script.Gameplay.Connect
|
|
|
|
|
{
|
2025-10-20 11:02:10 +08:00
|
|
|
[Serializable]
|
|
|
|
|
public class PreviousConnection
|
|
|
|
|
{
|
|
|
|
|
public int PreConnectionID;
|
|
|
|
|
public MonoBehaviour source;
|
|
|
|
|
public MonoBehaviour target;
|
|
|
|
|
}
|
2025-10-19 19:38:52 +08:00
|
|
|
public class ConnectionLineManager : MonoSingleton<ConnectionLineManager>
|
|
|
|
|
{
|
|
|
|
|
[SerializeField] private GameObject linePrefab;
|
2025-10-20 11:02:10 +08:00
|
|
|
[SerializeField]private List<PreviousConnection> preConnectionLines = new List<PreviousConnection>();
|
|
|
|
|
private List<ConnectionLine> connectionLines = new List<ConnectionLine>();
|
|
|
|
|
|
|
|
|
|
private void Start()
|
|
|
|
|
{
|
|
|
|
|
GeneratePreviousConnectionLines(preConnectionLines);
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-20 19:40:55 +08:00
|
|
|
public ConnectionLine GenerateConnectionLine(IConnectable a, IConnectable b)
|
2025-10-19 19:38:52 +08:00
|
|
|
{
|
2025-10-28 09:17:35 +08:00
|
|
|
// 先检测 a b 点是否已经存在连接线
|
|
|
|
|
foreach (var aline in a.ConnectionLines)
|
|
|
|
|
{
|
|
|
|
|
if (aline.PointA.GetGameObject() == b.GetGameObject() || aline.PointB.GetGameObject() == b.GetGameObject())
|
|
|
|
|
{
|
|
|
|
|
Debug.Log("ab点间已经存在连接线");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-19 19:38:52 +08:00
|
|
|
GameObject lineObject = Instantiate(linePrefab, this.transform);
|
|
|
|
|
ConnectionLine connectionLine = lineObject.GetComponent<ConnectionLine>();
|
|
|
|
|
if (connectionLine != null)
|
|
|
|
|
{
|
2025-10-20 19:40:55 +08:00
|
|
|
connectionLine.SetConnectable(a, b);
|
2025-10-19 19:38:52 +08:00
|
|
|
connectionLines.Add(connectionLine);
|
|
|
|
|
return connectionLine;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2025-10-20 19:40:55 +08:00
|
|
|
|
|
|
|
|
public void CutTargetConnectionLines(IConnectable target)
|
|
|
|
|
{
|
|
|
|
|
List<ConnectionLine> linesToRemove = new List<ConnectionLine>();
|
2025-10-21 08:51:37 +08:00
|
|
|
foreach (var line in target.ConnectionLines)
|
2025-10-20 19:40:55 +08:00
|
|
|
{
|
2025-10-21 08:51:37 +08:00
|
|
|
if (line != null)
|
2025-10-20 19:40:55 +08:00
|
|
|
{
|
|
|
|
|
linesToRemove.Add(line);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
foreach (var line in linesToRemove)
|
|
|
|
|
{
|
|
|
|
|
connectionLines.Remove(line);
|
|
|
|
|
Destroy(line.gameObject);
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-10-20 11:02:10 +08:00
|
|
|
|
|
|
|
|
public void GeneratePreviousConnectionLines(List<PreviousConnection> previousConnections)
|
|
|
|
|
{
|
|
|
|
|
foreach (var preConnection in previousConnections)
|
|
|
|
|
{
|
|
|
|
|
IConnectable source = preConnection.source as IConnectable;
|
|
|
|
|
IConnectable target = preConnection.target as IConnectable;
|
|
|
|
|
if (source != null && target != null)
|
|
|
|
|
{
|
2025-10-28 09:22:26 +08:00
|
|
|
var connectionLine = GenerateConnectionLine(source, target);
|
|
|
|
|
if (connectionLine == null)
|
|
|
|
|
{
|
2025-10-28 10:10:41 +08:00
|
|
|
Debug.LogWarning($"编号为{preConnection.PreConnectionID}的连接线存在重复连接,生成失败");
|
2025-10-28 09:22:26 +08:00
|
|
|
}
|
2025-10-20 11:02:10 +08:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2025-10-28 10:10:41 +08:00
|
|
|
Debug.LogWarning($"编号为{preConnection.PreConnectionID}的连接线的配置错误,生成失败");
|
2025-10-20 11:02:10 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-10-19 19:38:52 +08:00
|
|
|
}
|
|
|
|
|
}
|