Skip to main content

Combat Utilities

This page documents shared combat utilities used across attack handlers and combat systems.

prepareMobAttack

Shared mob projectile attack preparation for Magic and Ranged handlers. Validates entities, resolves NPC data, checks range/cooldown, faces target, and plays animation. Location: packages/shared/src/systems/shared/combat/handlers/AttackContext.ts

Signature

Parameters

Return Value

Returns MobAttackContext if all validation passes, or null if any check fails (attack aborted).

Validation Steps

The function performs the following checks in order:
  1. Entity Resolution - Resolve attacker and target entities (or use preResolved to skip)
  2. Alive Check - Verify both entities are alive
  3. NPC Data Lookup - Get mob configuration from getNPCById(mobData.type)
  4. Range Validation - Check distance using checkProjectileRange() with mob’s combatRange
  5. Position Validation - Get entity positions via getEntityPosition()
  6. Cooldown Check - Verify attack is not on cooldown
  7. Cooldown Claim - Set nextAttackTicks to prevent rapid-fire attacks
  8. Face Target - Rotate mob to face target via rotationManager
  9. Play Animation - Trigger attack animation via animationManager
If any step fails, the function returns null and the attack is aborted (no error thrown).

Usage Example

Performance Optimization

The preResolved parameter allows handlers to pass already-resolved mob entity and NPC data: Without preResolved (double lookup):
With preResolved (single lookup):
This eliminates redundant Map.get() calls when the handler needs NPC data for spell/arrow validation before calling prepareMobAttack().

checkProjectileRange

Shared projectile range check for Ranged and Magic handlers. Eliminates code duplication between attack handlers. Location: packages/shared/src/systems/shared/combat/handlers/AttackContext.ts

Signature

Parameters

Return Value

Returns the Chebyshev distance (tile distance) if in range, or -1 if out of range. When returning -1, the function automatically emits a COMBAT_ATTACK_FAILED event with reason "out_of_range".

Usage Example

Implementation Details

Performance Notes:
  • Uses pooled TileCoord objects from ctx._attackerTile and ctx._targetTile
  • Zero heap allocations per call
  • Chebyshev distance is O(1) calculation

CombatAttackContext

Interface that attack handlers receive to interact with the combat system. Provides access to services, systems, and mutable state without coupling to the full CombatSystem class. Location: packages/shared/src/systems/shared/combat/handlers/AttackContext.ts

Interface Definition

Usage

Attack handlers receive this context in their constructor:

Benefits

  • Decoupling: Handlers don’t depend on the full CombatSystem class
  • Testability: Easy to mock the context for unit tests
  • Type Safety: All methods and properties are strongly typed
  • Performance: Pooled tile objects (_attackerTile, _targetTile) avoid allocations

MobAttackContext

Result type returned by prepareMobAttack() containing all validated state needed for a mob projectile attack.

Interface Definition

Usage Example


EquipmentStatsCache

Cached equipment stats for a player, used by combat handlers to avoid repeated equipment system queries.

Interface Definition

Usage

Cache Invalidation:
  • Cache is cleared when player equips/unequips items
  • Cache is cleared when player changes combat style
  • Cache is rebuilt on next attack

AttackValidationResult

Result type for attack validation checks.

Interface Definition

Usage Example