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

65 lines
1.8 KiB
C#

using System;
using asap.core;
using UnityEngine;
using UnityEngine.UI;
public class EventGatherCoreTriggerView : MonoBehaviour
{
[SerializeField] private Button btn;
[SerializeField] private Animation ani;
private IEventAggregator _eventAggregator;
private int _idx;
private float _showDuration;
private readonly string AnimationShow = "football_show";
private void Start()
{
btn.onClick.AddListener(OnClick);
}
public void Init(int index, bool isUsed, float showDuration, IEventAggregator eventAggregator)
{
// Debug.Log($"[EventGatherCore] Init: {isUsed}]", this);
Set(isUsed);
_eventAggregator = eventAggregator;
_idx = index;
_showDuration = showDuration;
}
public void Set(bool isUsed)
{
if (!isUsed && !gameObject.activeSelf)
{
ani.Play(AnimationShow);
}
gameObject.SetActive(!isUsed);
}
private async void OnClick()
{
try
{
var res = EventGatherCoreSystem.Draw(_idx, out int rewardIdx, out int rewardId);
_eventAggregator.Publish(
new EventGatherCoreTriggerTaken()
{
Succeeded = res,
RewardIdx = rewardIdx,
Position = btn.transform.position,
RewardId = rewardId,
});
// Debug.Log($"[EventGatherCore] Trigger Click: {_showDuration}", this);
if (!res)
{
return;
}
await System.Threading.Tasks.Task.Delay(TimeSpan.FromSeconds(_showDuration));
gameObject.SetActive(false);
}
catch (Exception e)
{
Debug.Log($"[EventGatherCore] Trigger Click Error:{e.Message}\n{e.StackTrace}", this);
}
}
}