253 lines
9.8 KiB
C#
253 lines
9.8 KiB
C#
using Assets.MyGame.Scripts.Net.WebResponses;
|
||
using DG.Tweening;
|
||
using Newtonsoft.Json.Linq;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using TMPro;
|
||
using UnityEngine;
|
||
using UnityEngine.UI;
|
||
|
||
public class ChaoNengMoHeEvent : BJEventBase
|
||
{
|
||
/// <summary>Boss 死亡时传来的特殊值(服务端 res 数组),与幸运轮一致</summary>
|
||
private List<BossKillOption> _cardParams = new List<BossKillOption>();
|
||
|
||
/// <summary>倍数列表(Inspector 拖入,8 个)</summary>
|
||
public List<GameObject> obj_BeiShu = new List<GameObject>();
|
||
|
||
/// <summary>展示位置:随机选中的倍数移到这里</summary>
|
||
public Transform beishuweizhi;
|
||
|
||
/// <summary>OnEnable 时要写入第一个子对象 Text 的倍数数组</summary>
|
||
private readonly int[] defaultBeishu = { 50, 100, 230, 80, 280, 300, 130, 250 };
|
||
|
||
public Text txt_FishScore;
|
||
public Text txt_FishBeiShu;
|
||
|
||
/// <summary>选中的总倍数(int 存储,供 EndBeiShu 计算)</summary>
|
||
private int _totalBeishu;
|
||
|
||
/// <summary>显示后等待时间(秒),再飞向展示位置</summary>
|
||
public float showDelay = 1f;
|
||
/// <summary>加分动画时长(秒),移动到指定位置后滚动加分</summary>
|
||
public float scoreDuration = 2f;
|
||
|
||
private void OnEnable()
|
||
{
|
||
// 遍历 obj_BeiShu 每个元素,取它的 child(0) 的 Text,按顺序赋默认倍数
|
||
for (int i = 0; i < obj_BeiShu.Count && i < defaultBeishu.Length; i++)
|
||
{
|
||
if (obj_BeiShu[i] == null || obj_BeiShu[i].transform.childCount == 0) continue;
|
||
|
||
var textGo = obj_BeiShu[i].transform.GetChild(0);
|
||
var tmp = textGo.GetComponent<TextMeshProUGUI>();
|
||
if (tmp == null) tmp = textGo.GetComponentInChildren<TextMeshProUGUI>();
|
||
var text = textGo.GetComponent<UnityEngine.UI.Text>();
|
||
if (tmp != null)
|
||
tmp.text = defaultBeishu[i].ToString();
|
||
else if (text != null)
|
||
text.text = defaultBeishu[i].ToString();
|
||
}
|
||
|
||
}
|
||
public override void Init(long goldCount, int posID, long paobei, long fishScore, int beishu, Action<int> action, Action action1 = null)
|
||
{
|
||
base.Init(goldCount, posID, paobei, fishScore, beishu, action, action1);
|
||
ParseCardParams();
|
||
|
||
}
|
||
/// <summary>解析服务端传入的 _cardParams(与幸运轮一致)</summary>
|
||
private void ParseCardParams()
|
||
{
|
||
_cardParams.Clear();
|
||
if (txt_FishScore!=null)
|
||
{
|
||
txt_FishScore.text = "x" + fishScore;
|
||
}
|
||
if (obj == null) return;
|
||
|
||
try
|
||
{
|
||
JToken rootToken = obj is JToken t ? t : JToken.Parse(obj.ToString());
|
||
JToken resToken = rootToken["res"] ?? rootToken;
|
||
|
||
if (resToken is JArray jarr)
|
||
_cardParams = jarr.ToObject<List<BossKillOption>>() ?? new List<BossKillOption>();
|
||
else if (resToken is JObject jobj)
|
||
_cardParams = new List<BossKillOption> { jobj.ToObject<BossKillOption>() };
|
||
else
|
||
_cardParams = new List<BossKillOption>();
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
Debug.LogError($"[ChaoNengMoHe] 解析参数失败:{e.Message}; 原始={obj}");
|
||
_cardParams = new List<BossKillOption>();
|
||
}
|
||
|
||
Debug.Log($"[ChaoNengMoHe] 解析到 {_cardParams.Count} 个选项: [{string.Join(", ", _cardParams.Select(p => p.score))}]");
|
||
}
|
||
/// <summary>Timeline 结束触发:无爆奖,直接播放 bjEndUI 并退出</summary>
|
||
public void TimeLineEnd()
|
||
{
|
||
if (isScoreEnd) return;
|
||
isScoreEnd = true;
|
||
|
||
// 音效:存钱罐获得(金币喷出,PW_Piggybank_Gain)
|
||
AudioManager.Instance.PlayerFXByName("PW_Piggybank_Gain");
|
||
|
||
// 回加金币到玩家(击杀奖励)
|
||
JiaFen?.Invoke();
|
||
|
||
// 计算最终分数:_totalBeishu × fishScore(同 EndBeiShu 逻辑)
|
||
int finalScore = _totalBeishu * (int)fishScore;
|
||
|
||
// 显示结束 UI:定位在座位上方,显示最终分数
|
||
if (bjEndUI != null)
|
||
{
|
||
float y = bjEndUIOffsetY;
|
||
if (posID == 2 || posID == 3) y *= -1f;
|
||
Vector3 uiPos = GetSeatPosition() + Vector3.up * y;
|
||
|
||
var endUI = Instantiate(bjEndUI, uiPos, Quaternion.identity, CreateFishManager.Instance.bjEffectRoot);
|
||
endUI.Init(bJbeishu, maxScore, isSelf);
|
||
}
|
||
|
||
// 退出(回池/销毁)
|
||
die();
|
||
}
|
||
/// <summary>移动动画时长(秒)</summary>
|
||
public float moveDuration = 2f;
|
||
|
||
/// <summary>
|
||
/// 用 Timeline 触发的事件:
|
||
/// 1. 随机选 _cardParams.Count 个 obj_BeiShu,赋随机值
|
||
/// 2. DOTween 移动到 beishuweizhi(展示位置),然后设 false
|
||
/// 3. 设 false 后恢复到原位置,保证下次正常使用
|
||
/// </summary>
|
||
public void TriggerRandomizeBeishu()
|
||
{
|
||
if (obj_BeiShu == null || obj_BeiShu.Count == 0)
|
||
{
|
||
Debug.LogWarning("[ChaoNengMoHe] obj_BeiShu 为空");
|
||
return;
|
||
}
|
||
obj_BeiShu[0].transform.parent.gameObject.SetActive(true);
|
||
// 如果没有服务端数据,用默认倍数兜底
|
||
var sourceScores = _cardParams != null && _cardParams.Count > 0
|
||
? _cardParams.Select(p => (int)p.score).ToList()
|
||
: defaultBeishu.ToList();
|
||
|
||
// 随机打乱源数据
|
||
var shuffled = sourceScores.OrderBy(_ => UnityEngine.Random.value).ToList();
|
||
|
||
// 随机打乱 obj_BeiShu 索引,取前 count 个
|
||
var shuffledIndices = Enumerable.Range(0, obj_BeiShu.Count)
|
||
.OrderBy(_ => UnityEngine.Random.value).ToList();
|
||
|
||
int count = _cardParams != null && _cardParams.Count > 0 ? _cardParams.Count : obj_BeiShu.Count;
|
||
count = Mathf.Clamp(count, 1, obj_BeiShu.Count);
|
||
|
||
Debug.Log($"[ChaoNengMoHe] 触发,随机选 {count} 个,源数据=[{string.Join(", ", sourceScores)}]");
|
||
|
||
// 音效:存钱罐出现(PW_Piggybank_Appear)
|
||
AudioManager.Instance.PlayerFXByName("PW_Piggybank_Appear");
|
||
|
||
// 先全部隐藏(保证干净状态)
|
||
for (int i = 0; i < obj_BeiShu.Count; i++)
|
||
{
|
||
if (obj_BeiShu[i] == null) continue;
|
||
obj_BeiShu[i].SetActive(false);
|
||
}
|
||
|
||
_totalBeishu = 0;
|
||
List<GameObject> selected = new List<GameObject>();
|
||
|
||
for (int k = 0; k < count; k++)
|
||
{
|
||
int idx = shuffledIndices[k];
|
||
var go = obj_BeiShu[idx];
|
||
if (go == null) continue;
|
||
selected.Add(go);
|
||
|
||
// 赋值随机倍数(从打乱后的源数据取,够则按序,不够则循环)
|
||
int score = k < shuffled.Count ? (int)shuffled[k] : (int)shuffled[k % shuffled.Count];
|
||
_totalBeishu += score;
|
||
|
||
var tmp = go.GetComponent<TextMeshProUGUI>();
|
||
if (tmp == null) tmp = go.GetComponentInChildren<TextMeshProUGUI>();
|
||
var text = go.GetComponent<UnityEngine.UI.Text>();
|
||
if (tmp != null) tmp.text = score.ToString();
|
||
else if (text != null) text.text = score.ToString();
|
||
|
||
// 移动原位置缓存(记录)
|
||
if (!_originPositions.ContainsKey(go))
|
||
_originPositions[go] = go.transform.position;
|
||
|
||
// 先显示在原始位置
|
||
go.SetActive(true);
|
||
}
|
||
|
||
// 启动完整流程:等1秒 → 飞向展示位置 → 2秒内加分
|
||
StartCoroutine(FlowShowMoveScore(selected));
|
||
}
|
||
|
||
/// <summary>完整流程:显示后等1秒 → 飞到展示位置 → 2秒内加分 → 隐藏恢复原位置</summary>
|
||
private System.Collections.IEnumerator FlowShowMoveScore(List<GameObject> selected)
|
||
{
|
||
// 1. 显示后等 1 秒
|
||
yield return new WaitForSeconds(showDelay);
|
||
|
||
// 2. 飞向展示位置
|
||
// 音效:存钱罐摇晃(PW_Piggybank_Shake)
|
||
AudioManager.Instance.PlayerFXByName("PW_Piggybank_Shake");
|
||
foreach (var go in selected)
|
||
{
|
||
if (go == null) continue;
|
||
go.transform.DOKill();
|
||
if (beishuweizhi != null)
|
||
go.transform.DOMove(beishuweizhi.position, moveDuration).SetEase(Ease.OutBack);
|
||
}
|
||
yield return new WaitForSeconds(moveDuration);
|
||
|
||
// 3. 移动到位置后,直接隐藏并恢复原位置(不等加分过程)
|
||
foreach (var go in selected)
|
||
{
|
||
if (go == null) continue;
|
||
go.transform.DOKill();
|
||
go.SetActive(false);
|
||
if (_originPositions.TryGetValue(go, out var origin))
|
||
go.transform.position = origin;
|
||
}
|
||
|
||
// 4. 加分到 txt_FishBeiShu(0 → _totalBeishu),独立进行
|
||
// 音效:存钱罐加分(PW_Piggybank_AddScore)
|
||
AudioManager.Instance.PlayerFXByName("PW_Piggybank_AddScore");
|
||
if (txt_FishBeiShu != null)
|
||
{
|
||
float t = 0f;
|
||
while (t < 1f)
|
||
{
|
||
t += Time.deltaTime / scoreDuration;
|
||
txt_FishBeiShu.text = Mathf.RoundToInt(Mathf.Lerp(0f, _totalBeishu, t)).ToString();
|
||
yield return null;
|
||
}
|
||
txt_FishBeiShu.text = _totalBeishu.ToString();
|
||
}
|
||
}
|
||
|
||
/// <summary>处理最后的倍数:倍数 × 底分(用 int 值,不读 text)</summary>
|
||
public void EndBeiShu()
|
||
{
|
||
int final = _totalBeishu * (int)fishScore;
|
||
Debug.Log($"[ChaoNengMoHe] EndBeiShu: {_totalBeishu} × {fishScore} = {final}");
|
||
|
||
// 音效:存钱罐最终获得(PW_Piggybank_FinalGain)
|
||
AudioManager.Instance.PlayerFXByName("PW_Piggybank_FinalGain");
|
||
|
||
if (txt_FishBeiShu != null)
|
||
txt_FishBeiShu.text = final.ToString();
|
||
}
|
||
/// <summary>记录各 obj_BeiShu 的原始位置</summary>
|
||
private readonly Dictionary<GameObject, Vector3> _originPositions = new Dictionary<GameObject, Vector3>();
|
||
} |