You've already forked taptap2024_GJ_chidouren
129 lines
3.9 KiB
C#
129 lines
3.9 KiB
C#
using System;
|
|
using Framework.Timer;
|
|
using Framework.Utils;
|
|
using UnityEngine;
|
|
|
|
namespace Game.Component
|
|
{
|
|
/// <summary>
|
|
/// 双面卡牌容器
|
|
/// </summary>
|
|
public class DoubleCardEntity : MonoBehaviour
|
|
{
|
|
[SerializeField] private Transform _entity; //容器
|
|
[SerializeField] private CardShower _foreCard; //正面卡牌
|
|
[SerializeField] private CardShower _breakCard; //背面卡牌
|
|
|
|
private Transform _camera;
|
|
private TimeHandler _animHandler;
|
|
private int _curCardState;
|
|
private int _targetCardState;
|
|
private Action _callback;
|
|
|
|
private bool _hasFore => Vector3.Dot (this._camera.forward , this._entity.forward) < 0;
|
|
|
|
public bool IsOpen => this._targetCardState == 1;
|
|
|
|
public CardShower ForeCard => this._foreCard;
|
|
|
|
public CardShower BreakCard => this._breakCard;
|
|
|
|
private int CurCardState
|
|
{
|
|
get => this._curCardState;
|
|
set
|
|
{
|
|
if (value != this._curCardState)
|
|
{
|
|
this._curCardState = value;
|
|
if (value == -1)
|
|
{
|
|
this._breakCard.ShowCard ();
|
|
this._foreCard.HideCard ();
|
|
}
|
|
else if (value == 0)
|
|
{
|
|
this._breakCard.HideCard ();
|
|
this._foreCard.HideCard ();
|
|
}
|
|
else if (value == 1)
|
|
{
|
|
this._breakCard.HideCard ();
|
|
this._foreCard.ShowCard ();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
public void Init (Transform cameraTransform , int defaultState = 0)
|
|
{
|
|
this._camera = cameraTransform;
|
|
this._curCardState = -2;
|
|
SetTargetCard (defaultState, 0 , null);
|
|
}
|
|
|
|
public void ToFore (float duration , Action callback = null)
|
|
{
|
|
SetTargetCard (1 , duration , callback);
|
|
}
|
|
|
|
public void ToBreak (float duration, Action callback = null)
|
|
{
|
|
SetTargetCard (-1 , duration , callback);
|
|
}
|
|
|
|
private void SetTargetCard (int target, float duration , Action callback)
|
|
{
|
|
if (this.CurCardState == target && (this._animHandler == null || this._animHandler.IsDone))
|
|
{
|
|
callback?.Invoke ();
|
|
return;
|
|
}
|
|
|
|
this._animHandler?.Kill ();
|
|
this._callback = callback;
|
|
this._targetCardState = target;
|
|
this._animHandler = GameUpdateMgr.Instance.CreateTimer (duration , UpdateCardComplete , UpdateCard);
|
|
}
|
|
|
|
private void UpdateCardComplete ()
|
|
{
|
|
var targetEuler = this._targetCardState == 1 ? 0 : 180;
|
|
this._entity.eulerAngles = new Vector3 (0 , targetEuler , 0);
|
|
CheckCard ();
|
|
this._callback?.Invoke ();
|
|
}
|
|
|
|
private void UpdateCard (float progress)
|
|
{
|
|
var targetEuler = -180;
|
|
progress = this._targetCardState == 1 ? 1 - progress : progress;
|
|
var euler = targetEuler * progress;
|
|
this._entity.eulerAngles = new Vector3 (0 , euler , 0);
|
|
CheckCard ();
|
|
}
|
|
|
|
public void CheckCard ()
|
|
{
|
|
if (this._hasFore)
|
|
{
|
|
//背面
|
|
this.CurCardState = -1;
|
|
}
|
|
else
|
|
{
|
|
//正面
|
|
this.CurCardState = 1;
|
|
}
|
|
}
|
|
|
|
public void ResetData ()
|
|
{
|
|
this._animHandler?.Kill ();
|
|
this._curCardState = -2;
|
|
this._targetCardState = 0;
|
|
this._callback = null;
|
|
}
|
|
}
|
|
} |