Files
back_cantanBuilding/Assets/Scripts/SupplyDrop/SupplyDropCashEmitter.cs
2026-05-26 16:15:54 +08:00

105 lines
3.2 KiB
C#

using cfg;
using DG.Tweening;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SupplyDropCashEmitter : MonoBehaviour
{
//目标区域
public Transform targetAreaAA;
public Transform targetAreaBB;
//飞行时间
public float flyTime = 2f;
//发射间隔
public float emissionInterval = 0.5f;
//发射数量
public float emissionCount = 2f;
public int FlyIndexStart = 1;
public int FlyIndexEnd = 5;
SupplyDropGameplayCashCtrl ctrl;
float allWeight = 0f;
List<float> weightList = new List<float>();
List<int> dropCount = new List<int>();
public void StartPlay(SupplyDropGameplayCashCtrl ctrl,
float allWeight,
List<float> weightList,
List<int> dropCount)
{
this.ctrl = ctrl;
this.allWeight = allWeight;
this.weightList = weightList;
this.dropCount = dropCount;
StartCoroutine(EmitCash());
}
public Vector3 GetTargetAreaPos()
{
if (targetAreaAA != null && targetAreaBB != null)
{
float x = Random.Range(targetAreaAA.position.x, targetAreaBB.position.x);
float y = Random.Range(targetAreaAA.position.y, targetAreaBB.position.y);
float z = Random.Range(targetAreaAA.position.z, targetAreaBB.position.z);
return new Vector3(x, y, z);
}
else if (targetAreaAA != null)
{
return targetAreaAA.position;
}
else if (targetAreaBB != null)
{
return targetAreaBB.position;
}
return transform.position; // 默认返回当前对象位置
}
IEnumerator EmitCash()
{
while (true)
{
int type = 0;
float curWeight = 0f;
float randomValue = UnityEngine.Random.Range(0f, allWeight);
for (int i = 0; i < weightList.Count; i++)
{
curWeight += weightList[i];
if (randomValue <= curWeight)
{
// 找到对应的类型
type = i;
break;
}
}
for (int i = 0; i < emissionCount; i++)
{
SupplyDropCashItem item = ctrl.GetCashItem(type, true);
if (item != null)
{
item.gameObject.name = $"Cash_Down_{type}";
item.gameObject.SetActive(true);
item.SetClick(dropCount[type]);
if (FlyIndexStart < FlyIndexEnd)
{
item.PlayAni(UnityEngine.Random.Range(FlyIndexStart, FlyIndexEnd + 1)); // 随机播放动画
}
else
{
item.PlayAni(FlyIndexStart); // 随机播放动画
}
item.transform.position = transform.position;
item.transform.DOMove(GetTargetAreaPos(),
flyTime)
.OnComplete(() => ctrl.Recycle(item, true));
}
}
yield return new WaitForSeconds(emissionInterval);
}
}
}