65 lines
2.1 KiB
C#
65 lines
2.1 KiB
C#
// Assets/Scripts/World/Puzzle/PuzzleReceiver.cs
|
|
using BaseGames.Feedback;
|
|
using BaseGames.World;
|
|
using UnityEngine;
|
|
|
|
namespace BaseGames.Puzzle
|
|
{
|
|
/// <summary>
|
|
/// 谜题接收器,由 PuzzleWire 驱动。
|
|
/// 挂在谜题目标物件上(门/平台等),实现 IActivatable。
|
|
/// 子类覆写 OnActivate / OnDeactivate 实现具体行为。
|
|
/// </summary>
|
|
public class PuzzleReceiver : MonoBehaviour, IActivatable
|
|
{
|
|
[SerializeField] private bool _startsActivated = false;
|
|
[SerializeField] private string _receiverId; // 持久化唯一 ID(空串则不持久化)
|
|
[SerializeField] private SceneFeedback _activateFeedback;
|
|
[SerializeField] private SceneFeedback _deactivateFeedback;
|
|
|
|
[Header("持久化(SO 注入,非 Instance 单例)")]
|
|
[SerializeField] private WorldStateRegistry _worldState;
|
|
|
|
private bool _isActivated;
|
|
public bool IsActivated => _isActivated;
|
|
|
|
protected virtual void Awake()
|
|
{
|
|
bool savedState = !string.IsNullOrEmpty(_receiverId)
|
|
&& _worldState != null
|
|
&& _worldState.HasFlag("receiver_" + _receiverId);
|
|
_isActivated = savedState || _startsActivated;
|
|
}
|
|
|
|
protected virtual void Start()
|
|
{
|
|
if (_isActivated) OnActivate();
|
|
}
|
|
|
|
public void Activate()
|
|
{
|
|
if (_isActivated) return;
|
|
_isActivated = true;
|
|
_activateFeedback?.Play();
|
|
OnActivate();
|
|
|
|
if (!string.IsNullOrEmpty(_receiverId))
|
|
_worldState?.SetFlag("receiver_" + _receiverId);
|
|
}
|
|
|
|
public void Deactivate()
|
|
{
|
|
if (!_isActivated) return;
|
|
_isActivated = false;
|
|
_deactivateFeedback?.Play();
|
|
OnDeactivate();
|
|
|
|
if (!string.IsNullOrEmpty(_receiverId))
|
|
_worldState?.ClearFlag("receiver_" + _receiverId);
|
|
}
|
|
|
|
protected virtual void OnActivate() { }
|
|
protected virtual void OnDeactivate() { }
|
|
}
|
|
}
|