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

140 lines
3.8 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-28 13:56:11 +08:00
using PhxhSDK;
2025-04-10 15:19:01 +08:00
using TMPro;
2025-04-10 13:36:39 +08:00
using UnityEngine;
2025-04-28 14:26:49 +08:00
using UnityEngine.UI;
2025-04-10 13:36:39 +08:00
namespace Gameplay.FunctionLock
{
2025-04-28 13:56:11 +08:00
public sealed class FunctionLock : UIGameObjectWrapper
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-28 14:26:49 +08:00
private Button button;
2025-04-24 14:13:44 +08:00
/// <summary>
2025-04-28 13:56:11 +08:00
/// 功能锁数据
2025-04-24 14:13:44 +08:00
/// </summary>
2025-04-28 13:56:11 +08:00
private FunctionLockNode functionLockData;
2025-04-10 15:19:01 +08:00
/// <summary>
2025-04-28 13:56:11 +08:00
/// 解锁动画触发参数名
2025-04-10 15:19:01 +08:00
/// </summary>
2025-04-28 13:56:11 +08:00
private const string ANIM_TRIGGER_NAME = "Unlock";
2025-04-11 12:47:46 +08:00
/// <summary>
2025-04-28 13:56:11 +08:00
/// 动画时长
2025-04-11 12:47:46 +08:00
/// </summary>
2025-04-28 13:56:11 +08:00
private const float ANIM_TIME = 3f;
private bool isPlayingAnim = false;
public int FunctionLockId { get { return functionLockData.FunctionLockCfgId; } }
public DataFunctionLock cfg;
2025-04-14 16:05:14 +08:00
/// <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
public bool IsUnlock
{
2025-04-28 13:56:11 +08:00
get
2025-04-11 15:18:23 +08:00
{
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)
2025-04-28 13:56:11 +08:00
return true;
2025-04-11 15:18:23 +08:00
2025-04-24 14:40:03 +08:00
return FunctionLockManager.Instance.IsUnlock(cfg.FunctionType);
2025-04-11 15:18:23 +08:00
}
}
2025-04-28 13:56:11 +08:00
public FunctionLock(GameObject root) : base(root, true)
2025-04-10 15:19:01 +08:00
{
2025-04-28 13:56:11 +08:00
functionLockData = GoRoot.GetComponent<FunctionLockNode>();
animator = GetComponent<Animator>("Lock");
textContent = GetComponent<TMP_Text>("Lock/TextContent");
2025-04-28 14:26:49 +08:00
button = GetComponent<Button>("Lock");
2025-04-11 12:47:46 +08:00
2025-04-28 13:56:11 +08:00
cfg = TableManager.Instance.Tables.FunctionLockConfig.Get(functionLockData.FunctionLockCfgId);
2025-04-10 13:36:39 +08:00
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
2025-04-28 14:26:49 +08:00
button.onClick.AddListener(OnLock);
2025-04-10 13:36:39 +08:00
}
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-28 13:56:11 +08:00
IsScaleShow = false;
2025-04-11 17:38:09 +08:00
2025-04-28 13:56:11 +08:00
if (null != functionLockData.ButtonFunction)
functionLockData.ButtonFunction.interactable = true;
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);
2025-04-28 13:56:11 +08:00
IsScaleShow = true;
2025-04-11 17:38:09 +08:00
2025-04-28 13:56:11 +08:00
if (null != functionLockData.ButtonFunction)
functionLockData.ButtonFunction.interactable = false;
2025-04-24 14:13:44 +08:00
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-28 13:56:11 +08:00
/// <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((int)cfg.FunctionType); // 服务器保存解锁状态
await UniTask.WaitForSeconds(ANIM_TIME);
SetState();
FunctionLockManager.Instance.RemoveAnimState();
isPlayingAnim = false;
}
2025-04-10 13:36:39 +08:00
}
}