NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/Gameplay/FunctionLock/FunctionLock.cs

172 lines
4.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using cfg.FunctionLockCfg;
using Cysharp.Threading.Tasks;
using Framework;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
namespace Gameplay.FunctionLock
{
public sealed class FunctionLock : MonoBehaviour
{
/// <summary>
/// 解锁动画触发参数名
/// </summary>
private const string ANIM_TRIGGER_NAME = "Unlock";
/// <summary>
/// 动画时长
/// </summary>
private const float ANIM_TIME = 3f;
/// <summary>
/// 功能锁id
/// </summary>
public int FunctionLockCfgId;
/// <summary>
/// 功能按钮
/// </summary>
public Button ButtonFunction;
#region UI
/// <summary>
/// 功能锁根节点
/// </summary>
private GameObject Root;
/// <summary>
/// 解锁动画
/// </summary>
private Animator animator;
/// <summary>
/// 解锁描述
/// </summary>
private TMP_Text textContent;
/// <summary>
/// 锁按钮
/// </summary>
private Button buttonLock;
#endregion
/// <summary>
/// 解锁配置
/// </summary>
private DataFunctionLock cfg;
/// <summary>
/// 是否正在播放解锁动画
/// </summary>
private bool isPlayingAnim;
/// <summary>
/// UI名
/// </summary>
public string UIName
{
get
{
if (null == cfg)
return string.Empty;
return cfg.UIName;
}
}
/// <summary>
/// 是否解锁
/// </summary>
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<Animator>();
textContent = Root.transform.Find("Lock/TextContent")?.GetComponent<TMP_Text>();
buttonLock = Root.transform.Find("Lock")?.GetComponent<Button>();
cfg = TableManager.Instance.Tables.FunctionLockConfig.Get(FunctionLockCfgId);
if (null == cfg)
{
DebugUtil.LogError($"功能锁配置获取错误id{FunctionLockCfgId}");
return;
}
SetState(); // 设置状态
isPlayingAnim = false;
buttonLock.onClick.AddListener(OnLock);
}
/// <summary>
/// 播放解锁动画
/// </summary>
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;
}
/// <summary>
/// 设置状态
/// </summary>
/// <param name="isUnlock"></param>
private void SetState()
{
if (IsUnlock) // 已解锁
{
Root?.IsScaleShow(false);
if (null != ButtonFunction)
ButtonFunction.interactable = true;
buttonLock.interactable = false;
}
else // 未解锁
{
if (null != textContent)
textContent.text = CommonUtils.GetLocalizeText(cfg.DescKey);
Root?.IsScaleShow(true);
if (null != ButtonFunction)
ButtonFunction.interactable = false;
buttonLock.interactable = true;
FunctionLockManager.Instance.AddFunctionLock(this);
}
}
/// <summary>
/// 点击锁
/// </summary>
private void OnLock()
{
UITipsManager.ShowTips(CommonUtils.GetLocalizeText("ui_functionLockTip")); // 功能未解锁
}
}
}