This commit is contained in:
2024-12-12 01:07:26 +08:00
parent 2667419d98
commit 3fcff0598a
55 changed files with 1861 additions and 964 deletions
@@ -0,0 +1,44 @@
using UnityEngine;
namespace AibisDream
{
public class BottleSlot : MonoBehaviour
{
private CoolingMachineViewer _coolingMachine;
private void Awake()
{
InitComponentRefs();
}
private void InitComponentRefs()
{
_coolingMachine = GetComponentInParent<CoolingMachineViewer>();
}
private void OnTriggerEnter2D(Collider2D other)
{
if (_coolingMachine.isLocked) return;
if (other.gameObject.TryGetComponent<CoolingBottle>(out var bottle))
{
// 进入时只给吸附标记,松手才吸附
bottle.isSnapped = true;
}
}
private void OnTriggerExit2D(Collider2D other)
{
if (_coolingMachine.isLocked) return;
if (other.gameObject.TryGetComponent<CoolingBottle>(out var bottle))
{
// 离开时直接触发拔出逻辑,不等松手
bottle.isSnapped = false;
// 拔出
_coolingMachine.MoveOutBottle();
}
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: bdcb57f53aa757b45a44803a2441d888
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -1,5 +1,4 @@
using System;
using AibisDream.Framework;
using AibisDream.Framework;
using DG.Tweening;
using UnityEngine;
@@ -7,41 +6,97 @@ namespace AibisDream
{
public class CoolingBottle : MonoBehaviour, IMoveable
{
private GameObject _mask;
public CoolingMachineViewer coolingMachine;
private Transform _mask;
private Rigidbody2D _rb;
private static readonly Vector3 UpPos = new(0, 1.56f, 0);
private static readonly Vector3 UpPos = new(0, 1.86f, 0);
private static readonly Vector3 DownPos = Vector3.zero;
public bool isSnapped;
public bool isInMachine;
public bool isLocked;
public bool isEmpty;
public bool IsBlock { get; }
private void Awake()
{
InitComponentRefs();
}
private void InitComponentRefs()
{
_rb = GetComponent<Rigidbody2D>();
_mask = transform.Find("Mask");
coolingMachine = FindObjectOfType<CoolingMachineViewer>();
_mask.localPosition = isEmpty ? DownPos : UpPos;
}
public void LiquidDown(float duration)
{
_mask.transform.DOMove(DownPos, duration).OnComplete(() => isEmpty = true);
var targetPos = transform.parent.TransformPoint(DownPos);
_mask.transform.DOMove(targetPos, duration).OnComplete(() => isEmpty = true);
}
public void LiquidUp(float duration)
{
_mask.transform.DOMove(UpPos, duration).OnComplete(() => isEmpty = false);
var targetPos = transform.parent.TransformPoint(UpPos);
_mask.transform.DOMove(targetPos, duration).OnComplete(() => isEmpty = false);
}
public void StartMove()
{
}
public void Move(Vector2 mousePos)
{
// 锁定后无法移动
if (isLocked) return;
_rb.MovePosition(mousePos);
}
public void StopMove()
{
// 松手时若是已吸附,就触发插入逻辑
if (isSnapped) coolingMachine.InsertBottle(this);
}
/// <summary>
/// 松手后吸附至某个位置
/// </summary>
/// <param name="snapPos">吸附点位</param>
public void Snap(Vector3 snapPos)
{
// 吸附至指定位置
transform.position = snapPos;
// 停用重力
_rb.gravityScale = 0;
// 瓶子进入机器
isInMachine = true;
}
/// <summary>
/// 离开指定位置就停止吸附
/// </summary>
public void Unsnap()
{
// 重新启用重力
_rb.gravityScale = 1;
// 瓶子不在机器中
isInMachine = false;
}
public void Lock()
{
isLocked = true;
}
public void Unlock()
{
isLocked = false;
}
}
}
@@ -1,17 +1,30 @@
using DG.Tweening;
using UnityEngine;
namespace AibisDream
{
public class CoolingMachineSystem : MonoBehaviour, ICoolingMachine
public class CoolingMachineViewer : MonoBehaviour, ICoolingMachine
{
public bool isMachineEmpty;
public GameObject bottleSlot;
[Header("引用")] public GameObject bottleSlot;
public CoolingBottle bottle;
public GameObject pumpHead;
public GameObject lockSwitch;
public GameObject pumpButton;
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()
@@ -25,30 +38,35 @@ namespace AibisDream
/// </summary>
public void PumpCooling()
{
// 泵头下降
inLiquidChange = true;
// 液面上升
// 完成后液体满了
isMachineEmpty = 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)
@@ -56,25 +74,33 @@ namespace AibisDream
// 改变瓶子归属
newBottle.transform.parent = bottleSlot.transform;
bottle = newBottle;
// 插入瓶子
bottle.Snap(bottleSlot.transform.position);
}
public void LockBottle()
{
// 插槽回旋
// 卡扣下降
// 同步液体状态
isMachineEmpty = bottle.isEmpty;
// TODO 同步泵头状态
// 动画效果在拉杆的地方就处理了
// 触发锁定逻辑
isMachineEmpty = !bottle || bottle.isEmpty;
isLocked = true;
bottle?.Lock();
// 如果当前没有瓶子,就打开卡扣碰撞
if (!bottle) lockSlider.EnableHook();
}
public void InjectCool()
{
throw new System.NotImplementedException();
inLiquidChange = true;
// 泵头下降
var sq = GenePumpHeadAnime();
sq.OnComplete(() =>
{
inLiquidChange = false;
isMachineEmpty = true;
});
// 液面上升
bottle.LiquidDown(pumpDuration);
}
public void UnlinkRobot()
@@ -82,8 +108,44 @@ namespace AibisDream
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;
}
}
/// <summary>
@@ -100,4 +162,4 @@ namespace AibisDream
void InjectCool();
void UnlinkRobot();
}
}
}
@@ -0,0 +1,91 @@
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;
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 9f15b93b1e22b11459ca121bedee9ba7
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,47 @@
using UnityEngine;
namespace AibisDream
{
public class PumpButton : MonoBehaviour
{
private SpriteButton _button;
private CoolingMachineViewer _coolingMachine;
private void Awake()
{
_coolingMachine = GetComponentInParent<CoolingMachineViewer>();
}
private void OnEnable()
{
_button = new SpriteButton(gameObject);
_button.OnButtonDown += OnButtonDown;
}
private void OnDisable()
{
_button.OnButtonDown -= OnButtonDown;
_button = null;
}
private void Update()
{
_button.CheckButtonClick();
}
private void OnButtonDown()
{
if (!_coolingMachine.CanPump()) return;
if (_coolingMachine.isMachineEmpty)
{
_coolingMachine.PumpCooling();
}
else
{
Debug.Log("in");
_coolingMachine.InjectCool();
}
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: c1098201b6934115ac7c06afa1564dab
timeCreated: 1733580710