refactor(enemy): Boss 选招与战利品掉落改用 WeightedPick;删除零调用的 SelectWeightedSkill 死代码

This commit is contained in:
2026-07-27 09:58:40 +08:00
parent 67220b4bbd
commit 1d5214beea
3 changed files with 22 additions and 69 deletions
+10 -18
View File
@@ -39,8 +39,9 @@ namespace BaseGames.Enemies
public int CurrentPhase => _currentPhase;
private Coroutine _counterStaggerCoroutine;
// 缓存加权候选列表,避免 UseBossSkillWeighted() 每次 new List → GC 分配
private readonly List<(BossSkillSO skill, float w)> _weightedCandidates = new(8);
// 缓存加权候选与其有效权重(两者等长、下标对应),避免 UseBossSkillWeighted() 每次 new List → GC 分配
private readonly List<BossSkillSO> _weightedCandidates = new(8);
private readonly List<float> _candidateWeights = new(8);
// 单元素缓冲数组,供 ApplyCounterResponse 缓存当前技能,避免 new[] 分配
private readonly BossSkillSO[] _singleSkillBuf = new BossSkillSO[1];
@@ -110,31 +111,22 @@ namespace BaseGames.Enemies
// 筛选:在当前阶段可用 + 冷却就绪 + weight > 0
_weightedCandidates.Clear();
float totalWeight = 0f;
_candidateWeights.Clear();
foreach (var s in skills)
{
if (s == null || s.weight <= 0f) continue;
if (!_skillExecutor.CanUseSkill(s.skillId)) continue;
if (!IsSkillAvailableInPhase(s)) continue;
_weightedCandidates.Add(s);
// 防重复:上一个技能权重打折
float w = s.skillId == LastUsedSkillId ? s.weight * 0.3f : s.weight;
_weightedCandidates.Add((s, w));
totalWeight += w;
_candidateWeights.Add(s.skillId == LastUsedSkillId ? s.weight * 0.3f : s.weight);
}
if (_weightedCandidates.Count == 0 || totalWeight <= 0f) return false;
// 加权随机抽取
float roll = UnityEngine.Random.Range(0f, totalWeight);
BossSkillSO selected = null;
float accum = 0f;
foreach (var (skill, w) in _weightedCandidates)
{
accum += w;
if (roll <= accum) { selected = skill; break; }
}
selected ??= _weightedCandidates[_weightedCandidates.Count - 1].skill;
// 加权随机抽取(共享原语:无正权重/无候选时返回 -1)
int idx = BaseGames.Core.WeightedPick.Index(_candidateWeights);
if (idx < 0) return false;
BossSkillSO selected = _weightedCandidates[idx];
if (!CheckResourceCost(selected)) return false;