Files
ft/Client/Assets/Scripts/EventCubeMelt/CellRenderer.cs
2026-06-29 21:18:33 +08:00

471 lines
16 KiB
C#

// 单元格渲染
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using DG.Tweening;
using Spine.Unity;
using UnityEngine;
using UnityEngine.Serialization;
using UnityEngine.UI;
namespace EventCubeMelt
{
public class CellRenderer : MonoBehaviour
{
// 消除特效
[SerializeField] private Transform fxDisAppear;
//
[SerializeField]
private float animationDuration = 0.5f;
public int Row { get; set; }
public int Col { get; set; }
private SkeletonGraphic skeletonGraphic;
//
private Dictionary<CellType, Transform> _dictionary;
// 当前的CellType
public CellType CellTypeValue { get; private set; } = CellType.Empty;
// 当前的 GhostValue
public CellType CellGhostTypeValue { get; set; } = CellType.Empty;
// 阴影
private Transform _shadowTrans;
// 默认显示的Transform
private CanvasGroup _canvasGroup;
private Transform _normalTransform;
private Transform _disappearEffect;
private bool _isDisappear;
//-阴影
private ShadowType _shadowType = ShadowType.None;
private void Awake()
{
// _meshRenderer = GetComponent<MeshRenderer>();
// _propertyBlock = new MaterialPropertyBlock();
skeletonGraphic = transform.Find("MaterialCube_88/spine").GetComponent<SkeletonGraphic>();
_dictionary = new Dictionary<CellType, Transform>
{
{ CellType.Empty, transform.Find("EmptyCube") },
{ CellType.Ct99, transform.Find("NormalCube") },
{ CellType.Ct1, transform.Find("MaterialCube_01") },
{ CellType.Ct2, transform.Find("MaterialCube_02") },
{ CellType.Ct3, transform.Find("MaterialCube_03") },
{ CellType.Ct4, transform.Find("MaterialCube_04") },
{ CellType.Ct5, transform.Find("MaterialCube_05") },
{ CellType.Ct6, transform.Find("MaterialCube_06") },
{ CellType.Ct88 ,transform.Find("MaterialCube_88")},
// { CellType.Wall ,transform.Find("EmptyCube")},
};
_normalTransform = transform.Find("NormalCube");
_canvasGroup = GetComponent<CanvasGroup>();
// if (fxDisAppear)
// {
// _disappearEffect = Instantiate(fxDisAppear,transform);
// _disappearEffect?.gameObject.SetActive(false);
// }
_shadowTrans = transform.Find("shadow");
_isDisappear = false;
}
public void UpdatePieceVisual(CellType cellType,CellType left = CellType.Empty,CellType leftDown = CellType.Empty,CellType down = CellType.Empty)
{
//
if (_shadowTrans)
{
_shadowTrans.localScale = Vector3.one * 0.45f;
}
foreach (var (key, theTransForm) in _dictionary)
{
var visual = key == cellType;
theTransForm?.gameObject.SetActive(visual);
var image = theTransForm?.GetComponent<Image>();
if (image)
{
image.color = Color.white;
}
var material = theTransForm?.Find("Material")?.GetComponent<Image>();
if (material)
{
material.color = Color.white;
}
}
#region
// if (cellType == CellType.Empty)
// {
// _normalTransform.gameObject.SetActive(true);
// _normalTransform.GetComponent<Image>().color = new Color(0.5f, 1, 0, 0.2f);
// }
#endregion
if (cellType != CellType.Empty && cellType != CellType.Illegal)
{
UpdatePieceShadow(left, leftDown, down);
}
else
{
SetShadowAndDisplay(ShadowType.None);
}
// values = cellType.ToString();
CellTypeValue = cellType;
}
// 更新显示
public void UpdateVisual(CellType cellType,CellType left = CellType.Empty,CellType leftDown = CellType.Empty,CellType down = CellType.Empty)
{
foreach (var (key, theTransForm) in _dictionary)
{
// keyValuePair.Value?.gameObject.SetActive(keyValuePair.Key == cellType);
var visual = key == cellType;
theTransForm?.gameObject.SetActive(visual);
var image = theTransForm?.GetComponent<Image>();
if (image)
{
image.color = Color.white;
}
var material = theTransForm?.Find("Material")?.GetComponent<Image>();
if (material)
{
material.color = Color.white;
}
}
if (cellType != CellType.Empty && cellType != CellType.Illegal)
{
UpdateShadow(left, leftDown, down);
}
else
{
SetShadowAndDisplay(ShadowType.None);
}
// values = cellType.ToString();
CellTypeValue = cellType;
}
private void UpdatePieceShadow(CellType left, CellType leftDown, CellType down)
{
if (left == CellType.Illegal)
left = CellType.Empty;
if (leftDown == CellType.Illegal)
leftDown = CellType.Empty;
if (down == CellType.Illegal)
down = CellType.Empty;
if (left == CellType.Empty && leftDown == CellType.Empty && down == CellType.Empty)
{
SetShadowAndDisplay(ShadowType.LeftBottom);
}
else if (left == CellType.Empty && down == CellType.Empty)
{
SetShadowAndDisplay(ShadowType.LeftAndBottom);
}
else if (left == CellType.Empty)
{
SetShadowAndDisplay(ShadowType.Left);
}
else if (down == CellType.Empty)
{
SetShadowAndDisplay(ShadowType.Bottom);
}
else
{
SetShadowAndDisplay(ShadowType.None);
}
}
private void UpdateShadow(CellType left, CellType leftDown, CellType down)
{
if (left == CellType.Empty && leftDown == CellType.Empty && down == CellType.Empty)
{
SetShadowAndDisplay(ShadowType.LeftBottom);
}
else if (left == CellType.Empty && down == CellType.Empty)
{
SetShadowAndDisplay(ShadowType.LeftAndBottom);
}
else if (left == CellType.Empty)
{
SetShadowAndDisplay(ShadowType.Left);
}
else if (down == CellType.Empty)
{
SetShadowAndDisplay(ShadowType.Bottom);
}
else
{
SetShadowAndDisplay(ShadowType.None);
}
}
public void SetShadowAndDisplay( ShadowType sdType)
{
_shadowType = sdType;
OnShadowDisplay();
}
public RectTransform GetAniTransform()
{
return (RectTransform)transform;
}
public void OnShadowDisplay()
{
var left = _shadowTrans.Find("shadowleft");
var bottom = _shadowTrans.Find("shadowbottom");
var leftBottom = _shadowTrans.Find("shadowleftbottom");
left.gameObject.SetActive(false);
bottom.gameObject.SetActive(false);
leftBottom.gameObject.SetActive(false);
switch (_shadowType)
{
case ShadowType.Left:
left.gameObject.SetActive(true);
break;
case ShadowType.Bottom:
bottom.gameObject.SetActive(true);
break;
case ShadowType.LeftAndBottom:
left.gameObject.SetActive(true);
bottom.gameObject.SetActive(true);
break;
case ShadowType.LeftBottom:
leftBottom.gameObject.SetActive(true);
break;
case ShadowType.None:
break;
default:
throw new ArgumentOutOfRangeException();
}
}
// 更新GhostVisual
public void UpdateGhostVisual()
{
foreach (var (key, theTransForm) in _dictionary)
{
var visual = (key == CellGhostTypeValue);
theTransForm?.gameObject.SetActive(visual);
var image = theTransForm?.GetComponent<Image>();
if (image)
image.color = visual ? new Color(1f, 1f, 1f, 0.5f) : Color.white;
var material = theTransForm?.Find("Material")?.GetComponent<Image>();
if (material)
{
material.color = visual ? new Color(1f, 1f, 1f, 0.5f) : Color.white;
}
}
}
//
#region
// 消失带动画
private bool _runningClearAnimation;
private float delayDuration = 0.1f;
public async Task RunClearAnimation(int iNum,bool delay = false )
{
ELog("RunClearAnimation");
if (_runningClearAnimation)
return;
_runningClearAnimation = true;
var sequence = DOTween.Sequence();
if (delay)
{
sequence.AppendInterval(delayDuration * iNum); // delayDura
}
// var tween1 = transform.DOScale(Vector3.zero, animationDuration).SetEase(Ease.OutBack);
// var tween2 = _canvasGroup.DOFade(0,animationDuration).SetEase(Ease.OutBack).OnComplete(EndAction);
// 然后同时添加两个动画
if (CellTypeValue == CellType.Ct88)
{
// skeletonGraphic.AnimationState.SetAnimation(0, "open", false);
// sequence.AppendInterval(1f);
}
else
{
sequence.Append(transform.DOScale(Vector3.zero, animationDuration).SetEase(Ease.OutBack));
sequence.Join(_canvasGroup.DOFade(0, animationDuration).SetEase(Ease.OutBack));
}
sequence.OnComplete(EndAction);
// await Awaiters.Seconds(animationDuration + delayDuration * iNum);
await sequence.AsyncWaitForCompletion();
return;
void EndAction()
{
ELog("RunClearAnimation EndAction 111");
skeletonGraphic?.AnimationState?.ClearTracks();
skeletonGraphic?.Skeleton?.SetToSetupPose();
if (CellTypeValue != CellType.Ct88)
{
UpdatePieceVisual(CellType.Empty);
}
transform.localScale = Vector3.one;
_runningClearAnimation = false;
_canvasGroup.alpha = 1;
}
}
//
public async Task RunBombAnimation(int iNum, bool delay = false,float delayTime = 0f)
{
ELog("RunBombAnimationStart");
if (_runningClearAnimation)
return;
_runningClearAnimation = true;
var sequence = DOTween.Sequence();
if (delay)
{
sequence.AppendInterval(delayTime); // delayDura
}
// if (CellTypeValue == CellType.Ct88)
// {
skeletonGraphic.AnimationState.SetAnimation(0, "open", false);
sequence.AppendInterval(1f);
// }
sequence.OnComplete(EndAction);
// await Awaiters.Seconds(animationDuration + delayDuration * iNum);
await sequence.AsyncWaitForCompletion();
return;
void EndAction()
{
ELog("RunBombAnimation EndAction 111");
skeletonGraphic?.AnimationState?.ClearTracks();
skeletonGraphic?.Skeleton?.SetToSetupPose();
UpdatePieceVisual(CellType.Empty);
transform.localScale = Vector3.one;
_runningClearAnimation = false;
_canvasGroup.alpha = 1;
}
}
// public void AddDisappearEffect()
// {
// //-
// if (_isDisappear) return;
// _isDisappear = true;
//
// async void Action()
// {
// _disappearEffect.gameObject.SetActive(true);
// transform.localScale = Vector3.one;
// await Awaiters.Seconds(0.8f);
// _disappearEffect?.gameObject.SetActive(false);
// }
//
// transform.DOScale(Vector3.zero, 0.5f).SetEase(Ease.OutBack).OnComplete(Action);
// }
// public async void DoClear()
// {
// if (!_isDisappear)
// {
// return;
// }
// _isDisappear = false;
// transform.localScale = Vector3.one;
// // // 等待Next
// // await Awaiters.Seconds(0.8f);
// // // 消失
// // _disappearEffect?.gameObject.SetActive(false);
//
// }
#endregion
// public void InitForPiece()
// {
// _shadowTrans.localScale = Vector3.one * 0.45f;
// }
//
#region Log
private static void ELog(string message)
{
#if UNITY_EDITOR
if (!EventCubeMeltLogPolicy.EnableELog) return;
Debug.Log($"<color=orange>CellRender => {message}</color>");
#endif
}
#endregion
private Transform fxBombAni;
public async Task RunBombAni(int value)
{
ELog($"RunBombAni {value}");
UpdateVisual((CellType)value);
// await Awaiters.NextFrame;
// if (!fxBombAni)
// {
// fxBombAni = Instantiate(fx,transform);
// fxBombAni?.gameObject.SetActive(false);
// }
//
// if (fxBombAni)
// {
// await Awaiters.NextFrame;
// fxBombAni.gameObject.SetActive(true);
// await Awaiters.Seconds(0.5f);
// fxBombAni.gameObject.SetActive(false);
// }
// var sequence = DOTween.Sequence();
// // if (delay)
// // {
// // sequence.AppendInterval(delayDuration * iNum); // delayDura
// // }
// sequence.Join(_canvasGroup.DOFade(0, animationDuration).SetEase(Ease.OutBack));
// sequence.OnComplete(EndAction);
//
// void EndAction()
// {
// Debug.Log("RunBombAni EndAction 111");
// // skeletonGraphic?.AnimationState?.ClearTracks();
// // skeletonGraphic?.Skeleton?.SetToSetupPose();
// // UpdatePieceVisual(CellType.Empty);
// transform.localScale = Vector3.one;
// _runningClearAnimation = false;
// _canvasGroup.alpha = 1;
// }
}
}
}