diff --git a/impulse/Assets/Scripts/Road/CollectibleGenerator.cs b/impulse/Assets/Scripts/Road/CollectibleGenerator.cs index 8387975009d9d1b8f1527ee44c26eba2be2d8c98..d2360f618869b90b21a944e4b9fc14d416b9cae0 100644 --- a/impulse/Assets/Scripts/Road/CollectibleGenerator.cs +++ b/impulse/Assets/Scripts/Road/CollectibleGenerator.cs @@ -41,27 +41,38 @@ public class CollectibleGenerator : MonoBehaviour private Transform SelectSlotWithPreference(List<Transform> slots, int preferenceCode) { - slots.Sort((a, b) => a.position.x.CompareTo(b.position.x)); + slots.Sort((a, b) => a.position.x.CompareTo(b.position.x)); - int slotCount = slots.Count; - int chosenIndex = Random.Range(0, slotCount); + float[] probabilities; switch (preferenceCode) { case 1: - chosenIndex = Random.Range(0, Mathf.Max(1, slotCount / 3)); + probabilities = new float[] { 0.6f, 0.25f, 0.15f }; // Favor 0 slot break; - case 2: - chosenIndex = Random.Range(0, slotCount); + case 2: + probabilities = new float[] { 0.33f, 0.33f, 0.34f }; // No favor break; - case 3: - chosenIndex = Random.Range(2 * slotCount / 3, slotCount); + case 3: + probabilities = new float[] { 0.15f, 0.25f, 0.6f }; // Favor 2 slot break; - default: - chosenIndex = Random.Range(0, slotCount); + default: + probabilities = new float[] { 0.33f, 0.33f, 0.34f }; // Security of all distribution proba break; } - return slots[chosenIndex]; + float randomValue = Random.value; + float cumulativeProbability = 0f; + + for (int i = 0; i < slots.Count; i++) + { + cumulativeProbability += probabilities[i]; + if (randomValue <= cumulativeProbability) + return slots[i]; + } + + return slots[2]; // Security if random.value == 1 } + + }