126 lines
4.0 KiB
C#
126 lines
4.0 KiB
C#
using System.Collections;
|
|
using UnityEngine;
|
|
using TMPro;
|
|
|
|
public class Coin : MonoBehaviour
|
|
{
|
|
public CoinFlipUI flipUI;
|
|
public TextMeshProUGUI coinText;
|
|
|
|
[Header("Flying Coins")]
|
|
public CoinFlipUI smallCoinPrefab; // Ïðåôàá ìàëåíüêîé ìîíåòêè (CoinFlipUI)
|
|
public int spawnCount = 10; // Êîëè÷åñòâî ìîíåòîê
|
|
public float spawnRadius = 30f; // Ðàäèóñ ðàçáðîñà
|
|
public float fallDistance = 100f; // Ðàññòîÿíèå ïàäåíèÿ
|
|
public float duration = 0.8f; // Äëèòåëüíîñòü àíèìàöèè
|
|
|
|
private bool isAnimationPlaying = false; // Ôëàã äëÿ ïðåäîòâðàùåíèÿ îäíîâðåìåííûõ àíèìàöèé
|
|
|
|
|
|
public void FreezeCoin()
|
|
{
|
|
if (flipUI != null)
|
|
flipUI.FreezeCoin();
|
|
}
|
|
|
|
public void UnfreezeCoin()
|
|
{
|
|
if (flipUI != null)
|
|
flipUI.UnfreezeCoin();
|
|
}
|
|
|
|
// Èçìåíåíèå òåêñòà
|
|
public void ChangeCoinText(string text)
|
|
{
|
|
if (coinText != null)
|
|
coinText.text = text;
|
|
}
|
|
|
|
// -------------------- Àíèìàöèÿ ïîÿâëåíèÿ --------------------
|
|
public void PlayAddCoinsAnimation()
|
|
{
|
|
if (smallCoinPrefab == null || flipUI == null) return;
|
|
|
|
// Åñëè àíèìàöèÿ óæå èãðàåò, òî ïðîïóñêàåì âûçîâ
|
|
if (isAnimationPlaying) return;
|
|
|
|
StartCoroutine(SpawnCoinsRoutine());
|
|
}
|
|
|
|
private IEnumerator SpawnCoinsRoutine()
|
|
{
|
|
isAnimationPlaying = true;
|
|
|
|
RectTransform parentRt = flipUI.GetComponent<RectTransform>();
|
|
|
|
for (int i = 0; i < spawnCount; i++)
|
|
{
|
|
CoinFlipUI coin = Instantiate(smallCoinPrefab, parentRt.parent); // Ñîçäàåì â òîì æå êîíòåéíåðå, ÷òî è flipUI
|
|
RectTransform rt = coin.GetComponent<RectTransform>();
|
|
|
|
// Íà÷àëüíàÿ ïîçèöèÿ = ïîçèöèÿ ãëàâíîé ìîíåòû
|
|
rt.position = parentRt.position;
|
|
|
|
// Óìåíüøàåì ðàçìåðîì â 3 ðàçà
|
|
rt.localScale = smallCoinPrefab.transform.localScale * 0.33f;
|
|
|
|
// Äîáàâëÿåì ñëó÷àéíîå ñìåùåíèå âîêðóã ãëàâíîé ìîíåòû
|
|
Vector2 randomOffset = Random.insideUnitCircle * spawnRadius;
|
|
rt.anchoredPosition += randomOffset;
|
|
|
|
// Íàñòðàèâàåì ñëó÷àéíîå âðàùåíèå ìîíåòêè
|
|
coin.autoSpin = true;
|
|
coin.spinSpeed = Random.Range(300f, 800f); // Ñëó÷àéíàÿ ñêîðîñòü îò 300 äî 800 ãðàäóñîâ â ñåêóíäó
|
|
|
|
// Çàïóñêàåì àíèìàöèþ ñî ñëó÷àéíûì íàïðàâëåíèåì
|
|
Vector2 randomDirection = Random.insideUnitCircle.normalized; // Ñëó÷àéíîå íàïðàâëåíèå
|
|
float randomDistance = Random.Range(fallDistance * 0.5f, fallDistance * 1.5f); // Ñëó÷àéíàÿ äèñòàíöèÿ
|
|
StartCoroutine(FlyAndShrinkRoutine(rt, randomDirection, randomDistance));
|
|
}
|
|
|
|
// Æäåì îêîí÷àíèÿ àíèìàöèè ïåðåä ñáðàñûâàíèåì ôëàãà
|
|
yield return new WaitForSeconds(duration);
|
|
isAnimationPlaying = false;
|
|
}
|
|
|
|
private IEnumerator FlyAndShrinkRoutine(RectTransform rt, Vector2 direction, float distance)
|
|
{
|
|
if (rt == null) yield break;
|
|
|
|
Vector2 startPos = rt.anchoredPosition;
|
|
Vector2 endPos = startPos + direction * distance; // Ëåòèì â ñëó÷àéíîì íàïðàâëåíèè
|
|
Vector3 startScale = rt.localScale;
|
|
Vector3 endScale = Vector3.zero;
|
|
|
|
float elapsed = 0f;
|
|
while (elapsed < duration)
|
|
{
|
|
// Ïðîâåðÿåì, ÷òî îáúåêò íå áûë óäàëåí
|
|
if (rt == null) yield break;
|
|
|
|
elapsed += Time.deltaTime;
|
|
float t = elapsed / duration;
|
|
|
|
rt.anchoredPosition = Vector2.Lerp(startPos, endPos, t);
|
|
rt.localScale = Vector3.Lerp(startScale, endScale, t);
|
|
|
|
// Ïëàâíî óìåíüøàåì ïðîçðà÷íîñòü
|
|
var img = rt.GetComponent<UnityEngine.UI.Image>();
|
|
if (img != null)
|
|
{
|
|
var c = img.color;
|
|
c.a = Mathf.Lerp(1f, 0f, t);
|
|
img.color = c;
|
|
}
|
|
|
|
yield return null;
|
|
}
|
|
|
|
// Óäàëÿåì îáúåêò ïî çàâåðøåíèè
|
|
if (rt != null && rt.gameObject != null)
|
|
{
|
|
Destroy(rt.gameObject);
|
|
}
|
|
}
|
|
}
|