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(); private RectTransform GetRewardFlyTarget(int progress) { if (progress >= gems.Length || progress < 0) { Debug.LogError($"[EventCan] Invalid progress {progress}"); return null; } return emptySlots[progress].GetComponent(); } private const float _gemPopInterval = 0.5f; private RewardFlyBatchController _rewardFlyBatchController; public void Init(EventCanGemTaskInfo info, IEventAggregator ea, RewardFlyBatchController rewardFlyBatchController) { gemSprite = gems[0].transform.GetComponent().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().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($"[EventCan] -------------SnapShot---------------"); // 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(); var panel = (await UIManager.Instance.ShowUINotLoading(UITypes.EventCanRewardPopupPanel)).GetComponent(); 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(); 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(); } /// /// Display RewardFly from can open event /// /// can open event /// private async Task MakeRewardFly(EventCanOpenEvent e, EventCanGemTaskInfo taskInfoSnapShot) { try { // var rewardFly = Instantiate(rewardFlyPrefab, rewardFlyPrefab.transform.parent).GetComponent(); 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}"); } } }