ADR-012: Dependency Injection for Game Entities
- Status: Accepted
- Date: 2026-01-08
- Deciders: Simon
Context and Problem Statement
Initially, game entities might have relied on global variables or circular imports to access the game state (e.g., finding the closest target, spawning bullets). This led to fragile code and difficulties in testing and isolation.
Decision Drivers
- Testability: Easier to mock the game instance for unit tests.
- Modularity: Entities are more self-contained and don't depend on global state.
- Maintainability: Reduced circular dependencies and cleaner structure.
Considered Options
- Global Game Instance: Use a global
window.gameor equivalent. - Circular Imports: Each entity imports
game.tsand vice versa. - Dependency Injection: Pass the
Gameinstance to the entity via constructor or a specialized method.
Decision Outcome
Chosen option: Dependency Injection (implemented in commit 79531a9), because it decouples entities from the global game state and makes them significantly easier to test and reason about in isolation.
Consequences
- Positive: More robust entity management, better testability, and less fragile code.
- Negative: Increased complexity in entity creation and more parameters in constructors.
- Integration: Essential for the unit testing strategy (ADR-005).