Files
back_cantanBuilding/Assets/Scripts/UI/Potion/PotionItem.cs
2026-05-26 16:15:54 +08:00

462 lines
16 KiB
C#

using asap.core;
using cfg;
using DG.Tweening;
using EnhancedUI.EnhancedScroller;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace game
{
public class PotionItem : EnhancedScrollerCellView
{
public void SetData(EventPotionPanel potionPanel, EventPotionDataManager dataManager, PotionItemData data)
{
m_Panel = potionPanel;
m_DataManager = dataManager;
m_Data = data;
if (!m_IsInited) InitUI();
if (data.DataType == EventPotionDataType.Data_Wall)
{
shadow.gameObject.SetActive(false);
int count = m_GridList.Count;
for (int i = 0; i < count; ++i)
{
m_GridList[i].Clear();
m_GridList[i].SetData(data, i, null, false);
}
}
else if (data.DataType == EventPotionDataType.Data_Item || data.DataType == EventPotionDataType.Data_ItemAndOperation)
{
int count = m_GridList.Count;
int num = data.ColorTypeList.Count;
data.Log("111 " + this.gameObject.name);
EventPotionBlock potionBlock = m_DataManager.GetEventPotionBlock(num);
for (int i = 0; i < count; i++)
{
m_GridList[i].Clear();
if (i < num)
{
int itemType = data.ColorTypeList[i];
m_GridList[i].SetData(data, i, potionBlock, true);
HandleFromPotionGridUI(m_GridList[i]);
}
else
{
m_GridList[i].SetData(data, i, potionBlock, false);
}
}
data.Log("222 " + this.gameObject.name);
shadowRectTransform.sizeDelta = new Vector2(potionBlock.ShadowLength, shadowRectTransform.sizeDelta.y);
shadow.gameObject.SetActive(true);
}
}
#region
private EventPotionDataManager m_DataManager;
private PotionItemData m_Data;
public PotionItemData Data { get { return m_Data; } }
public int GetVaildNum()
{
if (m_Data == null) return 0;
return m_Data.ColorTypeList.Count;
}
public int GetLastValidIndex()
{
int count = GetVaildNum();
for (int i = count - 1; i >= 0; --i)
{
var gridState = m_Data.GetGridState(i);
if (gridState == EventPotionGridState.GridState_Over) continue;
if (gridState == EventPotionGridState.GridState_Occupied && m_Data.GetColorType(i) > 0)
{
return i;
}
return i;
}
return -1;
}
#endregion
#region GridUI从操作区跳回展示区操作
public int HandleFromPotionGridUI(PotionGridUI gridUI)
{
if (gridUI == null) return 0;
if (m_Panel == null) return 0;
if (m_DataManager == null) return 0;
int num = m_DataManager.PotionData.LevelDisplayData.ColorTypeList.Count;
int fromDisplayAreaIndex = gridUI.GetFromDisplayAreaIndex();
if (fromDisplayAreaIndex < 0 || fromDisplayAreaIndex >= num) return gridUI.ClearFromPotionGridUI();
PotionGridUI displayPotionGridUI = m_Panel.GetDisplayPotionGridUI(fromDisplayAreaIndex);
if (displayPotionGridUI == null) return gridUI.ClearFromPotionGridUI();
//gridUI.fromPotionGridUI = displayPotionGridUI;
gridUI.SetFromGridUI(displayPotionGridUI);
return 0;
}
#endregion
#region
public bool CheckOperationAreaIsAllOnline()
{
if (m_Data == null) return false;
int count = m_Data.ColorTypeList.Count;
for (int i = 0; i < count; ++i)
{
EventPotionGridState gridState = m_Data.GridStateList[i];
// 于该位置,状态应该为 GridState_Occupied 或者 GridState_Over
if (gridState != EventPotionGridState.GridState_Occupied && gridState != EventPotionGridState.GridState_Over)
{
return false;
}
int colorType = m_Data.ColorTypeList[i];
if (colorType <= 0)
{
return false;
}
}
return true;
}
public bool CheckGridListIsAllOnline()
{
if (m_Data == null) return false;
int count = m_Data.ColorTypeList.Count;
for (int i = 0; i < count; ++i)
{
if (m_Data.ColorTypeList[i] <= 0)
{
return false;
}
}
return true;
}
public bool CheckOperationIsOver()
{
if (m_Data == null) return true;
int count = m_Data.ColorTypeList.Count;
for (int i = 0; i < count; ++i)
{
var item = m_GridList[i];
EventPotionGridState gridState = item.GetGridState();
if (gridState == EventPotionGridState.GridState_Over) continue;
if (gridState != EventPotionGridState.GridState_Wrong && gridState != EventPotionGridState.GridState_Ok)
{
return true;
}
}
return false;
}
public bool CheckGridListIsAllOccupied()
{
foreach (var gridUI in m_GridList)
{
if (gridUI.GetGridState() == EventPotionGridState.GridState_Occupied)
{
return false;
}
}
return true;
}
public bool CheckGridListIsOk(Action<int, PotionGridUI> successCallback, Action<int, PotionGridUI> wrongCallback)
{
if (m_Data == null || m_Panel == null) return false;
bool flag = true;
int count = m_GridList.Count;
int num = m_Data.ColorTypeList.Count;
float baseTime = m_Panel.check_base_waiting_time;
float gapTime = m_Panel.check_gap_time;
int lastIndex = num - 1;
m_Panel.CommitColorTypeList.Clear();
for (var i = num - 1; i >= 0; --i)
{
if (i == lastIndex && m_Data.GridStateList[i] == EventPotionGridState.GridState_Over)
{
lastIndex--;
}
else
{
break;
}
}
int index = 0;
for (int i = 0; i < count; ++i)
{
if (i < num)
{
float time = baseTime + index * gapTime;
var item = m_GridList[i];
EventPotionGridState gridState = item.GetGridState();
if (gridState == EventPotionGridState.GridState_Over)
{
continue;
}
int colorType = item.GetColorType() % 100;
m_Panel.CommitColorTypeList.Add(colorType);
if (colorType == m_DataManager.PotionData.LevelTargetData.ColorTypeList[i])
{
m_DataManager.PotionData.CorrectNum++;
item.SetShelfTypeSuccess();
item.SetGridState(EventPotionGridState.GridState_Ok);
if (item.fromPotionGridUI != null)
{
item.fromPotionGridUI.SetGridState(EventPotionGridState.GridState_Ok);
}
if (flag)
{
if (i == lastIndex)
{
m_Data.isOver = true;
m_Panel.StartCoroutine(HandleCheckSucessStep(1, item, time, successCallback));
}
else
{
m_Panel.StartCoroutine(HandleCheckSucessStep(1, item, time, null));
}
}
else
{
//m_Panel.StartCoroutine(PotionGridUIChangeToSuccess(1, item, time + 0.1f, i == lastIndex, wrongCallback, m_Panel.Animation_Time_Vase_Success + 2f));
m_Panel.StartCoroutine(HandleCheckSucessStep(1, item, time, i == lastIndex ? wrongCallback : null));
}
}
else
{
flag = false;
item.SetGridState(EventPotionGridState.GridState_Wrong);
//if (item.fromPotionGridUI != null)
//{
// item.fromPotionGridUI.SetGridState(EventPotionGridState.GridState_Wrong);
//}
int itemType = colorType;
itemType = 100 + itemType;
item.SetColorType(itemType);
item.ResetImageSprite();
m_Panel.StartCoroutine(HandleCheckWrongStep(1, item, time, i == lastIndex ? wrongCallback : null));
}
index++;
}
}
return flag;
}
public IEnumerator HandleCheckSucessStep(int type, PotionGridUI gridUI, float waitTime, Action<int, PotionGridUI> callback)
{
gridUI.PlaySpinAnimation(0, m_Panel.check_spine_shake1_anim, true);
yield return new WaitForSeconds(waitTime);
gridUI.PlayAnimation(EventPotionConfig.anim_gild_success);
yield return new WaitForSeconds(m_Panel.check_waitfor_spine_shake2_time);
gridUI.PlaySpinAnimation(0, m_Panel.check_spine_shake2_anim, true);
yield return new WaitForSeconds(m_Panel.check_spine_shake2_duration_time);
gridUI.PlaySpinAnimation(0, EventPotionConfig.spine_potion_idle, true);
if (callback != null)
{
callback.Invoke(type, gridUI);
}
}
public IEnumerator HandleCheckWrongStep(int type, PotionGridUI gridUI, float waitTime, Action<int, PotionGridUI> callback)
{
gridUI.PlaySpinAnimation(0, m_Panel.check_spine_shake1_anim, true);
yield return new WaitForSeconds(waitTime);
gridUI.PlayAnimation(EventPotionConfig.anim_gild_wrong);
yield return new WaitForSeconds(m_Panel.check_waitfor_spine_shake2_time);
//gridUI.PlaySpinAnimation(0, m_Panel.check_spine_shake2_anim, true);
yield return new WaitForSeconds(m_Panel.check_spine_shake2_duration_time);
//gridUI.PlaySpinAnimation(0, EventPotionConfig.spine_potion_idle, true);
if (callback != null)
{
callback.Invoke(type, gridUI);
}
}
public void Reset_FromPotionGridUI_GridState()
{
int count = m_GridList.Count;
foreach (var item in m_GridList)
{
if (item.fromPotionGridUI != null)
{
EventPotionGridState gridState = item.fromPotionGridUI.GetGridState();
if (gridState == EventPotionGridState.GridState_Ok)
{
item.fromPotionGridUI.SetGridState(EventPotionGridState.GridState_Over);
}
else if (gridState == EventPotionGridState.GridState_Wrong)
{
item.fromPotionGridUI.SetGridState(EventPotionGridState.GridState_Empty);
}
else if (gridState == EventPotionGridState.GridState_Occupied)
{
item.fromPotionGridUI.SetGridState(EventPotionGridState.GridState_Empty);
}
}
}
}
public IEnumerator HandleJumpDownStep(int type, PotionGridUI src, PotionGridUI target, PotionGridUI temporary)
{
yield return new WaitForSeconds(m_Panel.jumpdown_waitfor_spine_shake_time);
target.PlaySpinAnimation(0, m_Panel.jumpdown_spine_shake_anim, true);
target.ShowIcon();
target.gameObject.SetActive(true);
temporary.IsIdle = true;
temporary.gameObject.SetActive(false);
yield return new WaitForSeconds(m_Panel.jumpdown_spine_shake_duration_time);
m_Panel.StopLockClickOperation();
target.PlaySpinAnimation(0, EventPotionConfig.spine_potion_idle, true);
}
public bool HandleJumpDown(PotionGridUI gridUI)
{
// 找到停靠点
if (gridUI.fromPotionGridUI == null) return false;
PotionGridUI target = gridUI.fromPotionGridUI;
target.Copy(gridUI, false, false);
target.HideUpperUI();
// 找到用于显示飞行轨迹的临时PotionGridUI并初始化和播放初始动画
PotionGridUI temporary = m_Panel.GetIdleTemporaryGridUI();
if (temporary == null) return false;
temporary.SetTemporaryData(gridUI);
// 返还代币
m_Panel.ConsumeTokens(EventPotionConfig.Token_Return_Num);
// 锁定操作
m_Panel.StartLockClickOperation();
m_Panel.StartLockScrollView();
// 更新数据
gridUI.SetGridState(EventPotionGridState.GridState_Empty);
gridUI.SetColorType(-1);
gridUI.HideUpperUI();
gridUI.ClearFromPotionGridUI();
target.SetGridState(EventPotionGridState.GridState_Empty);
temporary.PlaySpinAnimation(0, EventPotionConfig.spine_potion_fly, true);
temporary.PlayAnimation(EventPotionConfig.anim_gild_jump_down);
Vector3 targetPosition = target.transform.position;
temporary.DOKill();
temporary.transform.DOMove(targetPosition, m_Panel.jumpdown_move_duration_time).SetEase(Ease.OutQuad);
m_Panel.StartCoroutine(HandleJumpDownStep(1, gridUI, target, temporary));
return true;
}
#endregion
#region
public void ResumeFromGuide()
{
if (m_GridList == null || m_GridList.Count < 1) return;
foreach (var gridUI in m_GridList)
{
gridUI.ResumeFromGuide();
}
}
public bool ReturnAllUncertainGridUI()
{
int count = m_GridList.Count;
for (int i = 0; i < count; ++i)
{
var gridUI = m_GridList[i];
if (gridUI.GetGridState() == EventPotionGridState.GridState_Over) continue;
HandleJumpDown(gridUI);
}
return true;
}
#endregion
#region UI
private EventPotionPanel m_Panel;
private bool m_IsInited = false;
private List<PotionGridUI> m_GridList = new List<PotionGridUI>();
public List<PotionGridUI> GridList { get => m_GridList; }
private Image shadow;
private RectTransform shadowRectTransform;
private void InitUI()
{
shadow = transform.Find("shadow").GetComponent<Image>();
shadowRectTransform = shadow.GetComponent<RectTransform>();
int num = 8;
for (int i = 1; i < num; i++)
{
PotionGridUI gridUI = transform.Find($"gild_group/gild{i}").GetComponent<PotionGridUI>();
gridUI.Init(i - 1, OnBtnClickCallback, this, m_DataManager);
m_GridList.Add(gridUI);
}
m_IsInited = true;
}
public PotionGridUI GetGridUI(int index)
{
if (m_GridList == null || m_GridList.Count < 1) return null;
if (index < 0 || index > m_GridList.Count) return null;
return m_GridList[index];
}
public void OnBtnClickCallback(PotionGridUI gridUI)
{
if (m_Panel.IsClickOperationLocked) return;
HandleJumpDown(gridUI);
if (m_DataManager != null)
{
m_DataManager.SyncData();
}
}
#endregion
}
}