138 lines
4.0 KiB
C#
138 lines
4.0 KiB
C#
using System;
|
|
using System.Collections;
|
|
using AibisDream.Kit;
|
|
using DG.Tweening;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
|
|
namespace AibisDream.FixSystem
|
|
{
|
|
public class MoleModule : BodyModule
|
|
{
|
|
#region 效果组件
|
|
|
|
private Material _picMaterial;
|
|
private TMP_Text _screenText;
|
|
private SpriteRenderer _processBar;
|
|
|
|
#endregion
|
|
|
|
public bool isInMoleGame;
|
|
public bool isLightOn;
|
|
private static readonly int SineGlowFade = Shader.PropertyToID("_SineGlowFade");
|
|
private static readonly int AddColorFade = Shader.PropertyToID("_AddColorFade");
|
|
private static readonly int AddColorColor = Shader.PropertyToID("_AddColorColor");
|
|
|
|
private float _countDownPct;
|
|
private Tween _countDownTween;
|
|
|
|
public event Action<MoleModule, bool> OnLightMoleEnd;
|
|
|
|
protected override void InitReference()
|
|
{
|
|
base.InitReference();
|
|
_picMaterial = transform.Find("Module Pic").GetComponent<SpriteRenderer>().material;
|
|
_screenText = transform.Find("Screen Text").GetComponent<TMP_Text>();
|
|
_processBar = transform.Find("Process Bar").GetComponent<SpriteRenderer>();
|
|
}
|
|
|
|
// 重写PlugIn和PlugOut就好了
|
|
public override void PlugIn()
|
|
{
|
|
if (!isInMoleGame)
|
|
{
|
|
base.PlugIn();
|
|
}
|
|
else if (isLightOn)
|
|
{
|
|
// 打中了
|
|
HitMole();
|
|
}
|
|
}
|
|
|
|
public override void PlugOut()
|
|
{
|
|
// TODO 拔出来会有效果吗?存疑
|
|
}
|
|
|
|
#region 表现相关的功能
|
|
|
|
public void StartFlicker()
|
|
{
|
|
_screenText.text = "Checking...";
|
|
_picMaterial.SetFloat(SineGlowFade, 1);
|
|
}
|
|
|
|
public void StopFlicker()
|
|
{
|
|
_picMaterial.SetFloat(SineGlowFade, 0);
|
|
}
|
|
|
|
public void LightOn(float lightTime)
|
|
{
|
|
// 视觉部分
|
|
StartFlicker();
|
|
_picMaterial.SetColor(AddColorColor, Color.red);
|
|
_picMaterial.SetFloat(AddColorFade, 1);
|
|
_processBar.color = Color.red;
|
|
_processBar.transform.localScale = Vector3.one;
|
|
|
|
// 逻辑部分
|
|
isLightOn = true;
|
|
_countDownPct = 100;
|
|
_countDownTween = DOTween.To(() => _countDownPct, value => _countDownPct = value, 0, lightTime)
|
|
.OnUpdate(CountingDown)
|
|
.OnComplete(OverTime);
|
|
}
|
|
|
|
private void CountingDown()
|
|
{
|
|
// TODO 显示倒计时
|
|
_screenText.text = $"{_countDownPct:F1}%";
|
|
_processBar.transform.localScale = new Vector3(_countDownPct / 100, 1, 1);
|
|
}
|
|
|
|
private void OverTime()
|
|
{
|
|
// TODO 超时
|
|
isLightOn = false;
|
|
LightOff();
|
|
OnLightMoleEnd?.Invoke(this, false);
|
|
OnLightMoleEnd = null;
|
|
_countDownTween = null;
|
|
}
|
|
|
|
private void LightOff()
|
|
{
|
|
_picMaterial.SetFloat(AddColorFade, 0);
|
|
_processBar.color = Color.clear;
|
|
// TODO 继续闪烁
|
|
StartFlicker();
|
|
}
|
|
|
|
private void HitMole()
|
|
{
|
|
_countDownTween.Kill();
|
|
isLightOn = false;
|
|
// 命中后成功反馈
|
|
ActionKit.Sequence()
|
|
.Callback(() =>
|
|
{
|
|
if (ColorUtility.TryParseHtmlString("#67C23A", out var color))
|
|
{
|
|
_picMaterial.SetColor(AddColorColor, color);
|
|
}
|
|
})
|
|
.Callback(() => _processBar.color = Color.green)
|
|
.Callback(() => _screenText.text = "Success")
|
|
.Delay(0.7f)
|
|
.Callback(LightOff)
|
|
.Start(this);
|
|
|
|
OnLightMoleEnd?.Invoke(this, true);
|
|
OnLightMoleEnd = null;
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
} |