using cfg.FunctionLockCfg;
using Cysharp.Threading.Tasks;
using Framework;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
namespace Gameplay.FunctionLock
{
public sealed class FunctionLock : MonoBehaviour
{
///
/// 解锁动画触发参数名
///
private const string ANIM_TRIGGER_NAME = "Unlock";
///
/// 动画时长
///
private const float ANIM_TIME = 3f;
///
/// 功能锁id
///
public int FunctionLockCfgId;
///
/// 功能按钮
///
public Button ButtonFunction;
#region UI
///
/// 功能锁根节点
///
private GameObject Root;
///
/// 解锁动画
///
private Animator animator;
///
/// 解锁描述
///
private TMP_Text textContent;
#endregion
///
/// 解锁配置
///
private DataFunctionLock cfg;
///
/// 是否正在播放解锁动画
///
private bool isPlayingAnim;
///
/// UI名
///
public string UIName
{
get
{
if (null == cfg)
return string.Empty;
return cfg.UIName;
}
}
///
/// 是否解锁
///
public bool IsUnlock
{
get
{
#if !FUNCTION_LOCK
return true; // 未启用功能锁,永远处于解锁状态
#endif
if (null == cfg)
return false;
return FunctionLockManager.Instance.IsUnlock(cfg.ServerFlag);
}
}
private void Awake()
{
Root = gameObject;
animator = Root.transform.Find("Lock").GetComponent();
textContent = Root.transform.Find("Lock/TextContent")?.GetComponent();
cfg = TableManager.Instance.Tables.FunctionLockConfig.Get(FunctionLockCfgId);
if (null == cfg)
{
DebugUtil.LogError($"功能锁配置获取错误id;{FunctionLockCfgId}");
return;
}
SetState(); // 设置状态
isPlayingAnim = false;
}
///
/// 播放解锁动画
///
public async void PlayUnlockAnim()
{
if (isPlayingAnim)
return;
if (IsUnlock) // 已经解锁直接返回
return;
isPlayingAnim = true;
FunctionLockManager.Instance.AddAnimState();
animator.SetTrigger(ANIM_TRIGGER_NAME);
FunctionLockManager.Instance.SaveServer(cfg.ServerFlag); // 服务器保存解锁状态
await UniTask.WaitForSeconds(ANIM_TIME);
SetState();
FunctionLockManager.Instance.RemoveAnimState();
isPlayingAnim = false;
}
///
/// 设置状态
///
///
private void SetState()
{
if (IsUnlock) // 已解锁
{
Root?.IsScaleShow(false);
if (null != ButtonFunction)
ButtonFunction.interactable = true;
}
else // 未解锁
{
if (null != textContent)
textContent.text = CommonUtils.GetLocalizeText(cfg.DescKey);
Root?.IsScaleShow(true);
if (null != ButtonFunction)
ButtonFunction.interactable = false;
FunctionLockManager.Instance.AddFunctionLock(this);
}
}
}
}