How timetocode/nengi works
Major Architectural Components
• Core Library (core): This is the backbone of nengi.js. It contains most of the functionality divided into distinct areas:
– Binary Module (core/binary): Handles binary serialization/deserialization, with various types (e.g. Int8, Float32, ASCIIString, etc.) and helper utilities (e.g., BitBuffer, BitStream). This module is responsible for preparing game state snapshots and network messages for efficient transport.
– Protocol Module (core/protocol): Manages the rules and schemas used for transmitting commands, messages, entities, and other data, including helper functions for reading/writing and type selections.
– Snapshot Module (core/snapshot): Responsible for creating, reading, and writing snapshots that capture the game state. It breaks down the reading and writing of different entity operations (create, update, delete) into submodules.
– Client Module (core/client): Contains the machinery for client-side operations such as prediction, interpolation, and the management of world state (e.g., Chronus, Predictor, Interpolator). This is where the HTML5 client consumes and processes the networked updates.
– Instance Module (core/instance): Manages the server’s runtime state including entities, channels, caches, and state histories needed for authoritative synchronization and rollback features (historian, spatial channels, etc).
– Bot Module (core/bot): Supports AI or simulated clients (bots) that interact with the network engine.
– Common and Defaults (core/common and core/defaults.js): Contain shared utilities and default settings that span across the library.
• External Integration: External single files (external/EDictionary.js) and the index files (index.js, index-es6.js, etc.) that expose the library's API for both node.js and browser environments.
• Testing Framework (spec): A suite of spec/test files that target different modules (bot, channel, proxy, etc.), ensuring that the components work as expected.
Relationships and Interactions
• Data Flow:
– On the server, an nengi.Instance manages game entities. Changes to entities (via the Entity stack) are routed through the Instance module.
– The Protocol module translates these entity updates (create, update, delete) into binary messages using the Binary module.
– The Snapshot module then constructs snapshots of the current state, packaging them for transmission.
– Over on the client side (nengi.Client and associated client modules), incoming snapshots are processed. Interpolator and Predictor tools smooth out movement and state updates.
– Optional bot components feed simulated inputs through the system for testing or automated gameplay.
• Dependency Flow:
– The Protocol, Snapshot, and Binary modules are closely interrelated, as they work sequentially: the Protocol defines schema → Binary encodes/decodes these schemas → Snapshot builds or reads data packets.
– The Instance module integrates with both client and server submodules to provide a central source of truth.
Architectural Patterns and Principles
• Modular Design: The clear separation between binary handling, protocol definitions, snapshot management, and client logic.
• Separation of Concerns: Distinct modules for server runtime (instance, protocol, snapshot) versus client-side predictive and interpolation support.
• Performance and Efficiency: Emphasis on binary compression along with real-time synchronization techniques for responsiveness and volume scalability.
Relevant Technologies and Frameworks
• Node.js as the runtime for the server.
• HTML5 / WebSocket-based communication, with potential usage of libraries like cWS.js or uWS.js (as mentioned in the README).
• Industry-standard game development libraries (e.g., PIXI.js, Babylon.js, Three.js) are supported by the client.