Files
back_cantanBuilding/Assets/Scripts/EventCan/EventCanGemTaskView.cs
2026-05-26 16:15:54 +08:00

153 lines
5.6 KiB
C#

using asap.core;
using UnityEngine;
using UniRx;
using System;
using System.Threading.Tasks;
using GameCore;
using UnityEngine.UI;
public class EventCanGemTaskView : MonoBehaviour
{
[SerializeField] private GameObject[] gems, emptySlots;
Sprite gemSprite;
[SerializeField] private RewardItemNew reward;
private IEventAggregator _eventAggregator;
private EventCanGemTaskInfo _info;
[SerializeField] private RewardFlyBatchController rewardFlyBatchController;
// private RectTransform RewardFlyTarget => emptySlots[_info.CurrentGemProgress].GetComponent<RectTransform>();
private RectTransform GetRewardFlyTarget(int progress)
{
if (progress >= gems.Length || progress < 0)
{
Debug.LogError($"[EventCan] Invalid progress {progress}");
return null;
}
return emptySlots[progress].GetComponent<RectTransform>();
}
private const float _gemPopInterval = 0.5f;
private RewardFlyBatchController _rewardFlyBatchController;
public void Init(EventCanGemTaskInfo info, IEventAggregator ea, RewardFlyBatchController rewardFlyBatchController)
{
gemSprite = gems[0].transform.GetComponent<Image>().sprite;
int i = 0;
while (i < info.CurrentGemProgress)
{
gems[i].SetActive(true);
i++;
}
while (i < gems.Length)
{
gems[i].SetActive(false);
i++;
}
reward.SetData(info.Reward);
_info = info.DeepCopy();
_eventAggregator = ea;
_eventAggregator.GetEvent<EventCanOpenEvent>().Subscribe(OnCanOpen).AddTo(this);
_rewardFlyBatchController = rewardFlyBatchController;
}
private async void OnCanOpen(EventCanOpenEvent e)
{
if (e.RewardInfo.RewardType != _info.GemType)
return;
// if ((model.BlockState & EEventCanBlockState.Gem) == EEventCanBlockState.Gem)
// {
// return;
// }
var taskInfoSnapShot = e.GemTaskInfoSnapShot;
// Debug.Log($"<color=red>[EventCan] -------------SnapShot---------------</color>");
// Debug.Log($"[EventCan] GemType: {taskInfoSnapShot.GemType}");
// Debug.Log($"[EventCan] CurrentProgress: {taskInfoSnapShot.CurrentGemProgress}");
await MakeRewardFly(e, taskInfoSnapShot);
await UpdateGemTask(taskInfoSnapShot);
}
private async Task UpdateGemTask(EventCanGemTaskInfo infoSnapShot)
{
int i;
if (infoSnapShot.TaskId != _info.TaskId)
{
gems[infoSnapShot.TotalGemRequired - 1].SetActive(true);
// await Task.Delay(TimeSpan.FromSeconds(_gemPopInterval));
var clickAwaiter = new TaskCompletionSource<bool>();
var panel = (await UIManager.Instance.ShowUINotLoading(UITypes.EventCanRewardPopupPanel)).GetComponent<EventCanRewardPanel>();
panel.Init(_info.Reward, _info.GemIdx - 1, _eventAggregator, () => clickAwaiter.SetResult(true));
// UIManager.UnblockInput();
await clickAwaiter.Task;
// UIManager.BlockInput();
_info = infoSnapShot;
i = 0;
while (i < gems.Length)
{
gems[i].SetActive(false);
i++;
}
reward.SetData(infoSnapShot.Reward);
var model = GContext.container.Resolve<EventCanModel>();
model.BlockState &= ~EEventCanBlockState.Gem;
Debug.Log($"[EventCan] Gem Unblock: {model.BlockState}");
}
else
{
i = _info.CurrentGemProgress;
// while (i < infoSnapShot.CurrentGemProgress)
// {
// gems[i].SetActive(true);
// i++;
// await Task.Delay(TimeSpan.FromSeconds(_gemPopInterval));
// }
gems[i].SetActive(true);
i++;
while (i < gems.Length)
{
gems[i].SetActive(false);
i++;
}
_info.CurrentGemProgress = infoSnapShot.CurrentGemProgress;
}
// UIManager.UnblockInput();
}
/// <summary>
/// Display RewardFly from can open event
/// </summary>
/// <param name="e">can open event</param>
/// <returns></returns>
private async Task MakeRewardFly(EventCanOpenEvent e, EventCanGemTaskInfo taskInfoSnapShot)
{
try
{
// var rewardFly = Instantiate(rewardFlyPrefab, rewardFlyPrefab.transform.parent).GetComponent<RewardFly>();
var rewardFlyTarget = GetRewardFlyTarget((taskInfoSnapShot.CurrentGemProgress + 2) % 3);
var collectionItemFly = new CollectionItemFly
{
icon = gemSprite,
numStr = "",
sourcePos = e.CanPosition,
destPos = rewardFlyTarget.position,
scale = transform.localScale,
isPlayOpen = true,
isPlayClose = true,
isDestinationRewardStash = false,
targetIconSize = rewardFlyTarget.rect.width
};
var request = new BatchedRewardFlyRequest
{
icon = gemSprite,
StartPoint = new BatchedRewardFlyPoint(e.CanRt),
EndPoint = new BatchedRewardFlyPoint(rewardFlyTarget),
isDestinationRewardStash = false,
AnimationParamIndex = 0
};
await rewardFlyBatchController.OnRewardFlyRequestAsync(request);
}
catch (Exception ex)
{
Debug.Log($"[EventCan] BreakCanError: {ex.Message}\n{ex.StackTrace}");
}
}
}