冷却液完整版

This commit is contained in:
2024-12-24 21:43:10 +08:00
parent be6ac8b694
commit e70ef14143
29 changed files with 3802 additions and 1574 deletions
@@ -9,6 +9,7 @@ namespace AibisDream.FixSystem
public event Action<CoolingBottle> InsertSlotEvent;
public CoolingBottleData initBottleData;
private CoolingBottle _curBottle;
private GameObject _bottlePrefab;
private bool _inShelf;
@@ -32,6 +33,7 @@ namespace AibisDream.FixSystem
if (string.IsNullOrEmpty(initBottleData.name)) return;
var newBottle = Instantiate(_bottlePrefab, transform).GetComponent<CoolingBottle>();
_curBottle = newBottle;
// 加载瓶子数据
newBottle.Load(initBottleData);
newBottle.curSnapSlot = this;
@@ -43,11 +45,13 @@ namespace AibisDream.FixSystem
/// <param name="other"></param>
private void OnTriggerEnter2D(Collider2D other)
{
Debug.Log("Enter: " + other.gameObject.name);
// 如果当前Slot里已经有瓶子了,就返回
if (_curBottle != null) return;
// 尝试获取瓶子
if (other.gameObject.TryGetComponent<CoolingBottle>(out var bottle))
{
bottle.targetSnapSlot = this;
bottle.targetSnapSlotSet.Add(this);
}
}
@@ -57,23 +61,52 @@ namespace AibisDream.FixSystem
/// <param name="other"></param>
private void OnTriggerExit2D(Collider2D other)
{
Debug.Log("Exit: " + other.gameObject.name);
// 尝试获取瓶子
if (other.gameObject.TryGetComponent<CoolingBottle>(out var bottle))
{
bottle.targetSnapSlot = null;
bottle.targetSnapSlotSet.Remove(this);
}
}
/// <summary>
/// 移除当前瓶子
/// </summary>
public void RemoveBottle()
{
_curBottle = null;
}
/// <summary>
/// 插入时触发
/// </summary>
/// <param name="coolingBottle"></param>
public void InsertSlot(CoolingBottle coolingBottle)
{
coolingBottle.transform.parent = transform;
coolingBottle.SwitchColliderSize(_inShelf);
_curBottle = coolingBottle;
// 在架子上才正过来
if (_inShelf)
{
coolingBottle.Rotate(true);
}
InsertSlotEvent?.Invoke(coolingBottle);
}
/// <summary>
/// 是否有冷却液
/// </summary>
/// <returns>瓶子</returns>
public CoolingBottle GetCurBottle()
{
return _curBottle;
}
public void DestroyBottle()
{
if (_curBottle)
{
_curBottle = null;
Destroy(_curBottle);
}
}
}
}
@@ -1,28 +1,34 @@
using System;
using System.Collections;
using System.Collections.Generic;
using AibisDream.Framework;
using DG.Tweening;
using UnityEngine;
namespace AibisDream.FixSystem
{
[RequireComponent(typeof(Collider2D))]
[RequireComponent(typeof(Collider2D), typeof(Rigidbody2D))]
public class CoolingBottle : MonoBehaviour, IMoveable
{
private const float RotateTime = 0.3f;
private const float SnapMoveTime = 0.3f;
private static readonly Vector2 VerticalBox = new(1, 3.11f);
private static readonly Vector2 HorizontalBox = new(4.7f, 1.65f);
private static readonly Vector3 ClockwiseAngle = new(0, 0, 0);
private static readonly Vector3 ClockWiseScale = new(1, 1, 1);
private static readonly Vector3 AnticlockwiseAngle = new(0, 0, 90);
private static readonly Vector3 AnticlockwiseScale = new(1.45f, 1.45f, 1);
public CoolingBottleData data;
#region
private BoxCollider2D _collider;
private SpriteRenderer _verticalSprite;
private SpriteRenderer _horizontalSprite;
private Sprite _verticalSprite;
private Sprite _horizontalSprite;
public BottleSlot curSnapSlot;
public BottleSlot targetSnapSlot;
public readonly HashSet<BottleSlot> targetSnapSlotSet = new();
private Rigidbody2D _rb;
private SpriteRenderer _renderer;
#endregion
@@ -33,60 +39,94 @@ namespace AibisDream.FixSystem
private void InitRefs()
{
_collider = GetComponent<BoxCollider2D>();
_collider.size = VerticalBox;
_verticalSprite = transform.Find("竖立图片").GetComponent<SpriteRenderer>();
_horizontalSprite = transform.Find("水平图片").GetComponent<SpriteRenderer>();
_rb = GetComponent<Rigidbody2D>();
_renderer = GetComponent<SpriteRenderer>();
}
#region
public void LiquidDown()
{
}
public void LiquidUp()
{
}
#endregion
#region
public bool IsBlock { get; set; }
public void StartMove()
{
_rb.isKinematic = false;
curSnapSlot.RemoveBottle();
// 开始移动时就切换成横版
Rotate(false);
}
public void Rotate(bool isVertical)
{
StartCoroutine(RotateAsync(isVertical));
}
/// <summary>
/// 旋转瓶子
/// </summary>
/// <param name="isVertical">是否竖直</param>
/// <returns></returns>
private IEnumerator RotateAsync(bool isVertical)
{
var angle = isVertical ? ClockwiseAngle : AnticlockwiseAngle;
var scale = isVertical ? ClockWiseScale : AnticlockwiseScale;
// 动画序列
var sq = DOTween.Sequence();
Tween rotate = transform.DORotate(angle, RotateTime, RotateMode.FastBeyond360);
sq.Join(rotate);
Tween scaleTw = transform.DOScale(scale, RotateTime);
sq.Join(scaleTw);
while (sq.active && !sq.IsComplete())
{
yield return null;
}
}
public void Move(Vector2 mousePos)
{
transform.position = mousePos;
_rb.MovePosition(mousePos);
}
public void StopMove()
{
if (targetSnapSlot)
if (TryFindClosestSlot(out var targetSlot))
{
curSnapSlot = targetSnapSlot;
curSnapSlot = targetSlot;
}
targetSnapSlot = null;
targetSnapSlotSet.Clear();
// 吸附到目标位置
_rb.isKinematic = true;
transform.DOMove(curSnapSlot.transform.position, SnapMoveTime);
curSnapSlot.InsertSlot(this);
// TODO 停止描边
}
/// <summary>
/// 切换碰撞箱形状
/// </summary>
/// <param name="isVertical">是否水平</param>
public void SwitchColliderSize(bool isVertical)
public bool TryFindClosestSlot(out BottleSlot targetSlot)
{
_collider.size = isVertical ? VerticalBox : HorizontalBox;
// 没有目标Slot,返回
if (targetSnapSlotSet.Count == 0)
{
targetSlot = null;
return false;
}
// 寻找最近的Slot
var shortestDistance = float.MaxValue;
BottleSlot closestSlot = null;
foreach (var slot in targetSnapSlotSet)
{
var distance = Vector3.Distance(slot.transform.position, transform.position);
if (distance < shortestDistance)
{
shortestDistance = distance;
closestSlot = slot;
}
}
targetSlot = closestSlot;
return true;
}
#endregion
@@ -94,8 +134,8 @@ namespace AibisDream.FixSystem
public void Load(CoolingBottleData targetData)
{
data = targetData;
_verticalSprite.sprite = ResourceKit.LoadSpriteFromPsd($"Art/Fix/Cooling/冷却系统/{data.verticalSprite}");
_horizontalSprite.sprite = ResourceKit.LoadSpriteFromPsd($"Art/Fix/Cooling/冷却系统/{data.horizontalSprite}");
_verticalSprite = ResourceKit.LoadSpriteFromPsd($"Art/Fix/Cooling/冷却系统/{data.verticalSprite}");
_renderer.sprite = _verticalSprite;
}
}
@@ -1,6 +1,7 @@
using System.Collections;
using AibisDream.FixSystem;
using UnityEngine;
using Yarn.Unity;
namespace AibisDream.Framework
{
@@ -18,9 +19,9 @@ namespace AibisDream.Framework
public bool isLocked;
#endregion
private CoolingBottle CurBottle => _pumpUI.bottleSlot.GetCurBottle();
public CoolingBottle curBottle;
#endregion
private void Awake()
{
@@ -33,6 +34,8 @@ namespace AibisDream.Framework
_pumpUI = transform.Find("Pump Part").GetComponent<PumpController>();
_bottomUI = transform.Find("Bottom Part").GetComponent<BottleShelf>();
_tubeUI = transform.Find("Tube Part").GetComponent<TubeController>();
isLocked = true;
}
#region
@@ -57,48 +60,60 @@ namespace AibisDream.Framework
public void Unlock()
{
isLocked = false;
if (curBottle)
if (CurBottle)
{
curBottle.IsBlock = false;
CurBottle.IsBlock = false;
}
_pumpUI.OpenBuckle();
}
public void InsertBottle(CoolingBottle bottle)
{
// 设置为当前Bottle
curBottle = bottle;
// 插入似乎没有太多逻辑?
}
public void Lock()
{
isLocked = true;
// 已锁定的瓶子禁止使用
if (curBottle)
if (CurBottle)
{
curBottle.IsBlock = true;
CurBottle.IsBlock = true;
}
_pumpUI.CloseBuckle();
}
public void StartPump()
{
throw new System.NotImplementedException();
// 进行泵机操作
if (isLocked && CurBottle)
{
// 播放pump动画
_pumpUI.PlayPumpAnime();
// 播放泵机运行对话
// DialogController.Instance.StartDialogNode($"泵机运行_{CurBottle.data.name}");
Debug.Log($"泵机运行_{CurBottle.data.name}");
}
}
public void CloseBottleUI()
[YarnCommand("close_bottle_ui")]
public IEnumerator CloseBottleUI()
{
throw new System.NotImplementedException();
yield return _bottomUI.Hide();
}
[YarnCommand("close_cooling_system")]
public IEnumerator CloseCoolingSystem()
{
yield return _pumpUI.Hide();
_pumpUI.StopPumpAnime();
_pumpUI.DestroyBottle();
}
public void CloseCoolingSystem()
[YarnCommand("pull_out_tube")]
public IEnumerator PullOutTube()
{
throw new System.NotImplementedException();
}
public void PullOutTube()
{
throw new System.NotImplementedException();
yield return _tubeUI.PlayPullOutAnime();
}
#endregion
@@ -121,10 +136,10 @@ namespace AibisDream.Framework
// 点击泵机开关
public void StartPump();
// 冷却液架收回
public void CloseBottleUI();
public IEnumerator CloseBottleUI();
// 冷却系统UI收回
public void CloseCoolingSystem();
public IEnumerator CloseCoolingSystem();
// 拔管
public void PullOutTube();
public IEnumerator PullOutTube();
}
}
@@ -2,13 +2,13 @@
using AibisDream.Framework;
using DG.Tweening;
using UnityEngine;
using UnityEngine.Serialization;
namespace AibisDream.FixSystem
{
public class PumpController : MonoBehaviour
{
[Header("整体位置")]
public Vector3 hidePos;
[Header("整体位置")] public Vector3 hidePos;
public Vector3 showPos;
public Vector3 topBuckleUnlockPos;
@@ -17,22 +17,28 @@ namespace AibisDream.FixSystem
public Vector3 topBuckleLockPos;
public Vector3 leftBuckleLockPos;
public Vector3 rightBuckleLockPos;
[Header("移动用时")]
public float showDuration;
public Vector3 pumpUpPos;
public Vector3 pumpDownPos;
[Header("移动用时")] public float showDuration;
public float buckleDuration;
public float pumpDuration;
#region
private CoolingSystem _coolingSystem;
private SpriteButton _lockButton;
private SpriteButton _pumpButton;
private Transform _topBuckle;
private Transform _leftBuckle;
private Transform _rightBuckle;
private BottleSlot _bottleSlot;
[HideInInspector] public BottleSlot bottleSlot;
private GameObject _lockBox;
private Transform _pumpHead;
#endregion
@@ -48,20 +54,26 @@ namespace AibisDream.FixSystem
{
// 初始化引用
_coolingSystem = GetComponentInParent<CoolingSystem>();
var buckleGroup = transform.Find("按键卡扣组");
_lockButton = new SpriteButton(buckleGroup.Find("Lock Button").gameObject);
_pumpButton = new SpriteButton(buckleGroup.Find("Pump Button").gameObject);
_lockButton.OnButtonDown += LockButtonDown;
_pumpButton.OnButtonDown += PumpButtonDown;
_topBuckle = buckleGroup.Find("上下卡扣");
_leftBuckle = buckleGroup.Find("左卡扣");
_rightBuckle = buckleGroup.Find("右卡扣");
// _bottleSlot = GetComponentInChildren<BottleSlot>();
// _bottleSlot?.InsertSlotEvent += _coolingSystem.InsertBottle;
_lockBox = buckleGroup.Find("锁定占位").gameObject;
_lockBox.SetActive(true);
var pumpGroup = transform.Find("泵机组");
_pumpHead = pumpGroup.Find("泵头");
bottleSlot = GetComponentInChildren<BottleSlot>();
bottleSlot.InsertSlotEvent += _coolingSystem.InsertBottle;
}
#region
@@ -118,11 +130,14 @@ namespace AibisDream.FixSystem
{
yield return null;
}
gameObject.SetActive(false);
}
public void OpenBuckle()
{
_lockBox.SetActive(false);
_topBuckle.DOLocalMove(topBuckleUnlockPos, buckleDuration);
_leftBuckle.DOLocalMove(leftBuckleUnlockPos, buckleDuration);
_rightBuckle.DOLocalMove(rightBuckleUnlockPos, buckleDuration);
@@ -130,11 +145,30 @@ namespace AibisDream.FixSystem
public void CloseBuckle()
{
_lockBox.SetActive(true);
_topBuckle.DOLocalMove(topBuckleLockPos, buckleDuration);
_leftBuckle.DOLocalMove(leftBuckleLockPos, buckleDuration);
_rightBuckle.DOLocalMove(rightBuckleLockPos, buckleDuration);
}
public void PlayPumpAnime()
{
_pumpHead.DOLocalMove(pumpDownPos, pumpDuration).SetLoops(-1, LoopType.Yoyo)
.SetEase(Ease.Linear);
}
public void StopPumpAnime()
{
_pumpHead.DOKill();
_pumpHead.localPosition = pumpUpPos;
}
public void DestroyBottle()
{
bottleSlot.DestroyBottle();
}
#endregion
}
}