81 lines
2.2 KiB
C#
81 lines
2.2 KiB
C#
using UnityEngine;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
|
|
public class CoinPileController : MonoBehaviour
|
|
{
|
|
public GameObject coinPrefab;
|
|
public int minCoinCount = 5;
|
|
public int maxCoinCount = 15;
|
|
|
|
private int totalCoin;
|
|
|
|
[Header("Explosion")]
|
|
public float explodeRadius = 4f;
|
|
public float explodeForce = 2f;
|
|
|
|
[Header("Timing")]
|
|
public float stayTime = 1.5f;
|
|
public float flyTime = 0.8f;
|
|
|
|
[Header("Scale")]
|
|
public float shrinkStartDistance = 1.2f;
|
|
|
|
[Header("Fly Order")]
|
|
public float flyInterval = 0.06f; // 每个金币起飞间隔
|
|
|
|
|
|
private List<Coin> coins = new List<Coin>();
|
|
private Transform target;
|
|
|
|
public void Init(Transform targetTransform, int coinAmount)
|
|
{
|
|
target = targetTransform;
|
|
totalCoin = coinAmount;
|
|
SpawnCoins();
|
|
StartCoroutine(FlyAfterDelay());
|
|
GameManager.Ins.PlaySound3D("金币掉落",transform);
|
|
}
|
|
|
|
void SpawnCoins()
|
|
{
|
|
int count = Random.Range(minCoinCount, maxCoinCount + 1);
|
|
|
|
int visualCount = Mathf.Clamp(
|
|
totalCoin / 2,
|
|
minCoinCount,
|
|
maxCoinCount
|
|
);
|
|
|
|
int coinValuePerVisual = Mathf.Max(1, totalCoin / visualCount);
|
|
for (int i = 0; i < count; i++)
|
|
{
|
|
GameObject coinGO = Instantiate(coinPrefab, transform);
|
|
coinGO.transform.localPosition = Vector3.zero;
|
|
|
|
Coin coin = coinGO.GetComponent<Coin>();
|
|
coin.SetValue(coinValuePerVisual);
|
|
coins.Add(coin);
|
|
|
|
// 🌟 爆开
|
|
Vector3 dir = Random.insideUnitSphere.normalized;
|
|
Vector3 targetPos = transform.position + dir * Random.Range(1f, explodeRadius);
|
|
|
|
coin.ExplodeTo(targetPos, explodeForce);
|
|
}
|
|
}
|
|
|
|
IEnumerator FlyAfterDelay()
|
|
{
|
|
yield return new WaitForSeconds(stayTime);
|
|
GameManager.Ins.PlaySound3D("飞到金币ICON",transform);
|
|
for (int i = 0; i < coins.Count; i++)
|
|
{
|
|
coins[i].FlyTo(target, flyTime, shrinkStartDistance);
|
|
yield return new WaitForSeconds(flyInterval);
|
|
}
|
|
yield return new WaitForSeconds(2f);
|
|
Destroy(gameObject);
|
|
}
|
|
|
|
} |