Class AggressionState
Manages the aggression (threat) system for a single NPC. Owns the AggressionController instance, subscribes to global damage/heal/kill events, and tracks the per-NPC target re-evaluation timer.
Extracted from AIController to keep the controller focused on navigation and state management. One instance per NPC — plain C# class.
Event-driven combat entry: When the threat table transitions from empty to non-empty (first damage received), the OnCombatInitiated callback is invoked for immediate combat entry without waiting for the next physics sweep.
Replay safety: Event handlers are guarded to ignore damage/heal/kill events fired during prediction replay. Global combat events are not replay-suppressed by the damage controller, so we suppress them here.
public class AggressionState
- Inheritance
-
AggressionState
- Inherited Members
Constructors
AggressionState(ICharacter, float, float, float, float, float, float)
Creates a new aggression state, initialises the threat table, and subscribes to global damage/heal/kill events.
public AggressionState(ICharacter character, float damageWeight, float healingWeight, float hitBonus, float decayRate, float staleTimeout, float varietyChance)
Parameters
characterICharacterdamageWeightfloathealingWeightfloathitBonusfloatdecayRatefloatstaleTimeoutfloatvarietyChancefloat
Fields
OnCombatInitiated
Callback invoked when the threat table transitions from empty to non-empty.
public Action<ICharacter> OnCombatInitiated
Field Value
TargetReevaluationTimer
Per-NPC timer for mid-combat target re-evaluation. Decremented by the attacking state; reset when re-evaluation fires.
public float TargetReevaluationTimer
Field Value
Properties
Controller
The aggression controller that tracks per-character threat scores and handles target selection.
public AggressionController Controller { get; }
Property Value
HasAggression
True when this NPC is tracking anyone at all.
public bool HasAggression { get; }
Property Value
Remarks
Read by AggressionDispatcher to skip uninvolved NPCs without entering a handler. An out-of-combat NPC is the common case, so this needs to stay a field read.
Methods
Clear()
Clears all aggression data.
public void Clear()
Destroy()
Unsubscribes from global events and clears the threat table.
public void Destroy()
HandleDamaged(ICharacter, int)
Records damage this NPC took. Called by AggressionDispatcher, which has already established that this NPC is the defender.
public void HandleDamaged(ICharacter attacker, int amount)
Parameters
attackerICharacterThe character that dealt the damage.
amountintDamage dealt.
HandleHealed(ICharacter, ICharacter, int)
Records threat for a heal this NPC witnessed on one of its enemies.
public void HandleHealed(ICharacter healer, ICharacter healed, int amount)
Parameters
healerICharacterThe healing character.
healedICharacterThe healed character.
amountintAmount healed.
HandleKilled(ICharacter)
Forgets a character that has died.
public void HandleKilled(ICharacter victim)
Parameters
victimICharacterThe character that died.
RecordResourceSpent(long, int)
Records resource expenditure threat against a specific character. Called externally (e.g., from AbilityController when a caster spends mana).
public void RecordResourceSpent(long characterId, int amount)
Parameters
Tick(float)
Decays threat entries. Call once per tick or frame.
public void Tick(float deltaTime)
Parameters
deltaTimefloat