using UnityEngine;
using BaseGames.Core;
using BaseGames.Core.Events;
using BaseGames.World;
namespace BaseGames.Enemies
{
///
/// 战利品解算器(静态工具类)。
/// 根据 和当前难度决定掉落内容。
///
public static class LootResolver
{
///
/// 解算并执行战利品掉落。
/// 保底 Geo:直接加入玩家(通过事件频道)或在 生成拾取物。
/// 随机物品:加权随机后通过 CollectibleSpawner 实例化拾取物。
///
public static void Resolve(LootTableSO table, Vector2 worldPosition)
{
if (table == null) return;
// 保底 Geo
int guaranteedGeo = Random.Range(table.GuaranteedGeoMin, table.GuaranteedGeoMax + 1);
ApplyDifficultyGeoScale(ref guaranteedGeo);
if (guaranteedGeo > 0)
CollectibleSpawner.SpawnGeo(worldPosition, guaranteedGeo);
// 加权随机物品掉落
if (table.Entries == null || table.Entries.Length == 0) return;
var dm = ServiceLocator.GetOrDefault();
bool isHard = dm != null &&
(int)dm.CurrentLevel >= (int)DifficultyLevel.Hard;
float totalWeight = 0f;
foreach (var entry in table.Entries)
{
float w = entry.BaseWeight;
if (isHard && entry.ScaleWithDifficulty) w *= 1.5f;
totalWeight += w;
}
if (totalWeight <= 0f) return;
float roll = Random.Range(0f, totalWeight);
float accum = 0f;
foreach (var entry in table.Entries)
{
float w = entry.BaseWeight;
if (isHard && entry.ScaleWithDifficulty) w *= 1.5f;
accum += w;
if (roll <= accum)
{
if (!string.IsNullOrEmpty(entry.ItemId))
CollectibleSpawner.SpawnItem(worldPosition, entry.ItemId);
else if (entry.GeoAmount > 0)
CollectibleSpawner.SpawnGeo(worldPosition, entry.GeoAmount);
return;
}
}
}
private static void ApplyDifficultyGeoScale(ref int geo)
{
var dm = ServiceLocator.GetOrDefault();
if (dm?.CurrentScaler == null) return;
float mult = dm.CurrentScaler.GeoDropMultiplier;
geo = Mathf.RoundToInt(geo * mult);
}
}
}