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
| Parameter | Type | Description |
|---|---|---|
ctx | CombatAttackContext | Combat system context with access to services |
data | object | Attack data with entity IDs and types |
data.attackerId | string | Attacking mob entity ID |
data.targetId | string | Target entity ID |
data.attackerType | "mob" | Must be "mob" (type narrowing) |
data.targetType | "player" | "mob" | Target entity type |
combatRange | number | Fallback combat range if NPC manifest omits it |
animationType | "melee" | "ranged" | "magic" | Animation to play |
fallbackAttackSpeed | number | Fallback attack speed in ticks if NPC manifest omits it |
preResolved | object (optional) | Pre-resolved mob entity and NPC data to avoid double lookup |
preResolved.attacker | MobEntity | Already-resolved mob entity |
preResolved.npcData | NPCData | Already-resolved NPC configuration |
Return Value
ReturnsMobAttackContext if all validation passes, or null if any check fails (attack aborted).
Validation Steps
The function performs the following checks in order:- Entity Resolution - Resolve attacker and target entities (or use
preResolvedto skip) - Alive Check - Verify both entities are alive
- NPC Data Lookup - Get mob configuration from
getNPCById(mobData.type) - Range Validation - Check distance using
checkProjectileRange()with mob’scombatRange - Position Validation - Get entity positions via
getEntityPosition() - Cooldown Check - Verify attack is not on cooldown
- Cooldown Claim - Set
nextAttackTicksto prevent rapid-fire attacks - Face Target - Rotate mob to face target via
rotationManager - Play Animation - Trigger attack animation via
animationManager
null and the attack is aborted (no error thrown).
Usage Example
Performance Optimization
ThepreResolved parameter allows handlers to pass already-resolved mob entity and NPC data:
Without preResolved (double lookup):
preResolved (single lookup):
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
| Parameter | Type | Description |
|---|---|---|
ctx | CombatAttackContext | Combat system context |
attackerId | string | Attacking entity ID |
targetId | string | Target entity ID |
attacker | Entity | MobEntity | Attacking entity |
target | Entity | MobEntity | Target entity |
attackRange | number | Maximum attack range in tiles |
Return Value
Returns the Chebyshev distance (tile distance) if in range, or -1 if out of range. When returning -1, the function automatically emits aCOMBAT_ATTACK_FAILED event with reason "out_of_range".
Usage Example
Implementation Details
- Uses pooled
TileCoordobjects fromctx._attackerTileandctx._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 fullCombatSystem 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
CombatSystemclass - 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 byprepareMobAttack() 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 is cleared when player equips/unequips items
- Cache is cleared when player changes combat style
- Cache is rebuilt on next attack