Projext/Assets/Scripts/Enemy/BossAI/PatternSelector.cs

55 lines
2.7 KiB
C#
Raw Normal View History

2026-02-13 09:11:54 +00:00
using UnityEngine; // 유니티 기능을 불러올거에요 -> UnityEngine을
using System.Collections.Generic; // 딕셔너리를 사용할거에요 -> System.Collections.Generic을
public class PatternSelector // 클래스를 선언할거에요 -> 패턴 선택 로직을
{
private readonly BossCounterConfig _config; // 변수를 선언할거에요 -> 설정 참조를
public PatternSelector(BossCounterConfig config) // 생성자를 만들거에요 -> 설정을 주입받는
{
_config = config; // 값을 저장할거에요 -> 내부 변수에
}
public string SelectPattern(Dictionary<CounterType, int> counters) // 함수를 선언할거에요 -> 패턴 선택을
{
if (!_config) return "Normal"; // 반환할거에요 -> 설정 없으면 기본값을
float totalWeight = 0f; // 변수를 초기화할거에요 -> 총 가중치를
var weights = new Dictionary<string, float>(); // 딕셔너리를 만들거에요 -> 패턴별 확률을
foreach (var p in _config.patterns) // 반복할거에요 -> 모든 패턴 설정을
{
float w = p.baseWeight; // 값을 가져올거에요 -> 기본 가중치를
// 카운터가 있으면 가중치 증가
if (counters.TryGetValue(p.targetCounter, out int count))
w += count * p.weightMultiplier;
// ⭐ [핵심 수정] 중복 키 에러 방지 및 가중치 합산
if (weights.ContainsKey(p.patternName)) // 조건이 맞으면 실행할거에요 -> 이미 같은 패턴 이름이 있다면
{
weights[p.patternName] += w; // 더할거에요 -> 기존 확률에 합산 (조건 A or 조건 B)
}
else // 조건이 틀리면 실행할거에요 -> 처음 나온 패턴이라면
{
weights[p.patternName] = w; // 저장할거에요 -> 새 항목으로
}
totalWeight += w; // 더할거에요 -> 총합에 (이 부분 주의: 중복 합산 시 totalWeight 계산 로직 수정 필요)
}
// totalWeight 재계산 (중복 합산된 최종 값으로)
totalWeight = 0f;
foreach (var val in weights.Values) totalWeight += val;
float rnd = Random.Range(0, totalWeight); // 값을 뽑을거에요 -> 랜덤으로
float sum = 0; // 변수를 초기화할거에요 -> 누적합을
foreach (var pair in weights) // 반복할거에요 -> 각 패턴을
{
sum += pair.Value; // 더할거에요 -> 확률을
if (rnd <= sum) return pair.Key; // 반환할거에요 -> 당첨된 패턴을
}
return "Normal"; // 반환할거에요 -> 꽝이면 기본값을
}
}