242 lines
6.5 KiB
C#
242 lines
6.5 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
|
|
namespace game
|
|
{
|
|
public class LongPressStuffBase : MonoBehaviour
|
|
{
|
|
[Tooltip("物品ID")]
|
|
public int ID = 0;
|
|
|
|
[Tooltip("需要消耗的时间")]
|
|
[SerializeField] protected float m_ConsumeTime = 3f;
|
|
|
|
[Tooltip("是否需要呼吸变色功能,默认开启。")]
|
|
public bool m_IsBreathing = true;
|
|
|
|
[Tooltip("需要改变颜色的渲染器")]
|
|
public Renderer[] m_Renderers = null;
|
|
|
|
[Tooltip("吸气时间")]
|
|
public float m_BreatheInTime = 1f;
|
|
|
|
[Tooltip("呼气时间")]
|
|
public float m_BreatheOutTime = 1f;
|
|
|
|
[Tooltip("憋气时间")]
|
|
public float m_HoldBreathTime = 2f;
|
|
|
|
[Tooltip("颜色")]
|
|
public Color m_Color = Color.red;
|
|
|
|
#region 外部接口
|
|
/// <summary>
|
|
/// 进度
|
|
/// </summary>
|
|
public float Progress
|
|
{
|
|
get
|
|
{
|
|
if (m_Status == EventWashingActionStatus.Executing || m_Status == EventWashingActionStatus.Stopping)
|
|
return m_CumulativeTime / m_ConsumeTime;
|
|
else if (m_Status == EventWashingActionStatus.Ending)
|
|
return 1f;
|
|
return 0f;
|
|
}
|
|
}
|
|
|
|
public float CumulativeConsumeTime
|
|
{
|
|
get
|
|
{
|
|
if (m_CumulativeTime < 0f)
|
|
{
|
|
m_CumulativeTime = 0f;
|
|
}
|
|
else if (m_CumulativeTime > m_ConsumeTime)
|
|
{
|
|
m_CumulativeTime = m_ConsumeTime;
|
|
}
|
|
return m_CumulativeTime;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 启动操作
|
|
/// </summary>
|
|
/// <param name="overAction"></param>
|
|
public virtual void StartAction(UnityAction<LongPressStuffBase> overAction)
|
|
{
|
|
m_Status = EventWashingActionStatus.Executing;
|
|
m_OverAction = overAction;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 停止操作
|
|
/// </summary>
|
|
public virtual void StopAction()
|
|
{
|
|
m_Status = EventWashingActionStatus.Stopping;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 设置已经执行过的时间
|
|
/// </summary>
|
|
/// <param name="time"></param>
|
|
public void SetHaveExecutedTime(float time)
|
|
{
|
|
m_CumulativeTime = time;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 设置结束
|
|
/// </summary>
|
|
public void SetOver()
|
|
{
|
|
m_CumulativeTime = m_ConsumeTime;
|
|
}
|
|
|
|
public bool IsOver()
|
|
{
|
|
return m_Status == EventWashingActionStatus.Ending;
|
|
}
|
|
#endregion
|
|
|
|
#region 材质球处理相关
|
|
|
|
private float m_Time = 0f;
|
|
|
|
private Color m_StartColor = Color.white;
|
|
private Color m_GapColor = Color.white;
|
|
|
|
private int m_State = 0;
|
|
|
|
public virtual void StartBreathing()
|
|
{
|
|
m_IsBreathing = true;
|
|
}
|
|
public virtual void StopBreathing()
|
|
{
|
|
m_IsBreathing = false;
|
|
}
|
|
|
|
private void InitMaterials()
|
|
{
|
|
if (m_Renderers == null) return;
|
|
m_GapColor = m_Color - m_StartColor;
|
|
}
|
|
private void UpdateMaterials()
|
|
{
|
|
if (m_Renderers == null || !m_IsBreathing) return;
|
|
|
|
if (m_State == 0)
|
|
{
|
|
m_Time += Time.deltaTime;
|
|
if (m_Time >= m_BreatheInTime)
|
|
{
|
|
m_Time = m_BreatheOutTime;
|
|
m_State = 1;
|
|
}
|
|
}
|
|
else if (m_State == 1)
|
|
{
|
|
m_Time -= Time.deltaTime;
|
|
if (m_Time <= 0f)
|
|
{
|
|
m_Time = 0f;
|
|
m_State = 2;
|
|
}
|
|
}
|
|
else if (m_State == 2)
|
|
{
|
|
m_Time += Time.deltaTime;
|
|
if (m_Time >= m_HoldBreathTime)
|
|
{
|
|
m_Time = 0f;
|
|
m_State = 0;
|
|
}
|
|
}
|
|
|
|
Color color = Color.white;
|
|
if (m_State == 0 || m_State == 1)
|
|
{
|
|
float scale = m_Time / m_BreatheInTime;
|
|
color = m_StartColor + scale * m_GapColor;
|
|
}
|
|
|
|
foreach (var renderer in m_Renderers)
|
|
{
|
|
int count = renderer.materials.Length;
|
|
for (int i = 0; i < count; i++)
|
|
{
|
|
Material material = renderer.materials[i];
|
|
material.SetColor("_BaseColor", color);
|
|
}
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region 事件
|
|
protected UnityAction<LongPressStuffBase> m_OverAction = null; // 结束事件
|
|
#endregion
|
|
|
|
#region 状态&内部使用
|
|
protected EventWashingActionStatus m_Status = EventWashingActionStatus.Invalid; // 状态
|
|
protected float m_CumulativeTime = 0f; // 累计时间
|
|
|
|
protected EventWashingStuffType m_Type = 0; // 物品类型
|
|
|
|
public float ConsumeTime { get => m_ConsumeTime; }
|
|
|
|
public void SetStatus(EventWashingActionStatus status)
|
|
{
|
|
m_Status = status;
|
|
}
|
|
|
|
protected virtual void UpdateState()
|
|
{
|
|
if (m_Status == EventWashingActionStatus.Executing)
|
|
{
|
|
m_CumulativeTime += Time.deltaTime;
|
|
if (m_CumulativeTime >= m_ConsumeTime)
|
|
{
|
|
m_CumulativeTime = m_ConsumeTime;
|
|
m_Status = EventWashingActionStatus.Ending;
|
|
if (m_OverAction != null)
|
|
{
|
|
m_OverAction.Invoke(this);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region 生命周期
|
|
/// <summary>
|
|
/// 初始化
|
|
/// </summary>
|
|
public virtual void InitStuff()
|
|
{
|
|
InitMaterials();
|
|
}
|
|
|
|
// Start is called before the first frame update
|
|
protected virtual void Start()
|
|
{
|
|
InitStuff();
|
|
m_Status = EventWashingActionStatus.Invalid;
|
|
}
|
|
|
|
// Update is called once per frame
|
|
protected virtual void Update()
|
|
{
|
|
UpdateState();
|
|
|
|
UpdateMaterials();
|
|
}
|
|
#endregion
|
|
}
|
|
}
|