91 lines
2.6 KiB
C#
91 lines
2.6 KiB
C#
using AibisDream.Framework;
|
|
using UnityEngine;
|
|
|
|
namespace AibisDream
|
|
{
|
|
public class LockSlider : MonoBehaviour, IMoveable
|
|
{
|
|
private Transform _drawBar;
|
|
private Transform _hook;
|
|
private CoolingMachineViewer _coolingMachine;
|
|
private bool _isLocked;
|
|
|
|
private float Value
|
|
{
|
|
set
|
|
{
|
|
var sliderY = value * (offPos.position.y - onPos.position.y) + onPos.position.y;
|
|
transform.position = new Vector3(_drawBar.position.x, sliderY, _drawBar.position.z);
|
|
var hookY = value * (lockOffPos.position.y - lockOnPos.position.y) + lockOnPos.position.y;
|
|
_hook.position = new Vector3(_hook.position.x, hookY, _hook.position.z);
|
|
}
|
|
}
|
|
|
|
public Transform onPos;
|
|
public Transform offPos;
|
|
|
|
public Transform lockOnPos;
|
|
public Transform lockOffPos;
|
|
|
|
public bool IsBlock => _coolingMachine.inLiquidChange;
|
|
|
|
private void Awake()
|
|
{
|
|
_coolingMachine = GetComponentInParent<CoolingMachineViewer>();
|
|
_drawBar = transform;
|
|
_hook = transform.parent.Find("活动卡扣");
|
|
_isLocked = true;
|
|
}
|
|
|
|
public void StartMove()
|
|
{
|
|
}
|
|
|
|
public void Move(Vector2 mousePos)
|
|
{
|
|
var curY = Mathf.Clamp(mousePos.y, onPos.position.y, offPos.position.y);
|
|
|
|
Value = (curY - onPos.position.y) / (offPos.position.y - onPos.position.y);
|
|
}
|
|
|
|
public void StopMove()
|
|
{
|
|
var curValue = (_drawBar.position.y - onPos.position.y) / (offPos.position.y - onPos.position.y);
|
|
// 大于0.8或者小于0.2则切换状态,否则弹回去
|
|
_isLocked = curValue switch
|
|
{
|
|
// 移动停止时尝试吸附
|
|
>= 0.8f => false,
|
|
<= 0.2f => true,
|
|
_ => _isLocked
|
|
};
|
|
|
|
Value = _isLocked ? 0 : 1;
|
|
// 判定开闭锁
|
|
if (_isLocked)
|
|
{
|
|
_coolingMachine.LockBottle();
|
|
}
|
|
else
|
|
{
|
|
_coolingMachine.UnlockBottle();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 打开卡扣碰撞
|
|
/// </summary>
|
|
public void EnableHook()
|
|
{
|
|
_hook.GetComponent<Collider2D>().enabled = true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 关闭卡扣碰撞
|
|
/// </summary>
|
|
public void DisableHook()
|
|
{
|
|
_hook.GetComponent<Collider2D>().enabled = false;
|
|
}
|
|
}
|
|
} |