Skip to content
Snippets Groups Projects
Commit 13e2a6c5 authored by gawen.ackerman's avatar gawen.ackerman :robot:
Browse files

REMAKE : V2, collectible spawning generator using probability distribution

parent 4c2c3c33
No related branches found
No related tags found
No related merge requests found
......@@ -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
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment