2024-08-17 13:12:00 +00:00
|
|
|
|
using System;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using Random = UnityEngine.Random;
|
|
|
|
|
|
2024-08-18 12:44:44 +00:00
|
|
|
|
[CreateAssetMenu(fileName = "New Product Spawn Schedule", menuName = "Data/Product Spawn Schedule", order = 0)]
|
|
|
|
|
public class ProductSpawnSchedule : ScriptableObject
|
2024-08-17 13:12:00 +00:00
|
|
|
|
{
|
|
|
|
|
[Serializable]
|
|
|
|
|
public struct Product
|
|
|
|
|
{
|
|
|
|
|
public GameObject Prefab;
|
|
|
|
|
[Range(0,100)]
|
|
|
|
|
public int Probability;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Product[] Products;
|
|
|
|
|
|
|
|
|
|
public GameObject GetRandomProduct()
|
|
|
|
|
{
|
2024-08-17 19:07:25 +00:00
|
|
|
|
float randomValue = Random.Range(0, 100);
|
2024-08-17 13:12:00 +00:00
|
|
|
|
float sum = 0;
|
|
|
|
|
|
|
|
|
|
for (var i = 0; i < Products.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
var product = Products[i];
|
|
|
|
|
|
2024-08-17 19:07:25 +00:00
|
|
|
|
sum += product.Probability;
|
2024-08-17 13:12:00 +00:00
|
|
|
|
if (randomValue <= sum)
|
|
|
|
|
{
|
|
|
|
|
return product.Prefab;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|