Overview
Hyperscape uses an Entity Component System (ECS) architecture for all game logic. This pattern separates data from behavior, making the codebase modular and extensible.Core Concepts
Entities
Entities are game objects with 3D representation, networking, physics, and component-based architecture:Components
Components are registered data containers. Hyperscape uses a component factory system:| Component | Data |
|---|---|
transform | Position, rotation, scale (auto-added) |
health | Current/max HP, regeneration rate, isDead |
combat | Attack style, target, cooldown, range |
stats | All skills with levels and XP |
inventory | 28 item slots with quantities |
equipment | Weapon, armor slots |
movement | Speed, destination, path, isRunning |
stamina | Current/max, drain/regen rates |
Systems
Systems contain all game logic. They query entities by component and process them each tick.| System | Responsibility |
|---|---|
CombatSystem | Damage calculation, hit rolls |
MovementSystem | Pathfinding, position updates |
SkillSystem | XP gain, level calculations |
InventorySystem | Item management |
PersistenceSystem | Database saves |
Why ECS?
Composition Over Inheritance
Instead of deep class hierarchies, entities gain capabilities by adding components:Performance
Systems process entities in batches, enabling efficient updates for many game objects. Recent Optimizations:- Raycast Proxy: Invisible capsule meshes for instant entity click detection (~700-1800ms improvement over VRM SkinnedMesh raycast)
- Event-Driven Health: Client health bars use events instead of polling (eliminates race conditions)
- React Optimizations:
useMemoanduseCallbackin InventoryPanel for render efficiency - Map-Based Tracking: O(1) lookups for eat delay and attack cooldown management
- Model Normalization: Scale normalization runs once at load time, before caching
Flexibility
Add new features by creating new components and systems without modifying existing code.Data Flow
Working with ECS
Adding Components
Getting Components
Entity Lifecycle
Network Synchronization
Key Files
| Location | Purpose |
|---|---|
packages/shared/src/entities/ | Entity class definitions |
packages/shared/src/systems/ | System implementations |
packages/shared/src/components/ | Component definitions |
Design Principles
- Entities are data containers—no logic in entity classes
- Systems own all behavior—logic lives in systems
- Components are plain objects—no methods, just data
- Use existing systems—don’t create new ones without good reason