Danro/Assets/Scripts/UI/WinBannerController.cs

135 lines
4.5 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using RR.Common.Managers;
using RR.Game2D.Core.Managers;
namespace RR.Common.UI
{
/// <summary>
/// Контроллер баннера выигрыша.
/// Модальное окно, которое отображается при победе игрока.
/// </summary>
public class WinBannerController : Modal
{
[Header("Элементы баннера выигрыша")]
[SerializeField] private TextMeshProUGUI winText;
[SerializeField] private Button mainMenuButton;
[Header("QR-коды")]
[Tooltip("Контейнер для размещения QR-кодов. QR-коды создаются динамически.")]
[SerializeField] private RectTransform qrCodesContainer;
/// <summary>
/// Событие, вызываемое при закрытии баннера выигрыша.
/// </summary>
public event Action WinBannerDismissed;
private readonly List<GameObject> _createdQRCodeObjects = new();
protected override void Awake()
{
base.Awake();
if (mainMenuButton)
{
mainMenuButton.onClick.AddListener(OnMainMenuClicked);
}
}
/// <summary>
/// Показывает баннер выигрыша с указанным временем игры.
/// </summary>
/// <param name="gameTime">Время игры в секундах</param>
public void ShowWin(float gameTime)
{
if (winText)
{
winText.text = $"Время игры: {TimeFormatHelper.FormatTime(gameTime)}";
}
// Отображаем QR-коды
DisplayQRCodes();
Open();
}
/// <summary>
/// Отображает QR-коды из PrizeSystemManager.
/// Создаёт объекты полностью динамически без префаба.
/// </summary>
private void DisplayQRCodes()
{
// Очищаем ранее созданные QR-коды
ClearQRCodes();
if (qrCodesContainer == null) return;
if (PrizeSystemManager.Instance == null) return;
Texture2D[] qrCodes = PrizeSystemManager.Instance.GetQRCodes();
if (qrCodes == null || qrCodes.Length == 0) return;
// Создаём QR-коды динамически
foreach (Texture2D qrTexture in qrCodes)
{
if (qrTexture == null) continue;
// Создаём GameObject с Image
GameObject qrObject = new GameObject("QRCode", typeof(RectTransform), typeof(Image));
qrObject.transform.SetParent(qrCodesContainer, false);
// Конвертируем Texture2D в Sprite
Sprite qrSprite = Sprite.Create(
qrTexture,
new Rect(0, 0, qrTexture.width, qrTexture.height),
new Vector2(0.5f, 0.5f)
);
// Настраиваем Image
Image image = qrObject.GetComponent<Image>();
image.sprite = qrSprite;
image.raycastTarget = false;
_createdQRCodeObjects.Add(qrObject);
}
}
/// <summary>
/// Очищает созданные QR-код объекты.
/// </summary>
private void ClearQRCodes()
{
foreach (GameObject obj in _createdQRCodeObjects)
{
if (obj != null)
{
Destroy(obj);
}
}
_createdQRCodeObjects.Clear();
}
private void OnMainMenuClicked()
{
Close();
WinBannerDismissed?.Invoke();
if (GameManager.Instance)
{
GameManager.Instance.SetPause(false);
GameManager.Instance.LoadScene("MainMenu", false);
}
}
protected override void OnDisable()
{
base.OnDisable();
ClearQRCodes();
}
}
}