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

116 lines
3.3 KiB
C#
Raw Normal View History

using System;
using UnityEngine;
using Script.Gameplay.Interface;
namespace Script.Gameplay.Connect
{
// 只负责在场景中绘制连接线,并处理信号传递
public class ConnectionLine : MonoBehaviour, ISignalReceiver, ISignalSender
{
[SerializeField] private MonoBehaviour monoPointA;
[SerializeField] private MonoBehaviour monoPointB;
private IConnectable _pointA;
private IConnectable _pointB;
public IConnectable PointA
{
get => _pointA;
}
public IConnectable PointB
{
get => _pointB;
}
private LineRenderer line;
private void Awake()
{
_pointA = monoPointA as IConnectable;
_pointB = monoPointB as IConnectable;
line = GetComponent<LineRenderer>();
SetLineRendererPositions();
}
public void SetConnectable(IConnectable pointA, IConnectable pointB)
{
if (pointA == null || pointB == null)
{
Debug.Log("ConnectionLine requires two valid IConnectable points.");
return;
}
if (pointA == pointB)
{
Debug.Log("ConnectionLine cannot connect the same point.");
return;
}
_pointA = pointA;
_pointB = pointB;
pointA.ConnectionLines.Add(this);
pointB.ConnectionLines.Add(this);
SetLineRendererPositions();
}
private void SetLineRendererPositions()
{
if (_pointA != null && _pointB != null)
{
line.SetPositions(new Vector3[]
{
_pointA.GetPosition(),
_pointB.GetPosition()
});
}
}
// public void SignalActive(bool active, GameObject sender)
// {
// if (_pointA != null && _pointB != null)
// {
// if (sender == _pointA.GetGameObject())
// {
// _pointB.OnSignalReceived(active, sender);
// }
// else if (sender == _pointB.GetGameObject())
// {
// _pointA.OnSignalReceived(active, sender);
// }
// }
// }
private void OnDestroy()
{
_pointA.ConnectionLines.Remove(this);
_pointB.ConnectionLines.Remove(this);
}
2025-10-26 22:13:15 +08:00
public int NeedSignalCount { get; set; }
public int CurrentNeedSignalCount { get; set; }
public void OnSignalReceived(bool active, GameObject sender)
{
SendSignal(active, sender);
}
public bool IsEnableSendSignal { get; set; } = true;
public void SendSignal(bool active, GameObject sender)
{
if (_pointA.GetGameObject() != sender)
{
var pointA = _pointA as ISignalReceiver;
pointA?.OnSignalReceived(active, this.gameObject);
}
if (_pointB.GetGameObject() != sender)
{
var pointB = _pointB as ISignalReceiver;
pointB?.OnSignalReceived(active, this.gameObject);
}
}
}
}