TH1/Unity/Assets/Scripts/TH1_UI/View/Bottom/UIBottomTutorMissionRow.cs
2026-05-13 20:46:36 +08:00

126 lines
4.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections;
using System.Collections.Generic;
using Logic;
using Logic.Audio;
using Logic.Multilingual;
using RuntimeData;
using TH1_Core.Events;
using TH1_Core.Managers;
using TH1_Logic.Core;
using TH1_Logic.MatchConfig;
using TH1_UI.HintUI;
using TMPro;
using UI.HintUI;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems; // 需要引入此命名空间以处理指针事件
namespace TH1_UI.View.Info
{
public class UIBottomTutorMissionRow : MonoBehaviour
{
public TextMeshProUGUI Desc;
public Image Point;
public HintTrigger HintTrigger;
public Color GrayColor;
public Color GreenColor;
// 任务完成瞬变追踪:上次刷新时该任务是否已成功
private bool _prevSuccess;
private bool _initialized;
// 高亮闪烁协程句柄,避免重复触发
private Coroutine _highlightCoroutine;
private void Awake()
{
}
public void SetContent(PlayerTaskInfo taskInfo,PlayerTaskData taskData)
{
// 自定义描述优先CustomDesc 非空则用之,否则走默认的 PlayerTaskData.PlayerTaskDesc
var descKey = !string.IsNullOrEmpty(taskInfo.CustomDesc)
? taskInfo.CustomDesc
: taskData?.PlayerTaskDesc;
var str = MultilingualManager.Instance.GetMultilingualText(descKey);
str = str.Replace("{param1_cur}", taskInfo.Param1Cur.ToString());
str = str.Replace("{param1_tar}", taskInfo.Param1.ToString());
str = str.Replace("{param2_cur}", taskInfo.Param2Cur.ToString());
str = str.Replace("{param2_tar}", taskInfo.Param2.ToString());
str = str.Replace("{param3_cur}", taskInfo.Param3Cur.ToString());
str = str.Replace("{param3_tar}", taskInfo.Param3.ToString());
str = str.Replace("{param4_tar}", taskInfo.Param4.ToString());
Desc.text = str;
if (HintTrigger != null)
{
HintTrigger.DataProvider.HintDataType = HintDataType.PlayerTaskData;
HintTrigger.DataProvider.PlayerTaskType = taskData?.PlayerTaskType ?? PlayerTaskType.None;
// CustomHint 非空时由 HintDataProvider 优先使用,空则走 PlayerTaskData.HintDesc 默认
HintTrigger.DataProvider.PlayerTaskCustomHint = taskInfo.CustomHint;
}
Point.color = taskInfo.IsSuccess ? GreenColor : GrayColor;
Desc.color = taskInfo.IsSuccess ? GreenColor : GrayColor;
// 串行任务过渡反馈:检测 IsSuccess 由 false → true 的瞬变,触发短暂高亮+音效。
// _initialized 用来避免首次绑定(玩家进战斗时任务已成功的不该播)。
if (_initialized && !_prevSuccess && taskInfo.IsSuccess)
{
PlayCompleteFeedback();
}
_prevSuccess = taskInfo.IsSuccess;
_initialized = true;
}
/// <summary>
/// 任务完成的瞬变反馈:音效 + 短暂放大闪烁。
/// 音效复用现有的 UNIT_levelup等美术补 SFX/UI_taskComplete 后替换。
/// </summary>
private void PlayCompleteFeedback()
{
AudioManager.Instance.PlayAudio("SFX/UNIT_levelup");
if (!gameObject.activeInHierarchy) return;
if (_highlightCoroutine != null) StopCoroutine(_highlightCoroutine);
_highlightCoroutine = StartCoroutine(HighlightCoroutine());
}
private IEnumerator HighlightCoroutine()
{
// 0.6 秒内做一次 1.0 → 1.15 → 1.0 的缩放脉冲,作为完成反馈
var rt = transform as RectTransform;
if (rt == null) yield break;
const float duration = 0.6f;
const float peakScale = 1.15f;
float elapsed = 0f;
Vector3 baseScale = Vector3.one;
while (elapsed < duration)
{
elapsed += Time.unscaledDeltaTime;
float t = elapsed / duration;
// 0 → 1 → 0 的三角波
float pulse = 1f - Mathf.Abs(t * 2f - 1f);
rt.localScale = baseScale * (1f + (peakScale - 1f) * pulse);
yield return null;
}
rt.localScale = baseScale;
_highlightCoroutine = null;
}
private void OnDisable()
{
// 行被隐藏时重置追踪,避免下次激活闪一下
_initialized = false;
_prevSuccess = false;
if (_highlightCoroutine != null)
{
StopCoroutine(_highlightCoroutine);
_highlightCoroutine = null;
}
if (transform is RectTransform rt) rt.localScale = Vector3.one;
}
}
}