using DG.Tweening; using UnityEngine; using GameCore; using System; public class EventBreakBlock : MonoBehaviour { [SerializeField] private RewardItemNew reward; [SerializeField] private GameObject crack, mask, fxBreak; [SerializeField] private RectTransform rectTransform; public RectTransform RectTransform => rectTransform; private DrillBlockData _blockData; private float _fxLiftTime; /// /// Move Block from startPoint to endPoint within dropTime and set item to it /// /// Item inside. Could be empty. /// Screen Position /// Screen Position /// In Seconds public void Appear(DrillBlockData blockData, float dropHeight, Vector2 endPoint, AnimationCurve easeCurve, float dropTime = 0.5f, float fxLifeTime = 1f) { var startPoint = endPoint + Vector2.up * dropHeight; try { // Debug.Log($"[Break] Block Appear from {startPoint} to {endPoint}.", this); transform.position = startPoint; gameObject.SetActive(true); if (blockData.IsEmpty) SetEmpty(); else SetReward(blockData.Item); transform.DOMove(endPoint, dropTime).SetEase(easeCurve); _blockData = blockData; _fxLiftTime = fxLifeTime; } catch(Exception e) { Debug.Log($"Block Spawn Error: {e.Message}, {e.StackTrace}"); } } public void Break() { // Debug.Log($"[EventBreak] Block broken: {_blockData.IsEmpty}, {_blockData.ItemId}, {_blockData.ItemCount}.", this); PlayBreakFx(); gameObject.SetActive(false); if (_blockData == null || _blockData.IsEmpty) return; EventBreakAct.EventAggregator.Publish(new EventBreakBlockBreak{BreakPos = transform.position, Item = _blockData.Item}); } private async void PlayBreakFx() { try { fxBreak.SetActive(false); // await Awaiters.NextFrame; fxBreak.SetActive(true); // await Awaiters.NextFrame; await System.Threading.Tasks.Task.Delay(TimeSpan.FromSeconds(_fxLiftTime)); // await Awaiters.NextFrame; fxBreak.SetActive(false); } catch (Exception e) { Debug.Log($"[EventBreak]Block Break Fx Error:{e.Message}\n{e.StackTrace}", this); } } private void SetReward(ItemData item) { reward.SetData(item); reward.gameObject.SetActive(true); crack.SetActive(true); mask.SetActive(true); } private void SetEmpty() { reward.gameObject.SetActive(false); crack.SetActive(false); mask.SetActive(false); } }