95 lines
2.7 KiB
C#
95 lines
2.7 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; // äëèòåëüíîñòü àíèìàöèè
|
|
|
|
|
|
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;
|
|
|
|
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;
|
|
|
|
// çàïóñêàåì àíèìàöèþ
|
|
StartCoroutine(FlyAndShrinkRoutine(rt));
|
|
}
|
|
}
|
|
|
|
private IEnumerator FlyAndShrinkRoutine(RectTransform rt)
|
|
{
|
|
Vector2 startPos = rt.anchoredPosition;
|
|
Vector2 endPos = startPos - new Vector2(0, fallDistance);
|
|
Vector3 startScale = rt.localScale;
|
|
Vector3 endScale = Vector3.zero;
|
|
|
|
float elapsed = 0f;
|
|
while (elapsed < duration)
|
|
{
|
|
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;
|
|
}
|
|
|
|
Destroy(rt.gameObject);
|
|
}
|
|
}
|