feat(tarot): 添加塔罗展示背景压暗

This commit is contained in:
2026-07-19 20:29:01 +08:00
parent bfdcb41116
commit 94a817f5fa
@@ -2,6 +2,7 @@ using System.Collections;
using AibisDream;
using AibisDream.FixSystem;
using AibisDream.Framework;
using DG.Tweening;
using UnityEngine;
namespace AibisDream.MiniGame.Tarot
@@ -16,6 +17,13 @@ namespace AibisDream.MiniGame.Tarot
[SerializeField] private Sprite frontSprite;
[SerializeField] private Sprite backSprite;
[Header("Background Dim")]
[Tooltip("塔罗出现时的背景压暗强度。")]
[SerializeField] [Range(0f, 1f)] private float backgroundDimAlpha = 0.18f;
[Tooltip("背景压暗淡入淡出的时长。")]
[SerializeField] [Min(0f)] private float backgroundDimFadeDuration = 0.25f;
[Header("Dialogue Bubbles")]
[Tooltip("退出塔罗时恢复的气泡槽位;不填则尝试 Outside / BodyModule。")]
[SerializeField] private BubbleSlotGroup restoreBubbleSlotGroup;
@@ -27,6 +35,10 @@ namespace AibisDream.MiniGame.Tarot
private BubbleSlotGroupData _savedBubbleData;
private bool _bubbleSwapActive;
private SpriteRenderer _backgroundDimmer;
private Sprite _backgroundDimSprite;
private Tween _backgroundDimTween;
public TarotDeck Deck => _deck;
public bool IsCardSelected => _isCardSelected;
public int SelectedIndex => _selectedIndex;
@@ -92,6 +104,7 @@ namespace AibisDream.MiniGame.Tarot
_deck.OnCardSelected += OnCardSelected;
TryEnterTarotBubbleMode();
ShowBackgroundDim();
if (backSprite == null)
Debug.LogWarning(
@@ -150,6 +163,8 @@ namespace AibisDream.MiniGame.Tarot
public IEnumerator HideAll(float duration = 0.3f)
{
HideBackgroundDim(duration);
if (_deck != null)
yield return _deck.HideAll(duration);
@@ -170,9 +185,94 @@ namespace AibisDream.MiniGame.Tarot
{
if (_deck != null)
_deck.OnCardSelected -= OnCardSelected;
_backgroundDimTween?.Kill();
if (_backgroundDimSprite != null)
Destroy(_backgroundDimSprite);
RestoreTarotBubbleMode();
}
private void ShowBackgroundDim()
{
EnsureBackgroundDimmer();
if (_backgroundDimmer == null)
return;
UpdateBackgroundDimmerBounds();
_backgroundDimmer.gameObject.SetActive(true);
_backgroundDimTween?.Kill();
_backgroundDimTween = _backgroundDimmer
.DOFade(backgroundDimAlpha, backgroundDimFadeDuration)
.SetEase(Ease.OutQuad);
}
private void HideBackgroundDim(float duration)
{
if (_backgroundDimmer == null)
return;
_backgroundDimTween?.Kill();
_backgroundDimTween = _backgroundDimmer
.DOFade(0f, duration)
.SetEase(Ease.InQuad)
.OnComplete(() =>
{
if (_backgroundDimmer != null)
_backgroundDimmer.gameObject.SetActive(false);
});
}
private void EnsureBackgroundDimmer()
{
if (_backgroundDimmer != null)
return;
var dimmerObject = new GameObject("TarotBackgroundDimmer");
dimmerObject.transform.SetParent(transform, false);
_backgroundDimmer = dimmerObject.AddComponent<SpriteRenderer>();
_backgroundDimSprite = Sprite.Create(
Texture2D.whiteTexture,
new Rect(0f, 0f, 1f, 1f),
new Vector2(0.5f, 0.5f),
1f);
_backgroundDimSprite.name = "TarotBackgroundDimSprite";
_backgroundDimmer.sprite = _backgroundDimSprite;
_backgroundDimmer.sortingLayerName = "Background";
_backgroundDimmer.sortingOrder = -1;
_backgroundDimmer.color = new Color(0f, 0f, 0f, 0f);
dimmerObject.SetActive(false);
}
private void UpdateBackgroundDimmerBounds()
{
var camera = Camera.main;
if (camera == null || _backgroundDimmer == null)
return;
if (camera.orthographic)
{
float height = camera.orthographicSize * 2f;
float width = height * camera.aspect;
_backgroundDimmer.transform.position = new Vector3(
camera.transform.position.x,
camera.transform.position.y,
0f);
_backgroundDimmer.transform.localScale = new Vector3(width, height, 1f);
return;
}
float distance = Mathf.Abs(camera.transform.position.z);
Vector3 bottomLeft = camera.ViewportToWorldPoint(new Vector3(0f, 0f, distance));
Vector3 topRight = camera.ViewportToWorldPoint(new Vector3(1f, 1f, distance));
_backgroundDimmer.transform.position = (bottomLeft + topRight) * 0.5f;
_backgroundDimmer.transform.localScale = new Vector3(
Mathf.Abs(topRight.x - bottomLeft.x),
Mathf.Abs(topRight.y - bottomLeft.y),
1f);
}
private void TryEnterTarotBubbleMode()
{
if (!BubbleSlotKit.Instance.TryGet(BubbleSlotEnum.Tarot, out _))