Files
zeling_v2/Assets/_Game/Scripts/World/Puzzle/PuzzleWire.cs

52 lines
1.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Assets/Scripts/World/Puzzle/PuzzleWire.cs
// 逻辑连接器:将一组 PuzzleSwitch 连接到 PuzzleReceiverArchitecture 21_LiquidPuzzleModule §11
// 支持 AND / OR / XOR 激活逻辑Inspector 中配置,无需代码
using System.Linq;
using UnityEngine;
namespace BaseGames.Puzzle
{
public enum LogicType { AND, OR, XOR }
/// <summary>
/// 连接一个或多个 PuzzleSwitch 到 PuzzleReceiver。
/// 支持 AND / OR / XOR 激活逻辑。
/// 关卡设计师在 Inspector 中配置,无需编写代码。
/// </summary>
public class PuzzleWire : MonoBehaviour
{
[Header("输入开关")]
[SerializeField] private PuzzleSwitch[] _switches;
[Header("激活逻辑")]
[SerializeField] private LogicType _logic = LogicType.AND;
[Header("目标接收器")]
[SerializeField] private PuzzleReceiver _receiver;
private void Start()
{
if (_switches != null)
foreach (var sw in _switches)
if (sw != null) sw.OnStateChanged += _ => Evaluate();
Evaluate(); // 初始求值
}
private void Evaluate()
{
if (_switches == null || _receiver == null) return;
bool shouldActivate = _logic switch
{
LogicType.AND => System.Array.TrueForAll(_switches, s => s != null && s.IsActive),
LogicType.OR => System.Array.Exists(_switches, s => s != null && s.IsActive),
LogicType.XOR => _switches.Count(s => s != null && s.IsActive) % 2 == 1,
_ => false,
};
if (shouldActivate) _receiver.Activate();
else _receiver.Deactivate();
}
}
}