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

111 lines
3.4 KiB
C#

using UnityEngine;
using GameCore;
using asap.core;
using UniRx;
using asap.core.common;
using System.Threading.Tasks;
public class EventLuckMagicTaskView : MonoBehaviour
{
[SerializeField] private GameObject[] progressIcons, fxReceived;
[SerializeField] private RewardItemNew rewardDisplay;
[SerializeField] private Animation ani;
private int _progress = 0;
private IEventAggregator _eventAggregator;
private IObjectPoolService _objectPoolService;
private int _taskItemId;
private ItemData _reward;
private const string Normal = "target_nml", OnComplete = "target_full", Completed = "target_completed";
private RectTransform CurrentIcon
{
get
{
if (progressIcons.Length > _progress)
{
return progressIcons[_progress].GetComponent<RectTransform>();
}
Debug.LogError($"CurrentIcon is null, progress {_progress} not valid.");
return null;
}
}
public void Init(int progress, ItemData reward, int taskItemId)
{
rewardDisplay.SetData(reward);
UpdateProgress(progress);
PlayAnimation();
_eventAggregator = EventLuckMagicAct.EventAggregator;
// _eventAggregator?.GetEvent<EventRewardFlyRequest>().Subscribe(OnRewardFlyRequest).AddTo(this);
_objectPoolService = GContext.container.Resolve<IObjectPoolService>();
_progress = progress;
_taskItemId = taskItemId;
_reward = reward;
}
public void UpdateProgress(int progress, bool doesPlayFx = false)
{
for (int i = 0; i < progressIcons.Length; i++)
{
progressIcons[i].SetActive(i < progress);
fxReceived[i].SetActive(false);
}
_progress = progress;
if (doesPlayFx)
{
fxReceived[_progress - 1].SetActive(true);
}
}
public async Task OnRewardFlyRequest(EventRewardFlyStashRequest e)
{
if (e.Reward.id != _taskItemId)
return;
var pool = _objectPoolService.GetPool(typeof(RewardFly));
var rewardFly = pool.SpawnObject() as RewardFly;
var collectionItemFly = new CollectionItemFly
{
itemID = e.Reward.id,
numStr = "",
sourcePos = e.SourceIconRt.position,
destPos = CurrentIcon.position,
scale = transform.localScale,
isPlayOpen = true,
isPlayClose = true,
isDestinationRewardStash = true,
targetIconSize = CurrentIcon.rect.width
};
rewardFly.gameObject.SetActive(true);
await rewardFly.ShowAsync(collectionItemFly);
pool.DespawnObject(rewardFly);
UpdateProgress(GContext.container.Resolve<EventLuckMagicModel>().TaskProgress[e.Reward.id], true);
await PlayAnimationAsync();
}
public void PlayAnimation()
{
ani.Play(_progress >= 3 ? Completed : Normal);
}
private async Task PlayAnimationAsync()
{
if (_progress >= 3)
{
ani.Play(OnComplete);
await Task.Delay(System.TimeSpan.FromSeconds(ani.GetClip(OnComplete).length));
}
}
public void ResetTask(ItemData reward)
{
for (int i = 0; i < progressIcons.Length; i++)
{
progressIcons[i].SetActive(false);
fxReceived[i].SetActive(false);
}
_progress = 0;
rewardDisplay.SetData(reward);
_reward = reward;
}
}