RiftMayhem/Assets/Scripts/Drops/WeightedDrop.cs
Pedro Gomes fdd55142f9 Small class tunning & Rework item drops
- Slight increase to priest, necro and mage scaling
- Updated overall item drops on all enemies:
   - bosses are guaranteed to drop one item
   - drops are weighted and with min/max difficulty restrictions
2025-01-01 20:39:41 +00:00

156 lines
5.2 KiB
C#

using Kryz.CharacterStats.Examples;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class WeightedDrop
{
public Item drop;
public float weight;
public DifficultyLevels lowestPossibleDifficulty;
public DifficultyLevels highestPossibleDifficulty;
public int LowestDifficultyDrop => (int)lowestPossibleDifficulty;
public int HighestDifficultyDrop => (int)highestPossibleDifficulty;
public WeightedDrop(Item item, float weight)
{
this.drop = item;
this.weight = weight;
lowestPossibleDifficulty = DifficultyLevels.E;
highestPossibleDifficulty = DifficultyLevels.SS;
}
public WeightedDrop(Item item, float weight, DifficultyLevels lowestPossibleDifficulty, DifficultyLevels highestPossibleDifficulty)
{
this.drop = item;
this.weight = weight;
this.lowestPossibleDifficulty = lowestPossibleDifficulty;
this.highestPossibleDifficulty = highestPossibleDifficulty;
}
public static bool HaveDropsForDifficulty(List<WeightedDrop> table, int difficulty)
{
for (int i = 0; i < table.Count; i++)
{
if (table[i].LowestDifficultyDrop <= difficulty && table[i].HighestDifficultyDrop >= difficulty)
return true;
}
return false;
}
public static bool HaveDropsForDifficulty(List<WeightedDrop> table, DifficultyLevels difficulty)
{
for (int i = 0; i < table.Count; i++)
{
if (table[i].lowestPossibleDifficulty <= difficulty && table[i].highestPossibleDifficulty >= difficulty)
return true;
}
return false;
}
public static Item GetRandomDrop(List<WeightedDrop> items)
{
float totalWeight = 0;
foreach (var weightedItem in items)
{
totalWeight += weightedItem.weight;
}
float randomValue = UnityEngine.Random.Range(0f, totalWeight);
float currentWeight = 0;
foreach (var weightedItem in items)
{
currentWeight += weightedItem.weight;
if (randomValue <= currentWeight)
{
return weightedItem.drop;
}
}
// This should never happen if the weights are set up correctly
Debug.LogWarning("No item selected in weighted drop system. Returning default value.");
return null;
}
public static Item GetRandomDrop(List<WeightedDrop> items, DifficultyLevels currentDifficulty)
{
float totalWeight = 0;
// First pass: calculate total weight only for items within difficulty range
foreach (var weightedItem in items)
{
if (weightedItem.lowestPossibleDifficulty <= currentDifficulty &&
weightedItem.highestPossibleDifficulty >= currentDifficulty)
{
totalWeight += weightedItem.weight;
}
}
if (totalWeight <= 0)
{
Debug.LogWarning($"No items available for difficulty level {currentDifficulty}. Returning null.");
return null;
}
float randomValue = UnityEngine.Random.Range(0f, totalWeight);
float currentWeight = 0;
// Second pass: select item only from those within difficulty range
foreach (var weightedItem in items)
{
if (weightedItem.lowestPossibleDifficulty <= currentDifficulty &&
weightedItem.highestPossibleDifficulty >= currentDifficulty)
{
currentWeight += weightedItem.weight;
if (randomValue <= currentWeight)
{
return weightedItem.drop;
}
}
}
Debug.LogWarning("No item selected in weighted drop system. Returning null.");
return null;
}
public static Item GetRandomDrop(List<WeightedDrop> items, int currentDifficulty)
{
float totalWeight = 0;
// First pass: calculate total weight only for items within difficulty range
foreach (var weightedItem in items)
{
if (weightedItem.LowestDifficultyDrop <= currentDifficulty &&
weightedItem.HighestDifficultyDrop >= currentDifficulty)
{
totalWeight += weightedItem.weight;
}
}
if (totalWeight <= 0)
{
Debug.LogWarning($"No items available for difficulty level {currentDifficulty}. Returning null.");
return null;
}
float randomValue = UnityEngine.Random.Range(0f, totalWeight);
float currentWeight = 0;
// Second pass: select item only from those within difficulty range
foreach (var weightedItem in items)
{
if (weightedItem.LowestDifficultyDrop <= currentDifficulty &&
weightedItem.HighestDifficultyDrop >= currentDifficulty)
{
currentWeight += weightedItem.weight;
if (randomValue <= currentWeight)
{
return weightedItem.drop;
}
}
}
Debug.LogWarning("No item selected in weighted drop system. Returning null.");
return null;
}
}