using Cysharp.Threading.Tasks;
|
using UnityEngine;
|
|
public class GuildAtkDefBatTreasureWin : UIBase
|
{
|
[SerializeField] ImageEx bgImage;
|
[SerializeField] RectTransform noOpenRect;
|
[SerializeField] TextEx titleNameText;
|
[SerializeField] TextEx openCntText;
|
[SerializeField] ButtonEx awardButton;
|
[SerializeField] ButtonEx lastLineButton;
|
[SerializeField] ButtonEx nextLineButton;
|
[SerializeField] ImageEx timeImage;
|
[SerializeField] TextEx timeText;
|
[SerializeField] ScrollerController scroller;
|
|
private const int COLS = 5;
|
private const int MAX_ROWS = 8;
|
private int maxGridCount;
|
private int currentTopRow;
|
private bool isWin;
|
private int cachedDisplayState; // 缓存上一次的 displayState,变化时关窗
|
|
GuildAtkDefBatManager manager => GuildAtkDefBatManager.Instance;
|
|
private int pendingAnimGridIndex = 0;
|
private int pendingAnimAwardID = 0;
|
|
protected override void InitComponent()
|
{
|
base.InitComponent();
|
|
awardButton.AddListener(() =>
|
{
|
UIManager.Instance.OpenWindow<GuildAtkDefBatAwardPreviewWin>();
|
});
|
|
lastLineButton.AddListener(() =>
|
{
|
if (currentTopRow > 0)
|
{
|
currentTopRow--;
|
RefreshScroller();
|
}
|
});
|
|
nextLineButton.AddListener(() =>
|
{
|
int totalRows = Mathf.CeilToInt((float)maxGridCount / COLS);
|
if (currentTopRow < totalRows - MAX_ROWS)
|
{
|
currentTopRow++;
|
RefreshScroller();
|
}
|
});
|
}
|
|
protected override void OnPreOpen()
|
{
|
base.OnPreOpen();
|
scroller.OnRefreshCell += OnRefreshCell;
|
manager.OnUpdateAtkDefBatInfoEvent += OnUpdateAtkDefBatInfo;
|
manager.OnTreasureDataUpdateEvent += OnTreasureDataUpdate;
|
manager.OnTreasureGridOpenEvent += OnTreasureGridOpen;
|
GlobalTimeEvent.Instance.secondEvent += OnSecondEvent;
|
manager.isTreasureWinOpen = true;
|
cachedDisplayState = manager.GetGuildBattleDisplayState();
|
Display();
|
}
|
|
protected override void OnPreClose()
|
{
|
base.OnPreClose();
|
scroller.OnRefreshCell -= OnRefreshCell;
|
manager.OnUpdateAtkDefBatInfoEvent -= OnUpdateAtkDefBatInfo;
|
manager.OnTreasureDataUpdateEvent -= OnTreasureDataUpdate;
|
manager.OnTreasureGridOpenEvent -= OnTreasureGridOpen;
|
GlobalTimeEvent.Instance.secondEvent -= OnSecondEvent;
|
manager.isTreasureWinOpen = false;
|
}
|
|
private void OnUpdateAtkDefBatInfo()
|
{
|
// A525到达时,displayState变化则关窗(此时翻牌事件尚未触发)
|
int curDisplayState = manager.GetGuildBattleDisplayState();
|
if (curDisplayState != cachedDisplayState)
|
CloseWindow();
|
}
|
|
private void OnTreasureDataUpdate()
|
{
|
openCntText.text = Language.Get("GuildAtkDefBat16", manager.GetMyOpenRemainCount(), manager.GetMyMaxOpenCount());
|
}
|
|
private void OnTreasureGridOpen(int gridIndex, int awardId)
|
{
|
pendingAnimGridIndex = gridIndex;
|
pendingAnimAwardID = awardId;
|
RefreshScroller();
|
pendingAnimGridIndex = 0;
|
pendingAnimAwardID = 0;
|
}
|
|
private void OnSecondEvent()
|
{
|
DisplayTime();
|
}
|
|
public void Display()
|
{
|
int displayState = manager.GetGuildBattleDisplayState();
|
isWin = displayState == 4;
|
bool isTrucePeriod = displayState == 4 || displayState == 5;
|
|
if (isTrucePeriod)
|
{
|
titleNameText.text = Language.Get(isWin ? "GuildAtkDefBat18" : "GuildAtkDefBat19");
|
}
|
else
|
{
|
titleNameText.text = Language.Get("GuildAtkDefBat17");
|
}
|
|
bgImage.SetSprite(isWin ? "GuildAtkDefBatTreasureWin" : "GuildAtkDefBatTreasureFail");
|
|
// 不等于休战期就隐藏noOpenRect
|
noOpenRect.SetActive(!isTrucePeriod);
|
|
// 开启次数
|
openCntText.text = Language.Get("GuildAtkDefBat16", manager.GetMyOpenRemainCount(), manager.GetMyMaxOpenCount());
|
|
// 剩余时间(仅休战期)
|
DisplayTime();
|
|
// 计算最大格子数
|
maxGridCount = manager.CalculateMaxGridCount();
|
|
// 重置滚动位置
|
currentTopRow = 0;
|
|
// 刷新Scroller
|
RefreshScroller();
|
}
|
|
void DisplayTime()
|
{
|
int displayState = manager.GetGuildBattleDisplayState();
|
bool isTrucePeriod = displayState == 4 || displayState == 5;
|
timeImage.SetActive(isTrucePeriod);
|
timeText.SetActive(isTrucePeriod);
|
if (isTrucePeriod)
|
{
|
int cd = manager.GetCountdownToCurrentPeriodEnd();
|
timeText.text = Language.Get("GuildAtkDefBat49", TimeUtility.SecondsToShortDHMS(cd));
|
}
|
}
|
|
void RefreshScroller()
|
{
|
scroller.Refresh();
|
int totalRows = Mathf.CeilToInt((float)maxGridCount / COLS);
|
int visibleRows = Mathf.Min(MAX_ROWS, totalRows - currentTopRow);
|
|
for (int i = 0; i < visibleRows; i++)
|
{
|
scroller.AddCell(ScrollerDataType.Header, currentTopRow + i);
|
}
|
scroller.Restart();
|
|
lastLineButton.SetActive(currentTopRow > 0);
|
nextLineButton.SetActive(currentTopRow < totalRows - MAX_ROWS);
|
}
|
|
void OnRefreshCell(ScrollerDataType type, CellView cell)
|
{
|
var tc = cell as GuildAtkDefBatTreasureCell;
|
if (tc != null)
|
{
|
int rowIdx = cell.index;
|
int startIdx = rowIdx * COLS + 1;
|
tc.SetData(startIdx, maxGridCount, isWin, manager.treasureGridRecords, pendingAnimGridIndex);
|
}
|
}
|
|
}
|