using DG.Tweening;
using UnityEngine;
namespace AibisDream
{
public class CoolingMachineViewer : MonoBehaviour, ICoolingMachine
{
[Header("引用")] public GameObject bottleSlot;
public CoolingBottle bottle;
public LockSlider lockSlider;
public Transform pumpHead;
private SpriteButton _button;
[Header("参数")] public float pumpDuration = 3f;
public float resetDuration = 0.5f;
public float pumpHeadUpY;
public float pumpHeadDownY;
#region 状态
[Header("状态")] public bool isMachineEmpty;
public bool isLocked;
public bool inLiquidChange;
#endregion
#region 对外暴露
public void LinkRobot()
{
// TODO 这里是应该ta来控制吗?存疑
isMachineEmpty = true;
}
///
/// 泵出冷却液
///
public void PumpCooling()
{
inLiquidChange = true;
// 液面上升
bottle.LiquidUp(pumpDuration);
// 泵头下降
var sq = GenePumpHeadAnime();
sq.OnComplete(() =>
{
inLiquidChange = false;
isMachineEmpty = false;
});
}
public void UnlockBottle()
{
// 动画效果拉杆处理了
// 触发锁定逻辑
isMachineEmpty = false;
isLocked = false;
bottle?.Unlock();
lockSlider.DisableHook();
}
public void MoveOutBottle()
{
if (bottle == null) return;
// 瓶子置空
bottle.transform.parent = null;
// 停止吸附
bottle?.Unsnap();
}
public void InsertBottle(CoolingBottle newBottle)
{
// 改变瓶子归属
newBottle.transform.parent = bottleSlot.transform;
bottle = newBottle;
// 插入瓶子
bottle.Snap(bottleSlot.transform.position);
}
public void LockBottle()
{
// 动画效果在拉杆的地方就处理了
// 触发锁定逻辑
isMachineEmpty = !bottle || bottle.isEmpty;
isLocked = true;
bottle?.Lock();
// 如果当前没有瓶子,就打开卡扣碰撞
if (!bottle) lockSlider.EnableHook();
}
public void InjectCool()
{
inLiquidChange = true;
// 泵头下降
var sq = GenePumpHeadAnime();
sq.OnComplete(() =>
{
inLiquidChange = false;
isMachineEmpty = true;
});
// 液面上升
bottle.LiquidDown(pumpDuration);
}
public void UnlinkRobot()
{
throw new System.NotImplementedException();
}
public bool CanPump()
{
if (inLiquidChange)
{
Debug.Log("泵机正在工作");
return false;
}
if (!isLocked)
{
Debug.Log("卡扣尚未锁定");
return false;
}
if (!bottle)
{
Debug.Log("没有瓶子");
return false;
}
return true;
}
#endregion
private Sequence GenePumpHeadAnime()
{
var sequence = DOTween.Sequence();
// 下降动画
var pumpDownPos = new Vector3(pumpHead.position.x, pumpHeadDownY + transform.position.y,
pumpHead.position.z);
sequence.Append(pumpHead.DOMove(pumpDownPos, pumpDuration));
// 上升动画
var pumpUpPos = new Vector3(pumpHead.position.x, pumpHeadUpY + transform.position.y, pumpHead.position.z);
sequence.Append(pumpHead.DOMove(pumpUpPos, resetDuration));
return sequence;
}
}
///
/// 冷却系统有什么样的行为?
///
public interface ICoolingMachine
{
void LinkRobot();
void PumpCooling();
void UnlockBottle();
void MoveOutBottle();
void InsertBottle(CoolingBottle bottle);
void LockBottle();
void InjectCool();
void UnlinkRobot();
}
}