Skip to main content

Combat System API Reference

Complete API reference for Hyperscape’s combat system, including the new mob magic and ranged attack features.

Table of Contents

  1. Attack Handlers
  2. Attack Context Utilities
  3. Combat Constants
  4. Type Definitions
  5. Mob Visual Manager

Attack Handlers

MeleeAttackHandler

Handles close-range melee combat for players and mobs.
Range: 1 tile (standard) or 2 tiles (halberd)
Hit Delay: 0 ticks (instant)
Stats Used: attack, strength, defense

RangedAttackHandler

Handles bow and arrow combat for players and mobs.
Range: Up to 10 tiles (configurable via NPC combatRange)
Hit Delay: 1 + floor((3 + distance) / 6) ticks
Stats Used: ranged, defense
Resource: Arrows (players only)

MagicAttackHandler

Handles spell casting for players and mobs.
Range: Up to 10 tiles (configurable via NPC combatRange)
Hit Delay: 1 + floor((1 + distance) / 3) ticks
Stats Used: magic, defense
Resource: Runes (players only)

Attack Context Utilities

prepareMobAttack()

Shared utility for mob projectile attack preparation. Consolidates entity resolution, range checking, cooldown management, and animation for both magic and ranged handlers.
Validation Steps:
  1. Entity resolution (attacker and target)
  2. Alive checks for both entities
  3. Range validation using checkProjectileRange()
  4. Position validation
  5. Cooldown check
  6. Cooldown claim (sets next attack tick)
  7. Face target (rotation)
  8. Play combat animation
Returns:
Usage Example:

checkProjectileRange()

Shared range validation for ranged and magic attacks.
Range Check Logic:
  1. Get entity positions
  2. Convert to tile coordinates
  3. Calculate Chebyshev distance (max of dx, dz)
  4. Check distance > attackRange or distance === 0
  5. Emit failure event if out of range
  6. Return distance or -1

Combat Constants

Attack Ranges

Projectile Launch Delays

Hit Delay Formulas


Type Definitions

NPCCombatConfig

NPCAppearanceConfig

MobEntityConfig

CombatAttackContext


Mob Visual Manager

MobVisualManager

Manages mob visual state including VRM avatars, animations, and held weapons.
Static Properties:
Instance Properties:

Combat System Methods

Attack Routing

Combat State


Type Guards

Mob Type Guards


Events

COMBAT_MOB_NPC_ATTACK

Emitted when a mob initiates an attack.

COMBAT_PROJECTILE_LAUNCHED

Emitted when a projectile (spell or arrow) is created.

COMBAT_ATTACK_FAILED

Emitted when an attack fails validation.

Performance Considerations

Pre-Allocated Damage Params

Both MagicAttackHandler and RangedAttackHandler use pre-allocated parameter objects to avoid per-attack heap allocations:
Critical Invariant: The mob and player attack paths share the same pre-allocated params object. This is safe because:
  1. The tick loop is single-threaded
  2. Mob paths are synchronous (no await)
  3. Player paths claim cooldown before any await
⚠️ Warning: Do NOT add await between params mutation and damage calculation, or concurrent attacks could corrupt shared state.

Weapon Model Caching

Cache Behavior:
  1. First mob loads weapon GLB from network
  2. Weapon scene cached by URL
  3. Subsequent mobs clone from cache (shared geometry/materials)
  4. Concurrent loads for same URL share single promise
  5. Cache cleared on world teardown via clearWeaponCache()
Memory Efficiency:
  • Geometry and materials shared via clone(true)
  • Only Object3D hierarchy duplicated per mob
  • Failed loads cleaned from _pendingLoads to allow retries

Migration Notes

Upgrading from v2.x to v3.0

Breaking Changes:
  • None - all changes are backward compatible
New Features:
  • Mobs can now use attackType: "ranged" and attackType: "magic"
  • New NPC fields: spellId, arrowId, heldWeaponModel
  • New combat constants: MAGIC_RANGE, SPELL_LAUNCH_DELAY_MS, ARROW_LAUNCH_DELAY_MS
Existing Mobs:
  • All existing mobs default to attackType: "melee"
  • No changes required to existing NPC manifests
  • Held weapons are optional
To Add Ranged/Magic Mobs:
  1. Set attackType in combat config
  2. Add spellId (magic) or arrowId (ranged)
  3. Optionally add heldWeaponModel for visual weapon
  4. Adjust combatRange to appropriate value (7-10 tiles)

Combat API Reference for Hyperscape v3.0