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

172 lines
4.5 KiB
C#
Raw Normal View History

2025-04-10 13:36:39 +08:00
using cfg.FunctionLockCfg;
2025-04-10 15:19:01 +08:00
using Cysharp.Threading.Tasks;
2025-04-10 13:36:39 +08:00
using Framework;
2025-04-10 15:19:01 +08:00
using TMPro;
2025-04-10 13:36:39 +08:00
using UnityEngine;
using UnityEngine.UI;
namespace Gameplay.FunctionLock
{
public sealed class FunctionLock : MonoBehaviour
{
2025-04-11 12:47:46 +08:00
/// <summary>
/// 解锁动画触发参数名
/// </summary>
private const string ANIM_TRIGGER_NAME = "Unlock";
2025-04-10 15:19:01 +08:00
/// <summary>
/// 动画时长
/// </summary>
2025-04-11 15:18:23 +08:00
private const float ANIM_TIME = 3f;
2025-04-10 15:19:01 +08:00
2025-04-10 13:36:39 +08:00
/// <summary>
/// 功能锁id
/// </summary>
2025-04-11 15:18:23 +08:00
public int FunctionLockCfgId;
2025-04-10 15:19:01 +08:00
/// <summary>
/// 功能按钮
/// </summary>
public Button ButtonFunction;
2025-04-10 17:27:38 +08:00
#region UI
2025-04-10 13:36:39 +08:00
/// <summary>
/// 功能锁根节点
/// </summary>
2025-04-10 15:19:01 +08:00
private GameObject Root;
2025-04-10 13:36:39 +08:00
/// <summary>
2025-04-10 15:19:01 +08:00
/// 解锁动画
2025-04-10 13:36:39 +08:00
/// </summary>
2025-04-10 15:19:01 +08:00
private Animator animator;
/// <summary>
/// 解锁描述
/// </summary>
private TMP_Text textContent;
2025-04-24 14:13:44 +08:00
/// <summary>
/// 锁按钮
/// </summary>
private Button buttonLock;
2025-04-10 17:27:38 +08:00
#endregion
2025-04-11 12:47:46 +08:00
2025-04-10 15:19:01 +08:00
/// <summary>
/// 解锁配置
/// </summary>
private DataFunctionLock cfg;
2025-04-11 12:47:46 +08:00
/// <summary>
/// 是否正在播放解锁动画
/// </summary>
2025-04-14 16:05:14 +08:00
private bool isPlayingAnim;
/// <summary>
/// UI名
/// </summary>
public string UIName
{
get
{
if (null == cfg)
return string.Empty;
return cfg.UIName;
}
}
2025-04-11 12:47:46 +08:00
2025-04-11 15:18:23 +08:00
/// <summary>
/// 是否解锁
/// </summary>
public bool IsUnlock
{
get
{
2025-04-11 17:38:09 +08:00
#if !FUNCTION_LOCK
return true; // 未启用功能锁,永远处于解锁状态
#endif
2025-04-11 15:18:23 +08:00
if (null == cfg)
return false;
return FunctionLockManager.Instance.IsUnlock(cfg.ServerFlag);
}
}
2025-04-10 15:19:01 +08:00
private void Awake()
{
Root = gameObject;
animator = Root.transform.Find("Lock").GetComponent<Animator>();
textContent = Root.transform.Find("Lock/TextContent")?.GetComponent<TMP_Text>();
2025-04-24 14:13:44 +08:00
buttonLock = Root.transform.Find("Lock")?.GetComponent<Button>();
2025-04-11 12:47:46 +08:00
2025-04-11 15:18:23 +08:00
cfg = TableManager.Instance.Tables.FunctionLockConfig.Get(FunctionLockCfgId);
2025-04-10 13:36:39 +08:00
if (null == cfg)
{
2025-04-11 15:18:23 +08:00
DebugUtil.LogError($"功能锁配置获取错误id{FunctionLockCfgId}");
2025-04-10 13:36:39 +08:00
return;
}
2025-04-10 17:27:38 +08:00
SetState(); // 设置状态
2025-04-11 19:35:09 +08:00
2025-04-14 16:05:14 +08:00
isPlayingAnim = false;
2025-04-24 14:13:44 +08:00
buttonLock.onClick.AddListener(OnLock);
2025-04-10 13:36:39 +08:00
}
/// <summary>
/// 播放解锁动画
/// </summary>
2025-04-10 15:19:01 +08:00
public async void PlayUnlockAnim()
2025-04-10 13:36:39 +08:00
{
2025-04-14 16:05:14 +08:00
if (isPlayingAnim)
2025-04-11 12:47:46 +08:00
return;
2025-04-11 15:18:23 +08:00
if (IsUnlock) // 已经解锁直接返回
2025-04-11 12:47:46 +08:00
return;
2025-04-14 16:05:14 +08:00
isPlayingAnim = true;
FunctionLockManager.Instance.AddAnimState();
2025-04-11 12:47:46 +08:00
animator.SetTrigger(ANIM_TRIGGER_NAME);
FunctionLockManager.Instance.SaveServer(cfg.ServerFlag); // 服务器保存解锁状态
2025-04-10 15:19:01 +08:00
await UniTask.WaitForSeconds(ANIM_TIME);
2025-04-10 17:27:38 +08:00
SetState();
2025-04-11 12:47:46 +08:00
FunctionLockManager.Instance.RemoveAnimState();
2025-04-14 16:05:14 +08:00
isPlayingAnim = false;
2025-04-10 13:36:39 +08:00
}
/// <summary>
/// 设置状态
/// </summary>
/// <param name="isUnlock"></param>
2025-04-10 17:27:38 +08:00
private void SetState()
2025-04-10 13:36:39 +08:00
{
2025-04-11 15:18:23 +08:00
if (IsUnlock) // 已解锁
2025-04-10 13:36:39 +08:00
{
2025-04-11 17:38:09 +08:00
Root?.IsScaleShow(false);
if (null != ButtonFunction)
ButtonFunction.interactable = true;
2025-04-24 14:13:44 +08:00
buttonLock.interactable = false;
2025-04-10 13:36:39 +08:00
}
2025-04-11 12:47:46 +08:00
else // 未解锁
2025-04-10 13:36:39 +08:00
{
2025-04-11 17:38:09 +08:00
if (null != textContent)
textContent.text = CommonUtils.GetLocalizeText(cfg.DescKey);
Root?.IsScaleShow(true);
if (null != ButtonFunction)
ButtonFunction.interactable = false;
2025-04-24 14:13:44 +08:00
buttonLock.interactable = true;
2025-04-11 15:18:23 +08:00
FunctionLockManager.Instance.AddFunctionLock(this);
2025-04-10 13:36:39 +08:00
}
}
2025-04-24 14:13:44 +08:00
/// <summary>
/// 点击锁
/// </summary>
private void OnLock()
{
UITipsManager.ShowTips(CommonUtils.GetLocalizeText("ui_functionLockTip")); // 功能未解锁
}
2025-04-10 13:36:39 +08:00
}
}