Combat System API Reference
Complete API reference for Hyperscape’s combat system, including the new mob magic and ranged attack features.Table of Contents
Attack Handlers
MeleeAttackHandler
Handles close-range melee combat for players and mobs.Hit Delay: 0 ticks (instant)
Stats Used:
attack, strength, defense
RangedAttackHandler
Handles bow and arrow combat for players and mobs.combatRange)Hit Delay:
1 + floor((3 + distance) / 6) ticksStats Used:
ranged, defenseResource: Arrows (players only)
MagicAttackHandler
Handles spell casting for players and mobs.combatRange)Hit Delay:
1 + floor((1 + distance) / 3) ticksStats Used:
magic, defenseResource: 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.- Entity resolution (attacker and target)
- Alive checks for both entities
- Range validation using
checkProjectileRange() - Position validation
- Cooldown check
- Cooldown claim (sets next attack tick)
- Face target (rotation)
- Play combat animation
checkProjectileRange()
Shared range validation for ranged and magic attacks.- Get entity positions
- Convert to tile coordinates
- Calculate Chebyshev distance (max of dx, dz)
- Check
distance > attackRangeordistance === 0 - Emit failure event if out of range
- 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.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
BothMagicAttackHandler and RangedAttackHandler use pre-allocated parameter objects to avoid per-attack heap allocations:
- The tick loop is single-threaded
- Mob paths are synchronous (no
await) - Player paths claim cooldown before any
await
await between params mutation and damage calculation, or concurrent attacks could corrupt shared state.
Weapon Model Caching
- First mob loads weapon GLB from network
- Weapon scene cached by URL
- Subsequent mobs clone from cache (shared geometry/materials)
- Concurrent loads for same URL share single promise
- Cache cleared on world teardown via
clearWeaponCache()
- Geometry and materials shared via
clone(true) - Only Object3D hierarchy duplicated per mob
- Failed loads cleaned from
_pendingLoadsto allow retries
Migration Notes
Upgrading from v2.x to v3.0
Breaking Changes:- None - all changes are backward compatible
- Mobs can now use
attackType: "ranged"andattackType: "magic" - New NPC fields:
spellId,arrowId,heldWeaponModel - New combat constants:
MAGIC_RANGE,SPELL_LAUNCH_DELAY_MS,ARROW_LAUNCH_DELAY_MS
- All existing mobs default to
attackType: "melee" - No changes required to existing NPC manifests
- Held weapons are optional
- Set
attackTypein combat config - Add
spellId(magic) orarrowId(ranged) - Optionally add
heldWeaponModelfor visual weapon - Adjust
combatRangeto appropriate value (7-10 tiles)
Combat API Reference for Hyperscape v3.0