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

130 lines
3.3 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;
using System.Collections;
using System.Collections.Generic;
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>
private const float ANIM_TIME = 1.4f;
2025-04-10 13:36:39 +08:00
/// <summary>
/// 功能锁id
/// </summary>
public int FunctionLockId;
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-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>
public bool IsPlayingAnim { get; private set; }
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-11 12:47:46 +08:00
IsPlayingAnim = false;
2025-04-10 15:19:01 +08:00
}
2025-04-10 13:36:39 +08:00
private void Start()
{
2025-04-10 15:19:01 +08:00
cfg = TableManager.Instance.Tables.FunctionLockConfig.Get(FunctionLockId);
2025-04-10 13:36:39 +08:00
if (null == cfg)
{
DebugUtil.LogError($"功能锁配置获取错误id{FunctionLockId}");
return;
}
2025-04-10 17:27:38 +08:00
SetState(); // 设置状态
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-11 12:47:46 +08:00
if (IsPlayingAnim)
return;
if (IsUnlock())
return;
IsPlayingAnim = true;
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
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 12:47:46 +08:00
if (IsUnlock()) // 已解锁
2025-04-10 13:36:39 +08:00
{
Root.IsScaleShow(false);
ButtonFunction.interactable = true;
}
2025-04-11 12:47:46 +08:00
else // 未解锁
2025-04-10 13:36:39 +08:00
{
2025-04-10 15:19:01 +08:00
textContent.text = cfg.Desc;
2025-04-10 13:36:39 +08:00
Root.IsScaleShow(true);
ButtonFunction.interactable = false;
}
}
2025-04-11 12:47:46 +08:00
/// <summary>
/// 是否解锁
/// </summary>
/// <returns></returns>
private bool IsUnlock()
{
if (null == cfg)
return false;
return FunctionLockManager.Instance.IsUnlock(cfg.ServerFlag);
}
2025-04-10 13:36:39 +08:00
}
}