using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.Playables;
namespace EventMowForTreasure
{
[DisallowMultipleComponent]
public class EventMowForTreasureObstacleCell : MonoBehaviour
{
[SerializeField] protected Vector3 colliderBrokeSize;
[SerializeField] protected Vector3 colliderBrokeCenter;
[SerializeField] protected GameObject normalGameObject;
[SerializeField] protected GameObject brokeGameObject;
[SerializeField] protected GameObject heightLightGameObject;
[SerializeField] protected GameObject redHeightLightGameObject;
[Header("obstacle normal")]
[SerializeField][Tooltip("人物割草的timeLine/r/n或者其他默认的timeline")]
protected PlayableAsset normalTimelineAsset;
[SerializeField][Tooltip("等多长时间开始播这个timeline")]
protected float normalDelayPlayTime = 0.35f;
[SerializeField] [Tooltip("表示进入下一个动画需要等待的时间 注意:下一个动画需要先等待它自己的delayTime才开始")]
protected float normalBreakDelayTime = 0.2f;
[Header("obstacle speed")] [SerializeField]
[Tooltip("道具2或者道具3 需要播的加速的timeline")]
protected PlayableAsset speedTimelineAsset;
[SerializeField] [Tooltip("等多长时间开始播这个timeline")]
protected float speedDelayPlayTime = 0.35f;
[SerializeField] [Tooltip("表示进入下一个动画需要等待的时间 注意:下一个动画需要先等待它自己的delayTime才开始")]
protected float speedBreakDelayTime = 0.2f;
protected Action ClickAction;
protected BoxCollider Collider;
protected EventMowForTreasureObstacleData ObstacleData;
protected virtual void Awake()
{
Collider = gameObject.GetComponent();
if (!Collider) Collider = gameObject.AddComponent();
Collider.isTrigger = true;
}
public virtual void OnMouseDown()
{
if (ObstacleData == null) return;
if (IsPointerOverUI())
return;
ClickAction?.Invoke(this);
}
private bool IsPointerOverUI()
{
if (EventSystem.current == null)
return true;
var pointerEventData = new PointerEventData(EventSystem.current)
{
position = Input.mousePosition
};
var results = new List();
EventSystem.current.RaycastAll(pointerEventData, results);
return results.Count > 0;
}
public virtual void SetData(EventMowForTreasureObstacleData data, Action clickAction)
{
ObstacleData = data;
ClickAction = clickAction;
if (ObstacleData == null) return;
Show();
}
public virtual EventMowForTreasureObstacleData GetData()
{
return ObstacleData;
}
public virtual void ShowSelected(bool isSuperBreak = false,bool needShowRed=false)
{
if (ObstacleData.IsCollectionReward)
{
Show();
return;
}
var isSpecialObstacle = ObstacleData.Type == EEventMowForTreasureObstacleType.HeightObstacle;
var isShowGreen = !isSpecialObstacle ? !ObstacleData.IsBroke : !ObstacleData.IsBroke && isSuperBreak;
var isShowRed = !isSpecialObstacle ? !ObstacleData.IsBroke : !ObstacleData.IsBroke && !isSuperBreak ;
if (needShowRed)
{
isShowGreen = false;
isShowRed=true;
}
if (normalGameObject)
normalGameObject?.SetActive(false);
if (brokeGameObject)
brokeGameObject?.SetActive(ObstacleData.IsBroke);
if (heightLightGameObject)
heightLightGameObject?.SetActive(isShowGreen);
if (redHeightLightGameObject)
redHeightLightGameObject?.SetActive(isShowRed);
}
public virtual void Show()
{
if (normalGameObject)
normalGameObject?.SetActive(!ObstacleData.IsBroke);
if (brokeGameObject)
brokeGameObject?.SetActive(ObstacleData.IsBroke);
if (heightLightGameObject)
heightLightGameObject?.SetActive(false);
if (redHeightLightGameObject)
redHeightLightGameObject?.SetActive(false);
if (!ObstacleData.IsBroke||ObstacleData.Type==EEventMowForTreasureObstacleType.None) return;
Collider.center = colliderBrokeCenter;
Collider.size = colliderBrokeSize;
}
public virtual async Task PlayBreakEffect(bool isSpeed = false, Action specialToolCallBack = null,bool isAwaitCallBack=false)
{
if (isSpeed && ObstacleData.IsObstacleCell)
await PlayBreakEffectSpeed(specialToolCallBack);
else
await PlayBreakEffectNormal(specialToolCallBack,isAwaitCallBack);
}
protected virtual async Task PlayBreakEffectNormal(Action specialToolCallBack = null,bool isAwaitCallBack=false)
{
await Awaiters.Seconds(normalDelayPlayTime);
if (!normalGameObject || !normalGameObject.activeInHierarchy)
{
await Awaiters.Seconds(normalBreakDelayTime);
return;
}
if (!normalTimelineAsset)
normalTimelineAsset = normalGameObject.GetComponent()?.playableAsset;
var director = normalGameObject.GetComponent();
if (director&& normalTimelineAsset!=null)
{
director.stopped -=DirectorStopped;
director.stopped +=DirectorStopped;
director.Play(normalTimelineAsset);
}
await Awaiters.Seconds(normalBreakDelayTime);
specialToolCallBack?.Invoke();
}
protected virtual async Task PlayBreakEffectSpeed(Action specialToolCallBack = null)
{
await Awaiters.Seconds(speedDelayPlayTime);
if (!normalGameObject || !normalGameObject.activeInHierarchy || speedTimelineAsset == null)
{
await Awaiters.Seconds(speedBreakDelayTime);
return;
}
var director = normalGameObject.GetComponent();
if (director)
{
director.stopped -=DirectorStopped;
director.stopped +=DirectorStopped;
director.Play(speedTimelineAsset);
}
specialToolCallBack?.Invoke();
await Awaiters.Seconds(speedBreakDelayTime);
}
protected virtual void DirectorStopped(PlayableDirector director)
{
Show();
}
}
}