Files
2026-05-26 16:15:54 +08:00

150 lines
5.2 KiB
C#

using UnityEngine;
using UnityEngine.UI;
using System.Threading.Tasks;
using System;
using GameCore;
using asap.core;
public class EventCanCan : MonoBehaviour
{
[SerializeField] private Button btn;
[SerializeField] private GameObject[] goCans;
[SerializeField] private Animation ani;
[SerializeField] private GameObject fxDrop, audioDrop;
private int _idx;
private const string AnimationIdle = "can_idle", AnimationHint = "can_hint",
AnimationOpen = "can_open", AnimationAppear = "can_appear", AnimationDisappear = "can_disappear";
private void Start()
{
btn.onClick.AddListener(OnClick);
}
public void Init(int idx, EventCanCanInfo info)
{
_idx = idx;
gameObject.SetActive(info.DoesExist);
if (!info.DoesExist)
return;
SetColor(info.ColorIdx);
ani.Play(AnimationAppear);
}
public void Init(int idx, bool doShow)
{
_idx = idx;
gameObject.SetActive(doShow);
if (!doShow)
return;
SetRandomColor();
ani.Play(AnimationAppear);
audioDrop.SetActive(true);
fxDrop.SetActive(true);
}
private async void OnClick()
{
var model = GContext.container.Resolve<EventCanModel>();
if ((model.BlockState | EEventCanBlockState.Available) != EEventCanBlockState.Available)
{
// Debug.Log($"[EventCan] Can open blocked: {model.BlockState}");
return;
}
var res = EventCanAct.CanSystem.OpenCan(_idx);
if (res == EEventCanOpenResponse.InsufficientToken)
{
EventCanAct.EventAggregator.Publish(new EventCanInsufficientTicketEvent());
return;
}
if (res == EEventCanOpenResponse.Other)
{
return;
}
var canReward = EventCanAct.CanSystem.GenerateReward();
#if UNITY_EDITOR
if (EventCanAct.ParamsCtrl.IsDebugMode)
{
canReward = new EventCanRewardInfo();
canReward.ItemCount = 1;
canReward.RewardType = EventCanAct.ParamsCtrl.GemType;
canReward.ItemId = 704010001 + (int)EventCanAct.ParamsCtrl.GemType * 1000;
canReward.RewardId = 704010001 + (int)EventCanAct.ParamsCtrl.GemType * 1000;
}
#endif
int oldTaskId = 0;
if (canReward.RewardType == EEventCanRewardType.Blue ||
canReward.RewardType == EEventCanRewardType.Green ||
canReward.RewardType == EEventCanRewardType.Purple ||
canReward.RewardType == EEventCanRewardType.Red)
{
oldTaskId = model.GemTasks[canReward.RewardType].TaskId;
}
EventCanAct.CanSystem.GrantCanReward(canReward);
EventCanGemTaskInfo gemTaskInfoSnapShot = null;
if (canReward.RewardType == EEventCanRewardType.Blue ||
canReward.RewardType == EEventCanRewardType.Green ||
canReward.RewardType == EEventCanRewardType.Purple ||
canReward.RewardType == EEventCanRewardType.Red)
{
gemTaskInfoSnapShot = model.GemTasks[canReward.RewardType].DeepCopy();
if (oldTaskId != gemTaskInfoSnapShot.TaskId)
{
model.BlockState |= EEventCanBlockState.Gem;
// Debug.Log($"<color=red>[EventCan] Gem Block: {model.BlockState}</color>");
}
}
if (canReward.RewardType == EEventCanRewardType.Supply)
{
model.BlockState |= EEventCanBlockState.Supply;
// Debug.Log($"<color=red>[EventCan] Supply Block: {model.BlockState}</color>");
}
// Debug.Log($"[EventCan] Opening can with Gem {gemTaskInfoSnapShot}.");
// Debug.Log($"[EventCan] Opening can with Gem {gemTaskInfoSnapShot.GemType}.");
// Debug.Log($"[EventCan] Opening can with Gem {gemTaskInfoSnapShot.CurrentGemProgress}.");
await PlayAnimationAsync(AnimationOpen);
EventCanAct.EventAggregator.Publish(new EventCanOpenEvent(canReward, transform as RectTransform, gemTaskInfoSnapShot));
if (canReward.RewardType == EEventCanRewardType.Item)
GContext.Publish(new EventRewardFlyStashRequest(canReward.ToItemData(), goCans[0].GetComponent<RectTransform>()));
gameObject.SetActive(false);
}
private void SetColor(int idx)
{
if (idx < 0 || idx >= goCans.Length)
{
Debug.LogError($"[EventCan]idx{idx} out of range {goCans.Length}", this);
return;
}
for (int i = 0; i < goCans.Length; i++)
goCans[i].SetActive(i == idx);
}
private int SetRandomColor()
{
var canIdx = UnityEngine.Random.Range(0, goCans.Length);
for (int i = 0; i < goCans.Length; i++)
goCans[i].SetActive(i == canIdx);
return canIdx;
}
public async Task PlayAnimationAsync(string aniName)
{
ani.Play(aniName);
await Task.Delay(TimeSpan.FromSeconds(ani.GetClip(aniName).length));
}
public void PlayAnimation(string aniName)
{
ani.Play(aniName);
}
public async Task PlayAniamtionWithDelay(string aniName, float delay)
{
await Task.Delay(TimeSpan.FromSeconds(delay));
ani.Play(aniName);
}
}