Files
2025TapTapGameJam/Assets/Script/Gameplay/Connect/ConnectionLineManager.cs

88 lines
3.1 KiB
C#

using System;
using UnityEngine;
using System.Collections.Generic;
using Script.Gameplay.Interface;
using Core;
namespace Script.Gameplay.Connect
{
[Serializable]
public class PreviousConnection
{
public int PreConnectionID;
public MonoBehaviour source;
public MonoBehaviour target;
}
public class ConnectionLineManager : MonoSingleton<ConnectionLineManager>
{
[SerializeField] private GameObject linePrefab;
[SerializeField]private List<PreviousConnection> preConnectionLines = new List<PreviousConnection>();
private List<ConnectionLine> connectionLines = new List<ConnectionLine>();
private void Start()
{
GeneratePreviousConnectionLines(preConnectionLines);
}
public ConnectionLine GenerateConnectionLine(IConnectable a, IConnectable b)
{
// 先检测 a b 点是否已经存在连接线
foreach (var aline in a.ConnectionLines)
{
if (aline.PointA.GetGameObject() == b.GetGameObject() || aline.PointB.GetGameObject() == b.GetGameObject())
{
Debug.Log("ab点间已经存在连接线");
return null;
}
}
GameObject lineObject = Instantiate(linePrefab, this.transform);
ConnectionLine connectionLine = lineObject.GetComponent<ConnectionLine>();
if (connectionLine != null)
{
connectionLine.SetConnectable(a, b);
connectionLines.Add(connectionLine);
return connectionLine;
}
return null;
}
public void CutTargetConnectionLines(IConnectable target)
{
List<ConnectionLine> linesToRemove = new List<ConnectionLine>();
foreach (var line in target.ConnectionLines)
{
if (line != null)
{
linesToRemove.Add(line);
}
}
foreach (var line in linesToRemove)
{
connectionLines.Remove(line);
Destroy(line.gameObject);
}
}
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)
{
var connectionLine = GenerateConnectionLine(source, target);
if (connectionLine == null)
{
Debug.LogWarning($"编号为{preConnection.PreConnectionID}的连接线存在重复连接,生成失败");
}
}
else
{
Debug.LogWarning($"编号为{preConnection.PreConnectionID}的连接线的配置错误,生成失败");
}
}
}
}
}